Spaces:
Sleeping
Sleeping
File size: 2,030 Bytes
34bc852 baa217f d75dae7 174709a d75dae7 34bc852 baa217f 34bc852 baa217f d75dae7 baa217f 7582c4e d75dae7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
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)
|