DrishtiSharma commited on
Commit
2e7fe93
Β·
verified Β·
1 Parent(s): 99853f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -25
app.py CHANGED
@@ -71,13 +71,16 @@ def calculate_bmi(height, weight, gender):
71
  status = "overweight"
72
  else:
73
  status = "obese"
 
74
  return bmi, status
75
  except Exception:
76
  return None, "Invalid height or weight provided."
77
 
 
78
  def visualize_bmi(bmi):
79
  try:
80
  categories = ["Underweight", "Normal Weight", "Overweight", "Obese"]
 
81
  colors = ['blue', 'green', 'orange', 'red']
82
 
83
  plt.figure(figsize=(8, 4))
@@ -91,17 +94,21 @@ def visualize_bmi(bmi):
91
  plt_path = "bmi_chart.png"
92
  plt.savefig(plt_path)
93
  plt.close()
 
94
  return plt_path
95
  except Exception as e:
96
  return f"Error creating BMI visualization: {e}"
97
 
 
98
  def calculate_calories(age, weight, height, activity_level, gender):
99
  try:
 
100
  if gender.lower() == "male":
101
  bmr = 10 * weight + 6.25 * height - 5 * age + 5
102
  else:
103
  bmr = 10 * weight + 6.25 * height - 5 * age - 161
104
 
 
105
  activity_multipliers = {
106
  "Sedentary": 1.2,
107
  "Lightly active": 1.375,
@@ -109,7 +116,13 @@ def calculate_calories(age, weight, height, activity_level, gender):
109
  "Very active": 1.725,
110
  "Extra active": 1.9,
111
  }
112
- return round(bmr * activity_multipliers[activity_level], 2)
 
 
 
 
 
 
113
  except Exception as e:
114
  return f"Error in calorie calculation: {e}"
115
 
@@ -117,13 +130,12 @@ def calculate_calories(age, weight, height, activity_level, gender):
117
  with gr.Blocks() as demo:
118
  gr.Markdown("# FIT.AI - Your Fitness and Wellbeing Coach")
119
 
120
- with gr.Tab("Interactive Consultation"):
121
  with gr.Row():
122
- with gr.Row():
123
- user_name = gr.Textbox(placeholder="Enter your name", label="Name", interactive=True)
124
  user_age = gr.Number(label="Age (years)", value=25, precision=0)
125
  user_gender = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male")
126
- with gr.Row():
127
  user_weight = gr.Number(label="Weight (kg)", value=70, precision=1)
128
  user_height = gr.Number(label="Height (cm)", value=170, precision=1)
129
  activity_level = gr.Dropdown(
@@ -131,10 +143,9 @@ with gr.Blocks() as demo:
131
  label="Activity Level",
132
  value="Moderately active"
133
  )
134
- with gr.Row():
135
- chatbot = gr.Chatbot(label="Chat with FIT.AI", type="messages", height=500)
136
  text_input = gr.Textbox(placeholder="Type your question here...", label="Your Question")
137
- with gr.Row():
138
  submit_button = gr.Button("Submit")
139
  clear_button = gr.Button("Clear Chat")
140
 
@@ -148,27 +159,53 @@ with gr.Blocks() as demo:
148
  "gender": user_gender.value
149
  }
150
  bmi, status = calculate_bmi(user_details["height"], user_details["weight"], user_details["gender"])
151
- try:
152
- if bmi:
153
- bmi_path = visualize_bmi(bmi)
154
- calories = calculate_calories(
155
- user_details["age"], user_details["weight"], user_details["height"], user_details["activity_level"], user_details["gender"]
156
- )
157
- response = run_graph(message, history, user_details)
158
- history.append({"role": "user", "content": message})
159
- history.append({
160
- "role": "assistant",
161
- "content": f"{response}\n\nYour BMI is {bmi:.2f}, considered {status}.\nDaily calorie needs: {calories} kcal.",
162
- "image": bmi_path
163
- })
164
- else:
165
- history.append({"role": "assistant", "content": "Error in calculation."})
166
  return history, ""
167
- except Exception as e:
168
- history.append({"role": "assistant", "content": f"Error occurred: {e}"})
169
  return history, ""
170
 
171
  submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, text_input])
172
  clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, text_input])
173
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  demo.launch(share=True)
 
71
  status = "overweight"
72
  else:
73
  status = "obese"
74
+
75
  return bmi, status
76
  except Exception:
77
  return None, "Invalid height or weight provided."
78
 
79
+
80
  def visualize_bmi(bmi):
81
  try:
82
  categories = ["Underweight", "Normal Weight", "Overweight", "Obese"]
83
+ x_pos = np.arange(len(categories))
84
  colors = ['blue', 'green', 'orange', 'red']
85
 
86
  plt.figure(figsize=(8, 4))
 
94
  plt_path = "bmi_chart.png"
95
  plt.savefig(plt_path)
96
  plt.close()
97
+
98
  return plt_path
