Spaces:
Sleeping
Sleeping
Download tools/wikipedia_tool.py from EmincanY/First_agent_template: direct link, hf CLI and curl.
- Browser
- Download file 822 Bytes
-
https://huggingface.co/spaces/EmincanY/First_agent_template/resolve/main/tools/wikipedia_tool.py
- Command line
-
hf download hf://spaces/EmincanY/First_agent_template/tools/wikipedia_tool.py
-
curl -L -o wikipedia_tool.py https://huggingface.co/spaces/EmincanY/First_agent_template/resolve/main/tools/wikipedia_tool.py
822 Bytes
| from smolagents import tool | |
| import wikipediaapi | |
| def get_wikipedia_summary(topic: str) -> str: | |
| """Get a summary of a Wikipedia article | |
| Args: | |
| topic: Topic to get summary for | |
| """ | |
| wiki = wikipediaapi.Wikipedia( | |
| user_agent='HuggingFaceAgent/1.0 (https://huggingface.co/; contact@huggingface.co)', | |
| language='en' | |
| ) | |
| try: | |
| page = wiki.page(topic) | |
| if page.exists(): | |
| # Get first two sentences or first 500 characters, whichever is shorter | |
| summary = page.summary[:500] | |
| if len(summary) == 500: | |
| summary = summary[:summary.rindex('.')] + '.' | |
| return summary | |
| return f"No Wikipedia article found for: {topic}" | |
| except Exception as e: | |
| return f"Error fetching Wikipedia summary: {str(e)}" |