Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,22 +1,34 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
|
|
|
| 7 |
|
| 8 |
def summarize_text(text):
|
| 9 |
-
|
| 10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 12 |
return summary
|
| 13 |
|
|
|
|
| 14 |
iface = gr.Interface(
|
| 15 |
fn=summarize_text,
|
| 16 |
inputs="text",
|
| 17 |
outputs="text",
|
| 18 |
-
title="
|
| 19 |
-
description="Enter text to get a summary using
|
| 20 |
)
|
| 21 |
|
| 22 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import LEDTokenizer, LEDForConditionalGeneration
|
| 3 |
|
| 4 |
+
# Use Longformer Encoder-Decoder (LED) model
|
| 5 |
+
model_name = "allenai/led-large-16384"
|
| 6 |
+
tokenizer = LEDTokenizer.from_pretrained(model_name)
|
| 7 |
+
model = LEDForConditionalGeneration.from_pretrained(model_name)
|
| 8 |
|
| 9 |
def summarize_text(text):
|
| 10 |
+
# Tokenize input with truncation to fit within 16,384 tokens
|
| 11 |
+
inputs = tokenizer([text], max_length=16384, return_tensors="pt", truncation=True)
|
| 12 |
+
|
| 13 |
+
# Generate summary with adjusted parameters
|
| 14 |
+
summary_ids = model.generate(
|
| 15 |
+
inputs["input_ids"],
|
| 16 |
+
num_beams=4,
|
| 17 |
+
max_length=512, # Can be adjusted based on summary size needs
|
| 18 |
+
min_length=100,
|
| 19 |
+
early_stopping=True
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 23 |
return summary
|
| 24 |
|
| 25 |
+
# Gradio Interface
|
| 26 |
iface = gr.Interface(
|
| 27 |
fn=summarize_text,
|
| 28 |
inputs="text",
|
| 29 |
outputs="text",
|
| 30 |
+
title="Longformer Summarizer",
|
| 31 |
+
description="Enter text to get a summary using the Longformer Encoder-Decoder."
|
| 32 |
)
|
| 33 |
|
| 34 |
if __name__ == "__main__":
|