""" 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 ") 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()