Spaces:
Sleeping
Sleeping
Update app.py
Browse filesadded weather tool
app.py
CHANGED
|
@@ -33,6 +33,56 @@ 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 |
final_answer = FinalAnswerTool()
|
| 38 |
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 35 |
|
| 36 |
+
@tool
|
| 37 |
+
def get_weather(location: str) -> str:
|
| 38 |
+
"""A tool that fetches current weather information for a specified location.
|
| 39 |
+
Args:
|
| 40 |
+
location: A string representing a city name, coordinates, or location (e.g., 'London', 'New York, NY', '40.7128,-74.0060').
|
| 41 |
+
"""
|
| 42 |
+
try:
|
| 43 |
+
# Using wttr.in API which provides weather data in a simple format
|
| 44 |
+
# Adding format=j1 to get JSON response
|
| 45 |
+
url = f"https://wttr.in/{location}?format=j1"
|
| 46 |
+
headers = {'User-Agent': 'Mozilla/5.0'}
|
| 47 |
+
|
| 48 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 49 |
+
response.raise_for_status()
|
| 50 |
+
|
| 51 |
+
data = response.json()
|
| 52 |
+
|
| 53 |
+
# Extract relevant weather information
|
| 54 |
+
current = data['current_condition'][0]
|
| 55 |
+
location_info = data['nearest_area'][0]
|
| 56 |
+
|
| 57 |
+
# Build weather report
|
| 58 |
+
city = location_info.get('areaName', [{}])[0].get('value', location)
|
| 59 |
+
country = location_info.get('country', [{}])[0].get('value', '')
|
| 60 |
+
temp_c = current.get('temp_C', 'N/A')
|
| 61 |
+
temp_f = current.get('temp_F', 'N/A')
|
| 62 |
+
feels_like_c = current.get('FeelsLikeC', 'N/A')
|
| 63 |
+
feels_like_f = current.get('FeelsLikeF', 'N/A')
|
| 64 |
+
description = current.get('weatherDesc', [{}])[0].get('value', 'N/A')
|
| 65 |
+
humidity = current.get('humidity', 'N/A')
|
| 66 |
+
wind_speed_kmph = current.get('windspeedKmph', 'N/A')
|
| 67 |
+
wind_speed_mph = current.get('windspeedMiles', 'N/A')
|
| 68 |
+
wind_dir = current.get('winddir16Point', 'N/A')
|
| 69 |
+
|
| 70 |
+
weather_report = f"""Weather in {city}, {country}:
|
| 71 |
+
Temperature: {temp_c}°C ({temp_f}°F)
|
| 72 |
+
Feels like: {feels_like_c}°C ({feels_like_f}°F)
|
| 73 |
+
Condition: {description}
|
| 74 |
+
Humidity: {humidity}%
|
| 75 |
+
Wind: {wind_speed_kmph} km/h ({wind_speed_mph} mph) {wind_dir}"""
|
| 76 |
+
|
| 77 |
+
return weather_report
|
| 78 |
+
|
| 79 |
+
except requests.exceptions.RequestException as e:
|
| 80 |
+
return f"Error fetching weather data: Network error - {str(e)}"
|
| 81 |
+
except KeyError as e:
|
| 82 |
+
return f"Error parsing weather data: Missing expected data - {str(e)}"
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return f"Error fetching weather for '{location}': {str(e)}"
|
| 85 |
+
|
| 86 |
|
| 87 |
final_answer = FinalAnswerTool()
|
| 88 |
|