Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -18,35 +18,37 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
|
|
|
|
|
|
| 21 |
@tool
|
| 22 |
def get_weather(city: str) -> str:
|
| 23 |
"""
|
| 24 |
-
Fetches the current weather for a specified city using OpenWeatherMap API.
|
| 25 |
Args:
|
| 26 |
-
city: The name of the city (e.g., '
|
| 27 |
"""
|
| 28 |
-
API_KEY = "
|
| 29 |
BASE_URL = "http://api.openweathermap.org/data/2.5/weather"
|
| 30 |
-
params =
|
| 31 |
"q": city,
|
| 32 |
"appid": API_KEY,
|
| 33 |
-
"units": "metric"
|
| 34 |
}
|
| 35 |
try:
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
data =
|
| 39 |
-
|
| 40 |
weather_description = data['weather'][0]['description']
|
| 41 |
temp = data['main']['temp']
|
| 42 |
feels_like = data['main']['feels_like']
|
| 43 |
humidity = data['main']['humidity']
|
| 44 |
-
|
| 45 |
-
|
| 46 |
return (f"The weather in {city} is currently {weather_description}. "
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
except requests.exceptions.HTTPError as http_err:
|
| 51 |
if response.status_code == 401:
|
| 52 |
return "Error: Invalid API key. Please check your OpenWeatherMap API key."
|
|
|
|
| 18 |
"""
|
| 19 |
return "What magic will you build ?"
|
| 20 |
|
| 21 |
+
import requests
|
| 22 |
+
|
| 23 |
@tool
|
| 24 |
def get_weather(city: str) -> str:
|
| 25 |
"""
|
| 26 |
+
Fetches the current weather for a specified city using the OpenWeatherMap API.
|
| 27 |
Args:
|
| 28 |
+
city: The name of the city (e.g., 'Paris', 'Tokyo').
|
| 29 |
"""
|
| 30 |
+
API_KEY = "YOUR_API_KEY" # <-- IMPORTANT: Replace with your actual API key
|
| 31 |
BASE_URL = "http://api.openweathermap.org/data/2.5/weather"
|
| 32 |
+
params = {
|
| 33 |
"q": city,
|
| 34 |
"appid": API_KEY,
|
| 35 |
+
"units": "metric" # Use 'imperial' for Fahrenheit
|
| 36 |
}
|
| 37 |
try:
|
| 38 |
+
response = requests.get(BASE_URL, params=params)
|
| 39 |
+
response.raise_for_status() # Raises an exception for bad status codes (4xx or 5xx)
|
| 40 |
+
data = response.json()
|
| 41 |
+
|
| 42 |
weather_description = data['weather'][0]['description']
|
| 43 |
temp = data['main']['temp']
|
| 44 |
feels_like = data['main']['feels_like']
|
| 45 |
humidity = data['main']['humidity']
|
| 46 |
+
wind_speed = data['wind']['speed']
|
| 47 |
+
|
| 48 |
return (f"The weather in {city} is currently {weather_description}. "
|
| 49 |
+
f"Temperature: {temp}°C, feels like {feels_like}°C. "
|
| 50 |
+
f"Humidity is at {humidity}% and wind speed is {wind_speed} m/s.")
|
| 51 |
+
|
| 52 |
except requests.exceptions.HTTPError as http_err:
|
| 53 |
if response.status_code == 401:
|
| 54 |
return "Error: Invalid API key. Please check your OpenWeatherMap API key."
|