-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheval_math.py
52 lines (45 loc) · 1.71 KB
/
eval_math.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
47
48
49
50
51
52
import argparse
import json
import os
from utils import find_last_integer
def main(input_file, output_file):
results = []
total_cnt = 0
correct_cnt = 0
with open(input_file, "r") as file:
for line in file:
predict_data = json.loads(line)
ground_truth = find_last_integer(predict_data["label"])
prediction = predict_data["predict"].strip()
answer = find_last_integer(prediction)
# Add to results
results.append(
{"ground_truth": ground_truth, "predict": prediction, "answer": answer}
)
total_cnt += 1
if check_math(predict_data):
correct_cnt += 1
print(f"Accuracy for math: {correct_cnt} / {total_cnt} = {(correct_cnt / total_cnt):.2%}\n")
# Output to JSON file
if output_file:
os.makedirs(os.path.dirname(output_file), exist_ok=True)
with open(output_file, "w") as outfile:
json.dump(results, outfile, indent=4)
def check_math(predict_data):
ground_truth = find_last_integer(predict_data["label"])
prediction = predict_data["predict"].strip()
answer = find_last_integer(prediction)
try:
int_ground_truth = int(ground_truth)
int_answer = int(answer)
return int_ground_truth == int_answer
except ValueError:
return False
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Visualize the results of the model")
parser.add_argument("--input_file", type=str, help="Input jsonl file")
parser.add_argument(
"--output_file", type=str, default=None, help="Output json file"
)
args = parser.parse_args()
main(args.input_file, args.output_file)