Spaces:
Runtime error
Runtime error
Create abstractive.py
Browse files- abstractive.py +16 -0
abstractive.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# abstractive.py
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 3 |
+
|
| 4 |
+
model_name = "arousrihab/my-t5base-model"
|
| 5 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 6 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 7 |
+
|
| 8 |
+
def abstractive_summary(text, max_length_ratio=0.2, min_length_ratio=0.1):
|
| 9 |
+
total_length = len(text.split())
|
| 10 |
+
max_length = int(total_length * max_length_ratio)
|
| 11 |
+
min_length = int(total_length * min_length_ratio)
|
| 12 |
+
|
| 13 |
+
inputs = tokenizer.encode("summarize: " + text, return_tensors="pt", max_length=512, truncation=True)
|
| 14 |
+
summary_ids = model.generate(inputs, max_length=max_length, min_length=min_length, length_penalty=2.0, num_beams=4, early_stopping=True)
|
| 15 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 16 |
+
return summary
|