Premkumar2025 commited on
Commit
d1daeaf
·
verified ·
1 Parent(s): 577ff52

Update app.py

Browse files

Added a user pref tool , to determine whether the results should be based on datasets or web search

Files changed (1) hide show
  1. app.py +39 -8
app.py CHANGED
@@ -21,14 +21,11 @@ def calculate_age(birthdate: str) -> int:
21
  The calculated age as an integer, or -1 if parsing fails.
22
  """
23
  try:
24
- birth_date = parser.parse(birthdate, fuzzy=True) # Auto-detects date format
25
  today = datetime.today()
26
-
27
- # Ensure birthdate is not in the future
28
  if birth_date > today:
29
- return -1 # Invalid: Future birthdate
30
 
31
- # Calculate age
32
  age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day))
33
  return age
34
 
@@ -54,7 +51,7 @@ final_answer = FinalAnswerTool()
54
  search_tool = DuckDuckGoSearchTool()
55
 
56
  @tool
57
- def get_lifestyle_recommendations(age: int) -> str:
58
  """Fetches lifestyle recommendations based on age using web search.
59
  Args:
60
  age: The calculated age of the user.
@@ -66,7 +63,7 @@ def get_lifestyle_recommendations(age: int) -> str:
66
 
67
  @tool
68
  def get_lifestyle_recommendations_offline(age: int) -> str:
69
- """Provides daily lifestyle habits based on age.
70
  Args:
71
  age: The age of the user.
72
  """
@@ -85,7 +82,41 @@ def get_lifestyle_recommendations_offline(age: int) -> str:
85
 
86
  return "Age not in expected range. Try again."
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
90
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
91
 
@@ -105,7 +136,7 @@ with open("prompts.yaml", 'r') as stream:
105
 
106
  agent = CodeAgent(
107
  model=model,
108
- tools=[final_answer,get_lifestyle_recommendations,calculate_age,get_lifestyle_recommendations_offline], ## add your tools here (don't remove final answer)
109
  max_steps=6,
110
  verbosity_level=1,
111
  grammar=None,
 
21
  The calculated age as an integer, or -1 if parsing fails.
22
  """
23
  try:
24
+ birth_date = parser.parse(birthdate, fuzzy=True)
25
  today = datetime.today()
 
 
26
  if birth_date > today:
27
+ return -1
28
 
 
29
  age = today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day))
30
  return age
31
 
 
51
  search_tool = DuckDuckGoSearchTool()
52
 
53
  @tool
54
+ def get_lifestyle_recommendations_web(age: int) -> str:
55
  """Fetches lifestyle recommendations based on age using web search.
56
  Args:
57
  age: The calculated age of the user.
 
63
 
64
  @tool
65
  def get_lifestyle_recommendations_offline(age: int) -> str:
66
+ """Provides daily lifestyle habits based on age with the following dataset.
67
  Args:
68
  age: The age of the user.
69
  """
 
82
 
83
  return "Age not in expected range. Try again."
84
 
85
+ @tool
86
+ def determine_user_preference(user_input: str) -> bool:
87
+ """
88
+ Uses an LLM to classify whether the user prefers real-time (web-based) or offline recommendations.
89
+
90
+ Args:
91
+ user_input: The user's input as natural language.
92
+
93
+ Returns:
94
+ True if web-based recommendations should be used, False otherwise.
95
+ """
96
+ prompt = f"""
97
+ The user has provided the following request: "{user_input}".
98
+ Determine if the user is asking for real-time, AI-driven, or external data-based insights.
99
+ Respond with only 'yes' (for web-based recommendations) or 'no' (for offline recommendations).
100
+ """
101
+ response = agent.run(prompt)
102
+ return response.strip().lower() == "yes"
103
+
104
 
105
+ @tool
106
+ def get_lifestyle_recommendations(age: int) -> str:
107
+ """Fetches lifestyle recommendations based on age.
108
+ Args:
109
+ age: The calculated age of the user.
110
+ Returns:
111
+ A summary of lifestyle recommendations.
112
+ """
113
+ if determine_user_preference(user_input):
114
+ recommendations = get_lifestyle_recommendations_web(age) #web search
115
+ else:
116
+ recommendations = get_lifestyle_recommendations_offline(age) # Offline Dataset
117
+
118
+ return recommendations
119
+
120
  # If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
121
  # model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
122
 
 
136
 
137
  agent = CodeAgent(
138
  model=model,
139
+ tools=[final_answer,get_lifestyle_recommendations,calculate_age], ## add your tools here (don't remove final answer)
140
  max_steps=6,
141
  verbosity_level=1,
142
  grammar=None,