PritRai75's picture
Upload 7 files
281882c verified
Raw History Blame Contribute Delete
1.5 kB
"""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()