Spaces:
Runtime error
Runtime error
Update app.py
#1
by
wt002
- opened
app.py
CHANGED
@@ -7,45 +7,57 @@ from tools.final_answer import FinalAnswerTool
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
-
|
11 |
-
@tool
|
12 |
-
def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return type
|
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
|
17 |
-
arg2: the second argument
|
18 |
-
"""
|
19 |
-
return "What magic will you build ?"
|
20 |
|
21 |
-
|
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 (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 |
-
|
|
|
|
|
|
|
38 |
|
39 |
-
|
40 |
-
# model_id='https://pflgm2locj2t89co.us-east-1.aws.endpoints.huggingface.cloud'
|
41 |
|
42 |
-
model
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
)
|
48 |
|
|
|
|
|
49 |
|
50 |
# Import tool from Hub
|
51 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
|
|
7 |
|
8 |
from Gradio_UI import GradioUI
|
9 |
|
10 |
+
!pip install duckduckgo-search --upgrade
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
from duckduckgo_search import DDGS
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# Custom search tool class
|
15 |
+
class CustomDuckDuckGoSearchTool:
|
16 |
+
def __call__(self, query: str, max_results: int = 5):
|
17 |
+
try:
|
18 |
+
with DDGS() as ddgs:
|
19 |
+
results = []
|
20 |
+
for r in ddgs.text(query):
|
21 |
+
results.append(r)
|
22 |
+
if len(results) >= max_results:
|
23 |
+
break
|
24 |
+
return results
|
25 |
+
except Exception as e:
|
26 |
+
return f"Search error: {str(e)}"
|
27 |
|
28 |
+
# Dummy placeholder for `visit_webpage` tool
|
29 |
+
class VisitWebpageTool:
|
30 |
+
def __call__(self, url: str):
|
31 |
+
return f"Pretending to visit: {url}"
|
32 |
|
33 |
+
visit_webpage = VisitWebpageTool()
|
|
|
34 |
|
35 |
+
# Dummy model and ToolCallingAgent setup
|
36 |
+
class DummyModel:
|
37 |
+
def call(self, input_text):
|
38 |
+
return f"Model processing: {input_text}"
|
39 |
+
|
40 |
+
class ToolCallingAgent:
|
41 |
+
def __init__(self, tools, model, max_steps=10):
|
42 |
+
self.tools = tools
|
43 |
+
self.model = model
|
44 |
+
self.max_steps = max_steps
|
45 |
+
|
46 |
+
def run(self, query):
|
47 |
+
print(f"Running agent with query: {query}")
|
48 |
+
for tool in self.tools:
|
49 |
+
print("Tool output:", tool(query))
|
50 |
+
|
51 |
+
# Initialize the agent
|
52 |
+
model = DummyModel()
|
53 |
+
web_agent = ToolCallingAgent(
|
54 |
+
tools=[CustomDuckDuckGoSearchTool(), visit_webpage],
|
55 |
+
model=model,
|
56 |
+
max_steps=10
|
57 |
)
|
58 |
|
59 |
+
# Example usage
|
60 |
+
web_agent.run("Latest AI tools")
|
61 |
|
62 |
# Import tool from Hub
|
63 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|