-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevaluate.py
576 lines (499 loc) · 25.2 KB
/
evaluate.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
from dataclasses import dataclass, field
import math
from typing import Optional, Tuple
import torch
import transformers
import pandas as pd
from utils import *
from tqdm import tqdm
from sklearn.metrics import f1_score, precision_score, recall_score, accuracy_score
from openai import OpenAI
import os
import random
import api_secrets
os.environ['OPENAI_API_KEY'] = api_secrets.openai_api_key
client = OpenAI(
api_key=os.environ["OPENAI_API_KEY"],
organization=api_secrets.openai_org
)
@dataclass
class ModelArguments:
model_name_or_path: Optional[str] = field(default="facebook/opt-125m")
trust_remote_code: bool = field(
default=False,
metadata={
"help": "Whether or not to allow for custom models defined on the Hub in their own modeling files"
},
)
padding_side: str = field(
default="right", metadata={"help": "The padding side in tokenizer"}
)
@dataclass
class DataArguments:
data_path: str = field(
default=None, metadata={"help": "Path to the training data."}
)
eval_data_path: str = field(
default=None, metadata={"help": "Path to the evaluation data."}
)
task: str = field(
default="detection", metadata={"help": "The task to train on. Select from detection, cohesion, consistency, relevance, other, and rewriting."}
)
lazy_preprocess: bool = False
class Evaluator(object):
def __init__(self, model_args, model, eval_dataset, task, tokenizer=None, evaluate_all_metrics=False, print_results=False, with_gpt=False, rewriting_metric="acceptance_rate"):
""" Initialize the Evaluator.
Args:
args: TrainingArguments
model: Pretrained model or model_name ('gpt-4', 'ChatGPT')
tokenizer: Pretrained tokenizer
eval_dataset: Path to the evaluation dataset
metrics: 'hard' or 'soft'
print_results: True or False
aspect: 'acc' or 'prof'
topk: top k candidates to be considered as substitutes
"""
self.model_args = model_args
self.model = model
self.eval_dataset = eval_dataset
self.task = task
self.tokenizer = tokenizer
self.evaluate_all_metrics = evaluate_all_metrics
self.print_results = print_results
self.with_gpt = with_gpt
self.rewriting_metric = rewriting_metric
def calculate_metrics(self, labels, preds):
""" Calculate the precision, recall, and F1 score.
Args:
labels: gold labels -> list of strings
preds: predicted substitutes -> list of strings
Returns:
precision: precision score
recall: recall score
f1: F1 score
"""
# Verify if labels and preds have the same length
if len(labels) != len(preds):
raise ValueError("The length of labels and preds must be the same")
f1, precision, recall = {}, {}, {}
# f1_real = {}
# f1['macro'] = f1_score(labels, preds, average='macro')
# f1['weighted'] = f1_score(labels, preds, average='weighted')
# f1['micro'] = f1_score(labels, preds, average='micro')
precision['macro'] = precision_score(labels, preds, average='macro')
precision['weighted'] = precision_score(labels, preds, average='weighted')
precision['micro'] = precision_score(labels, preds, average='micro')
recall['macro'] = recall_score(labels, preds, average='macro')
recall['weighted'] = recall_score(labels, preds, average='weighted')
recall['micro'] = recall_score(labels, preds, average='micro')
# f1['macro'] = 2 * (precision['macro'] * recall['macro']) / (precision['macro'] + recall['macro'])
# f1['weighted'] = 2 * (precision['weighted'] * recall['weighted']) / (precision['weighted'] + recall['weighted'])
# f1['micro'] = 2 * (precision['micro'] * recall['micro']) / (precision['micro'] + recall['micro'])
f1['macro'] = f1_score(labels, preds, average='macro')
f1['weighted'] = f1_score(labels, preds, average='weighted')
f1['micro'] = f1_score(labels, preds, average='micro')
results = [precision, recall, f1]
return results
def calculate_win_rate(self, eval_df, shuffle=True):
selection2pos = {
'm': 1,
'M': 2
}
pos2selection = {
1: 'm',
2: 'M'
}
output = []
selection = []
pred_appearin_pos = [] # predictions positions (1 or 2)
# pred_pos = [] # predictions positions (1 or 2)
# gpt_selections = [] # GPT-4 selections ('m' or 'M')
for i in tqdm(range(len(eval_df))):
row = eval_df.iloc[i]
context = row['context']
sentence = row['sentence']
pred = row['Predictions']
ref = row['Rewrite'] # reference
output_1 = pred # first position
output_2 = ref # second position
pred_pos = 1
# randomly select between 1 and 2 for pred position
if shuffle:
pred_pos = selection2pos['m'] if random.random() > 0.5 else selection2pos['M']
pred_appearin_pos.append(pred_pos)
# pred_pos.append(pos)
if pred_pos == 1:
output_1 = pred
output_2 = ref
else:
output_1 = ref
output_2 = pred
if 'rewriting-nr' in self.task:
prompt = formulate_prompt_nr(context, sentence, output_1, output_2)
elif 'rewriting-r' in self.task:
r1, r2, r3, r4, r5, r6, r7 = row['R1'], row['R2'], row['R3'], row['R4'], row['R5'], row['R6'], row['R7']
reasons = [r1, r2, r3, r4, r5, r6, r7]
prompt = formulate_prompt_r(context, sentence, reasons, output_1, output_2)
win = None
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=prompt,
max_tokens=2048,
temperature=0
)
# output = response['choices'][0]['message']['content']
o = response.choices[0].message.content
print("---------------------------")
print("Index: ", i)
print("Pred model id: ", pos2selection[pred_pos])
print("Ref model id: ", pos2selection[3-pred_pos])
print(o)
judge_selection = o[-1] # 'm' or 'M'
selection.append(judge_selection)
win = 1 if selection2pos[judge_selection] == pred_pos else 0 # 1 if the target model wins, 0 otherwise
except Exception as e:
print(e)
print('Error with index ', i)
print('Context: ', context)
print('Sentence: ', sentence)
print('Predicted: ', pred)
print('Reference: ', ref)
selection.append('')
output.append(win)
# save selection list to a file
# pred_appearin_modelids = [pos2selection[p] for p in pred_appearin_pos] # 'm' or 'M' for pred models
if self.model_args.model_name_or_path.endswith(".csv"):
model_path = self.model_args.model_name_or_path.split("/")[-1].split('.csv')[0]
else:
model_path = self.model_args.model_name_or_path.split("/")[-2]
selection_pos = [selection2pos[s] for s in selection]
with open("output/rewriting/{}_gpt_selections.txt".format(model_path), "w") as f:
f.write("Pred model positions: " + str(pred_appearin_pos) + "\n")
f.write("Selections pos: " + str(selection_pos) + "\n")
f.write("--------------------------------------------------" + "\n")
for i, row in eval_df.iterrows():
context = row['context']
sentence = row['sentence']
pred = row['Predictions']
ref = row['Rewrite']
pred_position = pred_appearin_pos[i]
f.write("Context: " + context + "\n")
f.write("Sentence: " + sentence + "\n")
f.write("Predicted: " + pred + "\n")
f.write("Reference: " + ref + "\n")
f.write("Prediction model id: " + pos2selection[pred_position] + "\n")
f.write("Selection: " + selection[i] + "\n")
f.write("--------------------------------------------------" + "\n")
f.write("\n")
# calculate the win rate and exclude None
output = [o for o in output if o is not None]
win_rate = sum(output) / len(output)
return win_rate
def calculate_acceptance_rate(self, eval_df):
# calculate acceptance rate
acceptance_rate = 0
for i in tqdm(range(len(eval_df))):
row = eval_df.iloc[i]
context = row['context']
# sentence = row['sentence']
pred = row['Predictions']
# ref = row['Rewrite']
prompt = format_test_prompt_gpt_16shots_exp(context, pred)
try:
response = client.chat.completions.create(
model="gpt-4o",
messages=prompt,
max_tokens=2048,
temperature=0
)
o = response.choices[0].message.content
print(o)
o = o.split('\n')[0].strip()
if int(o) == 1:
acceptance_rate += 1
# acceptance_rate += int(o[-1] == 'm')
except Exception as e:
print(e)
print('Error with index ', i)
print('Context: ', context)
# print('Sentence: ', sentence)
print('Predicted: ', pred)
# print('Reference: ', ref)
acceptance_rate /= len(eval_df)
return acceptance_rate
def print_prediction_results(self, preds):
""" Print the prediction results. """
eval_df = pd.read_csv(self.eval_dataset, index_col=False)
print("Printing prediction results...")
print(preds)
print("--------------------------------------------------")
# store the predicted substitutes in a txt file
with open("output/predictions.txt", "w") as f:
for i, row in eval_df.iterrows():
context = row['context']
sentence = row['sentence']
f.write("Context: " + context + "\n")
f.write("Sentence: " + sentence + "\n")
f.write("Gold answer: " + str(row['label']) + "\n")
f.write("Predictions: " + str(preds[i]) + "\n")
f.write("--------------------------------------------------" + "\n")
f.write("\n")
def evaluate_detection_metrics(self, labels, preds):
return self.calculate_metrics(labels, preds)
def evaluate_rewriting_gpt4(self, eval_df):
if self.rewriting_metric == "acceptance_rate":
return self.calculate_acceptance_rate(eval_df)
elif self.rewriting_metric == "win_rate":
return self.calculate_win_rate(eval_df, shuffle=True)
def get_gold_labels(self):
""" Get the gold labels from the evaluation dataset. """
eval_df = pd.read_csv(self.eval_dataset, index_col=False)
gold_labels = []
for i in tqdm(range(len(eval_df))):
row = eval_df.iloc[i]
gold_labels.append(row['label'])
return gold_labels
def evaluate(self):
""" Evaluate the model on the given dataset. """
model_preds = []
eval_df = pd.read_csv(self.eval_dataset, index_col=False)
if self.model_args.model_name_or_path.endswith(".csv"):
# self.model in ['human']:
pred_df = pd.read_csv(self.model_args.model_name_or_path, index_col=False)
model_preds = pred_df['Predictions'] # human
elif 'gpt-4' in self.model_args.model_name_or_path:
for i in tqdm(range(len(eval_df))):
row = eval_df.iloc[i]
context = row['context']
sentence = row['sentence']
if self.task == 'detection':
# system_input = format_test_prompt_gpt(context, sentence)
system_input = format_test_prompt_gpt_16shots_exp(context, sentence)
elif self.task == 'cohesion':
system_input = format_test_prompt_cohesion_gpt_16shots_exp(context, sentence)
elif self.task == 'consistency':
system_input = format_test_prompt_consistency_gpt(context, sentence)
elif self.task == 'relevance':
system_input = format_test_prompt_relevance_gpt_16shots_exp(context, sentence)
elif self.task == 'other':
system_input = format_test_prompt_other_gpt_16shots_exp(context, sentence)
try:
response = client.chat.completions.create(
model=self.model_args.model_name_or_path,
messages=system_input,
max_tokens=2048,
temperature=0
)
# output = response['choices'][0]['message']['content']
output = response.choices[0].message.content
print(output)
pred = int(output.split('\n')[0].strip())
except Exception as e:
print(e)
# print("Generated answer: ", generated_texts)
print("Error with index ", i)
print("Context: ", context)
print("Sentence: ", sentence)
pred = -1
model_preds.append(pred)
else:
# eval mode
self.model.eval()
if self.tokenizer.pad_token is None:
self.tokenizer.pad_token = self.tokenizer.eos_token
# no gradient calculation
with torch.no_grad():
for i in tqdm(range(len(eval_df))):
row = eval_df.iloc[i]
context = row['context']
sentence = row['sentence']
if self.task == 'detection':
system_input = format_test_prompt(context, sentence)
# elif self.task in ['detection-llama3', 'cohesion-llama3', 'consistency-llama3', 'relevance-llama3', 'other-llama3']:
# system_input = format_test_prompt_llama3(context, sentence)
elif self.task == 'cohesion':
system_input = format_test_prompt_cohesion(context, sentence)
elif self.task == 'consistency':
system_input = format_test_prompt_consistency(context, sentence)
elif self.task == 'relevance':
system_input = format_test_prompt_relevance(context, sentence)
elif self.task == 'other':
system_input = format_test_prompt_other(context, sentence)
elif self.task == 'rewriting-nr':
system_input = format_test_prompt_rewriting_nr(context, sentence)
elif self.task == 'rewriting-nr-llama3-instruct':
system_input = format_test_prompt_rewriting_nr_llama3_instruct(context, sentence)
elif self.task == 'rewriting-r':
r1, r2, r3, r4, r5, r6, r7 = row['R1'], row['R2'], row['R3'], row['R4'], row['R5'], row['R6'], row['R7']
reasons = [r1, r2, r3, r4, r5, r6, r7]
system_input = format_test_prompt_rewriting_r(context, sentence, reasons)
elif self.task == 'rewriting-r-llama3-instruct':
r1, r2, r3, r4, r5, r6, r7 = row['R1'], row['R2'], row['R3'], row['R4'], row['R5'], row['R6'], row['R7']
reasons = [r1, r2, r3, r4, r5, r6, r7]
system_input = format_test_prompt_rewriting_r_llama3_instruct(context, sentence, reasons)
if 'llama3' not in self.task:
input_ids = tokenizer.encode(system_input, return_tensors='pt', add_special_tokens=True)
input_ids = input_ids.cuda()
# Generate the candidates.
generated_ids = self.model.generate(
input_ids,
max_length=self.tokenizer.model_max_length,
temperature=0.2,
pad_token_id=self.tokenizer.pad_token_id)
# Decode the candidates.
generated_texts = self.tokenizer.batch_decode(
generated_ids, skip_special_tokens=True)
# print(generated_texts)
try:
if self.task in ['detection', 'cohesion', 'consistency', 'relevance', 'other']:
pred = int(generated_texts[0].split("Answer: ")[1].strip())
else:
pred = generated_texts[0].split("Answer: ")[1].strip()
except Exception as e:
print(e)
print("Generated answer: ", generated_texts)
pred = None
model_preds.append(pred)
else:
terminators = [
self.tokenizer.eos_token_id,
self.tokenizer.convert_tokens_to_ids("<|eot_id|>")
]
input_ids = self.tokenizer.apply_chat_template(
system_input,
add_generation_prompt=True,
return_tensors="pt"
).to(model.device)
outputs = model.generate(
input_ids,
attention_mask=torch.ones_like(input_ids),
pad_token_id=tokenizer.pad_token_id,
max_new_tokens=512,
eos_token_id=terminators,
do_sample=True,
temperature=0.2,
top_p=0.9,
)
generated_ids = outputs[0][input_ids.shape[-1]:]
generated_texts = self.tokenizer.decode(generated_ids, skip_special_tokens=True)
if 'rewriting' in self.task:
model_preds.append(generated_texts)
else:
model_preds.append(int(generated_texts))
# print the results if print_results is True
if self.print_results:
self.print_prediction_results(model_preds)
if 'rewriting' not in self.task:
# self.task in ['detection', 'cohesion', 'consistency', 'relevance', 'other'] or self.task in ['detection-llama3', 'cohesion-llama3', 'consistency-llama3', 'relevance-llama3', 'other-llama3']:
# calculate precision, recall, and F1 for the predictions
gold_labels = self.get_gold_labels()
assert len(gold_labels) == len(model_preds)
print("Gold labels: ", gold_labels)
print("Predictions: ", model_preds)
results = self.evaluate_detection_metrics(gold_labels, model_preds)
print("Precision: ", results[0])
print("Recall: ", results[1])
print("F1: ", results[2])
else:
if self.rewriting_metric == "acceptance_rate": # rewriting main metric
eval_df['Predictions'] = model_preds
if 'human' in self.model_args.model_name_or_path:
save_model_path = self.model_args.model_name_or_path.split("/")[-1].split('.csv')[0]
else:
save_model_path = self.model_args.model_name_or_path.split("/")[-2]
# check if file exists
if not os.path.exists("output/rewriting/neg_448/{}_pred.csv".format(save_model_path)):
eval_df.to_csv("output/rewriting/neg_448/{}_pred.csv".format(save_model_path), index=False)
print("Predictions saved to output")
print("-------------------------------------------")
print("Now, evaluating with GPT-4 ... ")
acceptance_rate = self.evaluate_rewriting_gpt4(eval_df)
print("Acceptance rate: ", acceptance_rate)
else:
# add a new column to the dataframe
if not self.model_args.model_name_or_path.endswith(".csv"):
eval_df['Predictions'] = model_preds
save_model_path = self.model_args.model_name_or_path.split("/")[-2]
eval_df.to_csv("output/{}_pred.csv".format(save_model_path), index=False)
print("Predictions saved to output")
print("-------------------------------------------")
print("Now, evaluating with GPT-4 ... ")
win_rate = self.evaluate_rewriting_gpt4(eval_df) # [1, ....] a list indicating if the target model wins (1) or not (0) compared to human reference -> win rate
print("Win rate: ", win_rate)
else:
print("Now, evaluating with GPT-4 ... ")
win_rate = self.evaluate_rewriting_gpt4(eval_df)
print("Win rate: ", win_rate)
def predict_single_turn(self,
inputs: Tuple[str, str]):
""" Predict substitutes given a Tuple of target word and sentence. """
print("Predicting substitutes for target word: ", inputs[0])
context, sentence = inputs
system_input = format_test_prompt(context, sentence)
# for input_text, target_text in zip(inputs, targets):
input_ids = tokenizer.encode(system_input, return_tensors='pt', add_special_tokens=True)
input_ids = input_ids.cuda()
print("System input length: ", int(input_ids.ne(self.tokenizer.pad_token_id).sum()))
# Generate the candidates.
# eval mode
self.model.eval()
# no gradient calculation
with torch.no_grad():
generated_ids = self.model.generate(
input_ids,
max_length=self.tokenizer.model_max_length,
temperature=0.2)
# Decode the candidates.
generated_texts = self.tokenizer.batch_decode(
generated_ids, skip_special_tokens=True)
return generated_texts[0]
if __name__ == "__main__":
# load model
parser = transformers.HfArgumentParser(
(ModelArguments, DataArguments)
)
model_args, data_args = parser.parse_args_into_dataclasses()
if model_args.model_name_or_path.endswith(".csv"):
# evaluate
print("Evaluating predictions from ", model_args.model_name_or_path)
# evaluator = Evaluator(model_args, model_args.model_name_or_path, data_args.data_path, task=data_args.task)
evaluator = Evaluator(model_args, model_args.model_name_or_path, data_args.data_path, data_args.task, print_results=True)
metrics = evaluator.evaluate()
elif 'gpt-4' in model_args.model_name_or_path:
print("Evaluating predictions from GPT-4 ...")
evaluator = Evaluator(model_args, model_args.model_name_or_path, data_args.data_path, data_args.task, print_results=True, with_gpt=True)
metrics = evaluator.evaluate()
else:
# Set RoPE scaling factor
config = transformers.AutoConfig.from_pretrained(
model_args.model_name_or_path,
trust_remote_code=model_args.trust_remote_code,
)
# Load model and tokenizer
model = transformers.AutoModelForCausalLM.from_pretrained(
model_args.model_name_or_path,
config=config,
trust_remote_code=model_args.trust_remote_code,
)
tokenizer = transformers.AutoTokenizer.from_pretrained(
model_args.model_name_or_path,
padding_side='left',
use_fast=False,
trust_remote_code=model_args.trust_remote_code,
)
device = torch.device('cuda:0')
model.to(device)
model = model.bfloat16()
# evaluate
print("Evaluating...")
print("Task: ", data_args.task)
evaluator = Evaluator(model_args, model, data_args.data_path, data_args.task, tokenizer, print_results=True)
metrics = evaluator.evaluate()
# predict on single input
# target_word = "obligatory"
# sentence = "Even though it was an **obligatory** experience, I could take part in a community program"
# inputs = (target_word, sentence)
# evaluator = Evaluator(training_args, model, tokenizer, None)
# generated_texts = evaluator.predict_single_turn(inputs)
# print(generated_texts)