Views
No views yet
| Metric | # Value |
|---|---|
| ROUGE-2 | 17.37 |
1from transformers import BertTokenizerFast, EncoderDecoderModel
2import torch
3device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
4tokenizer = BertTokenizerFast.from_pretrained('mrm8488/bert-small2bert-small-finetuned-cnn_daily_mail-summarization')
5model = EncoderDecoderModel.from_pretrained('mrm8488/bert-small2bert-small-finetuned-cnn_daily_mail-summarization').to(device)
6
7def generate_summary(text):
8 # cut off at BERT max length 512
9 inputs = tokenizer([text], padding="max_length", truncation=True, max_length=512, return_tensors="pt")
10 input_ids = inputs.input_ids.to(device)
11 attention_mask = inputs.attention_mask.to(device)
12
13 output = model.generate(input_ids, attention_mask=attention_mask)
14
15 return tokenizer.decode(output[0], skip_special_tokens=True)
16
17text = "your text to be summarized here..."
18generate_summary(text)Created by Manuel Romero/@mrm8488 | LinkedIn
Made with ♥ in Spain