Spaces:
Sleeping
Sleeping
add get_weather tools to get weather of a location.
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ from tools.final_answer import FinalAnswerTool
|
|
| 7 |
|
| 8 |
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 my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
|
@@ -33,6 +35,35 @@ 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 |
model = HfApiModel(
|
|
@@ -51,7 +82,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 51 |
|
| 52 |
agent = CodeAgent(
|
| 53 |
model=model,
|
| 54 |
-
tools=[final_answer], ## add your tools here (don't remove final answer)
|
| 55 |
max_steps=6,
|
| 56 |
verbosity_level=1,
|
| 57 |
grammar=None,
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
api_key = '40547464b0a67a8f5d0e5fcd1364a8fe'
|
| 11 |
+
|
| 12 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 13 |
@tool
|
| 14 |
def my_cutom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
|
|
|
| 35 |
except Exception 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) -> str:
|
| 40 |
+
"""A tool that fetches the current weather data in a specified location.
|
| 41 |
+
Args:
|
| 42 |
+
location: A string representing the location to get weather data (e.g., 'New_York').
|
| 43 |
+
"""
|
| 44 |
+
base_url = "https://api.openweathermap.org/data/2.5/weather"
|
| 45 |
+
params = {
|
| 46 |
+
"q": location,
|
| 47 |
+
"appid": api_key,
|
| 48 |
+
"units": "metric"
|
| 49 |
+
}
|
| 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["name"],
|
| 58 |
+
"temperature": data["main"]["temp"],
|
| 59 |
+
"description": data["weather"][0]["description"],
|
| 60 |
+
"humidity": data["main"]["humidity"],
|
| 61 |
+
"wind_speed": data["wind"]["speed"]
|
| 62 |
+
}
|
| 63 |
+
return weather_info
|
| 64 |
+
except requests.exceptions.RequestException as e:
|
| 65 |
+
return {"error": str(e)}
|
| 66 |
+
|
| 67 |
|
| 68 |
final_answer = FinalAnswerTool()
|
| 69 |
model = HfApiModel(
|
|
|
|
| 82 |
|
| 83 |
agent = CodeAgent(
|
| 84 |
model=model,
|
| 85 |
+
tools=[final_answer, get_weather], ## add your tools here (don't remove final answer)
|
| 86 |
max_steps=6,
|
| 87 |
verbosity_level=1,
|
| 88 |
grammar=None,
|