Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,21 +33,17 @@ 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 |
@tool
|
| 37 |
-
def
|
| 38 |
"""
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
city_name: 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)
|
|
@@ -59,27 +55,18 @@ def get_city_coordinates(city_name: str) -> dict:
|
|
| 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 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 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 |
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
+
|
| 37 |
@tool
|
| 38 |
+
def get_weather_by_city(city_name: str) -> str:
|
| 39 |
"""
|
| 40 |
+
A tool that returns the current weather for the specified city.
|
| 41 |
+
Args:
|
| 42 |
+
city_name: city name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
+
# Получаем координаты города
|
| 45 |
# Формируем URL для запроса к API
|
| 46 |
url = f"https://geocoding-api.open-meteo.com/v1/search?name={city_name}&count=1&language=en&format=json"
|
|
|
|
| 47 |
try:
|
| 48 |
# Отправляем GET-запрос к API
|
| 49 |
response = requests.get(url)
|
|
|
|
| 55 |
# Проверяем, есть ли результаты
|
| 56 |
if "results" in data and len(data["results"]) > 0:
|
| 57 |
city_data = data["results"][0]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
else:
|
| 59 |
return {"error": "Город не найден"}
|
| 60 |
|
| 61 |
except requests.RequestException as e:
|
| 62 |
return {"error": f"Ошибка при запросе к API: {str(e)}"}
|
| 63 |
except Exception as e:
|
| 64 |
+
return {"error": f"Неожиданная ошибка: {str(e)}"}
|
| 65 |
+
|
| 66 |
+
coordinates = {
|
| 67 |
+
"latitude": city_data["latitude"],
|
| 68 |
+
"longitude": city_data["longitude"]
|
| 69 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
if "error" in coordinates:
|
| 71 |
return coordinates["error"]
|
| 72 |
|