Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,16 +1,14 @@
|
|
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 |
-
#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
|
@@ -25,33 +23,59 @@ 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:
|
34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
final_answer = FinalAnswerTool()
|
|
|
38 |
model = HfApiModel(
|
39 |
-
max_tokens=2096,
|
40 |
-
temperature=0.5,
|
41 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
42 |
-
custom_role_conversions=None,
|
43 |
)
|
44 |
|
45 |
-
|
46 |
-
# Import tool from Hub
|
47 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
48 |
|
49 |
with open("prompts.yaml", 'r') as stream:
|
50 |
prompt_templates = yaml.safe_load(stream)
|
51 |
|
|
|
52 |
agent = CodeAgent(
|
53 |
model=model,
|
54 |
-
tools=[
|
|
|
|
|
|
|
55 |
max_steps=6,
|
56 |
verbosity_level=1,
|
57 |
grammar=None,
|
@@ -61,5 +85,4 @@ agent = CodeAgent(
|
|
61 |
prompt_templates=prompt_templates
|
62 |
)
|
63 |
|
64 |
-
|
65 |
GradioUI(agent).launch()
|
|
|
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 |
from Gradio_UI import GradioUI
|
8 |
|
9 |
+
# Example tool that does nothing yet (you already had this)
|
10 |
@tool
|
11 |
+
def my_custom_tool(arg1: str, arg2: int) -> str:
|
|
|
12 |
"""A tool that does nothing yet
|
13 |
Args:
|
14 |
arg1: the first argument
|
|
|
23 |
timezone: A string representing a valid timezone (e.g., 'America/New_York').
|
24 |
"""
|
25 |
try:
|
|
|
26 |
tz = pytz.timezone(timezone)
|
|
|
27 |
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
|
28 |
return f"The current local time in {timezone} is: {local_time}"
|
29 |
except Exception as e:
|
30 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
31 |
|
32 |
+
# Instantiate the pre-made DuckDuckGoSearchTool.
|
33 |
+
duck_search_tool = DuckDuckGoSearchTool()
|
34 |
+
|
35 |
+
# Define a new tool that retrieves the latest AI news and trends.
|
36 |
+
@tool
|
37 |
+
def get_latest_ai_news() -> str:
|
38 |
+
"""A tool to retrieve the latest AI news and trends.
|
39 |
+
Returns:
|
40 |
+
A string summarizing the latest AI news.
|
41 |
+
"""
|
42 |
+
try:
|
43 |
+
query = "latest AI news and trends"
|
44 |
+
# Use the DuckDuckGoSearchTool's search method.
|
45 |
+
results = duck_search_tool.search(query)
|
46 |
+
# For demonstration, assume results is a list of dicts with a "title" key.
|
47 |
+
if results:
|
48 |
+
# Take the titles of the top 3 search results.
|
49 |
+
titles = [result.get("title", "No Title") for result in results[:3]]
|
50 |
+
summary = " | ".join(titles)
|
51 |
+
return "Latest AI News: " + summary
|
52 |
+
else:
|
53 |
+
return "No recent AI news found."
|
54 |
+
except Exception as e:
|
55 |
+
return f"Error fetching AI news: {str(e)}"
|
56 |
|
57 |
final_answer = FinalAnswerTool()
|
58 |
+
|
59 |
model = HfApiModel(
|
60 |
+
max_tokens=2096,
|
61 |
+
temperature=0.5,
|
62 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
|
63 |
+
custom_role_conversions=None,
|
64 |
)
|
65 |
|
66 |
+
# Import tool from Hub (example: text-to-image tool)
|
|
|
67 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
68 |
|
69 |
with open("prompts.yaml", 'r') as stream:
|
70 |
prompt_templates = yaml.safe_load(stream)
|
71 |
|
72 |
+
# Update the agent's tools list to include multiple tools:
|
73 |
agent = CodeAgent(
|
74 |
model=model,
|
75 |
+
tools=[
|
76 |
+
final_answer,
|
77 |
+
duck_search_tool,
|
78 |
+
get_latest_ai_news],
|
79 |
max_steps=6,
|
80 |
verbosity_level=1,
|
81 |
grammar=None,
|
|
|
85 |
prompt_templates=prompt_templates
|
86 |
)
|
87 |
|
|
|
88 |
GradioUI(agent).launch()
|