matesoft commited on
Commit
5e77416
Β·
verified Β·
1 Parent(s): 8a08a28

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +63 -0
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import os
4
+
5
+ # File to persist data
6
+ DATA_FILE = "bmi_data.csv"
7
+
8
+ # Load or create data file
9
+ if os.path.exists(DATA_FILE):
10
+ df_history = pd.read_csv(DATA_FILE)
11
+ else:
12
+ df_history = pd.DataFrame(columns=["Name", "Height", "Weight", "BMI", "Feeling"])
13
+
14
+ def bmi(name, height, weight, feeling):
15
+ bmi_value = weight / (height ** 2)
16
+ result_emoticon = "πŸ˜€" if bmi_value < 30 else "πŸ˜”"
17
+ output_str = f"Hello πŸ‘‹ {name}...\nYour BMI is {bmi_value:.2f}"
18
+ feeling_msg = "You're feeling happy today! 😊" if feeling == "Yes" else "Hope you feel better soon! 🌈"
19
+
20
+ # Append new entry
21
+ new_entry = {
22
+ "Name": name,
23
+ "Height": height,
24
+ "Weight": weight,
25
+ "BMI": round(bmi_value, 2),
26
+ "Feeling": feeling
27
+ }
28
+
29
+ global df_history
30
+ df_history = df_history._append(new_entry, ignore_index=True)
31
+ df_history.to_csv(DATA_FILE, index=False)
32
+
33
+ return output_str, result_emoticon, feeling_msg, df_history
34
+
35
+ interface = gr.Interface(
36
+ fn=bmi,
37
+ inputs=[
38
+ gr.Textbox(label="Name"),
39
+ gr.Slider(0.5, 2.5, step=0.01, label="Height in Meters"),
40
+ gr.Slider(20, 200, step=1, label="Weight in Kg"),
41
+ gr.Radio(["Yes", "No"], label="Are you feeling happy today?")
42
+ ],
43
+ outputs=[
44
+ gr.Text(label="BMI Result"),
45
+ gr.Text(label="Emoticon"),
46
+ gr.Text(label="Mood Message"),
47
+ gr.Dataframe(label="All-Time History")
48
+ ],
49
+ examples=[
50
+ ["mate", 1.7, 60, "Yes"],
51
+ ["guro", 1.75, 70, "Yes"],
52
+ ["levan", 1.69, 73, "Yes"]
53
+ ],
54
+
55
+ theme = gr.themes.Soft(),
56
+ title="BMI Calculator with Emotions 😊",
57
+ description="Your BMI is calculated and saved with your name and mood. Try an example or input your own info!"
58
+ )
59
+
60
+ interface.launch(
61
+ auth=("user", "userpass"),
62
+ auth_message='Check your <strong> Login Details</strong> sent to your <i>email πŸ“§ </i>'
63
+ )