Spaces:
Sleeping
Sleeping
Commit
·
9729baf
1
Parent(s):
ae7a494
feat: add tools and format the code base
Browse files- .gitignore +1 -0
- app.py +42 -21
- tools/final_answer.py +1 -1
- tools/visit_webpage.py +5 -5
- tools/web_search.py +0 -2
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
.venv
|
app.py
CHANGED
|
@@ -1,22 +1,41 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
arg2: the second argument
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -25,9 +44,7 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 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:
|
|
@@ -37,25 +54,29 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 41 |
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
|
| 46 |
-
|
|
|
|
| 47 |
)
|
| 48 |
|
| 49 |
|
| 50 |
# Import tool from Hub
|
| 51 |
-
image_generation_tool = load_tool(
|
|
|
|
| 52 |
|
| 53 |
with open("prompts.yaml", 'r') as stream:
|
| 54 |
prompt_templates = yaml.safe_load(stream)
|
| 55 |
-
|
| 56 |
agent = CodeAgent(
|
| 57 |
model=model,
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
max_steps=6,
|
| 60 |
verbosity_level=1,
|
| 61 |
grammar=None,
|
|
@@ -66,4 +87,4 @@ agent = CodeAgent(
|
|
| 66 |
)
|
| 67 |
|
| 68 |
|
| 69 |
-
GradioUI(agent).launch()
|
|
|
|
|
|
|
| 1 |
import datetime
|
|
|
|
| 2 |
import pytz
|
| 3 |
import yaml
|
|
|
|
| 4 |
|
| 5 |
from Gradio_UI import GradioUI
|
| 6 |
|
| 7 |
+
from smolagents import CodeAgent, HfApiModel, load_tool, tool
|
| 8 |
+
|
| 9 |
+
from tools.final_answer import FinalAnswerTool
|
| 10 |
+
from tools.web_search import DuckDuckGoSearchTool
|
| 11 |
+
from tools.visit_webpage import VisitWebpageTool
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
@tool
|
| 15 |
+
def visit_webpage(url: str) -> str:
|
| 16 |
+
"""Visits a webpage at the given URL and reads its content as a markdown string
|
| 17 |
+
Args:
|
| 18 |
+
url: The URL of the webpage to visit (e.g., 'http://www.wikipedia.com'.).
|
| 19 |
+
"""
|
| 20 |
+
visit_webpage = VisitWebpageTool()
|
| 21 |
+
|
| 22 |
+
webpage_content = visit_webpage.forward(url)
|
| 23 |
+
|
| 24 |
+
return webpage_content
|
| 25 |
+
|
| 26 |
+
|
| 27 |
@tool
|
| 28 |
+
def duck_duck_go_search(query: str) -> str:
|
| 29 |
+
"""A tool that makes a web search request using DuckDuckGo.
|
|
|
|
| 30 |
Args:
|
| 31 |
+
query: a string with the user's web search query (e.g., 'When was the city of London founded?').
|
|
|
|
| 32 |
"""
|
| 33 |
+
search = DuckDuckGoSearchTool()
|
| 34 |
+
|
| 35 |
+
search_result = search.forward(query)
|
| 36 |
+
|
| 37 |
+
return search_result
|
| 38 |
+
|
| 39 |
|
| 40 |
@tool
|
| 41 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 44 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
| 45 |
"""
|
| 46 |
try:
|
|
|
|
| 47 |
tz = pytz.timezone(timezone)
|
|
|
|
| 48 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 49 |
return f"The current local time in {timezone} is: {local_time}"
|
| 50 |
except Exception as e:
|
|
|
|
| 54 |
final_answer = FinalAnswerTool()
|
| 55 |
|
| 56 |
# If the agent does not answer, the model is overloaded, please use another model or the following Hugging Face Endpoint that also contains qwen2.5 coder:
|
| 57 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 58 |
|
| 59 |
model = HfApiModel(
|
| 60 |
+
max_tokens=2096,
|
| 61 |
+
temperature=0.5,
|
| 62 |
+
# it is possible that this model may be overloaded
|
| 63 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
| 64 |
+
custom_role_conversions=None,
|
| 65 |
)
|
| 66 |
|
| 67 |
|
| 68 |
# Import tool from Hub
|
| 69 |
+
image_generation_tool = load_tool(
|
| 70 |
+
"agents-course/text-to-image", trust_remote_code=True)
|
| 71 |
|
| 72 |
with open("prompts.yaml", 'r') as stream:
|
| 73 |
prompt_templates = yaml.safe_load(stream)
|
| 74 |
+
|
| 75 |
agent = CodeAgent(
|
| 76 |
model=model,
|
| 77 |
+
# add your tools here (don't remove final answer)
|
| 78 |
+
tools=[final_answer, image_generation_tool,
|
| 79 |
+
duck_duck_go_search, visit_webpage],
|
| 80 |
max_steps=6,
|
| 81 |
verbosity_level=1,
|
| 82 |
grammar=None,
|
|
|
|
| 87 |
)
|
| 88 |
|
| 89 |
|
| 90 |
+
GradioUI(agent).launch()
|
tools/final_answer.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from typing import Any
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
|
| 4 |
class FinalAnswerTool(Tool):
|
|
|
|
| 1 |
+
from typing import Any
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
|
| 4 |
class FinalAnswerTool(Tool):
|
tools/visit_webpage.py
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
-
|
| 4 |
-
import markdownify
|
| 5 |
-
import smolagents
|
| 6 |
|
| 7 |
class VisitWebpageTool(Tool):
|
| 8 |
name = "visit_webpage"
|
| 9 |
description = "Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."
|
| 10 |
-
inputs = {'url': {'type': 'string',
|
|
|
|
| 11 |
output_type = "string"
|
| 12 |
|
| 13 |
def forward(self, url: str) -> str:
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
|
| 3 |
from smolagents.tools import Tool
|
| 4 |
+
|
|
|
|
|
|
|
| 5 |
|
| 6 |
class VisitWebpageTool(Tool):
|
| 7 |
name = "visit_webpage"
|
| 8 |
description = "Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."
|
| 9 |
+
inputs = {'url': {'type': 'string',
|
| 10 |
+
'description': 'The url of the webpage to visit.'}}
|
| 11 |
output_type = "string"
|
| 12 |
|
| 13 |
def forward(self, url: str) -> str:
|
tools/web_search.py
CHANGED
|
@@ -1,6 +1,4 @@
|
|
| 1 |
-
from typing import Any, Optional
|
| 2 |
from smolagents.tools import Tool
|
| 3 |
-
import duckduckgo_search
|
| 4 |
|
| 5 |
class DuckDuckGoSearchTool(Tool):
|
| 6 |
name = "web_search"
|
|
|
|
|
|
|
| 1 |
from smolagents.tools import Tool
|
|
|
|
| 2 |
|
| 3 |
class DuckDuckGoSearchTool(Tool):
|
| 4 |
name = "web_search"
|