Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,35 +10,36 @@ from Gradio_UI import GradioUI
|
|
| 10 |
|
| 11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
#
|
|
|
|
| 42 |
|
| 43 |
|
| 44 |
|
|
@@ -74,14 +75,12 @@ custom_role_conversions=None,
|
|
| 74 |
# Import tool from Hub
|
| 75 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 76 |
|
| 77 |
-
web_agent = DuckDuckGoSearchTool()
|
| 78 |
-
|
| 79 |
with open("prompts.yaml", 'r') as stream:
|
| 80 |
prompt_templates = yaml.safe_load(stream)
|
| 81 |
|
| 82 |
agent = CodeAgent(
|
| 83 |
model=model,
|
| 84 |
-
tools=[final_answer, get_current_time_in_timezone,
|
| 85 |
max_steps=6,
|
| 86 |
verbosity_level=1,
|
| 87 |
grammar=None,
|
|
|
|
| 10 |
|
| 11 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 12 |
|
| 13 |
+
@tool
|
| 14 |
+
def web_search_tool(query: str, num_results: int = 3) -> str:
|
| 15 |
+
"""A tool that performs a web search and fetches up-to-date information using DuckDuckGo.
|
| 16 |
|
| 17 |
+
Args:
|
| 18 |
+
query: The search query or topic to look up on the web.
|
| 19 |
+
num_results: The number of top search results to return (default is 3).
|
| 20 |
|
| 21 |
+
Returns:
|
| 22 |
+
A string containing summarized up-to-date information from the web.
|
| 23 |
+
"""
|
| 24 |
+
try:
|
| 25 |
+
with DDGS() as ddg:
|
| 26 |
+
results = list(ddg.text(query, max_results=num_results))
|
| 27 |
|
| 28 |
+
if not results:
|
| 29 |
+
return f"No results found for '{query}'."
|
| 30 |
|
| 31 |
+
info_list = []
|
| 32 |
+
for i, r in enumerate(results):
|
| 33 |
+
title = r.get("title") or "No title"
|
| 34 |
+
body = r.get("body") or "No summary available."
|
| 35 |
+
link = r.get("href") or r.get("url", "")
|
| 36 |
+
info_list.append(f"{i+1}. {title}\n{body}\n({link})")
|
| 37 |
|
| 38 |
+
return f"Here are the top {num_results} results for '{query}':\n\n" + "\n\n".join(info_list)
|
| 39 |
|
| 40 |
+
except Exception as e:
|
| 41 |
+
# return the exception text so debugging in the UI is easier
|
| 42 |
+
return f"Error performing web search for '{query}': {type(e).__name__}: {e}"
|
| 43 |
|
| 44 |
|
| 45 |
|
|
|
|
| 75 |
# Import tool from Hub
|
| 76 |
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
|
| 77 |
|
|
|
|
|
|
|
| 78 |
with open("prompts.yaml", 'r') as stream:
|
| 79 |
prompt_templates = yaml.safe_load(stream)
|
| 80 |
|
| 81 |
agent = CodeAgent(
|
| 82 |
model=model,
|
| 83 |
+
tools=[final_answer, get_current_time_in_timezone, web_search_tool], ## add your tools here (don't remove final answer)
|
| 84 |
max_steps=6,
|
| 85 |
verbosity_level=1,
|
| 86 |
grammar=None,
|