Spaces:
Sleeping
Sleeping
| import tempfile | |
| import urllib.request | |
| from typing import List | |
| import requests | |
| from agentcourse_unit4.api.answer_data import AnswerData | |
| from agentcourse_unit4.api.question_response import to_question_response, QuestionResponse | |
| from agentcourse_unit4.api.submit_answers_response import to_submit_answers_response, SubmitAnswersResponse | |
| class AgentEvalApi: | |
| def __init__(self): | |
| self.api_url = "https://agents-course-unit4-scoring.hf.space" | |
| self.questions_url = f"{self.api_url}/questions" | |
| self.submit_url = f"{self.api_url}/submit" | |
| self.files_url = f"{self.api_url}/files" | |
| def get_questions(self) -> List[QuestionResponse]: | |
| """Get complete list of filtered questions with all associated data""" | |
| response = requests.get(self.questions_url, timeout=15) | |
| response.raise_for_status() | |
| return response.json(object_hook=to_question_response) | |
| def download_file(self, task_id: str, file_name: str) -> str: | |
| """Download the file associated with the given task_id to temp dir and return path to file""" | |
| file_path = None | |
| try: | |
| path_to_file = f"{tempfile.gettempdir()}/{file_name}" | |
| result = urllib.request.urlretrieve(f"{self.files_url}/{task_id}", filename=path_to_file) | |
| file_path = result[0] | |
| except Exception as e: | |
| print(f"Error downloading file: {str(e)}") | |
| return file_path | |
| def submit_answers(self, username: str, agent_code: str, answers: List[AnswerData]) -> SubmitAnswersResponse: | |
| """Submit answers from an agent, calculate score, and update leaderboard on HF""" | |
| request_body = { | |
| "username": username.strip(), | |
| "agent_code": agent_code, | |
| "answers": [{"task_id": a.task_id, "submitted_answer": a.answer} for a in answers] | |
| } | |
| response = requests.post(self.submit_url, json=request_body, timeout=60) | |
| response.raise_for_status() | |
| return response.json(object_hook=to_submit_answers_response) | |