Download tools.py from agents-course/Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 882 Bytes
-
https://huggingface.co/spaces/agents-course/Final_Assignment_Template/resolve/refs%2Fpr%2F420/tools.py
- Command line
-
hf download hf://spaces/agents-course/Final_Assignment_Template@refs/pr/420/tools.py
-
curl -L -o tools.py https://huggingface.co/spaces/agents-course/Final_Assignment_Template/resolve/refs%2Fpr%2F420/tools.py
882 Bytes
| from smolagents import tool | |
| from duckduckgo_search import DDGS | |
| from PyPDF2 import PdfReader | |
| def web_search(query: str) -> str: | |
| """ | |
| Search the web for information. | |
| Args: | |
| query: The search query to look up. | |
| Returns: | |
| Search results as text. | |
| """ | |
| results = DDGS().text(query, max_results=5) | |
| output = [] | |
| for r in results: | |
| output.append( | |
| f"{r['title']}\n{r['body']}\n{r['href']}" | |
| ) | |
| return "\n\n".join(output) | |
| def read_pdf(path: str) -> str: | |
| """ | |
| Read text content from a PDF file. | |
| Args: | |
| path: Path to the PDF file. | |
| Returns: | |
| Extracted text from the PDF. | |
| """ | |
| reader = PdfReader(path) | |
| text = "" | |
| for page in reader.pages: | |
| extracted = page.extract_text() | |
| if extracted: | |
| text += extracted | |
| return text[:12000] |