# File: utils/gaia_api.py import requests from typing import List, Dict, Optional import json class GaiaAPI: """Client for interacting with GAIA Benchmark API""" # Fixed API endpoint BASE_URL = "https://agents-course-unit4-scoring.hf.space" @classmethod def get_questions(cls) -> List[Dict]: """Fetch all GAIA questions""" try: response = requests.get(f"{cls.BASE_URL}/questions") response.raise_for_status() return response.json() except Exception as e: print(f"Error fetching questions: {str(e)}") # Return sample questions for testing return cls._get_sample_questions() @classmethod def get_random_question(cls) -> Dict: """Get a single random question""" try: response = requests.get(f"{cls.BASE_URL}/random-question") response.raise_for_status() return response.json() except Exception as e: print(f"Error fetching random question: {str(e)}") return cls._get_sample_questions()[0] @classmethod def get_file(cls, task_id: str) -> bytes: """Download file associated with task""" try: response = requests.get(f"{cls.BASE_URL}/files/{task_id}") response.raise_for_status() return response.content except Exception as e: print(f"Error fetching file for task {task_id}: {str(e)}") return b"" @classmethod def submit_answers(cls, username: str, agent_code: str, answers: List[Dict]) -> Dict: """Submit answers to GAIA for scoring""" try: payload = { "username": username, "agent_code": agent_code, "answers": answers } response = requests.post(f"{cls.BASE_URL}/submit", json=payload) response.raise_for_status() return response.json() except Exception as e: print(f"Error submitting answers: {str(e)}") return {"error": str(e), "score": 0} @classmethod def _get_sample_questions(cls) -> List[Dict]: """Sample questions for testing when API is unavailable""" return [ { "task_id": "sample_001", "question": "What is the capital of France?", "level": 1, "final_answer": "Paris" }, { "task_id": "sample_002", "question": "Calculate 15 * 8 + 7", "level": 1, "final_answer": "127" }, { "task_id": "sample_003", "question": "Name three programming languages commonly used for web development", "level": 1, "final_answer": "JavaScript, Python, PHP" } ]