Commit ·
72e59f2
1
Parent(s): 8c5c24b
fully functional
Browse files- app.py +14 -15
- requirements.txt +1 -0
- tools/__pycache__/get_weather.cpython-311.pyc +0 -0
- tools/calculate_tool.py +20 -0
- tools/get_weather.py +43 -0
- tools/visit_webpage.py +1 -0
app.py
CHANGED
|
@@ -1,22 +1,13 @@
|
|
| 1 |
-
from smolagents import CodeAgent,
|
| 2 |
import datetime
|
| 3 |
-
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
| 6 |
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_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:
|
|
@@ -55,7 +46,15 @@ with open("prompts.yaml", 'r') as stream:
|
|
| 55 |
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
|
|
|
| 3 |
import pytz
|
| 4 |
import yaml
|
| 5 |
from tools.final_answer import FinalAnswerTool
|
| 6 |
+
from tools.web_search import DuckDuckGoSearchTool
|
| 7 |
+
from tools.visit_webpage import VisitWebpageTool
|
| 8 |
+
from tools.calculate_tool import CalculateTool
|
| 9 |
from Gradio_UI import GradioUI
|
| 10 |
+
from tools.get_weather import GetWeatherTool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
@tool
|
| 13 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 46 |
|
| 47 |
agent = CodeAgent(
|
| 48 |
model=model,
|
| 49 |
+
tools=[
|
| 50 |
+
final_answer,
|
| 51 |
+
get_current_time_in_timezone,
|
| 52 |
+
CalculateTool(),
|
| 53 |
+
GetWeatherTool(),
|
| 54 |
+
DuckDuckGoSearchTool(),
|
| 55 |
+
VisitWebpageTool(),
|
| 56 |
+
image_generation_tool
|
| 57 |
+
],
|
| 58 |
max_steps=6,
|
| 59 |
verbosity_level=1,
|
| 60 |
grammar=None,
|
requirements.txt
CHANGED
|
@@ -3,3 +3,4 @@ smolagents==1.13.0
|
|
| 3 |
requests
|
| 4 |
duckduckgo_search
|
| 5 |
pandas
|
|
|
|
|
|
| 3 |
requests
|
| 4 |
duckduckgo_search
|
| 5 |
pandas
|
| 6 |
+
pytz
|
tools/__pycache__/get_weather.cpython-311.pyc
ADDED
|
Binary file (2.79 kB). View file
|
|
|
tools/calculate_tool.py
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents.tools import Tool
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
class CalculateTool(Tool):
|
| 5 |
+
name = "calculate"
|
| 6 |
+
description = "Evaluates a mathematical expression safely."
|
| 7 |
+
inputs = {
|
| 8 |
+
"expression": {
|
| 9 |
+
"type": "string",
|
| 10 |
+
"description": "A mathematical expression (e.g., '2 * 3 + 5')",
|
| 11 |
+
}
|
| 12 |
+
}
|
| 13 |
+
output_type = "string"
|
| 14 |
+
|
| 15 |
+
def forward(self, expression: str) -> str:
|
| 16 |
+
try:
|
| 17 |
+
result = eval(expression, {"__builtins__": {}}, {})
|
| 18 |
+
return f"The result of '{expression}' is: {result}"
|
| 19 |
+
except Exception as e:
|
| 20 |
+
return f"Error calculating '{expression}': {str(e)}"
|
tools/get_weather.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from asyncio import timeout
|
| 2 |
+
from warnings import catch_warnings
|
| 3 |
+
import os
|
| 4 |
+
from smolagents.tools import Tool
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
class GetWeatherTool(Tool):
|
| 8 |
+
name="get_weather"
|
| 9 |
+
description="Get weather information for a specific city using OpenWeatherAPI. Use this tool to fetch current weather data for a given city using the OpenWeatherAPI (e.g., 'Delhi')."
|
| 10 |
+
inputs={'city':{'type':'string',
|
| 11 |
+
'description':'The name of the city for which to fetch weather information (e.g., "Delhi").'
|
| 12 |
+
}
|
| 13 |
+
}
|
| 14 |
+
output_type="string"
|
| 15 |
+
|
| 16 |
+
def __init__(self, api_key:str=None, timeout:int=10):
|
| 17 |
+
super().__init__()
|
| 18 |
+
self.api_key=api_key or os.getenv("OPENWEATHER_API_KEY")
|
| 19 |
+
self.timeout=timeout
|
| 20 |
+
self.base_url="http://api.openweathermap.org/data/2.5/weather"
|
| 21 |
+
|
| 22 |
+
self.session=requests.Session()
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def forward(self, city:str) -> str:
|
| 26 |
+
params={
|
| 27 |
+
'q':city,
|
| 28 |
+
'appid':self.api_key,
|
| 29 |
+
'units':'metric'
|
| 30 |
+
}
|
| 31 |
+
try:
|
| 32 |
+
res=self.session.get(self.base_url, params=params, timeout=self.timeout)
|
| 33 |
+
res.raise_for_status()
|
| 34 |
+
data=res.json()
|
| 35 |
+
temp=data['main']['temp']
|
| 36 |
+
desc=data['weather'][0]['description']
|
| 37 |
+
return f"{city}:{temp} C, {desc}"
|
| 38 |
+
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"failed to fetch weather for {city}: {str(e)}"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
tools/visit_webpage.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
|
|
|
| 3 |
import requests
|
| 4 |
import markdownify
|
| 5 |
import smolagents
|
|
|
|
| 1 |
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
+
import re
|
| 4 |
import requests
|
| 5 |
import markdownify
|
| 6 |
import smolagents
|