Final_Assignment_Template / submit_answers.py
AndreasG10's picture
Upload 4 files
5b88a66 verified
Raw History Blame Contribute Delete
2.18 kB
"""
Submit answers.json to the course scoring API.
python submit_answers.py --username YOUR_HF_USERNAME --space YOUR_HF_USERNAME/YOUR_SPACE_NAME
--space is only used to build the agent_code link (https://huggingface.co/spaces/<space>/tree/main).
The course asks for a public Space containing your code; use --agent-code to pass a full URL instead.
"""
import argparse
import json
from pathlib import Path
import requests
API_URL = "https://agents-course-unit4-scoring.hf.space"
ap = argparse.ArgumentParser()
ap.add_argument("--username", required=True, help="your Hugging Face username")
g = ap.add_mutually_exclusive_group(required=True)
g.add_argument("--space", help="owner/space-name of your public Space")
g.add_argument("--agent-code", help="full URL to your public code, ending in /tree/main")
ap.add_argument("--file", default="answers.json")
ap.add_argument("--yes", action="store_true", help="skip the confirmation prompt")
args = ap.parse_args()
data = json.loads(Path(args.file).read_text(encoding="utf-8"))
agent_code = args.agent_code or f"https://huggingface.co/spaces/{args.space}/tree/main"
answers = [
{"task_id": tid, "submitted_answer": str(v.get("answer", "")).strip()}
for tid, v in sorted(data.items(), key=lambda kv: kv[1].get("index", 0))
]
print(f"User: {args.username}\nCode: {agent_code}\nAnswers: {len(answers)}")
empty = [a["task_id"] for a in answers if not a["submitted_answer"]]
if empty:
print(f"WARNING: {len(empty)} empty answers (they will count as wrong).")
for v in sorted(data.values(), key=lambda v: v.get("index", 0)):
print(f" [{v.get('index')}] {v.get('answer')!r}")
if not args.yes and input("\nSubmit? [y/N] ").strip().lower() != "y":
raise SystemExit("Cancelled.")
r = requests.post(
f"{API_URL}/submit",
json={"username": args.username.strip(), "agent_code": agent_code, "answers": answers},
timeout=60,
)
try:
r.raise_for_status()
except requests.HTTPError:
raise SystemExit(f"Submission failed ({r.status_code}): {r.text[:500]}")
res = r.json()
print(f"\nScore: {res.get('score')}% ({res.get('correct_count')}/{res.get('total_attempted')} correct)")
print(res.get("message", ""))