Spaces:
Sleeping
Sleeping
Edit web_browser_tool to use BeautifulSoup and limit response length
Browse files18 unmodified lines1920212223242526272829303132333418 unmodified linesreturn "What magic will you build ?"@tooldef web_browser_tool(url: str) -> str:"""A tool that fetches and returns the content of a webpageArgs:url: The URL of the webpage to fetch"""try:response = requests.get(url)response.raise_for_status()return response.textexcept Exception as e:return f"Error fetching webpage: {str(e)}"18 unmodified lines192021222324252627282930313233343536373839404142434445464748495018 unmodified linesreturn "What magic will you build ?"@tooldef web_browser_tool(url: str, max_length: int = 1000) -> str:"""A tool that fetches and returns a summarized version of webpage contentArgs:url: The URL of the webpage to fetchmax_length: Maximum length of returned content (default 1000 chars)"""try:response = requests.get(url)response.raise_for_status()# Parse HTMLsoup = BeautifulSoup(response.text, 'html.parser')# Remove scripts, styles, and other non-content elementsfor element in soup(['script', 'style', 'nav', 'header', 'footer', 'aside']):element.decompose()# Get main text contenttext = soup.get_text(separator=' ', strip=True)# Truncate and add ellipsis if neededif len(text) > max_length:text = text[:max_length] + "..."return textexcept Exception as e:return f"Error fetching webpage: {str(e)}"
app.py+19-3