99
  except Exception as e:
100
  return f"Error creating BMI visualization: {e}"
101
 
102
+
103
  def calculate_calories(age, weight, height, activity_level, gender):
104
  try:
105
+ # Base Metabolic Rate (BMR) calculation
106
  if gender.lower() == "male":
107
  bmr = 10 * weight + 6.25 * height - 5 * age + 5
108
  else:
109
  bmr = 10 * weight + 6.25 * height - 5 * age - 161
110
 
111
+ # Activity multiplier mapping
112
  activity_multipliers = {
113
  "Sedentary": 1.2,
114
  "Lightly active": 1.375,
 
116
  "Very active": 1.725,
117
  "Extra active": 1.9,
118
  }
119
+
120
+ if activity_level in activity_multipliers:
121
+ calories = bmr * activity_multipliers[activity_level]
122
+ else:
123
+ raise ValueError("Invalid activity level")
124
+
125
+ return round(calories, 2)
126
  except Exception as e:
127
  return f"Error in calorie calculation: {e}"
128
 
 
130
  with gr.Blocks() as demo:
131
  gr.Markdown("# FIT.AI - Your Fitness and Wellbeing Coach")
132
 
133
+ with gr.Tab("Chat with FIT.AI"):
134
  with gr.Row():
135
+ with gr.Column():
136
+ user_name = gr.Textbox(placeholder="Enter your name", label="Name")
137
  user_age = gr.Number(label="Age (years)", value=25, precision=0)
138
  user_gender = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male")
 
139
  user_weight = gr.Number(label="Weight (kg)", value=70, precision=1)
140
  user_height = gr.Number(label="Height (cm)", value=170, precision=1)
141
  activity_level = gr.Dropdown(
 
143
  label="Activity Level",
144
  value="Moderately active"
145
  )
146
+
147
+ chatbot = gr.Chatbot(label="Chat with FIT.AI", type="messages")
148
  text_input = gr.Textbox(placeholder="Type your question here...", label="Your Question")
 
149
  submit_button = gr.Button("Submit")
150
  clear_button = gr.Button("Clear Chat")
151
 
 
159
  "gender": user_gender.value
160
  }
161
  bmi, status = calculate_bmi(user_details["height"], user_details["weight"], user_details["gender"])
162
+ if bmi:
163
+ bmi_path = visualize_bmi(bmi)
164
+ calories = calculate_calories(user_details["age"], user_details["weight"], user_details["height"], user_details["activity_level"], user_details["gender"])
165
+ response = run_graph(message, history, user_details)
166
+ history.append({"role": "user", "content": message})
167
+ history.append({"role": "assistant", "content": response + f"\nYour BMI is {bmi:.2f}, considered {status}.\nDaily calorie needs: {calories} kcal.", "image": bmi_path})
 
 
 
 
 
 
 
 
 
168
  return history, ""
169
+ else:
170
+ history.append({"role": "assistant", "content": "Error in calculation."})
171
  return history, ""
172
 
173
  submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, text_input])
174
  clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, text_input])
175
 
176
+ with gr.Tab("BMI and Calorie Calculator"):
177
+ with gr.Row():
178
+ with gr.Column():
179
+ user_name = gr.Textbox(placeholder="Enter your name", label="Name")
180
+ user_age = gr.Number(label="Age (years)", value=25, precision=0)
181
+ user_gender = gr.Dropdown(choices=["Male", "Female"], label="Gender", value="Male")
182
+ user_weight = gr.Number(label="Weight (kg)", value=70, precision=1)
183
+ user_height = gr.Number(label="Height (cm)", value=170, precision=1)
184
+ activity_level = gr.Dropdown(
185
+ choices=["Sedentary", "Lightly active", "Moderately active", "Very active", "Extra active"],
186
+ label="Activity Level",
187
+ value="Moderately active"
188
+ )
189
+ calculate_button = gr.Button("Calculate")
190
+
191
+ with gr.Column():
192
+ bmi_output = gr.Label(label="BMI Result")
193
+ calorie_output = gr.Label(label="Calorie Needs")
194
+ bmi_chart = gr.Image(label="BMI Chart")
195
+
196
+ def calculate_metrics(age, weight, height, gender, activity_level):
197
+ bmi, status = calculate_bmi(height, weight, gender)
198
+ if bmi:
199
+ bmi_path = visualize_bmi(bmi)
200
+ calories = calculate_calories(age, weight, height, activity_level, gender)
201
+ return f"Your BMI is {bmi:.2f}, considered {status}.", f"Daily calorie needs: {calories} kcal", bmi_path
202
+ else:
203
+ return "Invalid inputs.", "", ""
204
+
205
+ calculate_button.click(
206
+ calculate_metrics,
207
+ inputs=[user_age, user_weight, user_height, user_gender, activity_level],
208
+ outputs=[bmi_output, calorie_output, bmi_chart]
209
+ )
210
+
211
  demo.launch(share=True)