Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,6 +33,65 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
|
@@ -55,7 +114,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
tools=[final_answer,
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
+
@tool
|
| 37 |
+
def get_city_coordinates(city_name: str) -> dict:
|
| 38 |
+
"""
|
| 39 |
+
Returns coordinates (latitude and longitude) for the specified city.
|
| 40 |
+
|
| 41 |
+
Arguments:
|
| 42 |
+
city_name (str): The name of the city, for example, "Berlin".
|
| 43 |
+
|
| 44 |
+
Returns:
|
| 45 |
+
dict: A dictionary with the keys " latitude "and " longitude".
|
| 46 |
+
If the city is not found, returns {"error": "City not found"}.
|
| 47 |
+
"""
|
| 48 |
+
# Формируем URL для запроса к API
|
| 49 |
+
url = f"https://geocoding-api.open-meteo.com/v1/search?name={city_name}&count=1&language=en&format=json"
|
| 50 |
+
|
| 51 |
+
try:
|
| 52 |
+
# Отправляем GET-запрос к API
|
| 53 |
+
response = requests.get(url)
|
| 54 |
+
response.raise_for_status() # Проверяем на ошибки HTTP
|
| 55 |
+
|
| 56 |
+
# Парсим JSON-ответ
|
| 57 |
+
data = response.json()
|
| 58 |
+
|
| 59 |
+
# Проверяем, есть ли результаты
|
| 60 |
+
if "results" in data and len(data["results"]) > 0:
|
| 61 |
+
city_data = data["results"][0]
|
| 62 |
+
return {
|
| 63 |
+
"latitude": city_data["latitude"],
|
| 64 |
+
"longitude": city_data["longitude"]
|
| 65 |
+
}
|
| 66 |
+
else:
|
| 67 |
+
return {"error": "Город не найден"}
|
| 68 |
+
|
| 69 |
+
except requests.RequestException as e:
|
| 70 |
+
return {"error": f"Ошибка при запросе к API: {str(e)}"}
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return {"error": f"Неожиданная ошибка: {str(e)}"}
|
| 73 |
+
|
| 74 |
+
@tool
|
| 75 |
+
def get_weather_by_city(city_name: str) -> str:
|
| 76 |
+
"""
|
| 77 |
+
A tool that returns the current weather for the specified city.
|
| 78 |
+
Args:
|
| 79 |
+
city_name: city name
|
| 80 |
+
"""
|
| 81 |
+
# Получаем координаты города
|
| 82 |
+
coordinates = get_city_coordinates(city_name)
|
| 83 |
+
if "error" in coordinates:
|
| 84 |
+
return coordinates["error"]
|
| 85 |
+
|
| 86 |
+
# Используем координаты для запроса погоды
|
| 87 |
+
weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={coordinates['latitude']}&longitude={coordinates['longitude']}¤t_weather=true"
|
| 88 |
+
try:
|
| 89 |
+
response = requests.get(weather_url)
|
| 90 |
+
response.raise_for_status()
|
| 91 |
+
weather_data = response.json()
|
| 92 |
+
return f"Текущая температура в {city_name}: {weather_data['current_weather']['temperature']}°C"
|
| 93 |
+
except requests.RequestException as e:
|
| 94 |
+
return f"Ошибка при запросе погоды: {str(e)}"
|
| 95 |
|
| 96 |
final_answer = FinalAnswerTool()
|
| 97 |
|
|
|
|
| 114 |
|
| 115 |
agent = CodeAgent(
|
| 116 |
model=model,
|
| 117 |
+
tools=[final_answer, get_weather_by_city()], ## add your tools here (don't remove final answer)
|
| 118 |
max_steps=6,
|
| 119 |
verbosity_level=1,
|
| 120 |
grammar=None,
|