-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtext_summarization_(mp).py
677 lines (479 loc) · 21.7 KB
/
text_summarization_(mp).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
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# -*- coding: utf-8 -*-
"""Text_Summarization (MP)
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1wLDRy3G3YoGtl0jN4GtXJvfUl3z1fskg
Data Ingestion
"""
import os
# Commented out IPython magic to ensure Python compatibility.
# %pwd
from dataclasses import dataclass
from pathlib import Path
@dataclass(frozen=True)
class DataIngestionConfig:
root_dir: Path
source_URL: str
local_data_file: Path
unzip_dir: Path
from textSummarizer.constants import *
from textSummarizer.utils.common import read_yaml, create_directories
class ConfigurationManager:
def __init__(
self,
config_filepath = CONFIG_FILE_PATH,
params_filepath = PARAMS_FILE_PATH):
self.config = read_yaml(config_filepath)
self.params = read_yaml(params_filepath)
create_directories([self.config.artifacts_root])
def get_data_ingestion_config(self) -> DataIngestionConfig:
config = self.config.data_ingestion
create_directories([config.root_dir])
data_ingestion_config = DataIngestionConfig(
root_dir=config.root_dir,
source_URL=config.source_URL,
local_data_file=config.local_data_file,
unzip_dir=config.unzip_dir
)
return data_ingestion_config
import os
import urllib.request as request
import zipfile
from textSummarizer.logging import logger
from textSummarizer.utils.common import get_size
class DataIngestion:
def __init__(self, config: DataIngestionConfig):
self.config = config
def download_file(self):
if not os.path.exists(self.config.local_data_file):
filename, headers = request.urlretrieve(
url = self.config.source_URL,
filename = self.config.local_data_file
)
logger.info(f"{filename} download! with following info: \n{headers}")
else:
logger.info(f"File already exists of size: {get_size(Path(self.config.local_data_file))}")
def extract_zip_file(self):
"""
zip_file_path: str
Extracts the zip file into the data directory
Function returns None
"""
unzip_path = self.config.unzip_dir
os.makedirs(unzip_path, exist_ok=True)
with zipfile.ZipFile(self.config.local_data_file, 'r') as zip_ref:
zip_ref.extractall(unzip_path)
try:
config = ConfigurationManager()
data_ingestion_config = config.get_data_ingestion_config()
data_ingestion = DataIngestion(config=data_ingestion_config)
data_ingestion.download_file()
data_ingestion.extract_zip_file()
except Exception as e:
raise e
"""Data Validation"""
from dataclasses import dataclass
from pathlib import Path
@dataclass(frozen=True)
class DataValidationConfig:
root_dir: Path
STATUS_FILE: str
ALL_REQUIRED_FILES: list
from textSummarizer.constants import *
from textSummarizer.utils.common import read_yaml, create_directories
class ConfigurationManager:
def __init__(
self,
config_filepath = CONFIG_FILE_PATH,
params_filepath = PARAMS_FILE_PATH):
self.config = read_yaml(config_filepath)
self.params = read_yaml(params_filepath)
create_directories([self.config.artifacts_root])
def get_data_validation_config(self) -> DataValidationConfig:
config = self.config.data_validation
create_directories([config.root_dir])
data_validation_config = DataValidationConfig(
root_dir=config.root_dir,
STATUS_FILE=config.STATUS_FILE,
ALL_REQUIRED_FILES=config.ALL_REQUIRED_FILES,
)
return data_validation_config
import os
from textSummarizer.logging import logger
class DataValiadtion:
def __init__(self, config: DataValidationConfig):
self.config = config
def validate_all_files_exist(self)-> bool:
try:
validation_status = None
all_files = os.listdir(os.path.join("artifacts","data_ingestion","samsum_dataset"))
for file in all_files:
if file not in self.config.ALL_REQUIRED_FILES:
validation_status = False
with open(self.config.STATUS_FILE, 'w') as f:
f.write(f"Validation status: {validation_status}")
else:
validation_status = True
with open(self.config.STATUS_FILE, 'w') as f:
f.write(f"Validation status: {validation_status}")
return validation_status
except Exception as e:
raise e
try:
config = ConfigurationManager()
data_validation_config = config.get_data_validation_config()
data_validation = DataValiadtion(config=data_validation_config)
data_validation.validate_all_files_exist()
except Exception as e:
raise e
"""Data transformation"""
from dataclasses import dataclass
from pathlib import Path
@dataclass(frozen=True)
class DataTransformationConfig:
root_dir: Path
data_path: Path
tokenizer_name: Path
from textSummarizer.constants import *
from textSummarizer.utils.common import read_yaml, create_directories
class ConfigurationManager:
def __init__(
self,
config_filepath = CONFIG_FILE_PATH,
params_filepath = PARAMS_FILE_PATH):
self.config = read_yaml(config_filepath)
self.params = read_yaml(params_filepath)
create_directories([self.config.artifacts_root])
def get_data_transformation_config(self) -> DataTransformationConfig:
config = self.config.data_transformation
create_directories([config.root_dir])
data_transformation_config = DataTransformationConfig(
root_dir=config.root_dir,
data_path=config.data_path,
tokenizer_name = config.tokenizer_name
)
return data_transformation_config
import os
from textSummarizer.logging import logger
from transformers import AutoTokenizer
from datasets import load_dataset, load_from_disk
class DataTransformation:
def __init__(self, config: DataTransformationConfig):
self.config = config
self.tokenizer = AutoTokenizer.from_pretrained(config.tokenizer_name)
def convert_examples_to_features(self,example_batch):
input_encodings = self.tokenizer(example_batch['dialogue'] , max_length = 1024, truncation = True )
with self.tokenizer.as_target_tokenizer():
target_encodings = self.tokenizer(example_batch['summary'], max_length = 128, truncation = True )
return {
'input_ids' : input_encodings['input_ids'],
'attention_mask': input_encodings['attention_mask'],
'labels': target_encodings['input_ids']
}
def convert(self):
dataset_samsum = load_from_disk(self.config.data_path)
dataset_samsum_pt = dataset_samsum.map(self.convert_examples_to_features, batched = True)
dataset_samsum_pt.save_to_disk(os.path.join(self.config.root_dir,"samsum_dataset"))
try:
config = ConfigurationManager()
data_transformation_config = config.get_data_transformation_config()
data_transformation = DataTransformation(config=data_transformation_config)
data_transformation.convert()
except Exception as e:
raise e
"""Model Training"""
from dataclasses import dataclass
from pathlib import Path
@dataclass(frozen=True)
class ModelTrainerConfig:
root_dir: Path
data_path: Path
model_ckpt: Path
num_train_epochs: int
warmup_steps: int
per_device_train_batch_size: int
weight_decay: float
logging_steps: int
evaluation_strategy: str
eval_steps: int
save_steps: float
gradient_accumulation_steps: int
from textSummarizer.constants import *
from textSummarizer.utils.common import read_yaml, create_directories
class ConfigurationManager:
def __init__(
self,
config_filepath = CONFIG_FILE_PATH,
params_filepath = PARAMS_FILE_PATH):
self.config = read_yaml(config_filepath)
self.params = read_yaml(params_filepath)
create_directories([self.config.artifacts_root])
def get_model_trainer_config(self) -> ModelTrainerConfig:
config = self.config.model_trainer
params = self.params.TrainingArguments
create_directories([config.root_dir])
model_trainer_config = ModelTrainerConfig(
root_dir=config.root_dir,
data_path=config.data_path,
model_ckpt = config.model_ckpt,
num_train_epochs = params.num_train_epochs,
warmup_steps = params.warmup_steps,
per_device_train_batch_size = params.per_device_train_batch_size,
weight_decay = params.weight_decay,
logging_steps = params.logging_steps,
evaluation_strategy = params.evaluation_strategy,
eval_steps = params.evaluation_strategy,
save_steps = params.save_steps,
gradient_accumulation_steps = params.gradient_accumulation_steps
)
return model_trainer_config
from transformers import TrainingArguments, Trainer
from transformers import DataCollatorForSeq2Seq
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from datasets import load_dataset, load_from_disk
import torch
class ModelTrainer:
def __init__(self, config: ModelTrainerConfig):
self.config = config
def train(self):
device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained(self.config.model_ckpt)
model_pegasus = AutoModelForSeq2SeqLM.from_pretrained(self.config.model_ckpt).to(device)
seq2seq_data_collator = DataCollatorForSeq2Seq(tokenizer, model=model_pegasus)
#loading data
dataset_samsum_pt = load_from_disk(self.config.data_path)
# trainer_args = TrainingArguments(
# output_dir=self.config.root_dir, num_train_epochs=self.config.num_train_epochs, warmup_steps=self.config.warmup_steps,
# per_device_train_batch_size=self.config.per_device_train_batch_size, per_device_eval_batch_size=self.config.per_device_train_batch_size,
# weight_decay=self.config.weight_decay, logging_steps=self.config.logging_steps,
# evaluation_strategy=self.config.evaluation_strategy, eval_steps=self.config.eval_steps, save_steps=1e6,
# gradient_accumulation_steps=self.config.gradient_accumulation_steps
# )
trainer_args = TrainingArguments(
output_dir=self.config.root_dir, num_train_epochs=1, warmup_steps=500,
per_device_train_batch_size=1, per_device_eval_batch_size=1,
weight_decay=0.01, logging_steps=10,
evaluation_strategy='steps', eval_steps=500, save_steps=1e6,
gradient_accumulation_steps=16
)
trainer = Trainer(model=model_pegasus, args=trainer_args,
tokenizer=tokenizer, data_collator=seq2seq_data_collator,
train_dataset=dataset_samsum_pt["train"],
eval_dataset=dataset_samsum_pt["validation"])
trainer.train()
## Save model
model_pegasus.save_pretrained(os.path.join(self.config.root_dir,"pegasus-samsum-model"))
## Save tokenizer
tokenizer.save_pretrained(os.path.join(self.config.root_dir,"tokenizer"))
try:
config = ConfigurationManager()
model_trainer_config = config.get_model_trainer_config()
model_trainer_config = ModelTrainer(config=model_trainer_config)
model_trainer_config.train()
except Exception as e:
raise e
"""Model Evaluation"""
from dataclasses import dataclass
from pathlib import Path
@dataclass(frozen=True)
class ModelEvaluationConfig:
root_dir: Path
data_path: Path
model_path: Path
tokenizer_path: Path
metric_file_name: Path
from textSummarizer.constants import *
from textSummarizer.utils.common import read_yaml, create_directories
class ConfigurationManager:
def __init__(
self,
config_filepath = CONFIG_FILE_PATH,
params_filepath = PARAMS_FILE_PATH):
self.config = read_yaml(config_filepath)
self.params = read_yaml(params_filepath)
create_directories([self.config.artifacts_root])
def get_model_evaluation_config(self) -> ModelEvaluationConfig:
config = self.config.model_evaluation
create_directories([config.root_dir])
model_evaluation_config = ModelEvaluationConfig(
root_dir=config.root_dir,
data_path=config.data_path,
model_path = config.model_path,
tokenizer_path = config.tokenizer_path,
metric_file_name = config.metric_file_name
)
return model_evaluation_config
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
from datasets import load_dataset, load_from_disk, load_metric
import torch
import pandas as pd
from tqdm import tqdm
class ModelEvaluation:
def __init__(self, config: ModelEvaluationConfig):
self.config = config
def generate_batch_sized_chunks(self,list_of_elements, batch_size):
"""split the dataset into smaller batches that we can process simultaneously
Yield successive batch-sized chunks from list_of_elements."""
for i in range(0, len(list_of_elements), batch_size):
yield list_of_elements[i : i + batch_size]
def calculate_metric_on_test_ds(self,dataset, metric, model, tokenizer,
batch_size=16, device="cuda" if torch.cuda.is_available() else "cpu",
column_text="article",
column_summary="highlights"):
article_batches = list(self.generate_batch_sized_chunks(dataset[column_text], batch_size))
target_batches = list(self.generate_batch_sized_chunks(dataset[column_summary], batch_size))
for article_batch, target_batch in tqdm(
zip(article_batches, target_batches), total=len(article_batches)):
inputs = tokenizer(article_batch, max_length=1024, truncation=True,
padding="max_length", return_tensors="pt")
summaries = model.generate(input_ids=inputs["input_ids"].to(device),
attention_mask=inputs["attention_mask"].to(device),
length_penalty=0.8, num_beams=8, max_length=128)
''' parameter for length penalty ensures that the model does not generate sequences that are too long. '''
# Finally, we decode the generated texts,
# replace the token, and add the decoded texts with the references to the metric.
decoded_summaries = [tokenizer.decode(s, skip_special_tokens=True,
clean_up_tokenization_spaces=True)
for s in summaries]
decoded_summaries = [d.replace("", " ") for d in decoded_summaries]
metric.add_batch(predictions=decoded_summaries, references=target_batch)
# Finally compute and return the ROUGE scores.
score = metric.compute()
return score
def evaluate(self):
device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained(self.config.tokenizer_path)
model_pegasus = AutoModelForSeq2SeqLM.from_pretrained(self.config.model_path).to(device)
#loading data
dataset_samsum_pt = load_from_disk(self.config.data_path)
rouge_names = ["rouge1", "rouge2", "rougeL", "rougeLsum"]
rouge_metric = load_metric('rouge')
score = self.calculate_metric_on_test_ds(
dataset_samsum_pt['test'][0:10], rouge_metric, model_pegasus, tokenizer, batch_size = 2, column_text = 'dialogue', column_summary= 'summary'
)
rouge_dict = dict((rn, score[rn].mid.fmeasure ) for rn in rouge_names )
df = pd.DataFrame(rouge_dict, index = ['pegasus'] )
df.to_csv(self.config.metric_file_name, index=False)
try:
config = ConfigurationManager()
model_evaluation_config = config.get_model_evaluation_config()
model_evaluation_config = ModelEvaluation(config=model_evaluation_config)
model_evaluation_config.evaluate()
except Exception as e:
raise e
"""Text Summarization"""
!nvidia-smi
!pip install transformers[sentencepiece] datasets sacrebleu rouge_score py7zr -q
!pip install --upgrade accelerate
!pip uninstall -y transformers accelerate
!pip install transformers accelerate
from transformers import pipeline, set_seed
from datasets import load_dataset, load_from_disk
import matplotlib.pyplot as plt
from datasets import load_dataset
import pandas as pd
from datasets import load_dataset, load_metric
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
import nltk
from nltk.tokenize import sent_tokenize
from tqdm import tqdm
import torch
nltk.download("punkt")
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
device = "cuda" if torch.cuda.is_available() else "cpu"
device
model_ckpt = "google/pegasus-cnn_dailymail"
tokenizer = AutoTokenizer.from_pretrained(model_ckpt)
model_pegasus = AutoModelForSeq2SeqLM.from_pretrained(model_ckpt).to(device)
!wget https://github.com/entbappy/Branching-tutorial/raw/master/summarizer-data.zip
!unzip summarizer-data.zip
dataset_samsum = load_from_disk('samsum_dataset')
dataset_samsum
split_lengths = [len(dataset_samsum[split])for split in dataset_samsum]
print(f"Split lengths: {split_lengths}")
print(f"Features: {dataset_samsum['train'].column_names}")
print("\nDialogue:")
print(dataset_samsum["test"][1]["dialogue"])
print("\nSummary:")
print(dataset_samsum["test"][1]["summary"])
def convert_examples_to_features(example_batch):
input_encodings = tokenizer(example_batch['dialogue'] , max_length = 1024, truncation = True )
with tokenizer.as_target_tokenizer():
target_encodings = tokenizer(example_batch['summary'], max_length = 128, truncation = True )
return {
'input_ids' : input_encodings['input_ids'],
'attention_mask': input_encodings['attention_mask'],
'labels': target_encodings['input_ids']
}
dataset_samsum_pt = dataset_samsum.map(convert_examples_to_features, batched = True)
dataset_samsum_pt["train"]
from transformers import DataCollatorForSeq2Seq
seq2seq_data_collator = DataCollatorForSeq2Seq(tokenizer, model=model_pegasus)
from transformers import TrainingArguments, Trainer
trainer_args = TrainingArguments(
output_dir='pegasus-samsum', num_train_epochs=1, warmup_steps=500,
per_device_train_batch_size=1, per_device_eval_batch_size=1,
weight_decay=0.01, logging_steps=10,
evaluation_strategy='steps', eval_steps=500, save_steps=1e6,
gradient_accumulation_steps=16
)
trainer = Trainer(model=model_pegasus, args=trainer_args,
tokenizer=tokenizer, data_collator=seq2seq_data_collator,
train_dataset=dataset_samsum_pt["test"],
eval_dataset=dataset_samsum_pt["validation"])
trainer.train()
# Evaluation
def generate_batch_sized_chunks(list_of_elements, batch_size):
"""split the dataset into smaller batches that we can process simultaneously
Yield successive batch-sized chunks from list_of_elements."""
for i in range(0, len(list_of_elements), batch_size):
yield list_of_elements[i : i + batch_size]
def calculate_metric_on_test_ds(dataset, metric, model, tokenizer,
batch_size=16, device=device,
column_text="article",
column_summary="highlights"):
article_batches = list(generate_batch_sized_chunks(dataset[column_text], batch_size))
target_batches = list(generate_batch_sized_chunks(dataset[column_summary], batch_size))
for article_batch, target_batch in tqdm(
zip(article_batches, target_batches), total=len(article_batches)):
inputs = tokenizer(article_batch, max_length=1024, truncation=True,
padding="max_length", return_tensors="pt")
summaries = model.generate(input_ids=inputs["input_ids"].to(device),
attention_mask=inputs["attention_mask"].to(device),
length_penalty=0.8, num_beams=8, max_length=128)
''' parameter for length penalty ensures that the model does not generate sequences that are too long. '''
# Finally, we decode the generated texts,
# replace the token, and add the decoded texts with the references to the metric.
decoded_summaries = [tokenizer.decode(s, skip_special_tokens=True,
clean_up_tokenization_spaces=True)
for s in summaries]
decoded_summaries = [d.replace("", " ") for d in decoded_summaries]
metric.add_batch(predictions=decoded_summaries, references=target_batch)
# Finally compute and return the ROUGE scores.
score = metric.compute()
return score
rouge_names = ["rouge1", "rouge2", "rougeL", "rougeLsum"]
rouge_metric = load_metric('rouge')
score = calculate_metric_on_test_ds(
dataset_samsum['test'][0:10], rouge_metric, trainer.model, tokenizer, batch_size = 2, column_text = 'dialogue', column_summary= 'summary'
)
rouge_dict = dict((rn, score[rn].mid.fmeasure ) for rn in rouge_names )
pd.DataFrame(rouge_dict, index = [f'pegasus'] )
## Save model
model_pegasus.save_pretrained("pegasus-samsum-model")
## Save tokenizer
tokenizer.save_pretrained("tokenizer")
#Load
tokenizer = AutoTokenizer.from_pretrained("/content/tokenizer")
#Prediction
gen_kwargs = {"length_penalty": 0.8, "num_beams":8, "max_length": 128}
sample_text = dataset_samsum["test"][0]["dialogue"]
reference = dataset_samsum["test"][0]["summary"]
pipe = pipeline("summarization", model="pegasus-samsum-model",tokenizer=tokenizer)
##
print("Dialogue:")
print(sample_text)
print("\nReference Summary:")
print(reference)
print("\nModel Summary:")
print(pipe(sample_text, **gen_kwargs)[0]["summary_text"])