File size: 1,879 Bytes
5e77416 fd6f038 5e77416 fd6f038 5e77416 fd6f038 5e77416 fd6f038 88b8da4 5e77416 fd6f038 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
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()
|