Update app.py
Browse files
app.py
CHANGED
@@ -12,6 +12,9 @@ import logging
|
|
12 |
import threading
|
13 |
import queue
|
14 |
|
|
|
|
|
|
|
15 |
# Initialize workflow
|
16 |
graph = create_workflow()
|
17 |
|
@@ -19,6 +22,9 @@ graph = create_workflow()
|
|
19 |
logging.basicConfig(level=logging.DEBUG)
|
20 |
logger = logging.getLogger(__name__)
|
21 |
|
|
|
|
|
|
|
22 |
# Helper Functions
|
23 |
def run_graph(input_message, history, user_details):
|
24 |
def invoke_workflow(q):
|
@@ -48,30 +54,31 @@ def run_graph(input_message, history, user_details):
|
|
48 |
]
|
49 |
|
50 |
logger.debug("Invoking workflow with messages: %s", messages)
|
|
|
|
|
51 |
response = graph.invoke({"messages": messages})
|
52 |
logger.debug("Workflow response: %s", response)
|
|
|
53 |
|
54 |
# Check and return tailored response
|
55 |
if "messages" in response and len(response["messages"]) > 1:
|
56 |
-
|
57 |
-
if assistant_message:
|
58 |
-
q.put(assistant_message)
|
59 |
-
else:
|
60 |
-
q.put("The assistant did not provide specific advice. Please refine your query.")
|
61 |
else:
|
62 |
q.put("The workflow did not return a valid response. Please try again.")
|
63 |
except Exception as e:
|
64 |
logger.error("Error in run_graph: %s", str(e))
|
|
|
65 |
q.put(f"An error occurred: {e}")
|
66 |
|
67 |
q = queue.Queue()
|
68 |
thread = threading.Thread(target=invoke_workflow, args=(q,))
|
69 |
thread.start()
|
70 |
-
thread.join(timeout=
|
71 |
|
72 |
if thread.is_alive():
|
73 |
-
logger.error("Workflow timed out.")
|
74 |
-
|
|
|
75 |
return q.get()
|
76 |
|
77 |
|
@@ -157,6 +164,7 @@ with gr.Blocks() as demo:
|
|
157 |
|
158 |
# Visualization Output
|
159 |
bmi_chart = gr.Image(label="BMI and Calorie Chart")
|
|
|
160 |
|
161 |
# Chat Outputs
|
162 |
with gr.Row():
|
@@ -194,20 +202,21 @@ with gr.Blocks() as demo:
|
|
194 |
f"- Activity Level: {user_details['activity_level']}\n"
|
195 |
f"- BMI: {bmi:.2f} ({status})\n"
|
196 |
f"- Daily Caloric Needs: {calories:.2f} kcal\n"
|
197 |
-
f"
|
198 |
)
|
199 |
|
200 |
response = run_graph(user_prompt, history, user_details)
|
201 |
history.append((f"User", message))
|
202 |
history.append(("FIT.AI", response))
|
203 |
-
return history, chart_path
|
204 |
|
205 |
except Exception as e:
|
206 |
logger.error("Error in submit_message: %s", str(e))
|
207 |
-
|
|
|
208 |
|
209 |
-
submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, bmi_chart])
|
210 |
-
clear_button.click(lambda: ([], ""), inputs=None, outputs=[chatbot, bmi_chart])
|
211 |
|
212 |
# Calculator + Visualization Tab
|
213 |
with gr.Tab("Calculator + Visualization"):
|
|
|
12 |
import threading
|
13 |
import queue
|
14 |
|
15 |
+
# Global timeout variable
|
16 |
+
TIMEOUT_SECONDS = 300
|
17 |
+
|
18 |
# Initialize workflow
|
19 |
graph = create_workflow()
|
20 |
|
|
|
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):
|
|
|
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 |
# Check and return tailored response
|
64 |
if "messages" in response and len(response["messages"]) > 1:
|
65 |
+
q.put(response["messages"][1].get("content", "No content in response."))
|
|
|
|
|
|
|
|
|
66 |
else:
|
67 |
q.put("The workflow did not return a valid response. Please try again.")
|
68 |
except Exception as e:
|
69 |
logger.error("Error in run_graph: %s", str(e))
|
70 |
+
debug_logs.append(f"Error: {str(e)}")
|
71 |
q.put(f"An error occurred: {e}")
|
72 |
|
73 |
q = queue.Queue()
|
74 |
thread = threading.Thread(target=invoke_workflow, args=(q,))
|
75 |
thread.start()
|
76 |
+
thread.join(timeout=TIMEOUT_SECONDS)
|
77 |
|
78 |
if thread.is_alive():
|
79 |
+
logger.error(f"Workflow timed out after {TIMEOUT_SECONDS} seconds.")
|
80 |
+
debug_logs.append(f"Workflow timed out after {TIMEOUT_SECONDS} seconds.")
|
81 |
+
return f"The request took longer than {TIMEOUT_SECONDS} seconds and timed out. Please try again."
|
82 |
return q.get()
|
83 |
|
84 |
|
|
|
164 |
|
165 |
# Visualization Output
|
166 |
bmi_chart = gr.Image(label="BMI and Calorie Chart")
|
167 |
+
debug_output = gr.Textbox(label="Debug Logs", interactive=False, lines=10)
|
168 |
|
169 |
# Chat Outputs
|
170 |
with gr.Row():
|
|
|
202 |
f"- Activity Level: {user_details['activity_level']}\n"
|
203 |
f"- BMI: {bmi:.2f} ({status})\n"
|
204 |
f"- Daily Caloric Needs: {calories:.2f} kcal\n"
|
205 |
+
f"\nProvide tailored advice based on these metrics."
|
206 |
)
|
207 |
|
208 |
response = run_graph(user_prompt, history, user_details)
|
209 |
history.append((f"User", message))
|
210 |
history.append(("FIT.AI", response))
|
211 |
+
return history, chart_path, "\n".join(debug_logs)
|
212 |
|
213 |
except Exception as e:
|
214 |
logger.error("Error in submit_message: %s", str(e))
|
215 |
+
debug_logs.append(f"Error: {str(e)}")
|
216 |
+
return history + [("FIT.AI", "An error occurred. Please try again.")], None, "\n".join(debug_logs)
|
217 |
|
218 |
+
submit_button.click(submit_message, inputs=[text_input, chatbot], outputs=[chatbot, bmi_chart, debug_output])
|
219 |
+
clear_button.click(lambda: ([], "", ""), inputs=None, outputs=[chatbot, bmi_chart, debug_output])
|
220 |
|
221 |
# Calculator + Visualization Tab
|
222 |
with gr.Tab("Calculator + Visualization"):
|