-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextractive_summarisation_evaluation.py
322 lines (269 loc) · 9.71 KB
/
extractive_summarisation_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
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
import argparse
import json
import os
import sys
from datasets import load_dataset
import faiss
import nltk
import numpy as np
from sentence_transformers import SentenceTransformer
import time
from tqdm import tqdm
import wandb
from tools import _load_model, load_config, search, summarise_question
sys.path.insert(0, "UniEval")
from utils import convert_to_json
from metric.evaluator import get_evaluator
working_directory = os.getcwd()
####################################################################
# CUSTOM ARGUMENTS
####################################################################
class ArgumentParser(argparse.ArgumentParser):
def __init__(self):
super().__init__(
description='InputOptions')
self.add_argument(
'-d', '--device',
choices=["cpu", "gpu"],
default="cpu",
help="The device on which to run the experiment:\
cpu or gpu")
self.add_argument(
'-rag', '--do_rag',
action="store_true",
help="Whether to do RAG instead of using ground \
truth documents from which to generate \
summaries."
)
self.add_argument(
'-m', '--model',
type=str,
help="The Sentence Transformer encoder model to use.")
self.add_argument(
'-o', '--output',
type=str,
help="Where to store results."
)
self.add_argument(
'-rm', '--rag_model',
default="all-mpnet-base-v2",
type=str,
help="The name of the encoding model for retrieval to use if doing \
RAG: currently only supports SentenceTransformers models. "
)
self.add_argument(
'-rk', '--rag_top_k',
default=2,
type=int,
help="The top documents to retrieve if doing RAG."
)
self.add_argument(
'-s', '--subset',
choices=["AR6", "AR5", "ALL"],
default="ALL",
help="The subset of the dataset to use."
)
self.add_argument(
'-tk', '--top_k',
default=2,
type=int,
help="The number of sentences to extract \
as the summary of the given input."
)
def error(self, message):
self.print_help(sys.stderr)
self.exit(2, '%s: error: %s\n' % (self.prog, message))
###################################################################
# MAIN FUNCTION
###################################################################
def main(args):
# initialize end of turn mark (dependent on model)
#if args.model.startswith("google/gemma"):
# end_turn = "<end_of_turn>\n"
#elif args.model.startswith("mistralai"):
# end_turn = "[/INST]"
#elif args.model.startswith("microsoft")
# initialize wandb
nltk.download("punkt")
wandb_config = load_config("config.yaml")[0]
os.environ["WANDB_PROJECT"] = wandb_config["project"]
try:
os.environ["WANDB_API_KEY"] = wandb_config["key"]
wandb.init(config=wandb_config, entity=wandb_config["entity"])
use_wandb = True
except wandb.errors.UsageError:
print("WARNING: NO WANDB KEY HAS BEEN SET! THE EXPERIMENT WILL BE LOGGED JUST LOCALLY!")
os.environ["WANDB_DISABLED"] = "true"
use_wandb = False
# model instantiation
device = "cpu" if args.device=="cpu" else "cuda"
model = SentenceTransformer(args.model, device=device)
# load the dataset
data = load_dataset("sumipcc_dataset", args.subset)
# initialize variables
all_coherence = []
all_consistency = []
all_fluency = []
all_relevance = []
all_overall = []
all_summaries = []
all_keys = []
# Initialize evaluator for a specific task
task = 'summarization'
evaluator = get_evaluator(task, device=device)
if use_wandb:
columns = [
"status",
"model_name",
"dataset",
"identifier",
"prompt",
"response",
"coherence",
"consistency",
"fluency",
"relevance",
"overall",
"response_time_seconds",
]
table = wandb.Table(columns=columns)
wandb.run.log_code(".")
ex_n = 0
status = "success"
new_prompt = ""
if args.do_rag:
encoding_model = SentenceTransformer(args.rag_model)
docs = set([])
for doc in data["test"]:
docs.update(doc["full_paragraphs"])
docs = list(docs)
embeddings = encoding_model.encode(docs)
index = faiss.IndexFlatL2(embeddings.shape[1])
index.add(embeddings)
faiss.write_index(index, 'all_index')
index = faiss.read_index('all_index')
for row in tqdm(data["test"]):
argument = row["summary_topic"]
if args.do_rag:
retrieved_docs = search(argument,
encoding_model,
index,
docs,
args.rag_top_k)
question = "\n".join(retrieved_docs)
else:
question = "\n".join(row["full_paragraphs"])
sentences = []
for sentence in nltk.tokenize.sent_tokenize(question):
sentences.append(sentence)
embeddings = model.encode(sentences)
s_index = faiss.IndexFlatL2(embeddings.shape[1])
s_index.add(embeddings)
faiss.write_index(s_index, 'all_sentences')
s_index = faiss.read_index('all_sentences')
reference = row["summary"]
key = row["ID"]
# print(key)
start = time.time()
retrieved_docs = search(argument,
model,
s_index,
sentences,
args.top_k)
print(status)
seconds = time.time()-start
summary = "\n".join(retrieved_docs).strip()
if not ex_n%20:
print("Example output:\n")
print(new_prompt)
print(summary)
all_summaries.append(summary)
all_keys.append(key)
# Prepare data for pre-trained evaluators
data_json = convert_to_json(output_list=[summary],
src_list=[question],
ref_list=[reference])
# Get multi-dimensional evaluation scores
eval_scores = evaluator.evaluate(data_json, print_result=True)
coherence = eval_scores[0]["coherence"]
consistency = eval_scores[0]["consistency"]
fluency = eval_scores[0]["fluency"]
relevance = eval_scores[0]["relevance"]
overall = eval_scores[0]["overall"]
all_coherence.append(coherence)
all_consistency.append(consistency)
all_fluency.append(fluency)
all_relevance.append(relevance)
all_overall.append(overall)
ex_n += 1
# log results to wandb (if using)
if use_wandb:
if status=="success":
table.add_data(
status,
args.model,
args.subset,
key,
new_prompt,
summary,
coherence,
consistency,
fluency,
relevance,
overall,
seconds
)
wandb.log(
{
"coherence": coherence,
"consistency": consistency,
"fluency": fluency,
"relevance": relevance,
"overall": overall
}
)
else:
table.add_data(
status,
args.model,
args.subset,
key,
new_prompt,
None,
None,
None,
None,
None,
None,
None
)
mean_coherence = np.mean(all_coherence)
mean_consistency = np.mean(all_consistency)
mean_fluency = np.mean(all_fluency)
mean_relevance = np.mean(all_relevance)
mean_overall = np.mean(all_overall)
if use_wandb:
# Set summary value for the line plots to be the mean overall scores
# Otherwise these are recorded as the final scores
wandb.run.summary["coherence"] = mean_coherence
wandb.run.summary["consistency"] = mean_consistency
wandb.run.summary["fluency"] = mean_fluency
wandb.run.summary["relevance"] = mean_relevance
wandb.run.summary["overall"] = mean_overall
wandb.log({"coherence_avg":mean_coherence})
wandb.log({"consistency_avg":mean_consistency})
wandb.log({"fluency_avg":mean_fluency})
wandb.log({"relevance_avg":mean_relevance})
wandb.log({"overall_avg":mean_overall})
wandb.log({"Summarisation Results": table})
with open(args.output, "w") as f:
json.dump({"coherence":mean_coherence,
"consistency":mean_consistency,
"fluency": mean_fluency,
"relevance": mean_relevance,
"overall": mean_overall
}, f)
if __name__ == '__main__':
parser = ArgumentParser()
args = parser.parse_args(sys.argv[1:])
main(args)