Update app.py
Browse files
app.py
CHANGED
@@ -57,7 +57,10 @@ def run_graph(input_message, history, user_details):
|
|
57 |
return f"An error occurred while processing your request: {e}"
|
58 |
|
59 |
|
60 |
-
def calculate_bmi(height, weight):
|
|
|
|
|
|
|
61 |
try:
|
62 |
height_m = height / 100
|
63 |
bmi = weight / (height_m ** 2)
|
@@ -69,14 +72,47 @@ def calculate_bmi(height, weight):
|
|
69 |
status = "overweight"
|
70 |
else:
|
71 |
status = "obese"
|
72 |
-
return f"Your BMI is {bmi:.2f}, which is considered {status}."
|
73 |
-
except:
|
74 |
-
return "Invalid height or weight provided."
|
75 |
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
try:
|
78 |
-
|
79 |
-
|
|
|
|
|
|
|
80 |
activity_multipliers = {
|
81 |
"Sedentary": 1.2,
|
82 |
"Lightly active": 1.375,
|
@@ -84,8 +120,9 @@ def calculate_calories(age, weight, height, activity_level):
|
|
84 |
"Very active": 1.725,
|
85 |
"Extra active": 1.9,
|
86 |
}
|
87 |
-
|
88 |
-
|
|
|
89 |
except Exception:
|
90 |
return "Invalid inputs for calorie calculation."
|
91 |
|
@@ -97,6 +134,7 @@ with gr.Blocks() as demo:
|
|
97 |
with gr.Row():
|
98 |
user_name = gr.Textbox(placeholder="Enter your name", label="Name")
|
99 |
user_age = gr.Number(label="Age (years)", value=25, precision=0)
|
|
|
100 |
user_weight = gr.Number(label="Weight (kg)", value=70, precision=1)
|
101 |
user_height = gr.Number(label="Height (cm)", value=170, precision=1)
|
102 |
activity_level = gr.Dropdown(
|
@@ -105,38 +143,27 @@ with gr.Blocks() as demo:
|
|
105 |
value="Moderately active"
|
106 |
)
|
107 |
|
108 |
-
#
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
clear_button = gr.Button("Clear Chat")
|
113 |
-
|
114 |
-
# Examples
|
115 |
-
examples = gr.Examples(
|
116 |
-
examples=[
|
117 |
-
"Create a personalized workout plan for me",
|
118 |
-
"What should I eat to lose 5 kg in two months?",
|
119 |
-
"Suggest a yoga routine for beginners",
|
120 |
-
"How much water should I drink daily based on my weight?",
|
121 |
-
],
|
122 |
-
inputs=text_input,
|
123 |
-
)
|
124 |
|
125 |
-
#
|
126 |
-
|
127 |
-
user_details = {
|
128 |
-
"name": user_name.value,
|
129 |
-
"age": user_age.value,
|
130 |
-
"weight": user_weight.value,
|
131 |
-
"height": user_height.value,
|
132 |
-
"activity_level": activity_level.value
|
133 |
-
}
|
134 |
-
response = run_graph(message, history, user_details)
|
135 |
-
history.append(("User", message))
|
136 |
-
history.append(("FIT.AI", response))
|
137 |
-
return history, ""
|
138 |
|
139 |
-
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
demo.launch(share=True)
|
|
|
57 |
return f"An error occurred while processing your request: {e}"
|
58 |
|
59 |
|
60 |
+
def calculate_bmi(height, weight, gender):
|
61 |
+
"""
|
62 |
+
Calculate BMI and determine the category based on height, weight, and gender.
|
63 |
+
"""
|
64 |
try:
|
65 |
height_m = height / 100
|
66 |
bmi = weight / (height_m ** 2)
|
|
|
72 |
status = "overweight"
|
73 |
else:
|
74 |
status = "obese"
|
|
|
|
|
|
|
75 |
|
76 |
+
return bmi, status
|
77 |
+
except:
|
78 |
+
return None, "Invalid height or weight provided."
|
79 |
+
|
80 |
+
|
81 |
+
def visualize_bmi(bmi):
|
82 |
+
"""
|
83 |
+
Create a visualization for BMI, showing the user's value against standard categories.
|
84 |
+
"""
|
85 |
+
categories = ["Underweight", "Normal Weight", "Overweight", "Obese"]
|
86 |
+
bmi_ranges = [18.5, 24.9, 29.9, 40]
|
87 |
+
x_pos = np.arange(len(categories))
|
88 |
+
user_position = min(len(bmi_ranges), sum(bmi > np.array(bmi_ranges)))
|
89 |
+
|
90 |
+
plt.figure(figsize=(8, 4))
|
91 |
+
plt.bar(x_pos, bmi_ranges, color=['blue', 'green', 'orange', 'red'], alpha=0.6, label="BMI Categories")
|
92 |
+
plt.axhline(y=bmi, color='purple', linestyle='--', linewidth=2, label=f"Your BMI: {bmi:.2f}")
|
93 |
+
plt.xticks(x_pos, categories)
|
94 |
+
plt.ylabel("BMI Value")
|
95 |
+
plt.title("BMI Visualization")
|
96 |
+
plt.legend()
|
97 |
+
plt.tight_layout()
|
98 |
+
|
99 |
+
plt_path = "bmi_chart.png"
|
100 |
+
plt.savefig(plt_path)
|
101 |
+
plt.close()
|
102 |
+
|
103 |
+
return plt_path
|
104 |
+
|
105 |
+
|
106 |
+
def calculate_calories(age, weight, height, activity_level, gender):
|
107 |
+
"""
|
108 |
+
Estimate daily calorie needs based on user inputs.
|
109 |
+
"""
|
110 |
try:
|
111 |
+
if gender.lower() == "male":
|
112 |
+
bmr = 10 * weight + 6.25 * height - 5 * age + 5
|
113 |
+
else:
|
114 |
+
bmr = 10 * weight + 6.25 * height - 5 * age - 161
|
115 |
+
|
116 |
activity_multipliers = {
|
117 |
"Sedentary": 1.2,
|
118 |
"Lightly active": 1.375,
|
|
|
120 |
"Very active": 1.725,
|
121 |
"Extra active": 1.9,
|
122 |
}
|
123 |
+
|
124 |
+
calories = bmr * activity_multipliers.get(activity_level, 1.2)
|
125 |
+
return calories
|
126 |
except Exception:
|
127 |
return "Invalid inputs for calorie calculation."
|
128 |
|
|
|
134 |
with gr.Row():
|
135 |
user_name = gr.Textbox(placeholder="Enter your name", label="Name")
|
136 |
user_age = gr.Number(label="Age (years)", value=25, precision=0)
|
137 |
+
user_gender = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male")
|
138 |
user_weight = gr.Number(label="Weight (kg)", value=70, precision=1)
|
139 |
user_height = gr.Number(label="Height (cm)", value=170, precision=1)
|
140 |
activity_level = gr.Dropdown(
|
|
|
143 |
value="Moderately active"
|
144 |
)
|
145 |
|
146 |
+
# Outputs
|
147 |
+
bmi_output = gr.Label(label="BMI Result")
|
148 |
+
calorie_output = gr.Label(label="Calorie Needs")
|
149 |
+
bmi_chart = gr.Image(label="BMI Chart")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
150 |
|
151 |
+
# Actions
|
152 |
+
calculate_button = gr.Button("Calculate")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
153 |
|
154 |
+
def calculate_metrics(age, weight, height, gender, activity_level):
|
155 |
+
bmi, status = calculate_bmi(height, weight, gender)
|
156 |
+
if bmi:
|
157 |
+
bmi_path = visualize_bmi(bmi)
|
158 |
+
calories = calculate_calories(age, weight, height, activity_level, gender)
|
159 |
+
return f"Your BMI is {bmi:.2f}, considered {status}.", f"Daily calorie needs: {calories:.2f} kcal", bmi_path
|
160 |
+
else:
|
161 |
+
return "Invalid inputs.", "", ""
|
162 |
+
|
163 |
+
calculate_button.click(
|
164 |
+
calculate_metrics,
|
165 |
+
inputs=[user_age, user_weight, user_height, user_gender, activity_level],
|
166 |
+
outputs=[bmi_output, calorie_output, bmi_chart]
|
167 |
+
)
|
168 |
|
169 |
demo.launch(share=True)
|