Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,7 +10,7 @@ from timezonefinder import TimezoneFinder
|
|
| 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., 'Бураево', '
|
| 14 |
Returns:
|
| 15 |
A string with the timezone (e.g., 'Europe/Samara') or an error message.
|
| 16 |
"""
|
|
@@ -18,9 +18,14 @@ def get_timezone_by_city(city: str) -> 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
tf = TimezoneFinder()
|
| 25 |
timezone = tf.timezone_at(lat=location.latitude, lng=location.longitude)
|
| 26 |
if not timezone:
|
|
@@ -64,22 +69,23 @@ except Exception as e:
|
|
| 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
|
| 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"
|
|
@@ -108,7 +114,7 @@ prompt_templates = {
|
|
| 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}}'.
|
| 112 |
"timezone = get_timezone_by_city(city)\n"
|
| 113 |
"if not timezone.startswith('Error'):\n"
|
| 114 |
" result = get_current_time_in_timezone(timezone)\n"
|
|
@@ -165,7 +171,7 @@ agent = CodeAgent(
|
|
| 165 |
def process_input(user_input):
|
| 166 |
try:
|
| 167 |
response = agent.run(user_input)
|
| 168 |
-
return response
|
| 169 |
except Exception as e:
|
| 170 |
return f"Error: {str(e)}"
|
| 171 |
|
|
|
|
| 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, optionally with region (e.g., 'Бураево', 'Осиновка, Бирский район').
|
| 14 |
Returns:
|
| 15 |
A string with the timezone (e.g., 'Europe/Samara') or an error message.
|
| 16 |
"""
|
|
|
|
| 18 |
return f"Error: Expected a string for city, got: {type(city)}"
|
| 19 |
try:
|
| 20 |
geolocator = Nominatim(user_agent="smolagents_bot")
|
| 21 |
+
# Попробуем поиск с полным запросом
|
| 22 |
location = geolocator.geocode(city, language="en")
|
| 23 |
if not location:
|
| 24 |
+
# Если полный запрос не сработал, попробуем только город (первое слово до запятой)
|
| 25 |
+
city_name = city.split(",")[0].strip()
|
| 26 |
+
location = geolocator.geocode(city_name, language="en")
|
| 27 |
+
if not location:
|
| 28 |
+
return f"Error: City '{city}' not found."
|
| 29 |
tf = TimezoneFinder()
|
| 30 |
timezone = tf.timezone_at(lat=location.latitude, lng=location.longitude)
|
| 31 |
if not timezone:
|
|
|
|
| 69 |
prompt_templates = {
|
| 70 |
"system_prompt": (
|
| 71 |
"You are a helpful assistant that responds in the same language as the user's query. "
|
| 72 |
+
"Detect the language and return only the result, without explanations or images. "
|
| 73 |
+
"If the query contains a city name (e.g., 'Birsk', 'Tønsberg', 'Бураево', 'Осиновка, Бирский район') or phrases like 'сколько времени', 'what time', 'quelle heure', "
|
| 74 |
"ALWAYS use the `get_timezone_by_city` tool to find the timezone, then use `get_current_time_in_timezone` to get the time. "
|
| 75 |
"NEVER redefine tools—use only the provided ones. "
|
| 76 |
"Always return the response via `final_answer()` in a code block like this:\n"
|
| 77 |
"```py\n"
|
| 78 |
"final_answer('YOUR ANSWER HERE')\n"
|
| 79 |
"```\n"
|
| 80 |
+
"Execute the tools in sequence: first call get_timezone_by_city(city), then get_current_time_in_timezone(timezone). "
|
| 81 |
+
"Pass the result as a string to final_answer(). Do NOT include tool calls inside final_answer(). "
|
| 82 |
+
"If the city is not found or the query is unclear, return an error message in the same language via final_answer(). "
|
| 83 |
+
"Do NOT generate code with multiple arguments for get_timezone_by_city—it accepts only one string."
|
| 84 |
),
|
| 85 |
"default": "Response: {{question}}",
|
| 86 |
"planning": {
|
| 87 |
"initial_plan": (
|
| 88 |
+
"Analyze the query: {{question}}. If it contains a city name (e.g., 'Birsk', 'Бураево', 'Осиновка, Бирский район') or phrases like 'сколько времени', "
|
| 89 |
"use `get_timezone_by_city` to find the timezone, then `get_current_time_in_timezone` to get the time. "
|
| 90 |
"Write code to execute the tools and return the result via final_answer() in a code block:\n"
|
| 91 |
"```py\n"
|
|
|
|
| 114 |
"Execute the task: {{question}}. If it contains a city name or phrases like 'сколько времени', use `get_timezone_by_city` and `get_current_time_in_timezone`. "
|
| 115 |
"Write code in a code block:\n"
|
| 116 |
"```py\n"
|
| 117 |
+
"city = '{{question}}'.strip() # Use full query as city\n"
|
| 118 |
"timezone = get_timezone_by_city(city)\n"
|
| 119 |
"if not timezone.startswith('Error'):\n"
|
| 120 |
" result = get_current_time_in_timezone(timezone)\n"
|
|
|
|
| 171 |
def process_input(user_input):
|
| 172 |
try:
|
| 173 |
response = agent.run(user_input)
|
| 174 |
+
return response if response else "Error: No response received from the agent."
|
| 175 |
except Exception as e:
|
| 176 |
return f"Error: {str(e)}"
|
| 177 |
|