-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFinetune_BanglaT5_for_bengali_news_summarization.py
369 lines (267 loc) · 9.63 KB
/
Finetune_BanglaT5_for_bengali_news_summarization.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
import json
import pandas as pd
import numpy as np
from pathlib import Path
import matplotlib.pyplot as plt
from torch.utils.data import Dataset, DataLoader
import pytorch_lightning as pl
from normalizer import normalize
from pytorch_lightning.callbacks import ModelCheckpoint
from sklearn.model_selection import train_test_split
from transformers import (
AdamW, AutoModelForSeq2SeqLM,AutoTokenizer as Tokenizer)
pl.seed_everything (42)
fname = 'bengali_train.jsonl'
with open(fname) as f:
data = [json.loads(line) for line in f]
# Convert the list of dictionaries to a Pandas dataframe
train_df = pd.DataFrame(data)
# Print the dataframe
train_df = train_df[['title','summary','text']]
train_df
train_df['len_summary'] = train_df['summary'].apply(lambda x: len(x.split()))
train_df['len_text'] = train_df['text'].apply(lambda x: len(x.split()))
train_df
max_index = train_df['len_summary'].idxmax()
sample = train_df.iloc[max_index]
sample['summary']
sample['len_summary']
min_index = train_df['len_summary'].idxmin()
sample = train_df.iloc[min_index]
sample['summary']
sample['len_summary']
# Plot histogram of length column
plt.hist(train_df['len_summary'], bins=30)
# Set x and y labels
plt.xlabel('Length of Summary')
plt.ylabel('Count')
# Show the plot
plt.show()
# Plot histogram of length column
plt.hist(train_df['len_text'], bins=30)
# Set x and y labels
plt.xlabel('Length of Text')
plt.ylabel('Count')
# Show the plot
plt.show()
num_short_summaries = (train_df['len_summary'] <= 3).sum()
num_short_summaries
train_df = train_df[train_df['len_summary'] > 3]
train_df.shape
# Plot histogram of length column
plt.hist(train_df['len_summary'], bins=30)
# Set x and y labels
plt.xlabel('Length of Summary')
plt.ylabel('Count')
# Show the plot
plt.show()
# Plot histogram of length column
plt.hist(train_df['len_text'], bins=30)
# Set x and y labels
plt.xlabel('Length of Summary')
plt.ylabel('Count')
# Show the plot
plt.show()
"""##**Val Data**"""
fname = 'bengali_val.jsonl'
with open(fname) as f:
data = [json.loads(line) for line in f]
# Convert the list of dictionaries to a Pandas dataframe
val_df = pd.DataFrame(data)
# Print the dataframe
val_df = val_df[['title','summary','text']]
val_df
val_df['len_summary'] = val_df['summary'].apply(lambda x: len(x.split()))
val_df['len_text'] = val_df['text'].apply(lambda x: len(x.split()))
val_df
max_index = val_df['len_summary'].idxmax()
sample = val_df.iloc[max_index]
sample['summary']
sample['len_summary']
min_index = val_df['len_summary'].idxmin()
sample = val_df.iloc[min_index]
sample['summary']
sample['len_summary']
# Plot histogram of length column
plt.hist(val_df['len_summary'], bins=30)
# Set x and y labels
plt.xlabel('Length of Summary')
plt.ylabel('Count')
# Show the plot
plt.show()
# Plot histogram of length column
plt.hist(val_df['len_text'], bins=30)
# Set x and y labels
plt.xlabel('Length of Text')
plt.ylabel('Count')
# Show the plot
plt.show()
"""## **Test Data**"""
fname = 'bengali_test.jsonl'
with open(fname) as f:
data = [json.loads(line) for line in f]
# Convert the list of dictionaries to a Pandas dataframe
test_df = pd.DataFrame(data)
# Print the dataframe
test_df = test_df[['title','summary','text']]
test_df
test_df['len_summary'] = test_df['summary'].apply(lambda x: len(x.split()))
test_df['len_text'] = test_df['text'].apply(lambda x: len(x.split()))
test_df
max_index = test_df['len_summary'].idxmax()
sample = test_df.iloc[max_index]
sample['summary']
sample['len_summary']
min_index = test_df['len_summary'].idxmin()
sample = test_df.iloc[min_index]
sample['summary']
sample['len_summary']
# Plot histogram of length column
plt.hist(test_df['len_summary'], bins=30)
# Set x and y labels
plt.xlabel('Length of Summary')
plt.ylabel('Count')
# Show the plot
plt.show()
# Plot histogram of length column
plt.hist(test_df['len_text'], bins=30)
# Set x and y labels
plt.xlabel('Length of Text')
plt.ylabel('Count')
# Show the plot
plt.show()
"""## **Model and Tokenizer**"""
MODEL_NAME = 'csebuetnlp/banglat5'
tokenizer = Tokenizer.from_pretrained(MODEL_NAME,use_fast=False)
LModel = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME,return_dict=True)
"""## **DataLoader**"""
class BengaliDataset(Dataset):
def __init__(self,data : pd.DataFrame,tokenizer : Tokenizer,source_max_token_len : int = 512,
target_max_token_len : int = 100):
self.tokenizer = tokenizer
self.data = data
self.source_max_token_len = source_max_token_len
self.target_max_token_len = target_max_token_len
def __len__(self):
return len(self.data)
def __getitem__(self,index : int):
data_row = self.data.iloc[index]
text = data_row['text']
summary = data_row['summary']
source_encoding = tokenizer(
normalize(text),
max_length = self.source_max_token_len,
padding = 'max_length',
truncation = True,
return_attention_mask = True,
return_tensors = "pt")
target_encoding = tokenizer(
normalize(summary),
max_length = self.target_max_token_len,
padding = 'max_length',
truncation = True,
return_attention_mask = True,
return_tensors = "pt")
labels = target_encoding["input_ids"]
labels[labels == 0] = -100
return dict(
text_input_ids = source_encoding['input_ids'].flatten(),
text_attention_mask = source_encoding['attention_mask'].flatten(),
labels = labels.flatten(),
labels_attention_mask = target_encoding["attention_mask"].flatten())
class BengaliDataModule(pl.LightningDataModule):
def __init__(self,train_df : pd.DataFrame,val_df : pd.DataFrame,test_df : pd.DataFrame,
tokenizer : Tokenizer,batch_size : int = 8,source_max_token_len : int = 512,
target_max_token_len : int = 100):
super().__init__()
self.batch_size = batch_size
self.train_df = train_df
self.test_df = test_df
self.val_df = val_df
self.tokenizer = tokenizer
self.source_max_token_len = source_max_token_len
self.target_max_token_len = target_max_token_len
def setup(self,stage=None):
self.train_dataset = BengaliDataset(self.train_df,self.tokenizer,self.source_max_token_len,self.target_max_token_len)
self.val_dataset = BengaliDataset(self.val_df,self.tokenizer,self.source_max_token_len,self.target_max_token_len)
self.test_dataset = BengaliDataset(self.test_df,self.tokenizer,self.source_max_token_len,self.target_max_token_len)
def train_dataloader(self):
return DataLoader(self.train_dataset,batch_size = self.batch_size,shuffle=True,num_workers=4)
def val_dataloader(self):
return DataLoader(self.val_dataset,batch_size = self.batch_size,num_workers=4)
def test_dataloader(self):
return DataLoader(self.test_dataset,batch_size = self.batch_size,num_workers=4)
class BengaliSummaryModel(pl.LightningModule):
def __init__(self):
super().__init__()
self.model = LModel
def forward(self,input_ids,attention_mask,decoder_attention_mask, labels=None):
output = self.model(
input_ids,
attention_mask = attention_mask,
labels = labels,
decoder_attention_mask = decoder_attention_mask
)
return output.loss, output.logits
def training_step(self, batch, batch_idx):
input_ids = batch["text_input_ids"]
attention_mask = batch["text_attention_mask"]
labels = batch["labels"]
labels_attention_mask = batch["labels_attention_mask"]
loss, outputs = self(
input_ids = input_ids,
attention_mask = attention_mask,
decoder_attention_mask = labels_attention_mask,
labels = labels
)
self.log("train_loss",loss,prog_bar=True,logger=True)
return loss
def validation_step(self, batch, batch_idx):
input_ids = batch["text_input_ids"]
attention_mask = batch["text_attention_mask"]
labels = batch["labels"]
labels_attention_mask = batch["labels_attention_mask"]
loss, outputs = self(
input_ids = input_ids,
attention_mask = attention_mask,
decoder_attention_mask = labels_attention_mask,
labels = labels
)
self.log("val_loss",loss,prog_bar=True,logger=True)
return loss
def test_step(self, batch, batch_idx):
input_ids = batch["text_input_ids"]
attention_mask = batch["text_attention_mask"]
labels = batch["labels"]
labels_attention_mask = batch["labels_attention_mask"]
loss, outputs = self(
input_ids = input_ids,
attention_mask = attention_mask,
decoder_attention_mask = labels_attention_mask,
labels = labels
)
self.log("test_loss",loss,prog_bar=True,logger=True)
return loss
def configure_optimizers(self):
return AdamW(self.parameters(),lr = 0.0001)
BATCH_SIZE = 16
N_EPOCHS = 10
data_module = BengaliDataModule(train_df,val_df,test_df,tokenizer,batch_size = BATCH_SIZE)
data_module.setup()
model = BengaliSummaryModel()
checkpoint_callback = ModelCheckpoint(
dirpath = 'checkpoints',
filename = 'BanglaT5',
save_top_k = 1,
verbose = True,
monitor = 'val_loss',
mode = 'min'
)
trainer = pl.Trainer(devices=1, accelerator="gpu",
callbacks=[checkpoint_callback],
max_epochs = N_EPOCHS,
)
trainer.fit(model,data_module)
trained_model = BengaliSummaryModel.load_from_checkpoint(trainer.checkpoint_callback.best_model_path)
trained_model.freeze()
trainer.test(trained_model, data_module)