Add tool get_weather
Browse files
app.py
CHANGED
@@ -9,15 +9,30 @@ from Gradio_UI import GradioUI
|
|
9 |
|
10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
11 |
@tool
|
12 |
-
def
|
13 |
-
|
14 |
-
|
15 |
Args:
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
18 |
"""
|
19 |
-
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
@tool
|
22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
23 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
9 |
|
10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
11 |
@tool
|
12 |
+
def get_weather(city: str, api_key: str) -> str:
|
13 |
+
"""Fetches the current weather for a given city using the OpenWeatherMap API.
|
14 |
+
|
15 |
Args:
|
16 |
+
city: The name of the city (e.g., 'New York').
|
17 |
+
api_key: Your OpenWeatherMap API key.
|
18 |
+
|
19 |
+
Returns:
|
20 |
+
A string containing the weather description and temperature.
|
21 |
"""
|
22 |
+
try:
|
23 |
+
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}&units=metric"
|
24 |
+
response = requests.get(url)
|
25 |
+
data = response.json()
|
26 |
+
|
27 |
+
if response.status_code != 200:
|
28 |
+
return f"Error fetching weather: {data.get('message', 'Unknown error')}"
|
29 |
+
|
30 |
+
weather_desc = data["weather"][0]["description"]
|
31 |
+
temp = data["main"]["temp"]
|
32 |
+
return f"The current weather in {city} is {weather_desc} with a temperature of {temp}°C."
|
33 |
+
|
34 |
+
except Exception as e:
|
35 |
+
return f"Error fetching weather data: {str(e)}"
|
36 |
@tool
|
37 |
def get_current_time_in_timezone(timezone: str) -> str:
|
38 |
"""A tool that fetches the current local time in a specified timezone.
|