Spaces:
Sleeping
Sleeping
| import requests | |
| class APIClient: | |
| """Handles all API interactions with error handling.""" | |
| def __init__(self, base_url): | |
| self.base_url = base_url.rstrip('/') | |
| def get_questions(self): | |
| """Fetch the full list of evaluation questions.""" | |
| try: | |
| resp = requests.get(f"{self.base_url}/questions", timeout=10) | |
| resp.raise_for_status() | |
| return resp.json() | |
| except Exception as e: | |
| raise RuntimeError(f"Failed to fetch questions: {e}") | |
| def get_random_question(self): | |
| """Fetch a single random question.""" | |
| try: | |
| resp = requests.get(f"{self.base_url}/random-question", timeout=10) | |
| resp.raise_for_status() | |
| return resp.json() | |
| except Exception as e: | |
| raise RuntimeError(f"Failed to fetch random question: {e}") | |
| def get_file(self, task_id): | |
| """Download a specific file associated with a given task ID.""" | |
| try: | |
| resp = requests.get(f"{self.base_url}/files/{task_id}", timeout=20) | |
| resp.raise_for_status() | |
| return resp.content | |
| except Exception as e: | |
| raise RuntimeError(f"Failed to fetch file for task {task_id}: {e}") | |
| def submit(self, username, agent_code, answers): | |
| """Submit agent answers and return the result.""" | |
| data = {"username": username, "agent_code": agent_code, "answers": answers} | |
| try: | |
| resp = requests.post(f"{self.base_url}/submit", json=data, timeout=60) | |
| resp.raise_for_status() | |
| return resp.json() | |
| except Exception as e: | |
| raise RuntimeError(f"Failed to submit answers: {e}") |