Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# =========================================
|
| 2 |
+
# Advanced BART-based Summarizer (Gradio)
|
| 3 |
+
# =========================================
|
| 4 |
+
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import torch
|
| 7 |
+
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 8 |
+
|
| 9 |
+
# -----------------------------
|
| 10 |
+
# 1. Load Model & Tokenizer
|
| 11 |
+
# -----------------------------
|
| 12 |
+
MODEL_NAME = "facebook/bart-large-cnn"
|
| 13 |
+
|
| 14 |
+
tokenizer = BartTokenizer.from_pretrained(MODEL_NAME)
|
| 15 |
+
model = BartForConditionalGeneration.from_pretrained(MODEL_NAME)
|
| 16 |
+
|
| 17 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 18 |
+
model = model.to(device)
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
# -----------------------------
|
| 22 |
+
# 2. Summarization Function
|
| 23 |
+
# -----------------------------
|
| 24 |
+
def summarize_text(text, max_len, min_len, num_beams):
|
| 25 |
+
if not text.strip():
|
| 26 |
+
return "Please enter some text."
|
| 27 |
+
|
| 28 |
+
inputs = tokenizer(
|
| 29 |
+
text,
|
| 30 |
+
max_length=1024,
|
| 31 |
+
return_tensors="pt",
|
| 32 |
+
truncation=True
|
| 33 |
+
).to(device)
|
| 34 |
+
|
| 35 |
+
summary_ids = model.generate(
|
| 36 |
+
inputs["input_ids"],
|
| 37 |
+
max_length=max_len,
|
| 38 |
+
min_length=min_len,
|
| 39 |
+
num_beams=num_beams,
|
| 40 |
+
length_penalty=2.0,
|
| 41 |
+
early_stopping=True,
|
| 42 |
+
no_repeat_ngram_size=3
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 46 |
+
return summary
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
# -----------------------------
|
| 50 |
+
# 3. Gradio UI
|
| 51 |
+
# -----------------------------
|
| 52 |
+
with gr.Blocks(title="Advanced BART Summarizer") as app:
|
| 53 |
+
|
| 54 |
+
gr.Markdown("## 🧠 Advanced BART Text Summarizer")
|
| 55 |
+
gr.Markdown("Summarize long documents using Facebook BART model")
|
| 56 |
+
|
| 57 |
+
with gr.Row():
|
| 58 |
+
input_text = gr.Textbox(
|
| 59 |
+
lines=15,
|
| 60 |
+
placeholder="Enter your text here...",
|
| 61 |
+
label="Input Text"
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
with gr.Row():
|
| 65 |
+
max_len = gr.Slider(50, 300, value=130, step=10, label="Max Length")
|
| 66 |
+
min_len = gr.Slider(10, 100, value=30, step=5, label="Min Length")
|
| 67 |
+
num_beams = gr.Slider(1, 8, value=4, step=1, label="Beam Size")
|
| 68 |
+
|
| 69 |
+
summarize_btn = gr.Button("Summarize")
|
| 70 |
+
|
| 71 |
+
output_text = gr.Textbox(
|
| 72 |
+
lines=10,
|
| 73 |
+
label="Summary"
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
summarize_btn.click(
|
| 77 |
+
summarize_text,
|
| 78 |
+
inputs=[input_text, max_len, min_len, num_beams],
|
| 79 |
+
outputs=output_text
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
# -----------------------------
|
| 83 |
+
# 4. Launch
|
| 84 |
+
# -----------------------------
|
| 85 |
+
app.launch()
|