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, optionally with region (e.g., 'Бураево', 'Осиновка, Бирский район').
|
| 14 |
Returns:
|
| 15 |
A string with the timezone (e.g., 'Europe/Samara') or an error message.
|
| 16 |
"""
|
|
@@ -18,14 +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 |
-
#
|
| 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,95 +69,34 @@ except Exception as e:
|
|
| 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
|
| 73 |
-
"If the query contains a city name (e.g., '
|
| 74 |
-
"
|
| 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 |
-
"
|
| 80 |
-
"Execute
|
| 81 |
-
"
|
| 82 |
"If the city is not found or the query is unclear, return an error message in the same language via final_answer(). "
|
| 83 |
-
"
|
| 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"
|
| 92 |
-
"final_answer('YOUR ANSWER HERE')\n"
|
| 93 |
-
"```"
|
| 94 |
-
),
|
| 95 |
-
"update_plan_pre_messages": (
|
| 96 |
-
"Review the query: {{question}} and previous steps. "
|
| 97 |
-
"If the plan needs adjustment (e.g., incorrect city or timezone), update it. "
|
| 98 |
-
"Write corrected code in a code block:\n"
|
| 99 |
-
"```py\n"
|
| 100 |
-
"final_answer('YOUR ANSWER HERE')\n"
|
| 101 |
-
"```"
|
| 102 |
-
),
|
| 103 |
-
"update_plan_post_messages": (
|
| 104 |
-
"Review the query: {{question}} and execution results. "
|
| 105 |
-
"If the result is incorrect or incomplete, adjust the plan and code. "
|
| 106 |
-
"Return the result via final_answer() in a code block:\n"
|
| 107 |
-
"```py\n"
|
| 108 |
-
"final_answer('YOUR ANSWER HERE')\n"
|
| 109 |
-
"```"
|
| 110 |
-
)
|
| 111 |
-
},
|
| 112 |
"managed_agent": {
|
| 113 |
"execute": (
|
| 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()
|
| 118 |
"timezone = get_timezone_by_city(city)\n"
|
| 119 |
"if not timezone.startswith('Error'):\n"
|
| 120 |
" result = get_current_time_in_timezone(timezone)\n"
|
| 121 |
" final_answer(result)\n"
|
| 122 |
"else:\n"
|
| 123 |
-
" final_answer(timezone)
|
| 124 |
"```"
|
| 125 |
),
|
| 126 |
-
"report": (
|
| 127 |
-
"Summarize the execution of the task: {{question}}. "
|
| 128 |
-
"Return the result via final_answer() in a code block:\n"
|
| 129 |
-
"```py\n"
|
| 130 |
-
"final_answer('YOUR ANSWER HERE')\n"
|
| 131 |
-
"```"
|
| 132 |
-
),
|
| 133 |
-
"task": (
|
| 134 |
-
"Define the task based on the query: {{question}}. "
|
| 135 |
-
"If it contains a city name or phrases like 'сколько времени', use `get_timezone_by_city` and `get_current_time_in_timezone`. "
|
| 136 |
-
"Return the task definition via final_answer() in a code block:\n"
|
| 137 |
-
"```py\n"
|
| 138 |
-
"final_answer('YOUR TASK DEFINITION HERE')\n"
|
| 139 |
-
"```"
|
| 140 |
-
)
|
| 141 |
},
|
| 142 |
-
"final_answer": {
|
| 143 |
-
"pre_messages": (
|
| 144 |
-
"Prepare the final response for the query: {{question}}. "
|
| 145 |
-
"Ensure the answer is clear and in the same language as the query. "
|
| 146 |
-
"Return the response via final_answer() in a code block:\n"
|
| 147 |
-
"```py\n"
|
| 148 |
-
"final_answer('YOUR ANSWER HERE')\n"
|
| 149 |
-
"```"
|
| 150 |
-
),
|
| 151 |
-
"template": "Final response: {{answer}}",
|
| 152 |
-
"post_messages": (
|
| 153 |
-
"Review the formatted response for the query: {{question}}. "
|
| 154 |
-
"Ensure the response is accurate and complete. "
|
| 155 |
-
"Return the final response via final_answer() in a code block:\n"
|
| 156 |
-
"```py\n"
|
| 157 |
-
"final_answer('YOUR ANSWER HERE')\n"
|
| 158 |
-
"```"
|
| 159 |
-
)
|
| 160 |
-
}
|
| 161 |
}
|
| 162 |
|
| 163 |
agent = CodeAgent(
|
|
|
|
| 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 or country (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. Try a different spelling or provide more context."
|
| 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, images, or repeating instructions. "
|
| 73 |
+
"If the query contains a city name (e.g., 'Бураево', 'Осиновка', 'Fosnavåg', 'न्यू यॉर्क') or phrases like 'сколько времени', 'what time', 'quelle heure', "
|
| 74 |
+
"use `get_timezone_by_city` to find the timezone, then `get_current_time_in_timezone` to get the time. "
|
|
|
|
| 75 |
"Always return the response via `final_answer()` in a code block like this:\n"
|
| 76 |
"```py\n"
|
| 77 |
"final_answer('YOUR ANSWER HERE')\n"
|
| 78 |
+
"```"
|
| 79 |
+
"Execute tools in sequence: first call get_timezone_by_city(city), then get_current_time_in_timezone(timezone). "
|
| 80 |
+
"Do NOT include tool calls or formatting like 'Current time in X: ' inside final_answer(). "
|
| 81 |
"If the city is not found or the query is unclear, return an error message in the same language via final_answer(). "
|
| 82 |
+
"NEVER generate code without the ```py``` block or with invalid syntax."
|
| 83 |
),
|
| 84 |
"default": "Response: {{question}}",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
"managed_agent": {
|
| 86 |
"execute": (
|
| 87 |
"Execute the task: {{question}}. If it contains a city name or phrases like 'сколько времени', use `get_timezone_by_city` and `get_current_time_in_timezone`. "
|
| 88 |
"Write code in a code block:\n"
|
| 89 |
"```py\n"
|
| 90 |
+
"city = '{{question}}'.strip()\n"
|
| 91 |
"timezone = get_timezone_by_city(city)\n"
|
| 92 |
"if not timezone.startswith('Error'):\n"
|
| 93 |
" result = get_current_time_in_timezone(timezone)\n"
|
| 94 |
" final_answer(result)\n"
|
| 95 |
"else:\n"
|
| 96 |
+
" final_answer(timezone)\n"
|
| 97 |
"```"
|
| 98 |
),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
}
|
| 101 |
|
| 102 |
agent = CodeAgent(
|