DrishtiSharma commited on
Commit
bed8647
Β·
verified Β·
1 Parent(s): 00c98d4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -42
app.py CHANGED
@@ -129,50 +129,61 @@ with gr.Blocks() as demo:
129
  value="Moderately active"
130
  )
131
 
132
- # Outputs
133
- chatbot = gr.Chatbot(label="Chat with FIT.AI")
134
- text_input = gr.Textbox(placeholder="Type your question here...", label="Your Question")
 
 
 
 
 
135
  submit_button = gr.Button("Submit")
136
  clear_button = gr.Button("Clear Chat")
137
 
138
  def submit_message(message, history=[]):
139
- user_details = {
140
- "name": user_name.value,
141
- "age": user_age.value,
142
- "weight": user_weight.value,
143
- "height": user_height.value,
144
- "activity_level": activity_level.value,
145
- "gender": user_gender.value
146
- }
147
- bmi, status = calculate_bmi(user_details['height'], user_details['weight'])
148
- calories = calculate_calories(
149
- user_details['age'], user_details['weight'], user_details['height'], user_details['activity_level'], user_details['gender']
150
- )
151
- chart_path = visualize_bmi_and_calories(bmi, calories)
152
-
153
- # Append metrics to response with actionable advice
154
- personalized_metrics = (
155
- f"Based on your metrics:\n"
156
- f"- BMI: {bmi:.2f} ({status})\n"
157
- f"- Daily Caloric Needs: {calories:.2f} kcal\n"
158
- f"\nAdvice: "
159
- )
160
- if status == "underweight":
161
- personalized_metrics += "Your BMI indicates you are underweight. Focus on consuming more calories from nutrient-dense foods, including lean proteins, healthy fats, and whole grains. Consider strength training exercises to build muscle mass."
162
- elif status == "normal weight":
163
- personalized_metrics += "Your BMI is in the normal range. Maintain your weight by balancing calorie intake with regular physical activity. Incorporate a mix of cardio and strength training for overall fitness."
164
- elif status == "overweight":
165
- personalized_metrics += "Your BMI indicates you are overweight. Aim for a calorie deficit by reducing portion sizes and increasing physical activity. Focus on a diet rich in vegetables, lean protein, and healthy fats."
166
- else:
167
- personalized_metrics += "Your BMI indicates you are obese. Consult a healthcare provider for a personalized weight loss plan. Start with small, sustainable changes, such as reducing sugary foods and increasing daily physical activity."
168
-
169
- response = personalized_metrics
170
- history.append((f"User", message))
171
- history.append(("FIT.AI", response))
172
- return history, chart_path
173
-
174
- submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, gr.Image()])
175
- clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, gr.Image()])
 
 
 
 
 
 
176
 
177
  with gr.Tab("Calculator + Visualization"):
178
  # Calculator Tab
@@ -189,11 +200,14 @@ with gr.Blocks() as demo:
189
 
190
  bmi_output = gr.Label(label="BMI Result")
191
  calorie_output = gr.Label(label="Calorie Needs")
192
- bmi_chart = gr.Image(label="BMI and Calorie Chart")
193
 
194
  calculate_button = gr.Button("Calculate")
195
 
196
  def calculate_metrics(age, weight, height, gender, activity_level):
 
 
 
197
  bmi, status = calculate_bmi(height, weight)
198
  bmi_path = visualize_bmi_and_calories(bmi, calculate_calories(age, weight, height, activity_level, gender))
199
  calories = calculate_calories(age, weight, height, activity_level, gender)
@@ -202,7 +216,7 @@ with gr.Blocks() as demo:
202
  calculate_button.click(
203
  calculate_metrics,
204
  inputs=[user_age_calc, user_weight_calc, user_height_calc, user_gender_calc, activity_level_calc],
205
- outputs=[bmi_output, calorie_output, bmi_chart]
206
  )
207
 
208
  demo.launch(share=True)
 
129
  value="Moderately active"
130
  )
131
 
