"""Helpers for the course scoring API. Usage: python questions.py # list all questions (one line each) python questions.py 6 # show question #6 in full python questions.py random # show one random question python questions.py 19 --file # also download #19's attached file into downloads/ """ import json import os import sys from pathlib import Path import requests from dotenv import load_dotenv load_dotenv() API_URL = "https://agents-course-unit4-scoring.hf.space" DOWNLOAD_DIR = Path(__file__).parent / "downloads" def get_questions() -> list[dict]: """GET /questions -> list of {task_id, question, Level, file_name}.""" resp = requests.get(f"{API_URL}/questions", timeout=30) resp.raise_for_status() return resp.json() def get_random_question() -> dict: """GET /random-question -> one question dict.""" resp = requests.get(f"{API_URL}/random-question", timeout=30) resp.raise_for_status() return resp.json() # The course API's /files endpoint currently returns 404 for every task, so we # fall back to the original GAIA dataset (gated: accept its terms on the Hub # and set HF_TOKEN). The 20 questions are all from the 2023 validation split. GAIA_FILE_URL = "https://huggingface.co/datasets/gaia-benchmark/GAIA/resolve/main/2023/validation/{}" def download_file(task_id: str, file_name: str) -> Path: """Save a task's attachment into downloads/ and return its local path.""" DOWNLOAD_DIR.mkdir(exist_ok=True) path = DOWNLOAD_DIR / file_name if path.exists(): # cache: don't re-download return path # 1. Course API resp = requests.get(f"{API_URL}/files/{task_id}", timeout=60) if not resp.ok: # 2. Fallback: GAIA dataset on the Hub token = os.getenv("HF_TOKEN") headers = {"Authorization": f"Bearer {token}"} if token else {} resp = requests.get(GAIA_FILE_URL.format(file_name), headers=headers, timeout=60) if resp.status_code in (401, 403): raise RuntimeError("GAIA dataset is gated: accept its terms at " "https://huggingface.co/datasets/gaia-benchmark/GAIA and set HF_TOKEN.") resp.raise_for_status() path.write_bytes(resp.content) return path def show(q: dict, number: int | None = None) -> None: label = f"#{number}" if number else "Question" print(f"{label} task_id={q['task_id']} level={q.get('Level')} file={q.get('file_name') or '-'}") print("-" * 80) print(q["question"]) print() if __name__ == "__main__": args = sys.argv[1:] if not args: for i, q in enumerate(get_questions(), 1): text = q["question"].replace("\n", " ") print(f"{i:2}. [{q.get('file_name') or '-'}] {text[:100]}") elif args[0] == "random": show(get_random_question()) else: n = int(args[0]) q = get_questions()[n - 1] show(q, n) if "--file" in args and q.get("file_name"): print("Downloaded to:", download_file(q["task_id"], q["file_name"])) elif "--json" in args: print(json.dumps(q, indent=2))