Update app.py
Browse files
app.py
CHANGED
@@ -13,7 +13,7 @@ from PIL import Image
|
|
13 |
import io
|
14 |
import base64
|
15 |
|
16 |
-
# Weather tool using
|
17 |
@tool
|
18 |
def get_weather(location: str) -> str:
|
19 |
"""Fetch current weather information for a specified location.
|
@@ -22,36 +22,74 @@ def get_weather(location: str) -> str:
|
|
22 |
location: A string representing the city name.
|
23 |
"""
|
24 |
try:
|
25 |
-
|
|
|
26 |
params = {
|
27 |
-
'key': '80ca2c7d7e0945b583b153111241802', # Free API key with limited usage
|
28 |
'q': location,
|
29 |
-
'
|
|
|
|
|
|
|
|
|
30 |
}
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
if
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
except Exception as e:
|
56 |
return f"Error fetching weather for {location}: {str(e)}"
|
57 |
|
|
|
13 |
import io
|
14 |
import base64
|
15 |
|
16 |
+
# Weather tool using Open Meteo (completely free, no API key required)
|
17 |
@tool
|
18 |
def get_weather(location: str) -> str:
|
19 |
"""Fetch current weather information for a specified location.
|
|
|
22 |
location: A string representing the city name.
|
23 |
"""
|
24 |
try:
|
25 |
+
# First, geocode the location to get coordinates
|
26 |
+
geocoding_url = f"https://nominatim.openstreetmap.org/search"
|
27 |
params = {
|
|
|
28 |
'q': location,
|
29 |
+
'format': 'json',
|
30 |
+
'limit': 1
|
31 |
+
}
|
32 |
+
headers = {
|
33 |
+
'User-Agent': 'WeatherToolAgent/1.0' # OpenStreetMap requires a user agent
|
34 |
}
|
35 |
|
36 |
+
geo_response = requests.get(geocoding_url, params=params, headers=headers)
|
37 |
+
geo_data = geo_response.json()
|
38 |
+
|
39 |
+
if not geo_data:
|
40 |
+
return f"Could not find coordinates for location: {location}"
|
41 |
+
|
42 |
+
# Extract latitude and longitude
|
43 |
+
lat = float(geo_data[0]['lat'])
|
44 |
+
lon = float(geo_data[0]['lon'])
|
45 |
+
display_name = geo_data[0]['display_name']
|
46 |
+
|
47 |
+
# Now get weather data from Open-Meteo
|
48 |
+
weather_url = "https://api.open-meteo.com/v1/forecast"
|
49 |
+
weather_params = {
|
50 |
+
'latitude': lat,
|
51 |
+
'longitude': lon,
|
52 |
+
'current': 'temperature_2m,relative_humidity_2m,apparent_temperature,precipitation,weather_code,wind_speed_10m',
|
53 |
+
'timezone': 'auto'
|
54 |
+
}
|
55 |
+
|
56 |
+
weather_response = requests.get(weather_url, params=weather_params)
|
57 |
+
weather_data = weather_response.json()
|
58 |
+
|
59 |
+
if 'current' not in weather_data:
|
60 |
+
return f"Error fetching weather data for {location}"
|
61 |
+
|
62 |
+
# WMO Weather interpretation codes (https://open-meteo.com/en/docs)
|
63 |
+
weather_codes = {
|
64 |
+
0: "Clear sky",
|
65 |
+
1: "Mainly clear", 2: "Partly cloudy", 3: "Overcast",
|
66 |
+
45: "Fog", 48: "Depositing rime fog",
|
67 |
+
51: "Light drizzle", 53: "Moderate drizzle", 55: "Dense drizzle",
|
68 |
+
56: "Light freezing drizzle", 57: "Dense freezing drizzle",
|
69 |
+
61: "Slight rain", 63: "Moderate rain", 65: "Heavy rain",
|
70 |
+
66: "Light freezing rain", 67: "Heavy freezing rain",
|
71 |
+
71: "Slight snow fall", 73: "Moderate snow fall", 75: "Heavy snow fall",
|
72 |
+
77: "Snow grains",
|
73 |
+
80: "Slight rain showers", 81: "Moderate rain showers", 82: "Violent rain showers",
|
74 |
+
85: "Slight snow showers", 86: "Heavy snow showers",
|
75 |
+
95: "Thunderstorm", 96: "Thunderstorm with slight hail", 99: "Thunderstorm with heavy hail"
|
76 |
+
}
|
77 |
+
|
78 |
+
current = weather_data['current']
|
79 |
+
weather_description = weather_codes.get(current['weather_code'], "Unknown")
|
80 |
+
|
81 |
+
weather_info = (
|
82 |
+
f"Weather in {display_name}:\n"
|
83 |
+
f"Condition: {weather_description}\n"
|
84 |
+
f"Temperature: {current['temperature_2m']}°C\n"
|
85 |
+
f"Feels like: {current['apparent_temperature']}°C\n"
|
86 |
+
f"Humidity: {current['relative_humidity_2m']}%\n"
|
87 |
+
f"Wind Speed: {current['wind_speed_10m']} km/h\n"
|
88 |
+
f"Precipitation: {current['precipitation']} mm"
|
89 |
+
)
|
90 |
+
|
91 |
+
return weather_info
|
92 |
+
|
93 |
except Exception as e:
|
94 |
return f"Error fetching weather for {location}: {str(e)}"
|
95 |
|