ArslanFOX commited on
Commit
da0140f
·
verified ·
1 Parent(s): 0087fb3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -15
app.py CHANGED
@@ -3,6 +3,31 @@ from smolagents import CodeAgent, HfApiModel, tool
3
  import datetime
4
  import pytz
5
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
  @tool
8
  def get_current_time_in_timezone(timezone: str) -> str:
@@ -39,30 +64,31 @@ except Exception as e:
39
  prompt_templates = {
40
  "system_prompt": (
41
  "You are a helpful assistant that responds in the same language as the user's query. "
42
- "Detect the language of the query and respond with the result only, without extra explanations. "
43
- "If the query contains a city name (e.g., 'Birsk', 'Tønsberg') or is explicitly about time (e.g., 'What time is it in Moscow?'), "
44
- "ALWAYS use the `get_current_time_in_timezone` tool with the appropriate timezone (e.g., 'Europe/Moscow' for Moscow, 'Asia/Yekaterinburg' for Birsk, 'Europe/Oslo' for Tønsberg). "
45
  "NEVER redefine tools—use only the provided ones. "
46
  "Always return the response via `final_answer()` in a code block like this:\n"
47
  "```py\n"
48
  "final_answer('YOUR ANSWER HERE')\n"
49
  "```\n"
50
- "Execute the tool first, then pass the result as a string to final_answer(). "
51
- "Do NOT include tool calls (e.g., get_current_time_in_timezone()) inside final_answer(). "
52
- "If the query is unclear or does not specify a city, return a clarification request in the same language via final_answer()."
53
  ),
54
  "default": "Response: {{question}}",
55
  "planning": {
56
  "initial_plan": (
57
- "Analyze the query: {{question}}. If it contains a city name (e.g., 'Birsk', 'Tønsberg') or is about time, use `get_current_time_in_timezone` with the correct timezone (e.g., 'Asia/Yekaterinburg' for Birsk, 'Europe/Oslo' for Tønsberg). "
58
- "Write code to execute the tool and return the result via final_answer() in a code block:\n"
 
59
  "```py\n"
60
  "final_answer('YOUR ANSWER HERE')\n"
61
  "```"
62
  ),
63
  "update_plan_pre_messages": (
64
  "Review the query: {{question}} and previous steps. "
65
- "If the plan needs adjustment (e.g., wrong timezone), update it. "
66
  "Write corrected code in a code block:\n"
67
  "```py\n"
68
  "final_answer('YOUR ANSWER HERE')\n"
@@ -79,11 +105,16 @@ prompt_templates = {
79
  },
80
  "managed_agent": {
81
  "execute": (
82
- "Execute the task: {{question}}. If it contains a city name or is about time, use `get_current_time_in_timezone`. "
83
  "Write code in a code block:\n"
84
  "```py\n"
85
- "result = get_current_time_in_timezone('TIMEZONE')\n"
86
- "final_answer(result)\n"
 
 
 
 
 
87
  "```"
88
  ),
89
  "report": (
@@ -95,7 +126,7 @@ prompt_templates = {
95
  ),
96
  "task": (
97
  "Define the task based on the query: {{question}}. "
98
- "If it contains a city name, use `get_current_time_in_timezone`. "
99
  "Return the task definition via final_answer() in a code block:\n"
100
  "```py\n"
101
  "final_answer('YOUR TASK DEFINITION HERE')\n"
@@ -125,8 +156,8 @@ prompt_templates = {
125
 
126
  agent = CodeAgent(
127
  model=model,
128
- tools=[final_answer, get_current_time_in_timezone],
129
- max_steps=2,
130
  verbosity_level=2,
131
  prompt_templates=prompt_templates,
132
  )
 
3
  import datetime
4
  import pytz
5
  import gradio as gr
6
+ from geopy.geocoders import Nominatim
7
+ from timezonefinder import TimezoneFinder
8
+
9
+ @tool
10
+ def get_timezone_by_city(city: str) -> str:
11
+ """Tool to fetch the timezone for a given city using geolocation.
12
+ Args:
13
+ city: A string representing the city name (e.g., 'Бураево', 'Tønsberg').
14
+ Returns:
15
+ A string with the timezone (e.g., 'Europe/Samara') or an error message.
16
+ """
17
+ if not isinstance(city, str):
18
+ return f"Error: Expected a string for city, got: {type(city)}"
19
+ try:
20
+ geolocator = Nominatim(user_agent="smolagents_bot")
21
+ location = geolocator.geocode(city, language="en")
22
+ if not location:
23
+ return f"Error: City '{city}' not found."
24
+ tf = TimezoneFinder()
25
+ timezone = tf.timezone_at(lat=location.latitude, lng=location.longitude)
26
+ if not timezone:
27
+ return f"Error: Timezone not found for city '{city}'."
28
+ return timezone
29
+ except Exception as e:
30
+ return f"Error fetching timezone for city '{city}': {str(e)}"
31
 
32
  @tool
33
  def get_current_time_in_timezone(timezone: str) -> str:
 
64
  prompt_templates = {
65
  "system_prompt": (
66
  "You are a helpful assistant that responds in the same language as the user's query. "
67
+ "Detect the language and return only the result, without explanations. "
68
+ "If the query contains a city name (e.g., 'Birsk', 'Tønsberg', 'Бураево') or phrases like 'сколько времени', 'what time', 'quelle heure', "
69
+ "ALWAYS use the `get_timezone_by_city` tool to find the timezone, then use `get_current_time_in_timezone` to get the time. "
70
  "NEVER redefine tools—use only the provided ones. "
71
  "Always return the response via `final_answer()` in a code block like this:\n"
72
  "```py\n"
73
  "final_answer('YOUR ANSWER HERE')\n"
74
  "```\n"
75
+ "Execute the tools in sequence (get_timezone_by_city, then get_current_time_in_timezone), then pass the result as a string to final_answer(). "
76
+ "Do NOT include tool calls inside final_answer(). "
77
+ "If the city is not found or the query is unclear, return an error message in the same language via final_answer()."
78
  ),
79
  "default": "Response: {{question}}",
80
  "planning": {
81
  "initial_plan": (
82
+ "Analyze the query: {{question}}. If it contains a city name (e.g., 'Birsk', 'Бураево') or phrases like 'сколько времени', "
83
+ "use `get_timezone_by_city` to find the timezone, then `get_current_time_in_timezone` to get the time. "
84
+ "Write code to execute the tools and return the result via final_answer() in a code block:\n"
85
  "```py\n"
86
  "final_answer('YOUR ANSWER HERE')\n"
87
  "```"
88
  ),
89
  "update_plan_pre_messages": (
90
  "Review the query: {{question}} and previous steps. "
91
+ "If the plan needs adjustment (e.g., incorrect city or timezone), update it. "
92
  "Write corrected code in a code block:\n"
93
  "```py\n"
94
  "final_answer('YOUR ANSWER HERE')\n"
 
105
  },
106
  "managed_agent": {
107
  "execute": (
108
+ "Execute the task: {{question}}. If it contains a city name or phrases like 'сколько времени', use `get_timezone_by_city` and `get_current_time_in_timezone`. "
109
  "Write code in a code block:\n"
110
  "```py\n"
111
+ "city = '{{question}}'.lower().split()[-1] # Extract last word as city\n"
112
+ "timezone = get_timezone_by_city(city)\n"
113
+ "if not timezone.startswith('Error'):\n"
114
+ " result = get_current_time_in_timezone(timezone)\n"
115
+ " final_answer(result)\n"
116
+ "else:\n"
117
+ " final_answer(timezone) # Return error message\n"
118
  "```"
119
  ),
120
  "report": (
 
126
  ),
127
  "task": (
128
  "Define the task based on the query: {{question}}. "
129
+ "If it contains a city name or phrases like 'сколько времени', use `get_timezone_by_city` and `get_current_time_in_timezone`. "
130
  "Return the task definition via final_answer() in a code block:\n"
131
  "```py\n"
132
  "final_answer('YOUR TASK DEFINITION HERE')\n"
 
156
 
157
  agent = CodeAgent(
158
  model=model,
159
+ tools=[final_answer, get_current_time_in_timezone, get_timezone_by_city],
160
+ max_steps=3,
161
  verbosity_level=2,
162
  prompt_templates=prompt_templates,
163
  )