|
import gradio as gr |
|
import pandas as pd |
|
import os |
|
|
|
|
|
DATA_FILE = "bmi_data.csv" |
|
|
|
|
|
if os.path.exists(DATA_FILE): |
|
df_history = pd.read_csv(DATA_FILE) |
|
else: |
|
df_history = pd.DataFrame(columns=["Name", "Height", "Weight", "BMI", "Feeling"]) |
|
|
|
def bmi(name, height, weight, feeling): |
|
bmi_value = weight / (height ** 2) |
|
result_emoticon = "π" if bmi_value < 30 else "π" |
|
output_str = f"Hello π {name}...\nYour BMI is {bmi_value:.2f}" |
|
feeling_msg = "You're feeling happy today! π" if feeling == "Yes" else "Hope you feel better soon! π" |
|
|
|
|
|
new_entry = { |
|
"Name": name, |
|
"Height": height, |
|
"Weight": weight, |
|
"BMI": round(bmi_value, 2), |
|
"Feeling": feeling |
|
} |
|
|
|
global df_history |
|
new_entry_df = pd.DataFrame([new_entry]) |
|
df_history = pd.concat([df_history, new_entry_df], ignore_index=True) |
|
df_history.to_csv(DATA_FILE, index=False) |
|
|
|
return output_str, result_emoticon, feeling_msg, df_history |
|
|
|
interface = gr.Interface( |
|
fn=bmi, |
|
inputs=[ |
|
gr.Textbox(label="Name"), |
|
gr.Slider(0.5, 2.5, step=0.01, label="Height in Meters"), |
|
gr.Slider(20, 200, step=1, label="Weight in Kg"), |
|
gr.Radio(["Yes", "No"], label="Are you feeling happy today?") |
|
], |
|
outputs=[ |
|
gr.Text(label="BMI Result"), |
|
gr.Text(label="Emoticon"), |
|
gr.Text(label="Mood Message"), |
|
gr.Dataframe(label="All-Time History") |
|
], |
|
examples=[ |
|
["mate", 1.7, 60, "Yes"], |
|
["guro", 1.75, 70, "Yes"], |
|
["levan", 1.69, 73, "Yes"] |
|
], |
|
|
|
theme=gr.themes.Soft(), |
|
title="BMI Calculator with Emotions π", |
|
description="Your BMI is calculated and saved with your name and mood. Try an example or input your own info!" |
|
) |
|
|
|
interface.launch() |
|
|