Spaces:
Build error
Build error
Download tools.py from UnivAI/UnivAI-agents: direct link, hf CLI and curl.
- Browser
- Download file 1.11 kB
-
https://huggingface.co/spaces/UnivAI/UnivAI-agents/resolve/main/tools.py
- Command line
-
hf download hf://spaces/UnivAI/UnivAI-agents/tools.py
-
curl -L -o tools.py https://huggingface.co/spaces/UnivAI/UnivAI-agents/resolve/main/tools.py
1.11 kB
| from ddgs import DDGS | |
| import requests | |
| from bs4 import BeautifulSoup | |
| import time | |
| def search_web(query): | |
| try: | |
| with DDGS() as ddgs: | |
| results = list(ddgs.text(query, max_results=5)) | |
| return results | |
| except Exception as e: | |
| print(f"Search error: {e}") | |
| return [] | |
| def scrape_page(url): | |
| try: | |
| response = requests.get(url, timeout=5) | |
| soup = BeautifulSoup(response.text, "html.parser") | |
| paragraphs = soup.find_all("p") | |
| content = " ".join([p.get_text() for p in paragraphs[:20]]) | |
| return content | |
| except: | |
| return "" | |
| def gather_research(topic): | |
| time.sleep(2) | |
| results = search_web(topic) | |
| if not results: | |
| return "No search results found. Try again in a few minutes.", [] | |
| all_content = "" | |
| sources = [] | |
| for r in results: | |
| url = r.get("href", "") # back to href - confirmed correct | |
| title = r.get("title", url) | |
| if url: | |
| all_content += scrape_page(url) + "\n" | |
| sources.append(f"- [{title}]({url})") | |
| return all_content, sources |