MCPcopy Create free account
hub / github.com/Felixgithub2017/MMCU / eval

Function eval

TestMOSS.py:91–162  ·  view source on GitHub ↗
(args, subject, dev_df, test_df)

Source from the content-addressed store, hash-verified

89 f.write(contents)
90
91def eval(args, subject, dev_df, test_df):
92 logfile = "MOSStestlogfile0512"
93 cors = []
94 #labels = []
95 preds = []
96 for i in range(test_df.shape[0]):
97 # get prompt and make sure it fits
98 k = args.ntrain
99 prompt_end = format_example(test_df, i, include_answer=False)
100 train_prompt = gen_prompt(dev_df, k)
101 prompt = train_prompt + prompt_end
102
103 #print("train_prompt:", train_prompt)
104 print("题目:", prompt)
105 with open(logfile, 'a', encoding='utf8') as f:
106 f.write(prompt+"\n")
107
108 try:
109 label = test_df.iloc[i, test_df.shape[1]-1]
110 # A B C D 特殊处理
111 label = label.replace(" ", "").replace("A", "A").replace("B", "B").replace("C", "C").replace("D", "D")
112 label = label.replace("\u3000", "").replace(",", "")
113 print("正确答案:", label)
114 with open(logfile, 'a', encoding='utf8') as f:
115 f.write("正确答案:"+label+"\n")
116 except Exception as e:
117 print(e)
118 break
119
120 while True:
121 try:
122 time.sleep(1)
123 pred = plain_chat(prompt)
124 pred = pred.replace("、", "").replace(".", "").replace(",", "").replace(";", "").replace(",", "")
125 try:
126 # 识别答案pattern
127 pred = find_valid_substrings(pred)[0]
128 except Exception as e:
129 print(e)
130 pred = "未成功回答"
131 print("模型预测答案:", pred)
132 with open(logfile, 'a', encoding='utf8') as f:
133 f.write("模型预测答案:"+pred+"\n")
134 break
135 except Exception as e:
136 print(e)
137 print("pausing")
138 time.sleep(10)
139 continue
140
141 try:
142 cor = pred == label
143 print("是否答对:", cor)
144 with open(logfile, 'a', encoding='utf8') as f:
145 f.write("是否答对:"+str(cor)+"\n")
146 cors.append(cor)
147 preds.append(pred+"|||"+label+"|||"+str(cor))
148

Callers 1

mainFunction · 0.70

Calls 4

format_exampleFunction · 0.70
gen_promptFunction · 0.70
plain_chatFunction · 0.70
find_valid_substringsFunction · 0.70

Tested by

no test coverage detected