DrishtiSharma commited on
Commit
30154de
Β·
verified Β·
1 Parent(s): 1e22d48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -76
app.py CHANGED
@@ -114,81 +114,95 @@ def calculate_calories(age, weight, height, activity_level, gender):
114
  with gr.Blocks() as demo:
115
  gr.Markdown("<strong>FIT.AI - Your Fitness and Wellbeing Coach</strong>")
116
 
117
- # User Info
118
- with gr.Row():
119
- user_name = gr.Textbox(placeholder="Enter your name", label="Name")
120
- user_age = gr.Number(label="Age (years)", value=25, precision=0)
121
- user_gender = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male")
122
- user_weight = gr.Number(label="Weight (kg)", value=70, precision=1)
123
- user_height = gr.Number(label="Height (cm)", value=170, precision=1)
124
- activity_level = gr.Dropdown(
125
- choices=["Sedentary", "Lightly active", "Moderately active", "Very active", "Extra active"],
126
- label="Activity Level",
127
- value="Moderately active"
128
- )
129
-
130
- # Outputs
131
- bmi_output = gr.Label(label="BMI Result")
132
- calorie_output = gr.Label(label="Calorie Needs")
133
- bmi_chart = gr.Image(label="BMI and Calorie Chart")
134
-
135
- chatbot = gr.Chatbot(label="Chat with FIT.AI")
136
- text_input = gr.Textbox(placeholder="Type your question here...", label="Your Question")
137
- submit_button = gr.Button("Submit")
138
- clear_button = gr.Button("Clear Chat")
139
-
140
- def submit_message(message, history=[]):
141
- user_details = {
142
- "name": user_name.value,
143
- "age": user_age.value,
144
- "weight": user_weight.value,
145
- "height": user_height.value,
146
- "activity_level": activity_level.value,
147
- "gender": user_gender.value
148
- }
149
- bmi, status = calculate_bmi(user_details['height'], user_details['weight'])
150
- calories = calculate_calories(
151
- user_details['age'], user_details['weight'], user_details['height'], user_details['activity_level'], user_details['gender']
152
- )
153
- chart_path = visualize_bmi_and_calories(bmi, calories)
154
-
155
- # Append metrics to response with actionable advice
156
- personalized_metrics = (
157
- f"Based on your metrics:\n"
158
- f"- BMI: {bmi:.2f} ({status})\n"
159
- f"- Daily Caloric Needs: {calories:.2f} kcal\n"
160
- f"\nAdvice: "
161
- )
162
- if status == "underweight":
163
- 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."
164
- elif status == "normal weight":
165
- 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."
166
- elif status == "overweight":
167
- 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."
168
- else:
169
- 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."
170
-
171
- response = personalized_metrics
172
- history.append((f"User", message))
173
- history.append(("FIT.AI", response))
174
- return history, chart_path
175
-
176
- submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, bmi_chart])
177
- clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, bmi_chart])
178
-
179
- # Actions
180
- calculate_button = gr.Button("Calculate")
181
-
182
- def calculate_metrics(age, weight, height, gender, activity_level):
183
- bmi, status = calculate_bmi(height, weight)
184
- bmi_path = visualize_bmi_and_calories(bmi, calculate_calories(age, weight, height, activity_level, gender))
185
- calories = calculate_calories(age, weight, height, activity_level, gender)
186
- return f"Your BMI is {bmi:.2f}, considered {status}.", f"Daily calorie needs: {calories:.2f} kcal", bmi_path
187
-
188
- calculate_button.click(
189
- calculate_metrics,
190
- inputs=[user_age, user_weight, user_height, user_gender, activity_level],
191
- outputs=[bmi_output, calorie_output, bmi_chart]
192
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
193
 
194
  demo.launch(share=True)
 
114
  with gr.Blocks() as demo:
115
  gr.Markdown("<strong>FIT.AI - Your Fitness and Wellbeing Coach</strong>")
116
 
117
+ with gr.Tabs():
118
+ with gr.Tab("Visualization + Chat"):
119
+ # User Info
120
+ with gr.Row():
121
+ user_name = gr.Textbox(placeholder="Enter your name", label="Name")
122
+ user_age = gr.Number(label="Age (years)", value=25, precision=0)
123
+ user_gender = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male")
124
+ user_weight = gr.Number(label="Weight (kg)", value=70, precision=1)
125
+ user_height = gr.Number(label="Height (cm)", value=170, precision=1)
126
+ activity_level = gr.Dropdown(
127
+ choices=["Sedentary", "Lightly active", "Moderately active", "Very active", "Extra active"],
128
+ label="Activity Level",
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
179
+ with gr.Row():
180
+ user_age_calc = gr.Number(label="Age (years)", value=25, precision=0)
181
+ user_gender_calc = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male")
182
+ user_weight_calc = gr.Number(label="Weight (kg)", value=70, precision=1)
183
+ user_height_calc = gr.Number(label="Height (cm)", value=170, precision=1)
184
+ activity_level_calc = gr.Dropdown(
185
+ choices=["Sedentary", "Lightly active", "Moderately active", "Very active", "Extra active"],
186
+ label="Activity Level",
187
+ value="Moderately active"
188
+ )
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)
200
+ return f"Your BMI is {bmi:.2f}, considered {status}.", f"Daily calorie needs: {calories:.2f} kcal", bmi_path
201
+
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)