agents_gaia / questions_api.py
Isics's picture
initial commit
32844c7
import shutil
from typing import TypedDict, Generator
import requests
from config import USER
Question = TypedDict("Question", {"task_id": str,
"question": str,
"level": str,
"file_name": str})
class QuestionsAPI:
questions: list[Question] = []
images_dir = "/Users/isaacgonzalez/Documents/Datasets/GAIA/2023/validation/"
def __init__(self, files_dir: str):
self._files_dir = files_dir
self.url = "https://agents-course-unit4-scoring.hf.space"
self._download_questions()
self._download_files()
def _download_questions(self) -> None:
self.questions = requests.get(f"{self.url}/questions").json()
def _download_files(self):
for question in self.questions:
if question["file_name"] != '':
self._download_file(question["file_name"])
def _download_file(self, file_name: str) -> None:
shutil.copy(f"{self.images_dir}/{file_name}", f"{self._files_dir}/{file_name}")
def questions_generator(self) -> Generator[Question, None, None]:
for question in self.questions:
yield question
def post_answers(self, answers: list[dict]) -> dict:
return requests.post(f"{self.url}/submit",
json={"username": USER,
"agent_code": "stringstri",
"answers": answers}).json()