Spaces:
Sleeping
Sleeping
File size: 12,956 Bytes
bd563a8 12a8b83 bd563a8 da0140f 61093db ac57ee0 da0140f 4711b64 da0140f ac57ee0 2f374e6 c420643 da0140f cced9c5 491a00e 12a8b83 4711b64 bd563a8 12a8b83 ac57ee0 12a8b83 ee7ca11 12a8b83 61093db ac57ee0 4711b64 61093db ac57ee0 61093db ac57ee0 f73f8cc ac57ee0 f73f8cc ac57ee0 61093db ac57ee0 f73f8cc 61093db ac57ee0 4711b64 ac57ee0 f73f8cc ac57ee0 f73f8cc ac57ee0 f73f8cc ac57ee0 f73f8cc ac57ee0 f73f8cc ac57ee0 bd563a8 4711b64 bd563a8 12a8b83 bd563a8 9796d6e 9a3dde4 bd563a8 ac57ee0 0719e6d 19f9b0f 48a667f 0719e6d f73f8cc 0719e6d f73f8cc 0719e6d f73f8cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
import os
from smolagents import CodeAgent, HfApiModel, tool
import datetime
import pytz
import gradio as gr
from geopy.geocoders import Nominatim
from timezonefinder import TimezoneFinder
import requests
from langdetect import detect
@tool
def get_timezone_by_city(city: str) -> str:
"""Fetches the timezone for a given city using geolocation.
Args:
city: A string representing the city name, optionally with region or country (e.g., 'Бураево', 'Sydney').
Returns:
A string with the timezone (e.g., 'Europe/Moscow') or an error message.
"""
if not isinstance(city, str):
return f"Error: Expected a string for city, got: {type(city)}"
try:
geolocator = Nominatim(user_agent="smolagents_bot")
location = geolocator.geocode(city, language="en")
if not location:
city_name = city.split(",")[0].strip().title()
location = geolocator.geocode(city_name, language="en")
if not location:
return f"Error: City '{city}' not found. Try a different spelling or provide more context."
tf = TimezoneFinder()
timezone = tf.timezone_at(lat=location.latitude, lng=location.longitude)
if not timezone:
return f"Error: Timezone not found for city '{city}'."
return timezone
except Exception as e:
return f"Error fetching timezone for city '{city}': {str(e)}"
@tool
def get_current_time_in_timezone(timezone: str) -> str:
"""Fetches the current local time in a specified timezone.
Args:
timezone: A string representing a valid timezone (e.g., 'Europe/Moscow').
Returns:
A string with the current time (e.g., '2025-05-03 12:00:00') or an error message.
"""
if not isinstance(timezone, str):
return f"Error: Expected a string for timezone, got: {type(timezone)}"
try:
tz = pytz.timezone(timezone)
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return local_time
except Exception as e:
return f"Error fetching time for timezone '{timezone}': {str(e)}"
@tool
def get_air_quality(city: str, lang: str = "en") -> str:
"""Fetches air quality data for a given city using OpenWeatherMap API.
Args:
city: A string representing the city name (e.g., 'Бирск', 'Sydney').
lang: A string representing the language code (e.g., 'en', 'ru').
Returns:
A string with air quality index (AQI) and pollutant levels or an error message.
"""
if not isinstance(city, str):
return f"Error: Expected a string for city, got: {type(city)}"
try:
geolocator = Nominatim(user_agent="smolagents_bot")
location = geolocator.geocode(city, language="en")
if not location:
city_name = city.split(",")[0].strip().title()
location = geolocator.geocode(city_name, language="en")
if not location:
return f"Error: City '{city}' not found for air quality data."
api_key = os.getenv("OPENWEATHERMAP_API_KEY", "93f518fda8d24cf89aee7050c26b27e9")
url = f"http://api.openweathermap.org/data/2.5/air_pollution?lat={location.latitude}&lon={location.longitude}&appid={api_key}"
response = requests.get(url)
if response.status_code != 200:
return f"Error fetching air quality for '{city}': API returned status {response.status_code}"
data = response.json()
aqi = data["list"][0]["main"]["aqi"]
components = data["list"][0]["components"]
translations = {
"en": {
"aqi_desc": {1: "Good", 2: "Fair", 3: "Moderate", 4: "Poor", 5: "Very Poor"},
"air_quality": "Air quality",
"pm2_5": "Fine particles (PM2.5)",
"pm10": "Coarse particles (PM10)",
"co": "Carbon monoxide (CO)"
},
"ru": {
"aqi_desc": {1: "Хорошее", 2: "Удовлетворительное", 3: "Среднее", 4: "Плохое", 5: "Очень плохое"},
"air_quality": "Качество воздуха",
"pm2_5": "Мелкие частицы (PM2.5)",
"pm10": "Крупные частицы (PM10)",
"co": "Угарный газ (CO)"
}
}
lang = lang if lang in translations else "en"
t = translations[lang]
aqi_emoji = "🌱" if aqi <= 2 else "😷" if aqi <= 4 else "☣️"
return (
f"😷 {t['air_quality']}: AQI {aqi} ({t['aqi_desc'].get(aqi, 'Unknown')}) {aqi_emoji}\n"
f"{t['pm2_5']}: {components['pm2_5']} µg/m³\n"
f"{t['pm10']}: {components['pm10']} µg/m³\n"
f"{t['co']}: {components['co']} µg/m³"
)
except Exception as e:
return f"Error fetching air quality for '{city}': {str(e)}"
@tool
def get_weather(city: str, lang: str = "en") -> str:
"""Fetches weather data for a given city using OpenWeatherMap API.
Args:
city: A string representing the city name (e.g., 'Бирск', 'Sydney').
lang: A string representing the language code (e.g., 'en', 'ru').
Returns:
A string with weather conditions, temperature, and humidity or an error message.
"""
if not isinstance(city, str):
return f"Error: Expected a string for city, got: {type(city)}"
try:
geolocator = Nominatim(user_agent="smolagents_bot")
location = geolocator.geocode(city, language="en")
if not location:
city_name = city.split(",")[0].strip().title()
location = geolocator.geocode(city_name, language="en")
if not location:
return f"Error: City '{city}' not found for weather data."
api_key = os.getenv("OPENWEATHERMAP_API_KEY", "93f518fda8d24cf89aee7050c26b27e9")
url = f"http://api.openweathermap.org/data/2.5/weather?lat={location.latitude}&lon={location.longitude}&appid={api_key}&units=metric"
response = requests.get(url)
if response.status_code != 200:
return f"Error fetching weather for '{city}': API returned status {response.status_code}"
data = response.json()
temp = data["main"]["temp"]
weather = data["weather"][0]["main"]
humidity = data["main"]["humidity"]
translations = {
"en": {
"weather": "Weather",
"temp": "Temperature",
"humidity": "Humidity",
"conditions": {
"Clear": "Clear ☀️",
"Clouds": "Cloudy ☁️",
"Rain": "Rain 🌧",
"Snow": "Snow ❄️",
"Thunderstorm": "Thunderstorm ⛈",
"Drizzle": "Drizzle 🌦",
"Mist": "Mist 🌫"
}
},
"ru": {
"weather": "Погода",
"temp": "Температура",
"humidity": "Влажность",
"conditions": {
"Clear": "Ясно ☀️",
"Clouds": "Облачно ☁️",
"Rain": "Дождь 🌧",
"Snow": "Снег ❄️",
"Thunderstorm": "Гроза ⛈",
"Drizzle": "Морось 🌦",
"Mist": "Туман 🌫"
}
}
}
lang = lang if lang in translations else "en"
t = translations[lang]
condition = t["conditions"].get(weather, weather)
return (
f"🌡️ {t['weather']}: {condition}\n"
f"{t['temp']}: {temp}°C\n"
f"{t['humidity']}: {humidity}%"
)
except Exception as e:
return f"Error fetching weather for '{city}': {str(e)}"
@tool
def final_answer(answer: str) -> str:
"""Returns the final answer.
Args:
answer: A string containing the final answer.
Returns:
The input answer string.
"""
return answer
try:
model = HfApiModel(
max_tokens=1000,
temperature=0.5,
model_id='mistralai/Mixtral-8x7B-Instruct-v0.1',
)
except Exception as e:
print(f"Error initializing model: {str(e)}")
prompt_templates = {
"system_prompt": (
"You are a helpful assistant that responds in the same language as the user's query. "
"If the query contains a city name (e.g., 'Бураево', 'Sydney', 'न्यू यॉर्क') or phrases like 'сколько времени', 'what time', "
"or a city in any context (e.g., 'Что там в Бирске?'), return current time, air quality, and weather for that city. "
"Use ONLY the provided functions: `get_timezone_by_city`, `get_current_time_in_timezone`, `get_air_quality`, `get_weather`, `final_answer`. "
"Detect query language with `detect` and pass it to `get_air_quality` and `get_weather` for translated output. "
"Return ONLY the result via `final_answer()` in a code block:\n"
"```py\n"
"final_answer('YOUR ANSWER HERE')\n"
"```\n"
"Do NOT include 'thoughts', 'code', explanations, extra imports, or invalid dates. "
"Do NOT define new functions or modify existing ones. "
"If the city is not found or query is unclear, return an error via `final_answer()`. "
"NEVER generate code without ```py``` block or with invalid syntax."
),
"default": "Response: {{question}}",
"planning": {
"initial_plan": (
"Analyze query: {{question}}. If it contains a city, get time, air quality, and weather using provided functions. "
"Return code:\n"
"```py\n"
"final_answer('YOUR ANSWER HERE')\n"
"```"
),
"update_plan_pre_messages": (
"Review query: {{question}}. Adjust plan. Return code:\n"
"```py\n"
"final_answer('YOUR ANSWER HERE')\n"
"```"
),
"update_plan_post_messages": (
"Review query: {{question}} and results. Adjust plan. Return code:\n"
"```py\n"
"final_answer('YOUR ANSWER HERE')\n"
"```"
),
},
"managed_agent": {
"execute": (
"Execute task: {{question}}. If it contains a city or phrases like 'сколько времени', use provided functions. "
"Return code:\n"
"```py\n"
"from langdetect import detect\n"
"query = '{{question}}'.strip()\n"
"city = query.split(' в ')[-1].strip().title() if ' в ' in query else query.title()\n"
"lang = detect(query) if query else 'en'\n"
"timezone = get_timezone_by_city(city)\n"
"if timezone.startswith('Error'):\n"
" final_answer(timezone)\n"
"else:\n"
" time_result = get_current_time_in_timezone(timezone)\n"
" air_result = get_air_quality(city, lang)\n"
" weather_result = get_weather(city, lang)\n"
" final_answer(f'🕒 Время в {city}: {time_result}\\n' + air_result + '\\n' + weather_result)\n"
"```"
),
"report": (
"Summarize task: {{question}}. Return code:\n"
"```py\n"
"final_answer('YOUR ANSWER HERE')\n"
"```"
),
"task": (
"Define task: {{question}}. Return code:\n"
"```py\n"
"final_answer('YOUR TASK DEFINITION HERE')\n"
"```"
),
},
"final_answer": {
"pre_messages": (
"Prepare response for: {{question}}. Return code:\n"
"```py\n"
"final_answer('YOUR ANSWER HERE')\n"
"```"
),
"template": "Final response: {{answer}}",
"post_messages": (
"Review response for: {{question}}. Return code:\n"
"```py\n"
"final_answer('YOUR ANSWER HERE')\n"
"```"
),
},
}
agent = CodeAgent(
model=model,
tools=[final_answer, get_current_time_in_timezone, get_timezone_by_city, get_air_quality, get_weather],
max_steps=3,
verbosity_level=2,
prompt_templates=prompt_templates,
)
def process_input(user_input):
try:
response = agent.run(user_input)
return response if response else "Error: No response received from the agent."
except Exception as e:
return f"Error: {str(e)}"
gr.Interface(
fn=process_input,
inputs="text",
outputs="text",
title="Helpful Assistant",
description="Hello! What city or town are you from? I can tell you the time, air quality, weather, and more!"
).launch() |