Spaces:
Sleeping
Sleeping
Update tools/weather.py
Browse files- tools/weather.py +74 -64
tools/weather.py
CHANGED
|
@@ -14,72 +14,13 @@ class WeatherForecast(Tool):
|
|
| 14 |
super().__init__()
|
| 15 |
|
| 16 |
def forward(self, city) -> str:
|
| 17 |
-
|
|
|
|
|
|
|
| 18 |
if len(results) == 0:
|
| 19 |
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 20 |
-
|
| 21 |
-
return "## Search Results\n\n"
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def get_weather_forecast(self, city: str) -> str:
|
| 25 |
-
latitude, longitude = self.get_coordinates(city)
|
| 26 |
-
|
| 27 |
-
if latitude is None or longitude is None:
|
| 28 |
-
print(f"Could not get coordinates for {city}. Cannot retrieve weather forecast.")
|
| 29 |
-
return None # Or raise an exception if you prefer
|
| 30 |
-
|
| 31 |
-
base_url = "https://api.open-meteo.com/v1/forecast"
|
| 32 |
-
params = {
|
| 33 |
-
"latitude": latitude,
|
| 34 |
-
"longitude": longitude,
|
| 35 |
-
"hourly": "temperature_2m",
|
| 36 |
-
"daily": "temperature_2m_max,temperature_2m_min,precipitation_sum",
|
| 37 |
-
"forecast_days": 7,
|
| 38 |
-
"timezone": "auto"
|
| 39 |
-
}
|
| 40 |
-
|
| 41 |
-
try:
|
| 42 |
-
response = requests.get(base_url, params=params)
|
| 43 |
-
response.raise_for_status() # Raise an exception for bad status codes
|
| 44 |
-
weather_data = response.json()
|
| 45 |
-
return weather_data
|
| 46 |
-
|
| 47 |
-
except requests.exceptions.RequestException as e:
|
| 48 |
-
print(f"Error getting weather forecast: {e}")
|
| 49 |
-
return None
|
| 50 |
-
|
| 51 |
-
# def get_weather_forecast(self, city_name: str) -> str:
|
| 52 |
-
# """A tool that fetches the current weather/temperature of a specified city.
|
| 53 |
-
# Args:
|
| 54 |
-
# city_name: A string representing a valid city (e.g., 'Bangalore').
|
| 55 |
-
# """
|
| 56 |
-
|
| 57 |
-
# latitude, longitude = self.get_coordinates(city)
|
| 58 |
-
# base_url = "https://api.open-meteo.com/v1/forecast" # No API key needed for this version
|
| 59 |
-
# params = {
|
| 60 |
-
# "latitude": latitude,
|
| 61 |
-
# "longitude": longitude,
|
| 62 |
-
# "hourly": "temperature_2m,precipitation,wind_speed",
|
| 63 |
-
# "daily": "temperature_2m_max,temperature_2m_min,precipitation_sum",
|
| 64 |
-
# "forecast_days": 7,
|
| 65 |
-
# "timezone": "auto"
|
| 66 |
-
# }
|
| 67 |
-
|
| 68 |
-
# try:
|
| 69 |
-
# response = requests.get(base_url, params=params) # No headers needed
|
| 70 |
-
# response.raise_for_status()
|
| 71 |
-
# weatherJSON = response.json()
|
| 72 |
-
# cityName = weatherJSON.get('location').get('name')
|
| 73 |
-
# cityTemp = weatherJSON.get('hourly').get('temperature_2m')
|
| 74 |
-
# averageTemp = round(mean(cityTemp), 1)
|
| 75 |
-
# hourlyunits = weatherJSON.get('hourly_units').get('temperature_2m')
|
| 76 |
-
# return f"The current Temperature in {cityName} is {averageTemp}{hourlyunits}!"
|
| 77 |
-
# except requests.exceptions.RequestException as e:
|
| 78 |
-
# print(f"Error fetching weather data: {e}")
|
| 79 |
-
# return None
|
| 80 |
-
# except json.JSONDecodeError as e:
|
| 81 |
-
# print(f"Error decoding JSON response: {e}")
|
| 82 |
-
# return None
|
| 83 |
|
| 84 |
def get_coordinates(self, city_name: str) -> [float, float]:
|
| 85 |
headers = {
|
|
@@ -104,3 +45,72 @@ class WeatherForecast(Tool):
|
|
| 104 |
except requests.exceptions.RequestException as e:
|
| 105 |
print(f"Error during geocoding: {e}")
|
| 106 |
return None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
super().__init__()
|
| 15 |
|
| 16 |
def forward(self, city) -> str:
|
| 17 |
+
weather = self.get_weather_forecast(city)
|
| 18 |
+
results = self.format_weather_for_agent(city, weather)
|
| 19 |
+
print(results)
|
| 20 |
if len(results) == 0:
|
| 21 |
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 22 |
+
|
| 23 |
+
return f"## Search Results\n\n{results}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
def get_coordinates(self, city_name: str) -> [float, float]:
|
| 26 |
headers = {
|
|
|
|
| 45 |
except requests.exceptions.RequestException as e:
|
| 46 |
print(f"Error during geocoding: {e}")
|
| 47 |
return None, None
|
| 48 |
+
|
| 49 |
+
def get_weather_forecast(self, city: str) -> Optional[dict[str, Any]]:
|
| 50 |
+
"""
|
| 51 |
+
Retrieves a 7-day weather forecast for a given city using the Open-Meteo API.
|
| 52 |
+
|
| 53 |
+
Args:
|
| 54 |
+
city: The name of the city.
|
| 55 |
+
|
| 56 |
+
Returns:
|
| 57 |
+
A dictionary containing the weather forecast data, or None if an error occurs
|
| 58 |
+
or if the city's coordinates cannot be found.
|
| 59 |
+
"""
|
| 60 |
+
latitude, longitude = self.get_coordinates(city)
|
| 61 |
+
if latitude is None or longitude is None:
|
| 62 |
+
print(f"Could not get coordinates for {city}. Cannot retrieve weather forecast.")
|
| 63 |
+
return None
|
| 64 |
+
|
| 65 |
+
base_url = "https://api.open-meteo.com/v1/forecast"
|
| 66 |
+
params = {
|
| 67 |
+
"latitude": latitude,
|
| 68 |
+
"longitude": longitude,
|
| 69 |
+
"hourly": "temperature_2m",
|
| 70 |
+
"daily": "temperature_2m_max,temperature_2m_min,precipitation_sum",
|
| 71 |
+
"forecast_days": 7,
|
| 72 |
+
"timezone": "auto"
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
try:
|
| 76 |
+
response = requests.get(base_url, params=params)
|
| 77 |
+
response.raise_for_status() # Raise HTTPError for bad responses (4xx or 5xx)
|
| 78 |
+
weather_data = response.json()
|
| 79 |
+
return weather_data
|
| 80 |
+
|
| 81 |
+
except requests.exceptions.RequestException as e:
|
| 82 |
+
print(f"Error getting weather forecast: {e}")
|
| 83 |
+
return None
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def format_weather_for_agent(self, city: str, weather_data: dict[str, Any]) -> str:
|
| 87 |
+
"""Formats the weather data into a string suitable for an agent's response."""
|
| 88 |
+
|
| 89 |
+
if weather_data is None:
|
| 90 |
+
return f"Could not retrieve weather forecast for {city}."
|
| 91 |
+
|
| 92 |
+
daily = weather_data.get("daily", {}) # Use .get to handle missing data gracefully
|
| 93 |
+
hourly = weather_data.get("hourly", {})
|
| 94 |
+
|
| 95 |
+
# Example formatting (customize as needed)
|
| 96 |
+
response_str = f"Weather forecast for {city}:\n"
|
| 97 |
+
|
| 98 |
+
if daily:
|
| 99 |
+
response_str += "Daily Forecast:\n"
|
| 100 |
+
for i in range(len(daily.get("temperature_2m_max", []))): # Handle missing data
|
| 101 |
+
max_temp = daily.get("temperature_2m_max", [None])[i] # Use .get with default
|
| 102 |
+
min_temp = daily.get("temperature_2m_min", [None])[i]
|
| 103 |
+
precipitation = daily.get("precipitation_sum", [None])[i]
|
| 104 |
+
response_str += (
|
| 105 |
+
f"Day {i+1}: Max: {max_temp}°C, Min: {min_temp}°C, Precipitation: {precipitation} mm\n"
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
if hourly:
|
| 109 |
+
response_str += "\nHourly Forecast (Example):\n"
|
| 110 |
+
# ... (Add hourly data formatting as needed) ...
|
| 111 |
+
hourly_temps = hourly.get("temperature_2m", [])[:3] # Show first 3 hours
|
| 112 |
+
for i, temp in enumerate(hourly_temps):
|
| 113 |
+
response_str += f"Hour {i+1}: {temp}°C\n"
|
| 114 |
+
# ...
|
| 115 |
+
|
| 116 |
+
return response_str
|