File size: 6,155 Bytes
555dbe6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import os
import requests
import sys # Added for command-line arguments
from agent import BasicAgent

# --- Constants ---
DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
DOWNLOAD_DIR = "downloaded_task_files" # Directory to save downloaded files
QUESTIONS_FILE = "data/questions.json"
# QUESTION_INDEX = 3 # Removed global constant

def run_single_question_test(question_index: int): # Added question_index parameter
    """
    Fetches a single question by index, downloads its associated file (if any),
    runs the BasicAgent on it, and prints the answer.
    """
    # Create download directory if it doesn't exist
    try:
        os.makedirs(DOWNLOAD_DIR, exist_ok=True)
        print(f"Ensured download directory exists: {DOWNLOAD_DIR}")
    except OSError as e:
        print(f"Error creating download directory {DOWNLOAD_DIR}: {e}")
        return

    # 1. Load Questions
    try:
        with open(QUESTIONS_FILE, 'r') as f:
            questions_data = json.load(f)
        if not questions_data or not isinstance(questions_data, list) or len(questions_data) <= question_index:
            print(f"Error: Could not load question at index {question_index} (0-indexed) from {QUESTIONS_FILE}. Total questions: {len(questions_data) if isinstance(questions_data, list) else 'N/A'}")
            return
        item = questions_data[question_index]
        print(f"Loaded question {question_index + 1} (from 1-indexed input): {item.get('question')}")
    except FileNotFoundError:
        print(f"Error: Questions file not found at {QUESTIONS_FILE}")
        return
    except json.JSONDecodeError:
        print(f"Error: Could not decode JSON from {QUESTIONS_FILE}")
        return
    except Exception as e:
        print(f"Error loading or parsing {QUESTIONS_FILE}: {e}")
        return

    # 2. Instantiate Agent
    try:
        agent = BasicAgent()
        print("Agent instantiated successfully.")
    except Exception as e:
        print(f"Error instantiating agent: {e}")
        return

    task_id = item.get("task_id")
    question_text = item.get("question")
    file_name = item.get("file_name")
    local_file_path = None

    if not task_id or question_text is None:
        print(f"Skipping item with missing task_id or question: {item}")
        return

    if file_name:
        download_url = f"{DEFAULT_API_URL}/files/{task_id}"
        local_file_path = os.path.join(DOWNLOAD_DIR, file_name)
        print(f"Attempting to download file for task {task_id} (using task_id in URL): {file_name} from {download_url}")
        try:
            file_response = requests.get(download_url, stream=True, timeout=30)
            file_response.raise_for_status()
            with open(local_file_path, 'wb') as f:
                for chunk in file_response.iter_content(chunk_size=8192):
                    f.write(chunk)
            print(f"Successfully downloaded to {local_file_path}")
        except requests.exceptions.RequestException as e:
            print(f"Failed to download {file_name}: {e}")
            local_file_path = None # Ensure agent doesn't get a path to a non-existent/failed file
        except Exception as e:
            print(f"An unexpected error occurred downloading {file_name}: {e}")
            local_file_path = None
    else:
        print("No file associated with this question.")

    # 3. Run Agent on the single question
    try:
        print(f"Running agent on question: {question_text}")
        if local_file_path:
            print(f"Providing file: {local_file_path}")
        submitted_answer = agent(question_text, file_path=local_file_path)
        print(f"\n--- Agent's Answer for Task ID: {task_id} ---")
        print(submitted_answer)
        print("--- End of Answer ---")

    except Exception as e:
         print(f"Error running agent on task {task_id}: {e}")

def run_all_questions_test():
    """
    Runs the test for all questions in the QUESTIONS_FILE.
    """
    print("Attempting to run tests for all questions...")
    try:
        with open(QUESTIONS_FILE, 'r') as f:
            questions_data = json.load(f)
        if not questions_data or not isinstance(questions_data, list):
            print(f"Error: Could not load questions from {QUESTIONS_FILE} or it's not a list.")
            return
        
        num_questions = len(questions_data)
        if num_questions == 0:
            print(f"No questions found in {QUESTIONS_FILE}.")
            return

        print(f"Found {num_questions} questions to test.")
        for i in range(num_questions):
            print(f"\n--- Running Test for Question {i+1}/{num_questions} ---")
            run_single_question_test(i)
            print(f"--- Finished Test for Question {i+1}/{num_questions} ---\n")
        print("All question tests completed.")

    except FileNotFoundError:
        print(f"Error: Questions file not found at {QUESTIONS_FILE}")
    except json.JSONDecodeError:
        print(f"Error: Could not decode JSON from {QUESTIONS_FILE}")
    except Exception as e:
        print(f"An unexpected error occurred while running all questions: {e}")

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python test_single_q.py <question_number | 'all'>")
        print("Example (single question): python test_single_q.py 4")
        print("Example (all questions): python test_single_q.py all")
        run_all_questions_test() # Default to running all if no argument is provided
        sys.exit(0)

    argument = sys.argv[1]

    if argument.lower() == "all":
        run_all_questions_test()
    else:
        try:
            question_number_arg = int(argument)
            if question_number_arg <= 0:
                print("Error: Question number must be a positive integer.")
                sys.exit(1)
            # Convert 1-indexed input to 0-indexed for list access
            question_idx_0_based = question_number_arg - 1
            run_single_question_test(question_idx_0_based)
        except ValueError:
            print("Error: Invalid argument. Please enter an integer for a specific question or 'all'.")
            sys.exit(1)