Added tools app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,27 @@
|
|
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:
|
23 |
"""A tool that fetches the current local time in a specified timezone.
|
@@ -33,29 +37,28 @@ def get_current_time_in_timezone(timezone: str) -> str:
|
|
33 |
except Exception as e:
|
34 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
35 |
|
36 |
-
|
37 |
final_answer = FinalAnswerTool()
|
38 |
|
39 |
-
#
|
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 |
-
model_id='Qwen/Qwen2.5-Coder-32B-Instruct'
|
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", 'r') as stream:
|
54 |
prompt_templates = yaml.safe_load(stream)
|
55 |
-
|
|
|
56 |
agent = CodeAgent(
|
57 |
model=model,
|
58 |
-
tools=[final_answer
|
59 |
max_steps=6,
|
60 |
verbosity_level=1,
|
61 |
grammar=None,
|
@@ -65,5 +68,5 @@ agent = CodeAgent(
|
|
65 |
prompt_templates=prompt_templates
|
66 |
)
|
67 |
|
68 |
-
|
69 |
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 |
+
# DuckDuckGo Search Tool (Renamed to match expected tool name)
|
10 |
@tool
|
11 |
+
def DuckDuckGoSearchTool(query: str) -> str:
|
12 |
+
"""A tool that performs a DuckDuckGo search and returns the top search result.
|
|
|
13 |
Args:
|
14 |
+
query: A search query string.
|
|
|
15 |
"""
|
16 |
+
response = requests.get(f"https://api.duckduckgo.com/?q={query}&format=json")
|
17 |
+
data = response.json()
|
18 |
+
if "AbstractText" in data and data["AbstractText"]:
|
19 |
+
return data["AbstractText"]
|
20 |
+
elif "RelatedTopics" in data and data["RelatedTopics"]:
|
21 |
+
return data["RelatedTopics"][0].get("Text", "No relevant results found.")
|
22 |
+
return "No relevant search results found."
|
23 |
|
24 |
+
# Timezone Tool
|
25 |
@tool
|
26 |
def get_current_time_in_timezone(timezone: str) -> str:
|
27 |
"""A tool that fetches the current local time in a specified timezone.
|
|
|
37 |
except Exception as e:
|
38 |
return f"Error fetching time for timezone '{timezone}': {str(e)}"
|
39 |
|
40 |
+
# Final Answer Tool
|
41 |
final_answer = FinalAnswerTool()
|
42 |
|
43 |
+
# AI Model Configuration
|
|
|
|
|
44 |
model = HfApiModel(
|
45 |
+
max_tokens=2096,
|
46 |
+
temperature=0.5,
|
47 |
+
model_id='Qwen/Qwen2.5-Coder-32B-Instruct', # it is possible that this model may be overloaded
|
48 |
+
custom_role_conversions=None,
|
49 |
)
|
50 |
|
51 |
+
# Import Image Generation Tool (Ensured correct name)
|
|
|
52 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
53 |
|
54 |
+
# Load Prompt Templates
|
55 |
with open("prompts.yaml", 'r') as stream:
|
56 |
prompt_templates = yaml.safe_load(stream)
|
57 |
+
|
58 |
+
# Create the Agent with Correct Tool Names
|
59 |
agent = CodeAgent(
|
60 |
model=model,
|
61 |
+
tools=[final_answer, DuckDuckGoSearchTool, get_current_time_in_timezone, image_generation_tool], # Fixed tool names
|
62 |
max_steps=6,
|
63 |
verbosity_level=1,
|
64 |
grammar=None,
|
|
|
68 |
prompt_templates=prompt_templates
|
69 |
)
|
70 |
|
71 |
+
# Launch Gradio UI
|
72 |
GradioUI(agent).launch()
|