File size: 1,161 Bytes
58bd51a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Minimal example: compute accuracy on ChartReal using normalize_answer.

Usage:
    python3 eval/eval_example.py path/to/predictions.jsonl path/to/data.jsonl
"""
import json
import sys
from normalize import normalize_answer


def main():
    if len(sys.argv) != 3:
        print("Usage: python3 eval_example.py <predictions.jsonl> <data.jsonl>")
        sys.exit(1)

    preds_path, data_path = sys.argv[1], sys.argv[2]

    # Index gold answers by example_id
    gold = {}
    with open(data_path, encoding="utf-8") as f:
        for line in f:
            d = json.loads(line)
            gold[d["example_id"]] = d

    # Score predictions
    n = c = 0
    with open(preds_path, encoding="utf-8") as f:
        for line in f:
            p = json.loads(line)
            ex = gold.get(p["example_id"])
            if not ex:
                continue
            n += 1
            if normalize_answer(
                p["prediction_raw"], ex["answer"], ex["answer_type"],
                gold_numeric=ex.get("answer_numeric"),
            ):
                c += 1

    print(f"Accuracy: {c}/{n} = {c/n*100:.2f}%")


if __name__ == "__main__":
    main()