Download questions.py from andrei-sferdian/Agents_Course_Final_Assignment: direct link, hf CLI and curl.
- Browser
- Download file 1.71 kB
-
https://huggingface.co/spaces/andrei-sferdian/Agents_Course_Final_Assignment/resolve/main/questions.py
- Command line
-
hf download hf://spaces/andrei-sferdian/Agents_Course_Final_Assignment/questions.py
-
curl -L -o questions.py https://huggingface.co/spaces/andrei-sferdian/Agents_Course_Final_Assignment/resolve/main/questions.py
1.71 kB
| 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 | |