File size: 1,710 Bytes
be70cf0
 
7dfe08e
be70cf0
 
 
7dfe08e
 
 
 
 
 
 
be70cf0
 
 
afc4478
 
 
 
be70cf0
 
afc4478
 
 
be70cf0
 
afc4478
be70cf0
 
 
 
 
afc4478
 
be70cf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7dfe08e
 
 
929e339
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import os
import random
from typing import Any, Optional, TypedDict

import requests

"""
GET /questions: Retrieve the full list of filtered evaluation questions.
GET /random-question: Fetch a single random question from the list.
GET /files/{task_id}: Download a specific file associated with a given task ID.
POST /submit: Submit agent answers, calculate the score, and update the leaderboard.
"""

API_URL = os.getenv("API_URL") or "https://agents-course-unit4-scoring.hf.space"


class Question(TypedDict):
    task_id: str
    question: str
    file_name: Optional[str]


class Answer(TypedDict):
    task_id: str
    answer: Optional[str]


def get_questions(limit: int | None = None) -> list[Question]:
    questions_url = API_URL + "/questions"
    print(f"Fetching questions from: {questions_url}")
    try:
        response = requests.get(questions_url, timeout=15)
        response.raise_for_status()
        questions = response.json()
        if not isinstance(questions, list):
            print("Invalid response format. Expected a list.")
            return []
        if not questions:
            print("Received empty task list.")
            return []

        return random.sample(
            questions,
            min(
                limit,
                len(questions),
            )
            if limit is not None
            else len(questions),
        )
    except Exception as e:
        print(f"Error fetching questions: {e}")
        return []


def get_file(task_id: str) -> bytes | Any:
    url = f"{API_URL}/files/{task_id}"
    print(f"Fetching file from: {url}")
    response = requests.get(url, timeout=15)
    response.raise_for_status()
    return response.content