Spaces:
Sleeping
Sleeping
update get_weather tool
Browse files
app.py
CHANGED
|
@@ -36,10 +36,16 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 36 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 37 |
|
| 38 |
@tool
|
| 39 |
-
def get_weather(location: str, api_key: str) ->
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
Args:
|
| 42 |
-
location:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
base_url = "https://api.openweathermap.org/data/2.5/weather"
|
| 45 |
params = {
|
|
@@ -50,15 +56,15 @@ def get_weather(location: str, api_key: str) -> str:
|
|
| 50 |
|
| 51 |
try:
|
| 52 |
response = requests.get(base_url, params=params)
|
| 53 |
-
response.raise_for_status()
|
| 54 |
data = response.json()
|
| 55 |
|
| 56 |
weather_info = {
|
| 57 |
-
"city": data
|
| 58 |
-
"temperature": data["main"]
|
| 59 |
-
"description": data["weather"][0]
|
| 60 |
-
"humidity": data["main"]
|
| 61 |
-
"wind_speed": data["wind"]
|
| 62 |
}
|
| 63 |
return weather_info
|
| 64 |
except requests.exceptions.RequestException as e:
|
|
|
|
| 36 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 37 |
|
| 38 |
@tool
|
| 39 |
+
def get_weather(location: str, api_key: str) -> dict:
|
| 40 |
+
"""
|
| 41 |
+
A tool that fetches the current weather data for a specified location.
|
| 42 |
+
|
| 43 |
Args:
|
| 44 |
+
location (str): The city name to get weather data (e.g., 'New York').
|
| 45 |
+
api_key (str): Your OpenWeatherMap API key.
|
| 46 |
+
|
| 47 |
+
Returns:
|
| 48 |
+
dict: A dictionary containing weather details or an error message.
|
| 49 |
"""
|
| 50 |
base_url = "https://api.openweathermap.org/data/2.5/weather"
|
| 51 |
params = {
|
|
|
|
| 56 |
|
| 57 |
try:
|
| 58 |
response = requests.get(base_url, params=params)
|
| 59 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
| 60 |
data = response.json()
|
| 61 |
|
| 62 |
weather_info = {
|
| 63 |
+
"city": data.get("name"),
|
| 64 |
+
"temperature": data["main"].get("temp"),
|
| 65 |
+
"description": data["weather"][0].get("description"),
|
| 66 |
+
"humidity": data["main"].get("humidity"),
|
| 67 |
+
"wind_speed": data["wind"].get("speed")
|
| 68 |
}
|
| 69 |
return weather_info
|
| 70 |
except requests.exceptions.RequestException as e:
|