Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool,HfApiModel,load_tool,tool
|
| 2 |
-
import datetime
|
| 3 |
-
from duckduckgo_search import DDGS
|
| 4 |
import requests
|
| 5 |
import pytz
|
| 6 |
import yaml
|
|
@@ -9,85 +8,54 @@ from tools.final_answer import FinalAnswerTool
|
|
| 9 |
|
| 10 |
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
"""
|
|
|
|
| 14 |
|
| 15 |
Args:
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
Returns:
|
| 19 |
-
|
| 20 |
"""
|
| 21 |
-
|
| 22 |
-
ddgs = DDGS()
|
| 23 |
-
results = DuckDuckGoSearchTool.text(web_search, max_results=10)
|
| 24 |
-
if not results:
|
| 25 |
-
raise Exception("No results found! Try a less restrictive/shorter query.")
|
| 26 |
-
|
| 27 |
-
postprocessed_results = [
|
| 28 |
-
f"[{r['title']}]({r['href']})\n{r['body']}" for r in results
|
| 29 |
-
]
|
| 30 |
-
return "## Search Results\n\n" + "\n\n".join(postprocessed_results)
|
| 31 |
|
| 32 |
-
@tool
|
| 33 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 34 |
-
"""
|
|
|
|
|
|
|
| 35 |
Args:
|
| 36 |
-
timezone: A
|
|
|
|
|
|
|
|
|
|
| 37 |
"""
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
tz = pytz.timezone(timezone)
|
| 41 |
-
# Get current time in that timezone
|
| 42 |
-
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
| 43 |
-
return f"The current local time in {timezone} is: {local_time}"
|
| 44 |
-
except Exception as e:
|
| 45 |
-
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
| 46 |
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
final_answer = FinalAnswerTool()
|
| 49 |
|
| 50 |
# 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:
|
| 51 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 52 |
|
| 53 |
-
model = HfApiModel(
|
| 54 |
-
max_tokens=2096,
|
| 55 |
-
temperature=0.5,
|
| 56 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded
|
| 57 |
-
custom_role_conversions=None,
|
| 58 |
-
)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
# Import tool from Hub
|
| 62 |
-
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 63 |
-
|
| 64 |
-
with open("prompts.yaml", 'r') as stream:
|
| 65 |
-
prompt_templates = yaml.safe_load(stream)
|
| 66 |
-
|
| 67 |
-
# Tools list (no registry object needed)
|
| 68 |
tool=[
|
| 69 |
-
|
| 70 |
get_current_time_in_timezone,
|
| 71 |
FinalAnswerTool(),
|
| 72 |
-
load_tool("agents-course/text-to-image", trust_remote_code=True),
|
| 73 |
]
|
| 74 |
|
| 75 |
-
model = HfApiModel(
|
| 76 |
-
model_id="Qwen/Qwen2.5-Coder-32B-Instruct",
|
| 77 |
-
max_tokens=2048,
|
| 78 |
-
temperature=0.5
|
| 79 |
-
)
|
| 80 |
-
with open("prompts.yaml", "r") as stream:
|
| 81 |
-
prompt_templates = yaml.safe_load(stream)
|
| 82 |
-
|
| 83 |
-
# --- Agent setup ---
|
| 84 |
-
|
| 85 |
agent = CodeAgent(
|
| 86 |
-
|
| 87 |
model= model,
|
| 88 |
add_base_tools=True,
|
| 89 |
-
name="MyAgent",
|
| 90 |
-
description="Agent with web search and time lookup tools"
|
| 91 |
)
|
| 92 |
|
| 93 |
|
|
|
|
| 1 |
from smolagents import CodeAgent,DuckDuckGoSearchTool,HfApiModel,load_tool,tool
|
| 2 |
+
import datetime
|
|
|
|
| 3 |
import requests
|
| 4 |
import pytz
|
| 5 |
import yaml
|
|
|
|
| 8 |
|
| 9 |
|
| 10 |
@tool
|
| 11 |
+
def add_numbers(a: int, b: int) -> int:
|
| 12 |
+
"""
|
| 13 |
+
Add two numbers together.
|
| 14 |
|
| 15 |
Args:
|
| 16 |
+
a (int): The first number.
|
| 17 |
+
b (int): The second number.
|
| 18 |
|
| 19 |
Returns:
|
| 20 |
+
int: The sum of the two numbers.
|
| 21 |
"""
|
| 22 |
+
return a + b
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
@tool
|
| 25 |
def get_current_time_in_timezone(timezone: str) -> str:
|
| 26 |
+
"""
|
| 27 |
+
Get the current time in a given timezone.
|
| 28 |
+
|
| 29 |
Args:
|
| 30 |
+
timezone (str): A valid timezone string (e.g., 'America/New_York').
|
| 31 |
+
|
| 32 |
+
Returns:
|
| 33 |
+
str: The current time in that timezone, formatted as a string.
|
| 34 |
"""
|
| 35 |
+
from datetime import datetime
|
| 36 |
+
import pytz
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
tz = pytz.timezone(timezone)
|
| 39 |
+
now = datetime.now(tz)
|
| 40 |
+
return now.strftime("%Y-%m-%d %H:%M:%S %Z")
|
| 41 |
|
| 42 |
final_answer = FinalAnswerTool()
|
| 43 |
|
| 44 |
# 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:
|
| 45 |
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
| 46 |
|
| 47 |
+
model = HfApiModel(model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it is possible that this model may be overloaded)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
tool=[
|
| 50 |
+
add_numbers,
|
| 51 |
get_current_time_in_timezone,
|
| 52 |
FinalAnswerTool(),
|
|
|
|
| 53 |
]
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
agent = CodeAgent(
|
| 56 |
+
tool=tool
|
| 57 |
model= model,
|
| 58 |
add_base_tools=True,
|
|
|
|
|
|
|
| 59 |
)
|
| 60 |
|
| 61 |
|