Spaces:
Sleeping
Sleeping
Add weather tool
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import requests
|
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
@@ -18,6 +19,33 @@ 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_current_time_in_timezone(timezone: str) -> str:
|
23 |
"""A tool that fetches the current local time in a specified timezone.
|
@@ -55,7 +83,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer, get_current_time_in_timezone], ## add your tools here (don't remove final answer)
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
|
|
4 |
import pytz
|
5 |
import yaml
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
+
import os
|
8 |
|
9 |
from Gradio_UI import GradioUI
|
10 |
|
|
|
19 |
"""
|
20 |
return "What magic will you build ?"
|
21 |
|
22 |
+
@tool
|
23 |
+
def get_weather(city: str) -> str:
|
24 |
+
"""
|
25 |
+
A tool that fetches the current weather for a given city.
|
26 |
+
Args:
|
27 |
+
city (str): Name of the city.
|
28 |
+
Returns:
|
29 |
+
str: Weather description or error message.
|
30 |
+
"""
|
31 |
+
api_key = "f0b344c8b3a80c96c6753b593d936659"
|
32 |
+
base_url = "https://api.openweathermap.org/data/2.5/weather"
|
33 |
+
params = {"q": city, "appid": api_key, "units": "metric", "lang": "en"}
|
34 |
+
|
35 |
+
try:
|
36 |
+
response = requests.get(base_url, params=params)
|
37 |
+
response.raise_for_status() # Raise an exception for HTTP errors
|
38 |
+
|
39 |
+
data = response.json()
|
40 |
+
weather_desc = data["weather"][0]["description"]
|
41 |
+
temp = data["main"]["temp"]
|
42 |
+
return f"The weather in {city} is {weather_desc} with a temperature of {temp}°C."
|
43 |
+
|
44 |
+
except requests.exceptions.HTTPError as e:
|
45 |
+
return f"HTTP Error: {e.response.status_code} - {e.response.reason}"
|
46 |
+
except Exception as e:
|
47 |
+
return f"Error fetching weather: {str(e)}"
|
48 |
+
|
49 |
@tool
|
50 |
def get_current_time_in_timezone(timezone: str) -> str:
|
51 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
83 |
|
84 |
agent = CodeAgent(
|
85 |
model=model,
|
86 |
+
tools=[final_answer, get_current_time_in_timezone, get_weather], ## add your tools here (don't remove final answer)
|
87 |
max_steps=6,
|
88 |
verbosity_level=1,
|
89 |
grammar=None,
|