Spaces:
Running on Zero
Running on Zero
Update app.py
Browse files316 unmodified lines3173183193203213223233241 unmodified line3263273283293303313323333343353363373383393403323333343353363373383393403413423433413423433443453463473483493503513523533543553563573583593473603613623633643653 unmodified lines36937037137237335735835936036136236336436536636736837437537637737837938038138238338438538638738838939039139219 unmodified lines412413414394415416417418419420316 unmodified lines return f"No web results found for '{query}'. Try search_wikipedia(query) or fetch_url_text(url)." return "\n\n".join(results_out)
wiki_cache = {}
@tool def search_wikipedia(query: str) -> str: """Searches English Wikipedia and returns clean plain text (including tables) of the best matching article.1 unmodified line query: Search query or exact Wikipedia article title. """ import re import time from html import unescape
cache_key = query.strip().lower() if cache_key in wiki_cache: return wiki_cache[cache_key]
headers = {"User-Agent": "HF-Agents-Course-Bot/1.0"} api_endpoint = "https://en.wikipedia.org/w/api.php" try: search_resp = None for attempt in range(3): search_resp = requests.get( api_endpoint, params={ "action": "query", "list": "search", "srsearch": query, "format": "json", "srlimit": 5, }, headers=headers, timeout=45, ) search_resp = requests.get( api_endpoint, params={ "action": "query", "list": "search", "srsearch": query, "format": "json", "srlimit": 5, }, headers=headers, timeout=45, ) if search_resp.status_code == 429 and attempt < 2: time.sleep(1.5 * (attempt + 1)) continue break search_resp.raise_for_status() results = search_resp.json().get("query", {}).get("search", []) if not results: return f"No Wikipedia results for '{query}'." out_empty = f"No Wikipedia results for '{query}'." wiki_cache[cache_key] = out_empty return out_empty
query_norm = query.strip().lower() selected = next(3 unmodified lines title = selected["title"] other_titles = [r["title"] for r in results if r["title"] != title]
parse_resp = None for attempt in range(3): parse_resp = requests.get( api_endpoint, params={ "action": "parse", "page": title, "prop": "text", "redirects": 1, "format": "json", }, headers=headers, timeout=45, ) parse_resp = requests.get( api_endpoint, params={ "action": "parse", "page": title, "prop": "text", "redirects": 1, "format": "json", }, headers=headers, timeout=45, ) if parse_resp.status_code == 429 and attempt < 2: time.sleep(1.5 * (attempt + 1)) continue break parse_resp.raise_for_status() parse_data = parse_resp.json().get("parse", {}) raw_html = parse_data.get("text", {}).get("*", "")19 unmodified lines header_info = f"Title: {resolved_title}" if other_titles: header_info += f"\nOther matching pages: {', '.join(other_titles)}" return f"{header_info}\n\n{text}" if text else f"{header_info}\n\nEmpty article text." output = f"{header_info}\n\n{text}" if text else f"{header_info}\n\nEmpty article text." wiki_cache[cache_key] = output return output except Exception as e: return f"Wikipedia error for '{query}': {str(e)}"
app.py+49-26