BMIxMood / app.py
matesoft's picture
Update app.py
fd6f038 verified
import gradio as gr
import pandas as pd
import os
# File to persist data
DATA_FILE = "bmi_data.csv"
# Load or create data file
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! 🌈"
# Append new entry as DataFrame for concat
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()