ahmedelgendy commited on
Commit
e795a77
·
verified ·
1 Parent(s): 560d019

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -27
app.py CHANGED
@@ -13,7 +13,7 @@ from PIL import Image
13
  import io
14
  import base64
15
 
16
- # Weather tool using a free API (Weather API by API-Ninjas)
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
- api_url = f"https://api.weatherapi.com/v1/current.json"
 
26
  params = {
27
- 'key': '80ca2c7d7e0945b583b153111241802', # Free API key with limited usage
28
  'q': location,
29
- 'aqi': 'no'
 
 
 
 
30
  }
31
 
32
- response = requests.get(api_url, params=params)
33
- data = response.json()
34
-
35
- if response.status_code == 200:
36
- location_name = data['location']['name']
37
- region = data['location']['region']
38
- country = data['location']['country']
39
- temperature_c = data['current']['temp_c']
40
- temperature_f = data['current']['temp_f']
41
- condition = data['current']['condition']['text']
42
- humidity = data['current']['humidity']
43
- wind_kph = data['current']['wind_kph']
44
- feels_like_c = data['current']['feelslike_c']
45
-
46
- return f"Weather in {location_name}, {region}, {country}:\n" \
47
- f"Condition: {condition}\n" \
48
- f"Temperature: {temperature_c}°C / {temperature_f}°F\n" \
49
- f"Feels like: {feels_like_c}°C\n" \
50
- f"Humidity: {humidity}%\n" \
51
- f"Wind Speed: {wind_kph} km/h"
52
- else:
53
- error_msg = data.get('error', {}).get('message', 'Unknown error')
54
- return f"Error fetching weather for {location}: {error_msg}"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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