File size: 1,509 Bytes
32844c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()