Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 4 |
+
|
| 5 |
+
model_name = "facebook/bart-large-cnn"
|
| 6 |
+
|
| 7 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
|
| 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 |
+
inputs = tokenizer(
|
| 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=50,
|
| 28 |
+
num_beams=4,
|
| 29 |
+
early_stopping=True
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
+
return tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 33 |
+
|
| 34 |
+
with gr.Blocks() as demo:
|
| 35 |
+
gr.Markdown("# 📄 BART Summarizer")
|
| 36 |
+
|
| 37 |
+
text_input = gr.Textbox(lines=10, label="Enter Text")
|
| 38 |
+
btn = gr.Button("Generate Summary")
|
| 39 |
+
output = gr.Textbox(label="Summary", lines=6)
|
| 40 |
+
|
| 41 |
+
btn.click(summarize_text, inputs=text_input, outputs=output)
|
| 42 |
+
|
| 43 |
+
demo.launch()
|