-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscore.py
More file actions
119 lines (97 loc) · 3.69 KB
/
score.py
File metadata and controls
119 lines (97 loc) · 3.69 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import json
import jsonlines
from openai import OpenAI
import concurrent.futures
import threading
from tqdm import tqdm
import os
import argparse
class Counter:
def __init__(self):
self.hit = 0
self.total = 0
self.lock = threading.Lock()
def increment_hit(self):
with self.lock:
self.hit += 1
def increment_total(self):
with self.lock:
self.total += 1
def process_item(item, index, client, prompt, writer, counter):
try:
answer = item["answer"]
gt = item['gt']
text = f"Answer: {answer} Groundtruth: {gt}"
conversation_history = [
{"role": "system",
"content": [{"type": "text", "text": prompt}]},
{"role": "user",
"content": [{"type": "text", "text": text}]}
]
response = client.chat.completions.create(
model='gpt-4o-mini-2024-07-18',
messages=conversation_history,
max_tokens=4096,
temperature=0
)
answer_corrected = response.choices[0].message.content.strip()
if answer_corrected not in ["0", "1"]:
answer_corrected = "0"
with threading.Lock():
writer.write({
'number': index,
"answer": answer,
"gt": gt,
'hit': answer_corrected
})
counter.increment_total()
if answer_corrected == "1":
counter.increment_hit()
except Exception as e:
print(f"Error in {index}: {str(e)}")
with threading.Lock():
writer.write({
'number': index,
"answer": answer,
"gt": gt,
'hit': f"error: {str(e)}"
})
counter.increment_total()
def main(args):
api_keys = os.getenv("OPENAI_API_KEY")
base_url = os.getenv("OPENAI_API_BASE")
prompt = "Compare the given two two-dimensional matrices. Allow for differences in format, but each value should be identical. If the two matrices are identical, reply with 1; otherwise, reply with 0."
input_file = args.input_file
output_file = args.output_file
max_workers = args.num_workers
client = OpenAI(
api_key=api_keys,
base_url=base_url
)
with open(input_file, 'r', encoding='utf-8') as f:
items = list(jsonlines.Reader(f))
counter = Counter()
with jsonlines.open(output_file, mode='w') as writer, \
concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
pbar = tqdm(total=len(items), desc="Processing")
futures = []
for i, item in enumerate(items):
future = executor.submit(
process_item,
item, i, client, prompt, writer, counter
)
future.add_done_callback(lambda _: pbar.update(1))
futures.append(future)
for future in concurrent.futures.as_completed(futures):
pass
pbar.close()
print(f"Hit: {counter.hit}")
print(f"Total: {counter.total}")
print(f"Acc: {counter.hit / counter.total:.4f}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Define hyper-parameters for reasoning')
parser.add_argument('--input_file', default="result.json", type=str, help='results after inference')
parser.add_argument('--output_file', default="result_score.json", type=str, help='scored results after scoring')
parser.add_argument('--num_workers', default=40, type=int, help='num workers in scoring')
args = parser.parse_args()
main(args)