-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_evaluation.py
158 lines (109 loc) · 5.46 KB
/
auto_evaluation.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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import os
import pandas as pd
def extract_final_answer(each_mod_answer):
"""提取待测模型回答中 \boxed{} 中的内容,如果待测模型没有根据要求使用 \boxed{} 的格式输出最终答案,则返回空字符串
params:
each_mod_answer: 待测模型给出的回答
return:
each_final_answer:\boxed{} 中的内容"""
start_index = 0
end_index = 0
each_mod_answer = repr(each_mod_answer)
if not each_mod_answer or each_mod_answer == '' or each_mod_answer == None:
each_final_answer = ''
return each_final_answer
for i in range(0, len(each_mod_answer)):
if end_index != 0:
try:
if each_mod_answer[-i-2] == 'd' and each_mod_answer[-i-3] == 'e' and each_mod_answer[-i-4] == 'x' and each_mod_answer[-i-5] == 'o':
start_index = -i
break
else:
continue
except:
return ''
else:
if each_mod_answer[-i] == '}':
end_index = -i
else:
continue
each_final_answer = each_mod_answer[start_index:end_index]
i = 0
while i <= len(each_final_answer):
try:
if each_final_answer[i] == '\\' and each_final_answer[i+1] == '\\':
each_final_answer = each_final_answer[:i] + each_final_answer[i+1:]
except:
i += 1
continue
i += 1
return each_final_answer
def auto_gene_evaluation_result():
#---------- initial config ----------#
current_dir = os.path.dirname(os.path.realpath(__file__))
#---#
questions_file_path = os.path.join(current_dir, 'math24o.xlsx')
questions_dataframe = pd.read_excel(questions_file_path)
mod_answers_file_path = os.path.join(current_dir, 'model_answers.xlsx')
mod_answers_dataframe = pd.read_excel(mod_answers_file_path)
id_list = questions_dataframe['id']
question_list = questions_dataframe['questions']
ref_answer_list = questions_dataframe['ref_answers']
mod_answer_list = mod_answers_dataframe['model_answers']
output_dataframe = pd.DataFrame({})
count = 1
#---------- essential loop ----------#
for each_id, each_question, each_ref_answer, each_mod_answer in zip(id_list, question_list, ref_answer_list, mod_answer_list):
each_final_answer = extract_final_answer(each_mod_answer)
try:
each_final_answer = float(each_final_answer)
except:
each_final_answer = each_final_answer
if each_final_answer == each_ref_answer:
each_score = 1
else:
# print(type(each_ref_answer), type(each_final_answer))
each_score = 0
#---------- output processing message ----------#
# match str(count)[-1]:
# case '1':
# print(">>> Evaluating " + str(count) + "st model's answer...")
# case '2':
# print(">>> Evaluating " + str(count) + "nd model's answer...")
# case '3':
# print(">>> Evaluating " + str(count) + "rd model's answer...")
# case _:
# print(">>> Evaluating " + str(count) + "th model's answer...")
#---------- for developer ----------#
new_dataframe = pd.DataFrame({'id': [each_id], 'questions': [each_question], 'ref_answers': [each_ref_answer], 'mod_answers': [each_mod_answer], 'final_answers':[each_final_answer], 'scores':[each_score]})
#---------- for user ----------#
# new_dataframe = pd.DataFrame({'id': [each_id], 'mod_answers': [each_mod_answer], 'scores':[each_score]})
#---------- preparation of output ----------#
output_dataframe = pd.concat([output_dataframe, new_dataframe], ignore_index=True)
output_file_path = os.path.join(current_dir, 'output.xlsx')
output_dataframe.to_excel(output_file_path, index=False)
#---------- output successfull message ----------#
match str(count)[-1]:
case '1':
print(">>> " + str(count) +
"st model's answer has been evaluated successfully.")
case '2':
print(">>> " + str(count) +
"nd model's answer has been evaluated successfully.")
case '3':
print(">>> " + str(count) +
"rd model's answer has been evaluated successfully.")
case _:
print(">>> " + str(count) +
"th model's answer has been evaluated successfully.")
#---------- after an evaluation, add 1 to count ----------#
count += 1
#----------------------------------------------#
score_list = output_dataframe['scores']
average = sum(score_list) / len(score_list)
average_dataframe = pd.DataFrame({'scores':[100*average]})
output_dataframe = pd.concat([output_dataframe, average_dataframe], ignore_index=True)
output_dataframe.to_excel(output_file_path, index=False)
print(">>> All model's answers have been evaluated successfully! The final score is " + str(100*average) + '. For more evaluation details, please check the output.xlsx file.')
if __name__ == '__main__':
auto_gene_evaluation_result()