File size: 2,455 Bytes
6715b3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json, argparse, pandas as pd
import os
import requests
from dotenv import load_dotenv

load_dotenv()
from agent import OptimaAgent

DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
DOWNLOAD_DIR = "downloaded_files" # Files downloaded by run_local.py go here directly
DATA_DIR = "data" # Root for persistent data like videos and frames (task_id structured)

def main(file: str):
    # Ensure root download and data directories exist
    os.makedirs(DOWNLOAD_DIR, exist_ok=True)
    os.makedirs(DATA_DIR, exist_ok=True) 
    # Further subdirectories like data/videos or data/frames will be created by tools

    agent = OptimaAgent()

    print(f"Processing questions from file: {file}...")
    qs = json.load(open(file))
    rows = []
    for item in qs:
        print("processing item:", item)
        file_path = None
        task_id = item["task_id"]

        if "file_name" in item and item["file_name"]:
            file_name = item["file_name"] # This is usually <task_id>.<ext>
            file_url = f"{DEFAULT_API_URL}/files/{task_id}"
            # Save directly into DOWNLOAD_DIR
            local_file_path = os.path.join(DOWNLOAD_DIR, file_name)

            print(f"Downloading file for task {task_id}: {file_name} to {local_file_path}...")
            try:
                response = requests.get(file_url, stream=True)
                response.raise_for_status()
                with open(local_file_path, "wb") as f:
                    for chunk in response.iter_content(chunk_size=8192):
                        f.write(chunk)
                print(f"Successfully downloaded to {local_file_path}")
                file_path = local_file_path
            except requests.exceptions.RequestException as e:
                print(f"Error downloading file for task {task_id}: {e}")

        # Call agent with question, task_id, and potential file path
        answer = agent(item["question"], task_id=task_id, file_path=file_path)
        rows.append(
            {
                "task_id": task_id,
                "question": item["question"][:60] + "…",
                "answer": answer,
            }
        )
    print(pd.DataFrame(rows))
    print("DONE")


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument(
        "-f",
        "--file",
        default="questions.json",
        help="Path to the questions JSON file.",
    )
    main(**vars(parser.parse_args()))