Spaces:
Sleeping
Sleeping
Update modules/abstractive.py
Browse files- modules/abstractive.py +7 -6
modules/abstractive.py
CHANGED
@@ -1,24 +1,25 @@
|
|
1 |
import torch
|
2 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
3 |
-
from transformers import PegasusTokenizer, PegasusForConditionalGeneration
|
4 |
|
5 |
def load_summarizers():
|
6 |
models = {
|
|
|
7 |
"T5": "Overglitch/t5-small-cnn-dailymail",
|
8 |
"BART": "facebook/bart-large-cnn",
|
9 |
}
|
10 |
summarizers = {}
|
11 |
for model_name, model_path in models.items():
|
|
|
|
|
|
|
|
|
|
|
12 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_path).to("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
14 |
summarizers[model_name] = (model, tokenizer)
|
15 |
return summarizers
|
16 |
|
17 |
-
def load_pegasus_model_and_tokenizer(model_name: str):
|
18 |
-
model = PegasusForConditionalGeneration.from_pretrained(model_name)
|
19 |
-
tokenizer = PegasusTokenizer.from_pretrained(model_name)
|
20 |
-
return model, tokenizer
|
21 |
-
|
22 |
|
23 |
def abstractive_summary(summarizers, model_name, text, max_length, num_beams):
|
24 |
model, tokenizer = summarizers[model_name]
|
|
|
1 |
import torch
|
2 |
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
3 |
+
from transformers import PegasusTokenizer, PegasusForConditionalGeneration
|
4 |
|
5 |
def load_summarizers():
|
6 |
models = {
|
7 |
+
"Pegasus": "google/pegasus-cnn_dailymail"
|
8 |
"T5": "Overglitch/t5-small-cnn-dailymail",
|
9 |
"BART": "facebook/bart-large-cnn",
|
10 |
}
|
11 |
summarizers = {}
|
12 |
for model_name, model_path in models.items():
|
13 |
+
if model_name == "Pegasus":
|
14 |
+
tokenizer = PegasusTokenizer.from_pretrained(model_path)
|
15 |
+
else:
|
16 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
17 |
+
|
18 |
model = AutoModelForSeq2SeqLM.from_pretrained(model_path).to("cuda" if torch.cuda.is_available() else "cpu")
|
19 |
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
20 |
summarizers[model_name] = (model, tokenizer)
|
21 |
return summarizers
|
22 |
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
def abstractive_summary(summarizers, model_name, text, max_length, num_beams):
|
25 |
model, tokenizer = summarizers[model_name]
|