Update app.py
Browse files
app.py
CHANGED
@@ -22,9 +22,6 @@ graph = create_workflow()
|
|
22 |
logging.basicConfig(level=logging.DEBUG)
|
23 |
logger = logging.getLogger(__name__)
|
24 |
|
25 |
-
# Debug logs for displaying in UI
|
26 |
-
debug_logs = []
|
27 |
-
|
28 |
# Helper Functions
|
29 |
def run_graph(input_message, history, user_details):
|
30 |
def invoke_workflow(q):
|
@@ -53,12 +50,7 @@ def run_graph(input_message, history, user_details):
|
|
53 |
{"role": "user", "content": input_message}
|
54 |
]
|
55 |
|
56 |
-
logger.debug("Invoking workflow with messages: %s", messages)
|
57 |
-
debug_logs.append(f"Workflow Input: {messages}")
|
58 |
-
|
59 |
response = graph.invoke({"messages": messages})
|
60 |
-
logger.debug("Workflow response: %s", response)
|
61 |
-
debug_logs.append(f"Workflow Response: {response}")
|
62 |
|
63 |
# Extract LLM response
|
64 |
llm_response = None
|
@@ -72,8 +64,6 @@ def run_graph(input_message, history, user_details):
|
|
72 |
else:
|
73 |
q.put("The workflow did not return a valid response. Please try again.")
|
74 |
except Exception as e:
|
75 |
-
logger.error("Error in run_graph: %s", str(e))
|
76 |
-
debug_logs.append(f"Error: {str(e)}")
|
77 |
q.put(f"An error occurred: {e}")
|
78 |
|
79 |
q = queue.Queue()
|
@@ -82,8 +72,6 @@ def run_graph(input_message, history, user_details):
|
|
82 |
thread.join(timeout=TIMEOUT_SECONDS)
|
83 |
|
84 |
if thread.is_alive():
|
85 |
-
logger.error(f"Workflow timed out after {TIMEOUT_SECONDS} seconds.")
|
86 |
-
debug_logs.append(f"Workflow timed out after {TIMEOUT_SECONDS} seconds.")
|
87 |
return f"The request took longer than {TIMEOUT_SECONDS} seconds and timed out. Please try again."
|
88 |
return q.get()
|
89 |
|
@@ -156,8 +144,6 @@ def calculate_calories(age, weight, height, activity_level, gender):
|
|
156 |
with gr.Blocks() as demo:
|
157 |
gr.Markdown("<strong>FIT.AI - Your Fitness and Wellbeing Coach</strong>")
|
158 |
|
159 |
-
debug_logs = []
|
160 |
-
|
161 |
with gr.Tabs():
|
162 |
with gr.Tab("Visualization + Chat"):
|
163 |
# User Input
|
@@ -175,7 +161,6 @@ with gr.Blocks() as demo:
|
|
175 |
|
176 |
# Visualization Output
|
177 |
bmi_chart = gr.Image(label="BMI and Calorie Chart")
|
178 |
-
debug_output = gr.Textbox(label="Debug Logs", interactive=False, lines=10)
|
179 |
|
180 |
# Chat Outputs
|
181 |
with gr.Row():
|
@@ -186,62 +171,47 @@ with gr.Blocks() as demo:
|
|
186 |
clear_button = gr.Button("Clear Chat")
|
187 |
|
188 |
def submit_message(message, history=[]):
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
)
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
if isinstance(response, str):
|
231 |
-
history.append(("FIT.AI", response))
|
232 |
-
else:
|
233 |
-
history.append(("FIT.AI", "An unexpected response was received. Check debug logs for details."))
|
234 |
-
|
235 |
-
return history, chart_path, "\n".join(debug_logs)
|
236 |
-
|
237 |
-
except Exception as e:
|
238 |
-
error_message = f"Error: {str(e)}"
|
239 |
-
logger.error(error_message)
|
240 |
-
debug_logs.append(error_message)
|
241 |
-
return history + [("FIT.AI", "An error occurred. Please try again.")], None, "\n".join(debug_logs)
|
242 |
-
|
243 |
-
submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, bmi_chart, debug_output])
|
244 |
-
clear_button.click(lambda: ([], "", ""), inputs=None, outputs=[chatbot, bmi_chart, debug_output])
|
245 |
|
246 |
# Calculator + Visualization Tab
|
247 |
with gr.Tab("Calculator + Visualization"):
|
@@ -262,22 +232,11 @@ with gr.Blocks() as demo:
|
|
262 |
calculate_button = gr.Button("Calculate")
|
263 |
|
264 |
def calculate_metrics(age, weight, height, gender, activity_level):
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
calories = calculate_calories(age, weight, height, activity_level, gender)
|
271 |
-
chart_path = visualize_bmi_and_calories(bmi, calories)
|
272 |
-
|
273 |
-
debug_logs.append(f"Calculated metrics: BMI={bmi:.2f}, Calories={calories:.2f}")
|
274 |
-
return f"Your BMI is {bmi:.2f}, considered {status}.", f"Daily calorie needs: {calories:.2f} kcal", chart_path
|
275 |
-
|
276 |
-
except Exception as e:
|
277 |
-
error_message = f"Error: {str(e)}"
|
278 |
-
logger.error(error_message)
|
279 |
-
debug_logs.append(error_message)
|
280 |
-
return "An error occurred.", "", None
|
281 |
|
282 |
calculate_button.click(
|
283 |
calculate_metrics,
|
|
|
22 |
logging.basicConfig(level=logging.DEBUG)
|
23 |
logger = logging.getLogger(__name__)
|
24 |
|
|
|
|
|
|
|
25 |
# Helper Functions
|
26 |
def run_graph(input_message, history, user_details):
|
27 |
def invoke_workflow(q):
|
|
|
50 |
{"role": "user", "content": input_message}
|
51 |
]
|
52 |
|
|
|
|
|
|
|
53 |
response = graph.invoke({"messages": messages})
|
|
|
|
|
54 |
|
55 |
# Extract LLM response
|
56 |
llm_response = None
|
|
|
64 |
else:
|
65 |
q.put("The workflow did not return a valid response. Please try again.")
|
66 |
except Exception as e:
|
|
|
|
|
67 |
q.put(f"An error occurred: {e}")
|
68 |
|
69 |
q = queue.Queue()
|
|
|
72 |
thread.join(timeout=TIMEOUT_SECONDS)
|
73 |
|
74 |
if thread.is_alive():
|
|
|
|
|
75 |
return f"The request took longer than {TIMEOUT_SECONDS} seconds and timed out. Please try again."
|
76 |
return q.get()
|
77 |
|
|
|
144 |
with gr.Blocks() as demo:
|
145 |
gr.Markdown("<strong>FIT.AI - Your Fitness and Wellbeing Coach</strong>")
|
146 |
|
|
|
|
|
147 |
with gr.Tabs():
|
148 |
with gr.Tab("Visualization + Chat"):
|
149 |
# User Input
|
|
|
161 |
|
162 |
# Visualization Output
|
163 |
bmi_chart = gr.Image(label="BMI and Calorie Chart")
|
|
|
164 |
|
165 |
# Chat Outputs
|
166 |
with gr.Row():
|
|
|
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 |
+
|
183 |
+
bmi, status = calculate_bmi(user_details['height'], user_details['weight'])
|
184 |
+
calories = calculate_calories(
|
185 |
+
user_details['age'], user_details['weight'], user_details['height'], user_details['activity_level'], user_details['gender']
|
186 |
+
)
|
187 |
+
chart_path = visualize_bmi_and_calories(bmi, calories)
|
188 |
+
|
189 |
+
user_prompt = (
|
190 |
+
f"User wants advice on: {message}\n"
|
191 |
+
f"User Details:\n"
|
192 |
+
f"- Name: {user_details['name']}\n"
|
193 |
+
f"- Age: {user_details['age']}\n"
|
194 |
+
f"- Gender: {user_details['gender']}\n"
|
195 |
+
f"- Weight: {user_details['weight']} kg\n"
|
196 |
+
f"- Height: {user_details['height']} cm\n"
|
197 |
+
f"- Activity Level: {user_details['activity_level']}\n"
|
198 |
+
f"- BMI: {bmi:.2f} ({status})\n"
|
199 |
+
f"- Daily Caloric Needs: {calories:.2f} kcal\n"
|
200 |
+
f"\nProvide tailored advice based on these metrics."
|
201 |
+
)
|
202 |
+
|
203 |
+
response = run_graph(user_prompt, history, user_details)
|
204 |
+
|
205 |
+
history.append(("User", message))
|
206 |
+
if isinstance(response, str):
|
207 |
+
history.append(("FIT.AI", response))
|
208 |
+
else:
|
209 |
+
history.append(("FIT.AI", "An unexpected response was received."))
|
210 |
+
|
211 |
+
return history, chart_path
|
212 |
+
|
213 |
+
submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, bmi_chart])
|
214 |
+
clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, bmi_chart])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
215 |
|
216 |
# Calculator + Visualization Tab
|
217 |
with gr.Tab("Calculator + Visualization"):
|
|
|
232 |
calculate_button = gr.Button("Calculate")
|
233 |
|
234 |
def calculate_metrics(age, weight, height, gender, activity_level):
|
235 |
+
bmi, status = calculate_bmi(height, weight)
|
236 |
+
calories = calculate_calories(age, weight, height, activity_level, gender)
|
237 |
+
chart_path = visualize_bmi_and_calories(bmi, calories)
|
238 |
+
|
239 |
+
return f"Your BMI is {bmi:.2f}, considered {status}.", f"Daily calorie needs: {calories:.2f} kcal", chart_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
240 |
|
241 |
calculate_button.click(
|
242 |
calculate_metrics,
|