Spaces:
Sleeping
Sleeping
File size: 1,499 Bytes
281882c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | """Small client for the course scoring API, plus an on-disk answer cache."""
import json
from pathlib import Path
import requests
API_URL = "https://agents-course-unit4-scoring.hf.space"
DOWNLOAD_DIR = Path("downloads")
CACHE_FILE = Path("answers_cache.json")
def fetch_questions() -> list[dict]:
r = requests.get(f"{API_URL}/questions", timeout=30)
r.raise_for_status()
return r.json()
def download_task_file(task_id: str, file_name: str) -> str | None:
"""Download the file attached to a task (if any). Returns its local path."""
if not file_name:
return None
DOWNLOAD_DIR.mkdir(exist_ok=True)
path = DOWNLOAD_DIR / f"{task_id}_{Path(file_name).name}"
if path.exists() and path.stat().st_size > 0:
return str(path)
r = requests.get(f"{API_URL}/files/{task_id}", timeout=60)
r.raise_for_status()
path.write_bytes(r.content)
return str(path)
def load_cache() -> dict:
if CACHE_FILE.exists():
try:
return json.loads(CACHE_FILE.read_text())
except json.JSONDecodeError:
pass
return {}
def save_cache(cache: dict) -> None:
CACHE_FILE.write_text(json.dumps(cache, indent=2, ensure_ascii=False))
def submit(username: str, agent_code: str, answers: list[dict]) -> dict:
payload = {"username": username.strip(), "agent_code": agent_code, "answers": answers}
r = requests.post(f"{API_URL}/submit", json=payload, timeout=120)
r.raise_for_status()
return r.json()
|