-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy patheval_output_jsonl.py
46 lines (37 loc) · 1.47 KB
/
eval_output_jsonl.py
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
import os
import argparse
import json
from tqdm import tqdm
from mcts_math.agents.utils import math_is_equiv
def parse_args():
args = argparse.ArgumentParser()
args.add_argument('--res_file', type=str, required=True, help="result file in jsonl.")
args.add_argument('--react', action="store_true", help="if using react_batch_demo.py, set True.")
args = args.parse_args()
return args
def eval_jsonl(res_file: str, react: bool = False) -> float:
cnt, total = 0, 0
with open(res_file, "r") as f:
for line in tqdm(f):
d = json.loads(line.strip())
answer = d["answer"]
if react:
# react is the only one solution.
last_tag = sorted(d["react"].keys(), key=lambda x: len(x), reverse=True)[0]
prediction = d["react"][last_tag]["final_answer"]
else:
if d["react"]["solutions"]:
# get top-1
prediction = d["react"]["solutions"][0]["final_answer"]
else:
prediction = ""
if math_is_equiv(answer, prediction):
cnt += 1
total += 1
return cnt / total
if __name__ == '__main__':
args = parse_args()
# you run the agent with qaf, including question and answer, e.g., `run_sbs.sh`
# the output res_file will include question, answer and model prediction with top-1 value.
acc = eval_jsonl(args.res_file, args.react)
print(acc)