-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add docstrings. Add summary model(not visible for now considering tha…
…t it works bad).
- Loading branch information
TMN
committed
Nov 25, 2023
1 parent
2a7bf87
commit 8a93a7d
Showing
9 changed files
with
131 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import torch | ||
from transformers import T5ForConditionalGeneration, T5Tokenizer | ||
|
||
MODEL_NAME = "cointegrated/rut5-base-absum" | ||
model = T5ForConditionalGeneration.from_pretrained(MODEL_NAME) | ||
tokenizer = T5Tokenizer.from_pretrained(MODEL_NAME) | ||
model.eval() | ||
|
||
|
||
def summarize( | ||
text, | ||
n_words=None, | ||
compression=None, | ||
max_length=1000, | ||
num_beams=3, | ||
do_sample=False, | ||
repetition_penalty=10.0, | ||
**kwargs | ||
): | ||
""" | ||
Summarize the text | ||
The following parameters are mutually exclusive: | ||
- n_words (int) is an approximate number of words to generate. | ||
- compression (float) is an approximate length ratio of summary and original text. | ||
""" | ||
try: | ||
if n_words: | ||
text = "[{}] ".format(n_words) + text | ||
elif compression: | ||
text = "[{0:.1g}] ".format(compression) + text | ||
x = tokenizer(text, return_tensors="pt", padding=True).to(model.device) | ||
with torch.inference_mode(): | ||
out = model.generate( | ||
**x, | ||
max_length=max_length, | ||
num_beams=num_beams, | ||
do_sample=do_sample, | ||
repetition_penalty=repetition_penalty, | ||
**kwargs | ||
) | ||
except Exception: | ||
print("###_Exception in summary prediction_###") | ||
return text | ||
else: | ||
return tokenizer.decode(out[0], skip_special_tokens=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters