Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,43 +1,33 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
model
|
| 9 |
-
|
| 10 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 11 |
-
model = model.to(device)
|
| 12 |
|
| 13 |
def summarize_text(text):
|
| 14 |
if not text.strip():
|
| 15 |
-
return "Please enter some text."
|
| 16 |
-
|
| 17 |
-
|
| 18 |
text,
|
| 19 |
-
return_tensors="pt",
|
| 20 |
-
truncation=True,
|
| 21 |
-
max_length=1024
|
| 22 |
-
).to(device)
|
| 23 |
-
|
| 24 |
-
summary_ids = model.generate(
|
| 25 |
-
inputs["input_ids"],
|
| 26 |
max_length=150,
|
| 27 |
-
min_length=
|
| 28 |
-
|
| 29 |
-
early_stopping=True
|
| 30 |
)
|
| 31 |
-
|
| 32 |
-
return
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
|
|
|
| 3 |
|
| 4 |
+
# Load summarization pipeline
|
| 5 |
+
summarizer = pipeline(
|
| 6 |
+
"summarization",
|
| 7 |
+
model="facebook/bart-large-cnn"
|
| 8 |
+
)
|
|
|
|
|
|
|
| 9 |
|
| 10 |
def summarize_text(text):
|
| 11 |
if not text.strip():
|
| 12 |
+
return "Please enter some text to summarize."
|
| 13 |
+
|
| 14 |
+
summary = summarizer(
|
| 15 |
text,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
max_length=150,
|
| 17 |
+
min_length=40,
|
| 18 |
+
do_sample=False
|
|
|
|
| 19 |
)
|
| 20 |
+
|
| 21 |
+
return summary[0]['summary_text']
|
| 22 |
+
|
| 23 |
+
# Gradio Interface
|
| 24 |
+
demo = gr.Interface(
|
| 25 |
+
fn=summarize_text,
|
| 26 |
+
inputs=gr.Textbox(lines=15, placeholder="Paste your text here..."),
|
| 27 |
+
outputs="text",
|
| 28 |
+
title="Advanced BART Text Summarizer",
|
| 29 |
+
description="Summarize long text using facebook/bart-large-cnn"
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
if __name__ == "__main__":
|
| 33 |
+
demo.launch()
|