Spaces:
Running on Zero
Running on Zero
Download tools/search_wikipedia.py from Ark4u/First_agent_template: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/spaces/Ark4u/First_agent_template/resolve/main/tools/search_wikipedia.py
- Command line
-
hf download hf://spaces/Ark4u/First_agent_template/tools/search_wikipedia.py
-
curl -L -o search_wikipedia.py https://huggingface.co/spaces/Ark4u/First_agent_template/resolve/main/tools/search_wikipedia.py
1.14 kB
| from urllib.parse import quote | |
| import requests | |
| from smolagents import tool | |
| def search_wikipedia(query: str) -> str: | |
| """Fetches an encyclopedic summary of a topic from Wikipedia. | |
| Args: | |
| query: The subject or topic name to search on Wikipedia (e.g., 'Alan Turing', 'Artificial intelligence'). | |
| """ | |
| try: | |
| encoded_query = quote(query) | |
| url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{encoded_query}" | |
| headers = {"User-Agent": "HF-Agents-Course-ArkAssistant/1.0"} | |
| resp = requests.get(url, headers=headers, timeout=10) | |
| if resp.status_code == 200: | |
| data = resp.json() | |
| extract = data.get("extract", "") | |
| title = data.get("title", query) | |
| if extract: | |
| return f"**{title}**: {extract}" | |
| return f"No extract available for '{query}'." | |
| if resp.status_code == 404: | |
| return f"No Wikipedia article found for '{query}'." | |
| return f"Wikipedia query returned status code {resp.status_code}." | |
| except Exception as e: | |
| return f"Error querying Wikipedia for '{query}': {str(e)}" | |