ArslanFOX commited on
Commit
cae2cb1
·
verified ·
1 Parent(s): 0719e6d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -26
app.py CHANGED
@@ -61,7 +61,7 @@ def get_air_quality(city: str, lang: str = "en") -> str:
61
 
62
  Args:
63
  city: A string representing the city name (e.g., 'Бирск', 'Sydney').
64
- lang: A string representing the language code (e.g., 'en', 'ru').
65
 
66
  Returns:
67
  A string with air quality index (AQI) and pollutant levels or an error message.
@@ -101,6 +101,13 @@ def get_air_quality(city: str, lang: str = "en") -> str:
101
  "pm2_5": "Мелкие частицы (PM2.5)",
102
  "pm10": "Крупные частицы (PM10)",
103
  "co": "Угарный газ (CO)"
 
 
 
 
 
 
 
104
  }
105
  }
106
  lang = lang if lang in translations else "en"
@@ -122,7 +129,7 @@ def get_weather(city: str, lang: str = "en") -> str:
122
 
123
  Args:
124
  city: A string representing the city name (e.g., 'Бирск', 'Sydney').
125
- lang: A string representing the language code (e.g., 'en', 'ru').
126
 
127
  Returns:
128
  A string with weather conditions, temperature, and humidity or an error message.
@@ -161,7 +168,8 @@ def get_weather(city: str, lang: str = "en") -> str:
161
  "Snow": "Snow ❄️",
162
  "Thunderstorm": "Thunderstorm ⛈",
163
  "Drizzle": "Drizzle 🌦",
164
- "Mist": "Mist 🌫"
 
165
  }
166
  },
167
  "ru": {
@@ -175,7 +183,23 @@ def get_weather(city: str, lang: str = "en") -> str:
175
  "Snow": "Снег ❄️",
176
  "Thunderstorm": "Гроза ⛈",
177
  "Drizzle": "Морось 🌦",
178
- "Mist": "Туман 🌫"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
179
  }
180
  }
181
  }
@@ -218,15 +242,20 @@ prompt_templates = {
218
  "If the query contains a city name (e.g., 'Бураево', 'Sydney', 'न्यू यॉर्क') or phrases like 'сколько времени', 'what time', "
219
  "or a city in any context (e.g., 'Что там в Бирске?'), return current time, air quality, and weather for that city. "
220
  "Use ONLY the provided functions: `get_timezone_by_city`, `get_current_time_in_timezone`, `get_air_quality`, `get_weather`, `final_answer`. "
221
- "Detect query language with `detect` and pass it to `get_air_quality` and `get_weather` for translated output. "
222
  "Return ONLY the result via `final_answer()` in a code block:\n"
223
  "```py\n"
224
  "final_answer('YOUR ANSWER HERE')\n"
225
  "```\n"
226
- "Do NOT include 'thoughts', 'code', explanations, extra imports, or invalid dates. "
227
- "Do NOT define new functions or modify existing ones. "
228
  "If the city is not found or query is unclear, return an error via `final_answer()`. "
229
- "NEVER generate code without ```py``` block or with invalid syntax."
 
 
 
 
 
 
230
  ),
231
  "default": "Response: {{question}}",
232
  "planning": {
@@ -256,17 +285,21 @@ prompt_templates = {
256
  "Return code:\n"
257
  "```py\n"
258
  "from langdetect import detect\n"
259
- "query = '{{question}}'.strip()\n"
260
- "city = query.split(' в ')[-1].strip().title() if ' в ' in query else query.title()\n"
261
- "lang = detect(query) if query else 'en'\n"
262
- "timezone = get_timezone_by_city(city)\n"
263
- "if timezone.startswith('Error'):\n"
264
- " final_answer(timezone)\n"
265
  "else:\n"
266
- " time_result = get_current_time_in_timezone(timezone)\n"
267
- " air_result = get_air_quality(city, lang)\n"
268
- " weather_result = get_weather(city, lang)\n"
269
- " final_answer(f'🕒 Время в {city}: {time_result}\\n' + air_result + '\\n' + weather_result)\n"
 
 
 
 
 
 
270
  "```"
271
  ),
272
  "report": (
@@ -302,7 +335,7 @@ prompt_templates = {
302
  agent = CodeAgent(
303
  model=model,
304
  tools=[final_answer, get_current_time_in_timezone, get_timezone_by_city, get_air_quality, get_weather],
305
- max_steps=3,
306
  verbosity_level=2,
307
  prompt_templates=prompt_templates,
308
  )
@@ -314,10 +347,11 @@ def process_input(user_input):
314
  except Exception as e:
315
  return f"Error: {str(e)}"
316
 
317
- gr.Interface(
318
- fn=process_input,
319
- inputs="text",
320
- outputs="text",
321
- title="Helpful Assistant",
322
- description="Hello! What city or town are you from? I can tell you the time, air quality, weather, and more!"
323
- ).launch()
 
 
61
 
62
  Args:
63
  city: A string representing the city name (e.g., 'Бирск', 'Sydney').
64
+ lang: A string representing the language code (e.g., 'en', 'ru', 'hi').
65
 
66
  Returns:
67
  A string with air quality index (AQI) and pollutant levels or an error message.
 
101
  "pm2_5": "Мелкие частицы (PM2.5)",
102
  "pm10": "Крупные частицы (PM10)",
103
  "co": "Угарный газ (CO)"
104
+ },
105
+ "hi": {
106
+ "aqi_desc": {1: "अच्छा", 2: "संतोषजनक", 3: "मध्यम", 4: "खराब", 5: "बहुत खराब"},
107
+ "air_quality": "वायु गुणवत्ता",
108
+ "pm2_5": "बारीक कण (PM2.5)",
109
+ "pm10": "मोटे कण (PM10)",
110
+ "co": "कार्बन मोनोऑक्साइड (CO)"
111
  }
112
  }
113
  lang = lang if lang in translations else "en"
 
129
 
130
  Args:
131
  city: A string representing the city name (e.g., 'Бирск', 'Sydney').
132
+ lang: A string representing the language code (e.g., 'en', 'ru', 'hi').
133
 
134
  Returns:
135
  A string with weather conditions, temperature, and humidity or an error message.
 
168
  "Snow": "Snow ❄️",
169
  "Thunderstorm": "Thunderstorm ⛈",
170
  "Drizzle": "Drizzle 🌦",
171
+ "Mist": "Mist 🌫",
172
+ "Haze": "Haze 🌫"
173
  }
