DrishtiSharma commited on
Commit
08c8080
Β·
verified Β·
1 Parent(s): 661355f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -40
app.py CHANGED
@@ -9,6 +9,8 @@ from agents import *
9
  from config import *
10
  from workflow import create_workflow
11
  import logging
 
 
12
 
13
  # Initialize workflow
14
  graph = create_workflow()
@@ -19,41 +21,51 @@ logger = logging.getLogger(__name__)
19
 
20
  # Helper Functions
21
  def run_graph(input_message, history, user_details):
22
- try:
23
- system_prompt = (
24
- "You are a fitness and health assistant. "
25
- "Provide advice based on the user's personal details and goals. "
26
- "If user details like weight, height, and activity level are provided, prioritize personalized advice. "
27
- "Incorporate metrics such as BMI and daily caloric needs directly into your suggestions. "
28
- "Visualize the user's metrics and provide actionable advice tailored to their needs. "
29
- "If details are missing, provide general advice but encourage users to share their metrics for tailored guidance."
30
- )
31
-
32
- # Compose input for the LLM
33
- user_details_summary = (
34
- f"Name: {user_details.get('name', 'Unknown')}, "
35
- f"Age: {user_details.get('age', 'Unknown')}, "
36
- f"Gender: {user_details.get('gender', 'Unknown')}, "
37
- f"Weight: {user_details.get('weight', 'Unknown')} kg, "
38
- f"Height: {user_details.get('height', 'Unknown')} cm, "
39
- f"Activity Level: {user_details.get('activity_level', 'Unknown')}"
40
- )
41
-
42
- messages = [
43
- {"role": "system", "content": system_prompt},
44
- {"role": "user", "content": f"User details: {user_details_summary}"},
45
- {"role": "user", "content": input_message}
46
- ]
47
-
48
- # Generate the response
49
- logger.debug("Invoking workflow with messages: %s", messages)
50
- response = graph.invoke({"messages": messages}, timeout=10) # Add a timeout for safety
51
- logger.debug("Workflow response: %s", response)
52
- return response["messages"][1]["content"]
53
-
54
- except Exception as e:
55
- logger.error("Error in run_graph: %s", str(e))
56
- return f"An error occurred while processing your request: {e}"
 
 
 
 
 
 
 
 
 
 
57
 
58
 
59
  def calculate_bmi(height, weight):
@@ -189,13 +201,11 @@ with gr.Blocks() as demo:
189
  ]
190
 
191
  logger.debug("Submitting messages to LLM: %s", messages)
192
- response = graph.invoke({"messages": messages}, timeout=10) # Add a timeout
193
  logger.debug("Received LLM response: %s", response)
194
 
195
- personalized_response = response["messages"][1]["content"]
196
-
197
  history.append((f"User", message))
198
- history.append(("FIT.AI", personalized_response))
199
  return history, chart_path
200
 
201
  except Exception as e:
@@ -240,4 +250,4 @@ with gr.Blocks() as demo:
240
  outputs=[bmi_output, calorie_output, bmi_chart_calc]
241
  )
242
 
243
- demo.launch(share=True)
 
9
  from config import *
10
  from workflow import create_workflow
11
  import logging
12
+ import threading
13
+ import queue
14
 
15
  # Initialize workflow
16
  graph = create_workflow()
 
21
 
22
  # Helper Functions
23
  def run_graph(input_message, history, user_details):
24
+ def invoke_workflow(q):
25
+ try:
26
+ system_prompt = (
27
+ "You are a fitness and health assistant. "
28
+ "Provide advice based on the user's personal details and goals. "
29
+ "If user details like weight, height, and activity level are provided, prioritize personalized advice. "
30
+ "Incorporate metrics such as BMI and daily caloric needs directly into your suggestions. "
31
+ "Visualize the user's metrics and provide actionable advice tailored to their needs. "
32
+ "If details are missing, provide general advice but encourage users to share their metrics for tailored guidance."
33
+ )
34
+
35
+ # Compose input for the LLM
36
+ user_details_summary = (
37
+ f"Name: {user_details.get('name', 'Unknown')}, "
38
+ f"Age: {user_details.get('age', 'Unknown')}, "
39
+ f"Gender: {user_details.get('gender', 'Unknown')}, "
40
+ f"Weight: {user_details.get('weight', 'Unknown')} kg, "
41
+ f"Height: {user_details.get('height', 'Unknown')} cm, "
42
+ f"Activity Level: {user_details.get('activity_level', 'Unknown')}"
43
+ )
44
+
45
+ messages = [
46
+ {"role": "system", "content": system_prompt},
47
+ {"role": "user", "content": f"User details: {user_details_summary}"},
48
+ {"role": "user", "content": input_message}
49
+ ]
50
+
51
+ logger.debug("Invoking workflow with messages: %s", messages)
52
+ response = graph.invoke({"messages": messages})
53
+ logger.debug("Workflow response: %s", response)
54
+ q.put(response["messages"][1]["content"])
55
+ except Exception as e:
56
+ logger.error("Error in run_graph: %s", str(e))
57
+ q.put(f"An error occurred while processing your request: {e}")
58
+
59
+ # Create a queue and thread for timeout handling
60
+ q = queue.Queue()
61
+ thread = threading.Thread(target=invoke_workflow, args=(q,))
62
+ thread.start()
63
+ thread.join(timeout=10) # Wait for 10 seconds
64
+
65
+ if thread.is_alive():
66
+ logger.error("Workflow timed out.")
67
+ return "The request timed out. Please try again later."
68
+ return q.get()
69
 
70
 
71
  def calculate_bmi(height, weight):
 
201
  ]
202
 
203
  logger.debug("Submitting messages to LLM: %s", messages)
204
+ response = run_graph(message, history, user_details)
205
  logger.debug("Received LLM response: %s", response)
206
 
 
 
207
  history.append((f"User", message))
208
+ history.append(("FIT.AI", response))
209
  return history, chart_path
210
 
211
  except Exception as e:
 
250
  outputs=[bmi_output, calorie_output, bmi_chart_calc]
251
  )
252
 
253
+ demo.launch(share=True)