Spaces:
Build error
Build error
Download tools/visit_webpage.py from NSruman/First_agent_template: direct link, hf CLI and curl.
- Browser
- Download file 1.15 kB
-
https://huggingface.co/spaces/NSruman/First_agent_template/resolve/main/tools/visit_webpage.py
- Command line
-
hf download hf://spaces/NSruman/First_agent_template/tools/visit_webpage.py
-
curl -L -o visit_webpage.py https://huggingface.co/spaces/NSruman/First_agent_template/resolve/main/tools/visit_webpage.py
1.15 kB
| from smolagents.tools import Tool | |
| import requests | |
| from markdownify import markdownify | |
| import re | |
| from urllib.parse import urlparse | |
| class VisitWebpageTool(Tool): | |
| name = "visit_webpage" | |
| description = "Visits a webpage and returns its content as markdown." | |
| inputs = {'url': {'type': 'string', 'description': 'The URL to visit'}} | |
| output_type = "string" | |
| def forward(self, url: str) -> str: | |
| try: | |
| if not re.match(r'^https?://', url): | |
| return "Error: Invalid URL protocol" | |
| parsed = urlparse(url) | |
| if not parsed.netloc: | |
| return "Error: Invalid URL format" | |
| response = requests.get(url, timeout=20) | |
| response.raise_for_status() | |
| content = markdownify(response.text) | |
| content = re.sub(r'\n{3,}', '\n\n', content.strip()) | |
| return content[:10000] # Limit output length | |
| except requests.exceptions.RequestException as e: | |
| return f"Error fetching page: {str(e)}" | |
| except Exception as e: | |
| return f"Unexpected error: {str(e)}" |