Spaces:
Runtime error
Runtime error
add location and weather tool
Browse files- app.py +14 -43
- requirements.txt +2 -0
- tools/__pycache__/final_answer.cpython-310.pyc +0 -0
- tools/__pycache__/get_location.cpython-310.pyc +0 -0
- tools/__pycache__/get_weather.cpython-310.pyc +0 -0
- tools/get_location.py +35 -0
- tools/get_weather.py +35 -0
app.py
CHANGED
@@ -1,4 +1,8 @@
|
|
1 |
-
|
|
|
|
|
|
|
|
|
2 |
import datetime
|
3 |
import requests
|
4 |
import pytz
|
@@ -6,56 +10,24 @@ import yaml
|
|
6 |
from tools.final_answer import FinalAnswerTool
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
@tool
|
12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
13 |
-
#Keep this format for the description / args / args description but feel free to modify the tool
|
14 |
-
"""A tool that does nothing yet
|
15 |
-
Args:
|
16 |
-
arg1: the first argument
|
17 |
-
arg2: the second argument
|
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.
|
24 |
-
Args:
|
25 |
-
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
26 |
-
"""
|
27 |
-
try:
|
28 |
-
# Create timezone object
|
29 |
-
tz = pytz.timezone(timezone)
|
30 |
-
# Get current time in that timezone
|
31 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
32 |
-
return f"The current local time in {timezone} is: {local_time}"
|
33 |
-
except Exception as e:
|
34 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
-
|
36 |
-
|
37 |
-
final_answer = FinalAnswerTool()
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
max_tokens=
|
44 |
-
temperature=0.5,
|
45 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
46 |
-
custom_role_conversions=None,
|
47 |
)
|
48 |
|
49 |
-
|
50 |
-
# Import tool from Hub
|
51 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
52 |
-
|
53 |
with open("prompts.yaml", 'r') as stream:
|
54 |
prompt_templates = yaml.safe_load(stream)
|
55 |
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
@@ -65,5 +37,4 @@ agent = CodeAgent(
|
|
65 |
prompt_templates=prompt_templates
|
66 |
)
|
67 |
|
68 |
-
|
69 |
GradioUI(agent).launch()
|
|
|
1 |
+
import os
|
2 |
+
|
3 |
+
import dotenv
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from smolagents import CodeAgent, load_tool, tool, OpenAIServerModel
|
6 |
import datetime
|
7 |
import requests
|
8 |
import pytz
|
|
|
10 |
from tools.final_answer import FinalAnswerTool
|
11 |
|
12 |
from Gradio_UI import GradioUI
|
13 |
+
from tools.get_location import GetLocationTool
|
14 |
+
from tools.get_weather import GetWeatherTool
|
15 |
|
16 |
+
load_dotenv()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
18 |
+
model = OpenAIServerModel(
|
19 |
+
"Qwen/Qwen2.5-Coder-32B-Instruct",
|
20 |
+
api_base="https://api.together.xyz/v1",
|
21 |
+
api_key=os.environ.get("TOGETHER_API_KEY"),
|
22 |
+
max_tokens=8096
|
|
|
|
|
|
|
23 |
)
|
24 |
|
|
|
|
|
|
|
|
|
25 |
with open("prompts.yaml", 'r') as stream:
|
26 |
prompt_templates = yaml.safe_load(stream)
|
27 |
|
28 |
agent = CodeAgent(
|
29 |
model=model,
|
30 |
+
tools=[GetLocationTool(), GetWeatherTool(), FinalAnswerTool()], ## add your tools here (don't remove final answer)
|
31 |
max_steps=6,
|
32 |
verbosity_level=1,
|
33 |
grammar=None,
|
|
|
37 |
prompt_templates=prompt_templates
|
38 |
)
|
39 |
|
|
|
40 |
GradioUI(agent).launch()
|
requirements.txt
CHANGED
@@ -3,3 +3,5 @@ smolagents
|
|
3 |
requests
|
4 |
duckduckgo_search
|
5 |
pandas
|
|
|
|
|
|
3 |
requests
|
4 |
duckduckgo_search
|
5 |
pandas
|
6 |
+
dotenv
|
7 |
+
smolagents[openai]
|
tools/__pycache__/final_answer.cpython-310.pyc
ADDED
Binary file (921 Bytes). View file
|
|
tools/__pycache__/get_location.cpython-310.pyc
ADDED
Binary file (1.72 kB). View file
|
|
tools/__pycache__/get_weather.cpython-310.pyc
ADDED
Binary file (1.77 kB). View file
|
|
tools/get_location.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from smolagents.tools import Tool
|
2 |
+
import requests
|
3 |
+
from typing import Any, Optional
|
4 |
+
|
5 |
+
|
6 |
+
class GetLocationTool(Tool):
|
7 |
+
name = "get_location"
|
8 |
+
description = "Get geographical coordinates (lat, lon) by using name of the location (city name or area name)."
|
9 |
+
inputs = {'name': {'type': 'string', 'description': 'The city name or area name of the location to check.'}}
|
10 |
+
output_type = "any"
|
11 |
+
|
12 |
+
def forward(self, name: str) -> Any:
|
13 |
+
try:
|
14 |
+
import requests
|
15 |
+
import os
|
16 |
+
from urllib.parse import quote
|
17 |
+
from requests.exceptions import RequestException
|
18 |
+
except ImportError as e:
|
19 |
+
raise ImportError(
|
20 |
+
"You must install packages `requests` and `urllib` to run this tool: for instance run `pip install requests urllib`."
|
21 |
+
) from e
|
22 |
+
try:
|
23 |
+
url = "http://api.openweathermap.org/geo/1.0/direct?q=" + quote(name) + "&limit=5&appid=" + os.environ["WEATHER_API_KEY"]
|
24 |
+
# Send a GET request to the URL with a 20-second timeout
|
25 |
+
response = requests.get(url, timeout=20)
|
26 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
27 |
+
return response.json()
|
28 |
+
|
29 |
+
except requests.exceptions.Timeout:
|
30 |
+
return "The request timed out. Please try again later or check the URL."
|
31 |
+
except RequestException as e:
|
32 |
+
return f"Error fetching the api: {str(e)}"
|
33 |
+
except Exception as e:
|
34 |
+
return f"An unexpected error occurred: {str(e)}"
|
35 |
+
|
tools/get_weather.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from typing import Any, Optional
|
2 |
+
from smolagents.tools import Tool
|
3 |
+
import requests
|
4 |
+
import smolagents
|
5 |
+
|
6 |
+
class GetWeatherTool(Tool):
|
7 |
+
name = "get_weather"
|
8 |
+
description = "Get the weather information for a given latitude and longitude."
|
9 |
+
inputs = {'lat': {'type': 'number', 'description': 'Latitude, decimal (-90; 90).'}, 'lon': {'type': 'number', 'description': 'Longitude, decimal (-180; 180).'}}
|
10 |
+
output_type = "any"
|
11 |
+
|
12 |
+
def forward(self, lat: float, lon: float) -> Any:
|
13 |
+
try:
|
14 |
+
import requests
|
15 |
+
import os
|
16 |
+
from urllib.parse import quote
|
17 |
+
from requests.exceptions import RequestException
|
18 |
+
except ImportError as e:
|
19 |
+
raise ImportError(
|
20 |
+
"You must install packages `requests` and `urllib` to run this tool: for instance run `pip install requests urllib`."
|
21 |
+
) from e
|
22 |
+
try:
|
23 |
+
url = "https://api.openweathermap.org/data/3.0/onecall?lat=" + str(lat) + "&lon=" + str(lon) + "&appid=" + os.environ["WEATHER_API_KEY"]
|
24 |
+
# Send a GET request to the URL with a 20-second timeout
|
25 |
+
response = requests.get(url, timeout=20)
|
26 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
27 |
+
return response.json()
|
28 |
+
|
29 |
+
except requests.exceptions.Timeout:
|
30 |
+
return "The request timed out. Please try again later or check the URL."
|
31 |
+
except RequestException as e:
|
32 |
+
return f"Error fetching the api: {str(e)}"
|
33 |
+
except Exception as e:
|
34 |
+
return f"An unexpected error occurred: {str(e)}"
|
35 |
+
|