CoolFace
Apppublic

Csplk/multi-agent-web-browser

sourceHugging Faceupdated 2y agoView on Hugging Face
1likes
visit_webpage_tool.py36 linesDownload Raw Back to root
1from transformers import Tool2import requests3from markdownify import markdownify as md4from requests.exceptions import RequestException5import re6 7class VisitWebpageTool(Tool):8    name = "visit_webpage"9    description = "Visits a webpage at the given URL and returns its content as a markdown string."10    inputs = {11        "url": {12            "type": "text",13            "description": "The URL of the webpage to visit.",14        }15    }16    output_type = "text"17 18    def forward(self, url: str) -> str:19        try:20            # Send a GET request to the URL21            response = requests.get(url)22            response.raise_for_status()  # Raise an exception for bad status codes23 24            # Convert the HTML content to Markdown25            markdown_content = md(response.text).strip()26 27            # Remove multiple line breaks28            markdown_content = re.sub(r"\n{3,}", "\n\n", markdown_content)29 30            return markdown_content31 32        except RequestException as e:33            return f"Error fetching the webpage: {str(e)}"34        except Exception as e:35            return f"An unexpected error occurred: {str(e)}"36