Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,13 +33,8 @@ 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 |
-
|
| 38 |
-
"""A tool that fetches the current weather of a specified city.
|
| 39 |
-
Args:
|
| 40 |
-
city: A string representing a city
|
| 41 |
-
"""
|
| 42 |
-
# Endpoint lat e long extraction
|
| 43 |
geocoding_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1"
|
| 44 |
geocoding_response = requests.get(geocoding_url)
|
| 45 |
|
|
@@ -49,20 +44,66 @@ def get_weather(city: str) -> str:
|
|
| 49 |
# Estrai le coordinate della città
|
| 50 |
latitude = data['results'][0]['latitude']
|
| 51 |
longitude = data['results'][0]['longitude']
|
| 52 |
-
|
| 53 |
-
weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t_weather=true&temperature_unit=celsius" # In Celsius
|
| 54 |
-
weather_response = requests.get(weather_url)
|
| 55 |
-
if weather_response.status_code == 200:
|
| 56 |
-
weather_data = weather_response.json()
|
| 57 |
-
unit = weather_data['current_weather_units']['temperature']
|
| 58 |
-
temperature = weather_data['current_weather']['temperature']
|
| 59 |
-
return f"{temperature}{unit}"
|
| 60 |
-
else:
|
| 61 |
-
return "Error on fetching weather data."
|
| 62 |
else:
|
| 63 |
print(f"City {city} not found.")
|
| 64 |
else:
|
| 65 |
print("Error on fetching geo coordinates.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
final_answer = FinalAnswerTool()
|
| 68 |
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
+
def get_geo_coordinates(city):
|
| 37 |
+
# Endpoint lat e long extraction
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
geocoding_url = f"https://geocoding-api.open-meteo.com/v1/search?name={city}&count=1"
|
| 39 |
geocoding_response = requests.get(geocoding_url)
|
| 40 |
|
|
|
|
| 44 |
# Estrai le coordinate della città
|
| 45 |
latitude = data['results'][0]['latitude']
|
| 46 |
longitude = data['results'][0]['longitude']
|
| 47 |
+
return latitude, longitude
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
else:
|
| 49 |
print(f"City {city} not found.")
|
| 50 |
else:
|
| 51 |
print("Error on fetching geo coordinates.")
|
| 52 |
+
|
| 53 |
+
def get_weather_description(code):
|
| 54 |
+
weather_codes = {
|
| 55 |
+
0: "Clear sky",
|
| 56 |
+
1: "Mainly clear",
|
| 57 |
+
2: "Partly cloudy",
|
| 58 |
+
3: "Overcast",
|
| 59 |
+
45: "Fog and depositing rime fog",
|
| 60 |
+
48: "Fog and depositing rime fog",
|
| 61 |
+
51: "Drizzle: Light intensity",
|
| 62 |
+
53: "Drizzle: Moderate intensity",
|
| 63 |
+
55: "Drizzle: Dense intensity",
|
| 64 |
+
56: "Freezing Drizzle: Light intensity",
|
| 65 |
+
57: "Freezing Drizzle: Dense intensity",
|
| 66 |
+
61: "Rain: Slight intensity",
|
| 67 |
+
63: "Rain: Moderate intensity",
|
| 68 |
+
65: "Rain: Heavy intensity",
|
| 69 |
+
66: "Freezing Rain: Light intensity",
|
| 70 |
+
67: "Freezing Rain: Heavy intensity",
|
| 71 |
+
71: "Snow fall: Slight intensity",
|
| 72 |
+
73: "Snow fall: Moderate intensity",
|
| 73 |
+
75: "Snow fall: Heavy intensity",
|
| 74 |
+
77: "Snow grains",
|
| 75 |
+
80: "Rain showers: Slight intensity",
|
| 76 |
+
81: "Rain showers: Moderate intensity",
|
| 77 |
+
82: "Rain showers: Violent intensity",
|
| 78 |
+
85: "Snow showers: Slight intensity",
|
| 79 |
+
86: "Snow showers: Heavy intensity",
|
| 80 |
+
95: "Thunderstorm: Slight or moderate",
|
| 81 |
+
96: "Thunderstorm with slight hail",
|
| 82 |
+
99: "Thunderstorm with heavy hail"
|
| 83 |
+
}
|
| 84 |
+
return weather_code.get(code, "Code not found")
|
| 85 |
+
|
| 86 |
+
@tool
|
| 87 |
+
def get_weather(city: str) -> str:
|
| 88 |
+
"""A tool that fetches the current weather of a specified city.
|
| 89 |
+
Args:
|
| 90 |
+
city: A string representing a city
|
| 91 |
+
"""
|
| 92 |
+
latitude, longitude = get_geo_coordinates(city)
|
| 93 |
+
weather_url = f"https://api.open-meteo.com/v1/forecast?latitude={latitude}&longitude={longitude}¤t_weather=true&temperature_unit=celsius" # In Celsius
|
| 94 |
+
weather_response = requests.get(weather_url)
|
| 95 |
+
if weather_response.status_code == 200:
|
| 96 |
+
weather_data = weather_response.json()
|
| 97 |
+
unit = weather_data['current_weather_units']['temperature']
|
| 98 |
+
temperature = weather_data['current_weather']['temperature']
|
| 99 |
+
windspeed = weather_data['current_weather']['windspeed']
|
| 100 |
+
wind_unit = weather_data['current_weather_units']['windspeed']
|
| 101 |
+
weather_description = get_weather_description(weather_data['current_weather']['weathercode'])
|
| 102 |
+
return f"The current temperature on {city} is {temperature}{unit}, the windspeed is {windspeed}{wind_unit} and the condition is {weather_description}"
|
| 103 |
+
else:
|
| 104 |
+
return "Error on fetching weather data."
|
| 105 |
+
|
| 106 |
+
|
| 107 |
|
| 108 |
final_answer = FinalAnswerTool()
|
| 109 |
|