Spaces:
Sleeping
Sleeping
Added in web_search and visit_webpage tools to app.py
Browse files
app.py
CHANGED
|
@@ -8,15 +8,36 @@ from tools.final_answer import FinalAnswerTool
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
|
|
|
| 11 |
@tool
|
| 12 |
-
def
|
| 13 |
-
|
| 14 |
-
"""A tool that does nothing yet
|
| 15 |
Args:
|
| 16 |
-
|
| 17 |
-
|
|
|
|
| 18 |
"""
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
@tool
|
| 22 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
@@ -53,9 +74,11 @@ image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_co
|
|
| 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 +88,4 @@ agent = CodeAgent(
|
|
| 65 |
prompt_templates=prompt_templates
|
| 66 |
)
|
| 67 |
|
| 68 |
-
|
| 69 |
GradioUI(agent).launch()
|
|
|
|
| 8 |
from Gradio_UI import GradioUI
|
| 9 |
|
| 10 |
# Below is an example of a tool that does nothing. Amaze us with your creativity !
|
| 11 |
+
# Define the tools
|
| 12 |
@tool
|
| 13 |
+
def web_search(query: str) -> str:
|
| 14 |
+
"""A tool that performs a web search using DuckDuckGo and returns the top search results.
|
|
|
|
| 15 |
Args:
|
| 16 |
+
query: The search query to perform on DuckDuckGo.
|
| 17 |
+
Returns:
|
| 18 |
+
A string containing the top search results.
|
| 19 |
"""
|
| 20 |
+
try:
|
| 21 |
+
search_tool = DuckDuckGoSearchTool()
|
| 22 |
+
results = search_tool.run(query)
|
| 23 |
+
return "\n".join([f"{i+1}. {result['title']} - {result['href']}" for i, result in enumerate(results)])
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return f"Error performing web search: {str(e)}"
|
| 26 |
+
|
| 27 |
+
@tool
|
| 28 |
+
def visit_webpage(url: str) -> str:
|
| 29 |
+
"""A tool that visits a webpage and returns its content.
|
| 30 |
+
Args:
|
| 31 |
+
url: The URL of the webpage to visit.
|
| 32 |
+
Returns:
|
| 33 |
+
A string containing the content of the webpage.
|
| 34 |
+
"""
|
| 35 |
+
try:
|
| 36 |
+
response = requests.get(url)
|
| 37 |
+
response.raise_for_status() # Raises an HTTPError for bad responses
|
| 38 |
+
return response.text
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"Error visiting webpage: {str(e)}"
|
| 41 |
|
| 42 |
@tool
|
| 43 |
def get_current_time_in_timezone(timezone: str) -> str:
|
|
|
|
| 74 |
with open("prompts.yaml", 'r') as stream:
|
| 75 |
prompt_templates = yaml.safe_load(stream)
|
| 76 |
|
| 77 |
+
|
| 78 |
+
# Reinitialize the agent with the new tools
|
| 79 |
agent = CodeAgent(
|
| 80 |
model=model,
|
| 81 |
+
tools=[final_answer, web_search, visit_webpage], # Added web_search and visit_webpage here
|
| 82 |
max_steps=6,
|
| 83 |
verbosity_level=1,
|
| 84 |
grammar=None,
|
|
|
|
| 88 |
prompt_templates=prompt_templates
|
| 89 |
)
|
| 90 |
|
|
|
|
| 91 |
GradioUI(agent).launch()
|