Update app.py
Browse files
app.py
CHANGED
@@ -101,11 +101,13 @@ def visualize_bmi(bmi):
|
|
101 |
|
102 |
def calculate_calories(age, weight, height, activity_level, gender):
|
103 |
try:
|
|
|
104 |
if gender.lower() == "male":
|
105 |
bmr = 10 * weight + 6.25 * height - 5 * age + 5
|
106 |
else:
|
107 |
bmr = 10 * weight + 6.25 * height - 5 * age - 161
|
108 |
|
|
|
109 |
activity_multipliers = {
|
110 |
"Sedentary": 1.2,
|
111 |
"Lightly active": 1.375,
|
@@ -114,71 +116,75 @@ def calculate_calories(age, weight, height, activity_level, gender):
|
|
114 |
"Extra active": 1.9,
|
115 |
}
|
116 |
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
|
|
121 |
|
122 |
# Interface Components
|
123 |
with gr.Blocks() as demo:
|
124 |
-
gr.Markdown("
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
137 |
)
|
138 |
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
"
|
156 |
-
"
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
return history, ""
|
162 |
-
|
163 |
-
submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, text_input])
|
164 |
-
clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, text_input])
|
165 |
-
|
166 |
-
# Actions
|
167 |
-
calculate_button = gr.Button("Calculate")
|
168 |
-
|
169 |
-
def calculate_metrics(age, weight, height, gender, activity_level):
|
170 |
-
bmi, status = calculate_bmi(height, weight, gender)
|
171 |
-
if bmi:
|
172 |
-
bmi_path = visualize_bmi(bmi)
|
173 |
-
calories = calculate_calories(age, weight, height, activity_level, gender)
|
174 |
-
return f"Your BMI is {bmi:.2f}, considered {status}.", f"Daily calorie needs: {calories:.2f} kcal", bmi_path
|
175 |
-
else:
|
176 |
-
return "Invalid inputs.", "", ""
|
177 |
-
|
178 |
-
calculate_button.click(
|
179 |
-
calculate_metrics,
|
180 |
-
inputs=[user_age, user_weight, user_height, user_gender, activity_level],
|
181 |
-
outputs=[bmi_output, calorie_output, bmi_chart]
|
182 |
-
)
|
183 |
|
184 |
demo.launch(share=True)
|
|
|
101 |
|
102 |
def calculate_calories(age, weight, height, activity_level, gender):
|
103 |
try:
|
104 |
+
# Base Metabolic Rate (BMR) calculation
|
105 |
if gender.lower() == "male":
|
106 |
bmr = 10 * weight + 6.25 * height - 5 * age + 5
|
107 |
else:
|
108 |
bmr = 10 * weight + 6.25 * height - 5 * age - 161
|
109 |
|
110 |
+
# Activity multiplier mapping
|
111 |
activity_multipliers = {
|
112 |
"Sedentary": 1.2,
|
113 |
"Lightly active": 1.375,
|
|
|
116 |
"Extra active": 1.9,
|
117 |
}
|
118 |
|
119 |
+
if activity_level in activity_multipliers:
|
120 |
+
calories = bmr * activity_multipliers[activity_level]
|
121 |
+
else:
|
122 |
+
raise ValueError("Invalid activity level")
|
123 |
+
|
124 |
+
return round(calories, 2)
|
125 |
+
except Exception as e:
|
126 |
+
return f"Error in calorie calculation: {e}"
|
127 |
|
128 |
# Interface Components
|
129 |
with gr.Blocks() as demo:
|
130 |
+
gr.Markdown("# FIT.AI - Your Fitness and Wellbeing Coach")
|
131 |
+
|
132 |
+
with gr.Tab("BMI and Calorie Calculator"):
|
133 |
+
with gr.Row():
|
134 |
+
with gr.Column():
|
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(
|
141 |
+
choices=["Sedentary", "Lightly active", "Moderately active", "Very active", "Extra active"],
|
142 |
+
label="Activity Level",
|
143 |
+
value="Moderately active"
|
144 |
+
)
|
145 |
+
calculate_button = gr.Button("Calculate")
|
146 |
+
|
147 |
+
with gr.Column():
|
148 |
+
bmi_output = gr.Label(label="BMI Result")
|
149 |
+
calorie_output = gr.Label(label="Calorie Needs")
|
150 |
+
bmi_chart = gr.Image(label="BMI Chart")
|
151 |
+
|
152 |
+
def calculate_metrics(age, weight, height, gender, activity_level):
|
153 |
+
bmi, status = calculate_bmi(height, weight, gender)
|
154 |
+
if bmi:
|
155 |
+
bmi_path = visualize_bmi(bmi)
|
156 |
+
calories = calculate_calories(age, weight, height, activity_level, gender)
|
157 |
+
return f"Your BMI is {bmi:.2f}, considered {status}.", f"Daily calorie needs: {calories} kcal", bmi_path
|
158 |
+
else:
|
159 |
+
return "Invalid inputs.", "", ""
|
160 |
+
|
161 |
+
calculate_button.click(
|
162 |
+
calculate_metrics,
|
163 |
+
inputs=[user_age, user_weight, user_height, user_gender, activity_level],
|
164 |
+
outputs=[bmi_output, calorie_output, bmi_chart]
|
165 |
)
|
166 |
|
167 |
+
with gr.Tab("Chat with FIT.AI"):
|
168 |
+
chatbot = gr.Chatbot(label="Chat with FIT.AI")
|
169 |
+
text_input = gr.Textbox(placeholder="Type your question here...", label="Your Question")
|
170 |
+
submit_button = gr.Button("Submit")
|
171 |
+
clear_button = gr.Button("Clear Chat")
|
172 |
+
|
173 |
+
def submit_message(message, history=[]):
|
174 |
+
user_details = {
|
175 |
+
"name": user_name.value,
|
176 |
+
"age": user_age.value,
|
177 |
+
"weight": user_weight.value,
|
178 |
+
"height": user_height.value,
|
179 |
+
"activity_level": activity_level.value,
|
180 |
+
"gender": user_gender.value
|
181 |
+
}
|
182 |
+
response = run_graph(message, history, user_details)
|
183 |
+
history.append(("User", message))
|
184 |
+
history.append(("FIT.AI", response))
|
185 |
+
return history, ""
|
186 |
+
|
187 |
+
submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, text_input])
|
188 |
+
clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, text_input])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
189 |
|
190 |
demo.launch(share=True)
|