132
+ # Visualization Output
133
+ bmi_chart = gr.Image(label="BMI and Calorie Chart")
134
+
135
+ # Chat Outputs
136
+ with gr.Row():
137
+ chatbot = gr.Chatbot(label="Chat with FIT.AI")
138
+ text_input = gr.Textbox(placeholder="Type your question here...", label="Your Question")
139
+
140
  submit_button = gr.Button("Submit")
141
  clear_button = gr.Button("Clear Chat")
142
 
143
  def submit_message(message, history=[]):
144
+ try:
145
+ user_details = {
146
+ "name": user_name.value,
147
+ "age": user_age.value,
148
+ "weight": user_weight.value,
149
+ "height": user_height.value,
150
+ "activity_level": activity_level.value,
151
+ "gender": user_gender.value
152
+ }
153
+ if None in user_details.values():
154
+ raise ValueError("Please fill in all the details for personalized advice.")
155
+
156
+ bmi, status = calculate_bmi(user_details['height'], user_details['weight'])
157
+ calories = calculate_calories(
158
+ user_details['age'], user_details['weight'], user_details['height'], user_details['activity_level'], user_details['gender']
159
+ )
160
+ chart_path = visualize_bmi_and_calories(bmi, calories)
161
+
162
+ # Append metrics to response with actionable advice
163
+ personalized_metrics = (
164
+ f"Based on your metrics:\n"
165
+ f"- BMI: {bmi:.2f} ({status})\n"
166
+ f"- Daily Caloric Needs: {calories:.2f} kcal\n"
167
+ f"\nAdvice: "
168
+ )
169
+ if status == "underweight":
170
+ personalized_metrics += "Your BMI indicates you are underweight. Focus on consuming more calories from nutrient-dense foods, including lean proteins, healthy fats, and whole grains. Consider strength training exercises to build muscle mass."
171
+ elif status == "normal weight":
172
+ personalized_metrics += "Your BMI is in the normal range. Maintain your weight by balancing calorie intake with regular physical activity. Incorporate a mix of cardio and strength training for overall fitness."
173
+ elif status == "overweight":
174
+ personalized_metrics += "Your BMI indicates you are overweight. Aim for a calorie deficit by reducing portion sizes and increasing physical activity. Focus on a diet rich in vegetables, lean protein, and healthy fats."
175
+ else:
176
+ personalized_metrics += "Your BMI indicates you are obese. Consult a healthcare provider for a personalized weight loss plan. Start with small, sustainable changes, such as reducing sugary foods and increasing daily physical activity."
177
+
178
+ response = personalized_metrics
179
+ history.append((f"User", message))
180
+ history.append(("FIT.AI", response))
181
+ return history, chart_path
182
+ except ValueError as ve:
183
+ return history + [("FIT.AI", str(ve))], None
184
+
185
+ submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, bmi_chart])
186
+ clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, bmi_chart])
187
 
188
  with gr.Tab("Calculator + Visualization"):
189
  # Calculator Tab
 
200
 
201
  bmi_output = gr.Label(label="BMI Result")
202
  calorie_output = gr.Label(label="Calorie Needs")
203
+ bmi_chart_calc = gr.Image(label="BMI and Calorie Chart")
204
 
205
  calculate_button = gr.Button("Calculate")
206
 
207
  def calculate_metrics(age, weight, height, gender, activity_level):
208
+ if not all([age, weight, height, gender, activity_level]):
209
+ return "Please fill in all fields.", "", None
210
+
211
  bmi, status = calculate_bmi(height, weight)
212
  bmi_path = visualize_bmi_and_calories(bmi, calculate_calories(age, weight, height, activity_level, gender))
213
  calories = calculate_calories(age, weight, height, activity_level, gender)
 
216
  calculate_button.click(
217
  calculate_metrics,
218
  inputs=[user_age_calc, user_weight_calc, user_height_calc, user_gender_calc, activity_level_calc],
219
+ outputs=[bmi_output, calorie_output, bmi_chart_calc]
220
  )
221
 
222
  demo.launch(share=True)