Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add T5 to supported summarization models #115

Merged
merged 1 commit into from
Nov 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions summertime/model/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
MBartModel,
LexRankModel,
LongformerModel,
T5Model,
PegasusModel,
TextRankModel,
)
Expand All @@ -16,6 +17,7 @@
MBartModel,
LexRankModel,
LongformerModel,
T5Model,
PegasusModel,
TextRankModel,
MultiDocJointModel,
Expand Down
2 changes: 1 addition & 1 deletion summertime/model/defaults.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ def __init__(self, device="cpu"):
super(summarizer, self).__init__(device)

def show_capability(self):
print("Pegasus is the default singe-document summarization model.")
print("Pegasus is the default single-document summarization model.")
super(summarizer, self).show_capability()
1 change: 1 addition & 0 deletions summertime/model/single_doc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
from .lexrank_model import LexRankModel
from .longformer_model import LongformerModel
from .textrank_model import TextRankModel
from .t5_model import T5Model

from .multilingual import MBartModel
47 changes: 47 additions & 0 deletions summertime/model/single_doc/t5_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from transformers import T5Tokenizer, T5ForConditionalGeneration
from .base_single_doc_model import SingleDocSummModel


class T5Model(SingleDocSummModel):

# static variables
model_name = "T5"
is_extractive = False
is_neural = True

def __init__(self, device="cpu"):
super(T5Model, self).__init__(
trained_domain="Web Crawl", max_input_length=1024, max_output_length=None
)

self.device = device
model_name = "t5-large"
self.tokenizer = T5Tokenizer.from_pretrained(model_name)
self.model = T5ForConditionalGeneration.from_pretrained(model_name).to(device)

def summarize(self, corpus, queries=None):
self.assert_summ_input_type(corpus, queries)

batch = self.tokenizer(
corpus, truncation=True, padding="longest", return_tensors="pt"
).to(self.device)
encoded_summaries = self.model.generate(**batch)
summaries = self.tokenizer.batch_decode(
encoded_summaries, skip_special_tokens=True
)

return summaries

@classmethod
def show_capability(cls) -> None:
basic_description = cls.generate_basic_description()
more_details = (
"Introduced in 2020, T5 is a large pretrained language model trained on web crawl using "
"transfer learning approaches and teacher forcing.\n "
"Strengths: \n - High accuracy \n "
"Weaknesses: \n - High memory usage \n "
"Initialization arguments: \n "
"- `device = 'cpu'` specifies the device the model is stored on and uses for computation. "
"Use `device='gpu'` to run on an Nvidia GPU."
)
print(f"{basic_description} \n {'#'*20} \n {more_details}")