Spaces:
Sleeping
Sleeping
Update app.py
Browse filesadded duckduckgo
app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
@@ -7,27 +7,35 @@ 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 |
-
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|
| 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:
|
|
@@ -36,33 +44,39 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
| 36 |
|
| 37 |
final_answer = FinalAnswerTool()
|
| 38 |
|
| 39 |
-
# If the agent does not answer, the model is overloaded
|
| 40 |
-
#
|
|
|
|
| 41 |
|
| 42 |
model = HfApiModel(
|
| 43 |
-
max_tokens=2096,
|
| 44 |
-
temperature=0.5,
|
| 45 |
-
model_id=
|
| 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",
|
| 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,
|
| 62 |
planning_interval=None,
|
| 63 |
name=None,
|
| 64 |
description=None,
|
| 65 |
-
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
|
|
|
|
| 1 |
+
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool, tool
|
| 2 |
import datetime
|
| 3 |
import requests
|
| 4 |
import pytz
|
|
|
|
| 7 |
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
+
|
| 11 |
@tool
|
| 12 |
+
def duckduckgo_search(query: str, max_results: int = 5) -> str:
|
| 13 |
+
"""Searches DuckDuckGo and returns the top results.
|
|
|
|
| 14 |
Args:
|
| 15 |
+
query: The search query to look up.
|
| 16 |
+
max_results: The maximum number of search results to return.
|
| 17 |
"""
|
| 18 |
+
try:
|
| 19 |
+
search_tool = DuckDuckGoSearchTool()
|
| 20 |
+
results = search_tool(query)
|
| 21 |
+
|
| 22 |
+
if not results:
|
| 23 |
+
return "No results found."
|
| 24 |
+
|
| 25 |
+
return str(results)
|
| 26 |
+
|
| 27 |
+
except Exception as e:
|
| 28 |
+
return f"Error searching DuckDuckGo: {str(e)}"
|
| 29 |
+
|
| 30 |
|
| 31 |
@tool
|
| 32 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 33 |
"""A tool that fetches the current local time in a specified timezone.
|
| 34 |
Args:
|
| 35 |
+
timezone: A string representing a valid timezone, for example 'America/New_York'.
|
| 36 |
"""
|
| 37 |
try:
|
|
|
|
| 38 |
tz = pytz.timezone(timezone)
|
|
|
|
| 39 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 40 |
return f"The current local time in {timezone} is: {local_time}"
|
| 41 |
except Exception as e:
|
|
|
|
| 44 |
|
| 45 |
final_answer = FinalAnswerTool()
|
| 46 |
|
| 47 |
+
# If the agent does not answer, the model is overloaded.
|
| 48 |
+
# You can use another model or this Hugging Face Endpoint that also contains Qwen2.5 Coder:
|
| 49 |
+
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 50 |
|
| 51 |
model = HfApiModel(
|
| 52 |
+
max_tokens=2096,
|
| 53 |
+
temperature=0.5,
|
| 54 |
+
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 55 |
+
custom_role_conversions=None,
|
| 56 |
)
|
| 57 |
|
| 58 |
|
| 59 |
# Import tool from Hub
|
| 60 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 61 |
|
| 62 |
+
with open("prompts.yaml", "r") as stream:
|
| 63 |
prompt_templates = yaml.safe_load(stream)
|
| 64 |
+
|
| 65 |
+
|
| 66 |
agent = CodeAgent(
|
| 67 |
model=model,
|
| 68 |
+
tools=[
|
| 69 |
+
duckduckgo_search,
|
| 70 |
+
get_current_time_in_timezone,
|
| 71 |
+
final_answer,
|
| 72 |
+
],
|
| 73 |
max_steps=6,
|
| 74 |
verbosity_level=1,
|
| 75 |
grammar=None,
|
| 76 |
planning_interval=None,
|
| 77 |
name=None,
|
| 78 |
description=None,
|
| 79 |
+
prompt_templates=prompt_templates,
|
| 80 |
)
|
| 81 |
|
| 82 |
|