-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText Summarization Tool_using_LLM.py
More file actions
70 lines (50 loc) · 1.95 KB
/
Text Summarization Tool_using_LLM.py
File metadata and controls
70 lines (50 loc) · 1.95 KB
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
# -*- coding: utf-8 -*-
"""
Created on Sun July 13 00:11:52 2024
@author: Raj
"""
from datasets import load_dataset
dataset = load_dataset('cnn_dailymail', '3.0.0')
train_data = dataset['train']
val_data = dataset['validation']
test_data = dataset['test']
from transformers import T5Tokenizer
tokenizer = T5Tokenizer.from_pretrained('t5-small')
def preprocess_data(data):
inputs = [doc['article'] for doc in data]
targets = [doc['highlights'] for doc in data]
model_inputs = tokenizer(inputs, max_length=512, truncation=True, padding='max_length', return_tensors='pt')
labels = tokenizer(targets, max_length=150, truncation=True, padding='max_length', return_tensors='pt')
model_inputs['labels'] = labels['input_ids']
return model_inputs
train_dataset = preprocess_data(train_data)
val_dataset = preprocess_data(val_data)
test_dataset = preprocess_data(test_data)
from transformers import T5ForConditionalGeneration, Trainer, TrainingArguments
model = T5ForConditionalGeneration.from_pretrained('t5-small')
training_args = TrainingArguments(
output_dir='./results',
evaluation_strategy='epoch',
learning_rate=5e-5,
per_device_train_batch_size=4,
per_device_eval_batch_size=4,
num_train_epochs=3,
weight_decay=0.01,
logging_dir='./logs',
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
)
trainer.train()
from datasets import load_metric
metric = load_metric('rouge')
def compute_metrics(eval_pred):
predictions, labels = eval_pred
decoded_preds = tokenizer.batch_decode(predictions, skip_special_tokens=True)
decoded_labels = tokenizer.batch_decode(labels, skip_special_tokens=True)
result = metric.compute(predictions=decoded_preds, references=decoded_labels, use_stemmer=True)
return result
trainer.evaluate(test_dataset, metric_key_prefix='test')