File size: 8,820 Bytes
909bf64
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
from typing import Dict, List, Tuple
import re
import tempfile
from pathlib import Path
import pandas as pd
import requests
from agent import GaiaAgent
from pandas import DataFrame

# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
QUESTIONS_URL = f"{DEFAULT_API_URL}/questions"
SUBMIT_URL = f"{DEFAULT_API_URL}/submit"
FILE_PATH = f"{DEFAULT_API_URL}/files/"


# --- Helper Methods ---
def fetch_all_questions() -> Dict:
    """Fetches all questions from the specified API endpoint.
    This function retrieves a list of questions from the API, handles potential errors
    such as network issues, invalid responses, or empty question lists, and returns
    the questions as a dictionary.
    Returns:
        Dict: A dictionary containing the questions data retrieved from the API.
    Raises:
        UserWarning: If there is an error fetching the questions, such as network issues,
            invalid JSON response, or an empty question list.  The exception message
            provides details about the specific error encountered.
    """
    print(f"Fetching questions from: {QUESTIONS_URL}")
    response = requests.get(QUESTIONS_URL, timeout=15)
    try:
        response.raise_for_status()
        questions_data = response.json()
        if not questions_data:
            print("Fetched questions list is empty.")
            raise UserWarning("Fetched questions list is empty or invalid format.")
        print(f"Fetched {len(questions_data)} questions.")
        return questions_data
    except requests.exceptions.RequestException as e:
        print(f"Error fetching questions: {e}")
        raise UserWarning(f"Error fetching questions: {e}")
    except requests.exceptions.JSONDecodeError as e:
        print(f"Error decoding JSON response from questions endpoint: {e}")
        print(f"Response text: {response.text[:500]}")
        raise UserWarning(f"Error decoding server response for questions: {e}")
    except Exception as e:
        print(f"An unexpected error occurred fetching questions: {e}")
        raise UserWarning(f"An unexpected error occurred fetching questions: {e}")


def submit_answers(submission_data: dict, results_log: list) -> Tuple[str, DataFrame]:
    """Submits answers to the scoring API and returns the submission status and results.
    This function sends the provided answers to the scoring API, handles potential errors
    such as network issues, server errors, or invalid responses, and returns a status
    message indicating the success or failure of the submission, along with a DataFrame
    containing the results log.
    Args:
        submission_data (dict): A dictionary containing the answers to be submitted.
            Expected to have a structure compatible with the scoring API.
        results_log (list): A list of dictionaries containing the results log.
            This log is converted to a Pandas DataFrame and returned.
    Returns:
        Tuple[str, DataFrame]: A tuple containing:
            - A status message (str) indicating the submission status and any relevant
              information or error messages.
            - A Pandas DataFrame containing the results log.
    """
    try:
        response = requests.post(SUBMIT_URL, json=submission_data, timeout=60)
        response.raise_for_status()
        result_data = response.json()
        final_status = (
            f"Submission Successful!\n"
            f"User: {result_data.get('username')}\n"
            f"Overall Score: {result_data.get('score', 'N/A')}% "
            f"({result_data.get('correct_count', '?')}/"
            f"{result_data.get('total_attempted', '?')} correct)\n"
            f"Message: {result_data.get('message', 'No message received.')}"
        )
        print("Submission successful.")
        results_df = pd.DataFrame(results_log)
        return final_status, results_df
    except requests.exceptions.HTTPError as e:
        error_detail = f"Server responded with status {e.response.status_code}."
        try:
            error_json = e.response.json()
            error_detail += f" Detail: {error_json.get('detail', e.response.text)}"
        except requests.exceptions.JSONDecodeError:
            error_detail += f" Response: {e.response.text[:500]}"
        status_message = f"Submission Failed: {error_detail}"
        print(status_message)
        results_df = pd.DataFrame(results_log)
        return status_message, results_df
    except requests.exceptions.Timeout:
        status_message = "Submission Failed: The request timed out."
        print(status_message)
        results_df = pd.DataFrame(results_log)
        return status_message, results_df
    except requests.exceptions.RequestException as e:
        status_message = f"Submission Failed: Network error - {e}"
        print(status_message)
        results_df = pd.DataFrame(results_log)
        return status_message, results_df
    except Exception as e:
        status_message = f"An unexpected error occurred during submission: {e}"
        print(status_message)
        results_df = pd.DataFrame(results_log)
        return status_message, results_df


def run_agent(
    gaia_agent: GaiaAgent, questions_data: List[Dict]
) -> Tuple[List[Dict], List[Dict]]:
    """Runs the agent on a list of questions and returns the results and answers.
    This function iterates through a list of questions, runs the provided agent on each
    question, and collects the results and answers. It handles potential errors during
    agent execution and returns the results log and the answers payload.
    Args:
        gaia_agent (GaiaAgent): An instance of the GaiaAgent class, which is responsible for
            generating answers to the questions.
        questions_data (List[Dict]): A list of dictionaries, where each dictionary
            represents a question and contains at least the 'task_id' and 'question' keys.
    Returns:
        Tuple[List[Dict], List[Dict]]: A tuple containing:
            - A list of dictionaries representing the results log, where each dictionary
              contains the 'Task ID', 'Question', and 'Submitted Answer'.
            - A list of dictionaries representing the answers payload, where each dictionary
              contains the 'task_id' and 'submitted_answer'.
    """
    results_log = []
    answers_payload = []

    print(f"🚀 Running agent on {len(questions_data)} questions...")
    for item in questions_data:
        task_id = item.get("task_id")
        question_text = item.get("question")
        question_text = process_file(task_id, question_text)
        if not task_id or question_text is None:
            print(f"⚠️ Skipping invalid item (missing task_id or question): {item}")
            continue
        try:
            submitted_answer = gaia_agent(task_id, question_text)
            answers_payload.append(
                {"task_id": task_id, "submitted_answer": submitted_answer}
            )
        except Exception as e:
            print(f"❌ Error running agent on task {task_id}: {e}")
            submitted_answer = f"AGENT ERROR: {e}"

        results_log.append(
            {
                "Task ID": task_id,
                "Question": question_text,
                "Submitted Answer": submitted_answer,
            }
        )
    return results_log, answers_payload


def process_file(task_id: str, question_text: str) -> str:
    """
    Attempt to download a file associated with a task from the API.
    - If the file exists (HTTP 200), it is saved to a temp directory and the local file path is returned.
    - If no file is found (HTTP 404), returns None.
    - For all other HTTP errors, the exception is propagated to the caller.
    """
    file_url = f"{FILE_PATH}{task_id}"

    try:
        response = requests.get(file_url, timeout=30)
        response.raise_for_status()
    except requests.exceptions.RequestException as exc:
        print(f"Exception in download_file>> {str(exc)}")
        return question_text # Unable to get the file

    # Determine filename from 'Content-Disposition' header, fallback to task_id
    content_disposition = response.headers.get("content-disposition", "")
    filename = task_id
    match = re.search(r'filename="([^"]+)"', content_disposition)
    if match:
        filename = match.group(1)

    # Save file in a temp directory
    temp_storage_dir = Path(tempfile.gettempdir()) / "gaia_cached_files"
    temp_storage_dir.mkdir(parents=True, exist_ok=True)

    file_path = temp_storage_dir / filename
    file_path.write_bytes(response.content)
    return (
                f"{question_text}\n\n"
                f"---\n"
                f"A file was downloaded for this task and saved locally at:\n"
                f"{str(file_path)}\n"
                f"---\n\n"
            )