Spaces:
Sleeping
Sleeping
error handling and location resolution
Browse files- tools/weather.py +33 -14
tools/weather.py
CHANGED
|
@@ -2,6 +2,7 @@ from smolagents.tools import Tool
|
|
| 2 |
import requests
|
| 3 |
from typing import Any, Optional
|
| 4 |
import os # Added for accessing environment variables
|
|
|
|
| 5 |
|
| 6 |
class WeatherTool(Tool):
|
| 7 |
"""Tool for getting weather information for a location."""
|
|
@@ -26,22 +27,40 @@ class WeatherTool(Tool):
|
|
| 26 |
Returns:
|
| 27 |
A string with the current weather information.
|
| 28 |
"""
|
|
|
|
|
|
|
|
|
|
| 29 |
try:
|
| 30 |
-
#
|
| 31 |
-
api_key = os.getenv('OPENWEATHERMAP_API_KEY')
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
else:
|
| 44 |
-
return f"Error fetching weather for {location}: {
|
| 45 |
except Exception as e:
|
| 46 |
return f"Error fetching weather for {location}: {str(e)}"
|
| 47 |
|
|
|
|
| 2 |
import requests
|
| 3 |
from typing import Any, Optional
|
| 4 |
import os # Added for accessing environment variables
|
| 5 |
+
import re
|
| 6 |
|
| 7 |
class WeatherTool(Tool):
|
| 8 |
"""Tool for getting weather information for a location."""
|
|
|
|
| 27 |
Returns:
|
| 28 |
A string with the current weather information.
|
| 29 |
"""
|
| 30 |
+
# Validate that location contains only allowed characters: letters, digits, spaces, and hyphens
|
| 31 |
+
if not re.fullmatch(r'[A-Za-z0-9\s-]+', location):
|
| 32 |
+
return "Error: Location contains invalid characters. Only letters, digits, spaces, and hyphens are allowed."
|
| 33 |
try:
|
| 34 |
+
# Get the API key from environment variables
|
| 35 |
+
api_key = os.getenv('OPENWEATHERMAP_API_KEY')
|
| 36 |
+
if not api_key:
|
| 37 |
+
return "Error: OPENWEATHERMAP_API_KEY not set in environment variables."
|
| 38 |
+
|
| 39 |
+
# First, geocode the location to get latitude and longitude
|
| 40 |
+
geo_url = f"http://api.openweathermap.org/geo/1.0/direct?q={location}&limit=1&appid={api_key}"
|
| 41 |
+
geo_response = requests.get(geo_url)
|
| 42 |
+
geo_data = geo_response.json()
|
| 43 |
+
if not geo_data:
|
| 44 |
+
return f"Error: Could not geocode location '{location}'."
|
| 45 |
+
lat = geo_data[0].get('lat')
|
| 46 |
+
lon = geo_data[0].get('lon')
|
| 47 |
+
if lat is None or lon is None:
|
| 48 |
+
return f"Error: Latitude or longitude not found for location '{location}'."
|
| 49 |
+
|
| 50 |
+
# Call the OpenWeatherMap One Call API 3.0 endpoint
|
| 51 |
+
weather_url = f"https://api.openweathermap.org/data/3.0/onecall?lat={lat}&lon={lon}&exclude=minutely,hourly,daily,alerts&appid={api_key}&units=metric"
|
| 52 |
+
weather_response = requests.get(weather_url)
|
| 53 |
+
weather_data = weather_response.json()
|
| 54 |
+
|
| 55 |
+
if weather_response.status_code == 200:
|
| 56 |
+
current = weather_data.get('current', {})
|
| 57 |
+
temp = current.get('temp', 'N/A')
|
| 58 |
+
weather_desc = current.get('weather', [{}])[0].get('description', 'N/A')
|
| 59 |
+
humidity = current.get('humidity', 'N/A')
|
| 60 |
+
wind_speed = current.get('wind_speed', 'N/A')
|
| 61 |
+
return f"The current weather in {location} is {temp}°C with {weather_desc}. Humidity: {humidity}%, Wind speed: {wind_speed} m/s."
|
| 62 |
else:
|
| 63 |
+
return f"Error fetching weather for {location}: {weather_data.get('message', 'Unknown error')}"
|
| 64 |
except Exception as e:
|
| 65 |
return f"Error fetching weather for {location}: {str(e)}"
|
| 66 |
|