DrishtiSharma commited on
Commit
0406d98
Β·
verified Β·
1 Parent(s): e95330e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -25,14 +25,12 @@ def run_graph(input_message, history, user_details):
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')}, "
@@ -42,6 +40,7 @@ def run_graph(input_message, history, user_details):
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}"},
@@ -51,20 +50,20 @@ def run_graph(input_message, history, user_details):
51
  logger.debug("Invoking workflow with messages: %s", messages)
52
  response = graph.invoke({"messages": messages})
53
  logger.debug("Workflow response: %s", response)
54
- # Check for proper response structure
 
55
  if "messages" in response and len(response["messages"]) > 1:
56
  q.put(response["messages"][1].get("content", "No content in response."))
57
  else:
58
  q.put("The workflow did not return a valid response. Please try again.")
59
  except Exception as e:
60
  logger.error("Error in run_graph: %s", str(e))
61
- q.put(f"An error occurred while processing your request: {e}")
62
 
63
- # Create a queue and thread for timeout handling
64
  q = queue.Queue()
65
  thread = threading.Thread(target=invoke_workflow, args=(q,))
66
  thread.start()
67
- thread.join(timeout=300) # Increased timeout to 60 seconds
68
 
69
  if thread.is_alive():
70
  logger.error("Workflow timed out.")
@@ -181,6 +180,7 @@ with gr.Blocks() as demo:
181
  chart_path = visualize_bmi_and_calories(bmi, calories)
182
 
183
  user_prompt = (
 
184
  f"User Details:\n"
185
  f"- Name: {user_details['name']}\n"
186
  f"- Age: {user_details['age']}\n"
@@ -193,7 +193,7 @@ with gr.Blocks() as demo:
193
  f"\nProvide tailored advice based on these metrics."
194
  )
195
 
196
- response = run_graph(message, history, user_details)
197
  history.append((f"User", message))
198
  history.append(("FIT.AI", response))
199
  return history, chart_path
 
25
  try:
26
  system_prompt = (
27
  "You are a fitness and health assistant. "
28
+ "Provide actionable, tailored advice based on the user's personal details and goals. "
29
+ "Incorporate BMI and daily caloric needs into your suggestions. "
30
+ "Offer steps for achieving goals like weight loss or fitness improvement."
 
 
31
  )
32
 
33
+ # Summarize user details
34
  user_details_summary = (
35
  f"Name: {user_details.get('name', 'Unknown')}, "
36
  f"Age: {user_details.get('age', 'Unknown')}, "
 
40
  f"Activity Level: {user_details.get('activity_level', 'Unknown')}"
41
  )
42
 
43
+ # Messages for the workflow
44
  messages = [
45
  {"role": "system", "content": system_prompt},
46
  {"role": "user", "content": f"User details: {user_details_summary}"},
 
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
  q.put(response["messages"][1].get("content", "No content in response."))
57
  else:
58
  q.put("The workflow did not return a valid response. Please try again.")
59
  except Exception as e:
60
  logger.error("Error in run_graph: %s", str(e))
61
+ q.put(f"An error occurred: {e}")
62
 
 
63
  q = queue.Queue()
64
  thread = threading.Thread(target=invoke_workflow, args=(q,))
65
  thread.start()
66
+ thread.join(timeout=30)
67
 
68
  if thread.is_alive():
69
  logger.error("Workflow timed out.")
 
180
  chart_path = visualize_bmi_and_calories(bmi, calories)
181
 
182
  user_prompt = (
183
+ f"User wants advice on: {message}\n"
184
  f"User Details:\n"
185
  f"- Name: {user_details['name']}\n"
186
  f"- Age: {user_details['age']}\n"
 
193
  f"\nProvide tailored advice based on these metrics."
194
  )
195
 
196
+ response = run_graph(user_prompt, history, user_details)
197
  history.append((f"User", message))
198
  history.append(("FIT.AI", response))
199
  return history, chart_path