174
  },
175
  "ru": {
 
183
  "Snow": "Снег ❄️",
184
  "Thunderstorm": "Гроза ⛈",
185
  "Drizzle": "Морось 🌦",
186
+ "Mist": "Туман 🌫",
187
+ "Haze": "Туман 🌫"
188
+ }
189
+ },
190
+ "hi": {
191
+ "weather": "मौसम",
192
+ "temp": "तापमान",
193
+ "humidity": "नमी",
194
+ "conditions": {
195
+ "Clear": "साफ ☀️",
196
+ "Clouds": "बादल ☁️",
197
+ "Rain": "बारिश 🌧",
198
+ "Snow": "बर्फ ❄️",
199
+ "Thunderstorm": "तूफान ⛈",
200
+ "Drizzle": "बूंदाबांदी 🌦",
201
+ "Mist": "कोहरा 🌫",
202
+ "Haze": "धुंध 🌫"
203
  }
204
  }
205
  }
 
242
  "If the query contains a city name (e.g., 'Бураево', 'Sydney', 'न्यू यॉर्क') or phrases like 'сколько времени', 'what time', "
243
  "or a city in any context (e.g., 'Что там в Бирске?'), return current time, air quality, and weather for that city. "
244
  "Use ONLY the provided functions: `get_timezone_by_city`, `get_current_time_in_timezone`, `get_air_quality`, `get_weather`, `final_answer`. "
245
+ "Do NOT use `detect` or any other functions directly in the code. "
246
  "Return ONLY the result via `final_answer()` in a code block:\n"
247
  "```py\n"
248
  "final_answer('YOUR ANSWER HERE')\n"
249
  "```\n"
250
+ "Do NOT include 'thoughts', explanations, extra imports, new functions, or text outside the ```py``` block. "
 
251
  "If the city is not found or query is unclear, return an error via `final_answer()`. "
252
+ "Ensure the output matches the format:\n"
253
+ "🕒 Время в [City]: [Time]\n"
254
+ "😷 [Air Quality Label]: AQI [Number] ([Description]) [Emoji]\n"
255
+ "[Pollutants]\n"
256
+ "🌡️ [Weather Label]: [Condition]\n"
257
+ "[Temperature Label]: [Temp]°C\n"
258
+ "[Humidity Label]: [Humidity]%"
259
  ),
260
  "default": "Response: {{question}}",
261
  "planning": {
 
285
  "Return code:\n"
286
  "```py\n"
287
  "from langdetect import detect\n"
288
+ "query = '{{question}}'.strip().lower()\n"
289
+ "city = query.split(' в ')[-1].strip().title() if ' в ' in query else query.strip().title()\n"
290
+ "if city in ['привет', 'что', 'там', 'сколько', 'времени', '']:\n"
291
+ " final_answer('Error: Please specify a valid city name')\n"
 
 
292
  "else:\n"
293
+ " lang = detect(query) if query else 'en'\n"
294
+ " city_normalized = city.replace('Нью-Дели', 'New Delhi').replace('Нью-Йорк', 'New York')\n"
295
+ " timezone = get_timezone_by_city(city_normalized)\n"
296
+ " if timezone.startswith('Error'):\n"
297
+ " final_answer(timezone)\n"
298
+ " else:\n"
299
+ " time_result = get_current_time_in_timezone(timezone)\n"
300
+ " air_result = get_air_quality(city_normalized, lang)\n"
301
+ " weather_result = get_weather(city_normalized, lang)\n"
302
+ " final_answer(f'🕒 Время в {city_normalized}: {time_result}\\n' + air_result + '\\n' + weather_result)\n"
303
  "```"
304
  ),
305
  "report": (
 
335
  agent = CodeAgent(
336
  model=model,
337
  tools=[final_answer, get_current_time_in_timezone, get_timezone_by_city, get_air_quality, get_weather],
338
+ max_steps=1,
339
  verbosity_level=2,
340
  prompt_templates=prompt_templates,
341
  )
 
347
  except Exception as e:
348
  return f"Error: {str(e)}"
349
 
350
+ if __name__ == "__main__":
351
+ gr.Interface(
352
+ fn=process_input,
353
+ inputs="text",
354
+ outputs="text",
355
+ title="Helpful Assistant",
356
+ description="Hello! What city or town are you from? I can tell you the time, air quality, weather, and more!"
357
+ ).launch()