Added VisitWebpageTool
Browse files
app.py
CHANGED
@@ -20,6 +20,47 @@ huggingface_hub.login(os.getenv('HF_TOKEN'))
|
|
20 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
21 |
|
22 |
# Initialize the search tool
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
search_tool = DuckDuckGoSearchTool()
|
24 |
|
25 |
@tool
|
@@ -82,7 +123,7 @@ with open("prompts.yaml", 'r') as stream:
|
|
82 |
|
83 |
agent = CodeAgent(
|
84 |
model=model,
|
85 |
-
tools=[final_answer, say_hello, image_generation_tool, search_tool], ## add your tools here (don't remove final answer)
|
86 |
max_steps=6,
|
87 |
verbosity_level=1,
|
88 |
grammar=None,
|
|
|
20 |
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
|
21 |
|
22 |
# Initialize the search tool
|
23 |
+
|
24 |
+
class VisitWebpageTool(Tool):
|
25 |
+
name = "visit_webpage"
|
26 |
+
description = (
|
27 |
+
"Visits a webpage at the given url and reads its content as a markdown string. Use this to browse webpages."
|
28 |
+
)
|
29 |
+
inputs = {
|
30 |
+
"url": {
|
31 |
+
"type": "string",
|
32 |
+
"description": "The url of the webpage to visit.",
|
33 |
+
}
|
34 |
+
}
|
35 |
+
output_type = "string"
|
36 |
+
|
37 |
+
def forward(self, url: str) -> str:
|
38 |
+
try:
|
39 |
+
import re
|
40 |
+
|
41 |
+
import requests
|
42 |
+
from markdownify import markdownify
|
43 |
+
from requests.exceptions import RequestException
|
44 |
+
|
45 |
+
from smolagents.utils import truncate_content
|
46 |
+
except ImportError as e:
|
47 |
+
raise ImportError(
|
48 |
+
"You must install packages `markdownify` and `requests` to run this tool: for instance run `pip install markdownify requests`."
|
49 |
+
) from e
|
50 |
+
try:
|
51 |
+
response = requests.get(url, timeout=20)
|
52 |
+
response.raise_for_status() # Raise an exception for bad status codes
|
53 |
+
markdown_content = markdownify(response.text).strip()
|
54 |
+
markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)
|
55 |
+
return truncate_content(markdown_content, 40000)
|
56 |
+
|
57 |
+
except requests.exceptions.Timeout:
|
58 |
+
return "The request timed out. Please try again later or check the URL."
|
59 |
+
except RequestException as e:
|
60 |
+
return f"Error fetching the webpage: {str(e)}"
|
61 |
+
except Exception as e:
|
62 |
+
return f"An unexpected error occurred: {str(e)}"
|
63 |
+
|
64 |
search_tool = DuckDuckGoSearchTool()
|
65 |
|
66 |
@tool
|
|
|
123 |
|
124 |
agent = CodeAgent(
|
125 |
model=model,
|
126 |
+
tools=[final_answer, say_hello, image_generation_tool, search_tool, VisitWebpageTool()], ## add your tools here (don't remove final answer)
|
127 |
max_steps=6,
|
128 |
verbosity_level=1,
|
129 |
grammar=None,
|