File size: 4,019 Bytes
7cb8e31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Run a model on a FireWorldBench split and compute the six metrics.



Usage (from the repo root):

    python scripts/run_eval.py --split main_synthetic

    python scripts/run_eval.py --split mmodalfire_c06 --group-by physical



Requires the OpenAI-compatible endpoint env vars:

    OPENAI_BASE_URL, OPENAI_API_KEY, OPENAI_MODEL

"""
import argparse
import json
import os
import subprocess
import sys
from pathlib import Path

SPLITS = {
    "main_synthetic": ("main_synthetic/full_test_A", "main_synthetic"),
    "mmodalfire_c06": ("mmodalfire_c06/full_test_A", "mmodalfire_c06"),
}


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--split", choices=tuple(SPLITS), required=True)
    parser.add_argument("--group-by", choices=("task", "physical", "fire"), default="physical",
                        help="report cells by physical axis P1-P5 (default), fire axis T1-T3, "
                             "or fine-grained task (36)")
    parser.add_argument("--provider", default="openai")
    parser.add_argument("--model", default=None, help="default: $OPENAI_MODEL")
    parser.add_argument("--max-requests", type=int, default=None,
                        help="run only the first N questions (for a quick test)")
    parser.add_argument("--track", choices=("S", "I"), default=None,
                        help="run only text track S or vision track I. Text-only models MUST use --track S "
                             "(I-track items carry images and will fail on a text model); vision models may use --track I.")
    parser.add_argument("--data-dir", default=None, help="default: ./data")
    parser.add_argument("--out-dir", default="results")
    args = parser.parse_args()

    root = Path(__file__).resolve().parent.parent
    data_dir = Path(args.data_dir) if args.data_dir else root / "data"
    split, source = SPLITS[args.split]
    questions = data_dir / split / "questions.jsonl"
    gold = data_dir / split / "gold.jsonl"
    if not questions.exists():
        sys.exit(f"Data not found at {questions}\nRun: cd scripts && bash download.sh")
    if not gold.exists():
        sys.exit(f"Gold not found at {gold}")

    model = args.model or os.environ.get("OPENAI_MODEL")
    if not model:
        sys.exit("Missing model. Set OPENAI_MODEL or pass --model.")

    out_dir = Path(args.out_dir)
    out_dir.mkdir(parents=True, exist_ok=True)
    preds = out_dir / f"{args.split}.jsonl"
    result = out_dir / f"{args.split}-{args.group_by}.json"

    # Step 1: run inference (resumable; never reads Gold)
    cmd = [sys.executable, str(root / "scripts" / "run_api.py"),
           "--provider", args.provider,
           "--questions", str(questions),
           "--source-root", str(data_dir / source),
           "--output", str(preds),
           "--model", model]
    if args.max_requests:
        cmd += ["--max-requests", str(args.max_requests)]
    if args.track:
        cmd += ["--track", args.track]
    if args.track == "I":
        print("[track I] vision track: ensure your model endpoint supports images.", flush=True)
    elif args.track == "S":
        print("[track S] text track: running text-only items.", flush=True)
    else:
        print("[track all] running S+I together. Text-only model? pass --track S (I items carry images).", flush=True)
    print(">", " ".join(cmd), flush=True)
    subprocess.run(cmd, check=True)

    # Step 2: compute the six metrics
    cmd = [sys.executable, str(root / "scripts" / "score_six_metrics.py"),
           "--gold", str(gold), "--predictions", str(preds),
           "--output", str(result), "--group-by", args.group_by]
    print(">", " ".join(cmd), flush=True)
    subprocess.run(cmd, check=True)

    print(f"\nDone. Six metrics written to {result}")
    print(json.dumps(json.load(open(result))["overall"], ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()