Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,38 @@ from transformers import BartForConditionalGeneration, BartTokenizer
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
|
|
|
|
| 5 |
print("Loading model...")
|
| 6 |
-
|
|
|
|
| 7 |
tokenizer = BartTokenizer.from_pretrained(MODEL_NAME)
|
| 8 |
model = BartForConditionalGeneration.from_pretrained(MODEL_NAME)
|
| 9 |
-
device = "
|
| 10 |
model = model.to(device)
|
| 11 |
-
print(f"Ready β
on {device}")
|
| 12 |
|
|
|
|
|
|
|
|
|
|
| 13 |
def summarize_text(user_input, history, max_len, min_len, bullet_mode):
|
| 14 |
user_input = user_input.strip()
|
| 15 |
|
|
|
|
| 16 |
if not user_input:
|
| 17 |
-
history.append({"role": "assistant", "content": "β οΈ Please enter some text."})
|
| 18 |
return history, history
|
| 19 |
|
|
|
|
| 20 |
if len(user_input.split()) < 30:
|
| 21 |
history.append({"role": "user", "content": user_input})
|
| 22 |
-
history.append({"role": "assistant", "content":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
return history, history
|
| 24 |
|
|
|
|
| 25 |
try:
|
| 26 |
inputs = tokenizer(
|
| 27 |
user_input,
|
|
@@ -30,7 +42,7 @@ def summarize_text(user_input, history, max_len, min_len, bullet_mode):
|
|
| 30 |
truncation=True
|
| 31 |
).to(device)
|
| 32 |
|
| 33 |
-
|
| 34 |
inputs["input_ids"],
|
| 35 |
max_new_tokens=int(max_len),
|
| 36 |
min_new_tokens=int(min_len),
|
|
@@ -40,18 +52,29 @@ def summarize_text(user_input, history, max_len, min_len, bullet_mode):
|
|
| 40 |
no_repeat_ngram_size=3
|
| 41 |
)
|
| 42 |
|
| 43 |
-
summary = tokenizer.decode(
|
| 44 |
|
|
|
|
| 45 |
if bullet_mode:
|
| 46 |
sentences = summary.replace("?", ".").replace("!", ".").split(". ")
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
else:
|
| 50 |
out = f"π **Summary**:\n\n{summary}"
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
label = user_input[:80] + "..." if len(user_input) > 80 else user_input
|
| 57 |
history.append({"role": "user", "content": label})
|
|
@@ -59,7 +82,7 @@ def summarize_text(user_input, history, max_len, min_len, bullet_mode):
|
|
| 59 |
|
| 60 |
except Exception as e:
|
| 61 |
history.append({"role": "user", "content": user_input[:60] + "..."})
|
| 62 |
-
history.append({"role": "assistant", "content": f"β Error: {e}"})
|
| 63 |
|
| 64 |
return history, history
|
| 65 |
|
|
@@ -68,40 +91,81 @@ def clear_chat():
|
|
| 68 |
return [], []
|
| 69 |
|
| 70 |
|
|
|
|
| 71 |
with gr.Blocks(title="Text Summarizer", theme=gr.themes.Soft()) as demo:
|
| 72 |
|
| 73 |
-
gr.Markdown("
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
with gr.Row():
|
|
|
|
|
|
|
| 76 |
with gr.Column(scale=7):
|
| 77 |
-
chatbot = gr.Chatbot(
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
with gr.Row():
|
| 81 |
-
|
| 82 |
-
placeholder="Paste text here...",
|
| 83 |
-
lines=3,
|
| 84 |
show_label=False,
|
|
|
|
| 85 |
scale=8
|
| 86 |
)
|
| 87 |
with gr.Column(scale=2):
|
| 88 |
-
|
| 89 |
-
|
| 90 |
|
|
|
|
| 91 |
with gr.Column(scale=3):
|
| 92 |
gr.Markdown("### βοΈ Settings")
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
gr.Markdown("""
|
| 97 |
---
|
| 98 |
**π‘ Tips:**
|
| 99 |
-
- Works best with 100β1000 word inputs
|
| 100 |
-
-
|
| 101 |
-
|
|
|
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
|
| 5 |
+
# ββ 1. Load Model ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 6 |
print("Loading model...")
|
| 7 |
+
|
| 8 |
+
MODEL_NAME = "sshleifer/distilbart-cnn-12-6" # lightweight, works on free tier
|
| 9 |
tokenizer = BartTokenizer.from_pretrained(MODEL_NAME)
|
| 10 |
model = BartForConditionalGeneration.from_pretrained(MODEL_NAME)
|
| 11 |
+
device = "cpu" # free HF Spaces has no GPU
|
| 12 |
model = model.to(device)
|
|
|
|
| 13 |
|
| 14 |
+
print("Model ready β
")
|
| 15 |
+
|
| 16 |
+
# ββ 2. Summarization Function ββββββββββββββββββββββββββββββββββββββ
|
| 17 |
def summarize_text(user_input, history, max_len, min_len, bullet_mode):
|
| 18 |
user_input = user_input.strip()
|
| 19 |
|
| 20 |
+
# Empty input
|
| 21 |
if not user_input:
|
| 22 |
+
history.append({"role": "assistant", "content": "β οΈ Please enter some text to summarize."})
|
| 23 |
return history, history
|
| 24 |
|
| 25 |
+
# Short input β treat as greeting
|
| 26 |
if len(user_input.split()) < 30:
|
| 27 |
history.append({"role": "user", "content": user_input})
|
| 28 |
+
history.append({"role": "assistant", "content": (
|
| 29 |
+
"π Hello! I'm your **Text Summarizer**.\n\n"
|
| 30 |
+
"Paste any long article, paragraph, or document (30+ words) "
|
| 31 |
+
"and I'll summarize it instantly.\n\n"
|
| 32 |
+
"Use the βοΈ settings on the right to adjust length and format."
|
| 33 |
+
)})
|
| 34 |
return history, history
|
| 35 |
|
| 36 |
+
# Summarize
|
| 37 |
try:
|
| 38 |
inputs = tokenizer(
|
| 39 |
user_input,
|
|
|
|
| 42 |
truncation=True
|
| 43 |
).to(device)
|
| 44 |
|
| 45 |
+
summary_ids = model.generate(
|
| 46 |
inputs["input_ids"],
|
| 47 |
max_new_tokens=int(max_len),
|
| 48 |
min_new_tokens=int(min_len),
|
|
|
|
| 52 |
no_repeat_ngram_size=3
|
| 53 |
)
|
| 54 |
|
| 55 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 56 |
|
| 57 |
+
# Format output
|
| 58 |
if bullet_mode:
|
| 59 |
sentences = summary.replace("?", ".").replace("!", ".").split(". ")
|
| 60 |
+
bullets = "\n".join(
|
| 61 |
+
f"β’ {s.strip().capitalize()}"
|
| 62 |
+
for s in sentences if s.strip()
|
| 63 |
+
)
|
| 64 |
+
out = f"π **Summary (Bullet Points)**:\n\n{bullets}"
|
| 65 |
else:
|
| 66 |
out = f"π **Summary**:\n\n{summary}"
|
| 67 |
|
| 68 |
+
# Word count stats
|
| 69 |
+
orig_words = len(user_input.split())
|
| 70 |
+
summ_words = len(summary.split())
|
| 71 |
+
reduction = round((1 - summ_words / orig_words) * 100, 1)
|
| 72 |
+
out += (
|
| 73 |
+
f"\n\n---\n"
|
| 74 |
+
f"π *Original: {orig_words} words β "
|
| 75 |
+
f"Summary: {summ_words} words | "
|
| 76 |
+
f"Reduced by {reduction}%*"
|
| 77 |
+
)
|
| 78 |
|
| 79 |
label = user_input[:80] + "..." if len(user_input) > 80 else user_input
|
| 80 |
history.append({"role": "user", "content": label})
|
|
|
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
history.append({"role": "user", "content": user_input[:60] + "..."})
|
| 85 |
+
history.append({"role": "assistant", "content": f"β Error: {str(e)}"})
|
| 86 |
|
| 87 |
return history, history
|
| 88 |
|
|
|
|
| 91 |
return [], []
|
| 92 |
|
| 93 |
|
| 94 |
+
# ββ 3. Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 95 |
with gr.Blocks(title="Text Summarizer", theme=gr.themes.Soft()) as demo:
|
| 96 |
|
| 97 |
+
gr.Markdown("""
|
| 98 |
+
# π Text Summarization Chatbox
|
| 99 |
+
### Powered by `sshleifer/distilbart-cnn-12-6`
|
| 100 |
+
Paste any long text and get an instant summary!
|
| 101 |
+
""")
|
| 102 |
|
| 103 |
with gr.Row():
|
| 104 |
+
|
| 105 |
+
# Left β Chat
|
| 106 |
with gr.Column(scale=7):
|
| 107 |
+
chatbot = gr.Chatbot(
|
| 108 |
+
height=450,
|
| 109 |
+
bubble_full_width=False,
|
| 110 |
+
type="messages" # β
required for Gradio 6.x
|
| 111 |
+
)
|
| 112 |
+
state = gr.State([])
|
| 113 |
|
| 114 |
with gr.Row():
|
| 115 |
+
txt_input = gr.Textbox(
|
| 116 |
+
placeholder="Paste your article, report, or any long text here...",
|
|
|
|
| 117 |
show_label=False,
|
| 118 |
+
lines=3,
|
| 119 |
scale=8
|
| 120 |
)
|
| 121 |
with gr.Column(scale=2):
|
| 122 |
+
submit_btn = gr.Button("β¨ Summarize", variant="primary")
|
| 123 |
+
clear_btn = gr.Button("ποΈ Clear", variant="secondary")
|
| 124 |
|
| 125 |
+
# Right β Settings
|
| 126 |
with gr.Column(scale=3):
|
| 127 |
gr.Markdown("### βοΈ Settings")
|
| 128 |
+
|
| 129 |
+
max_length = gr.Slider(
|
| 130 |
+
minimum=50, maximum=300,
|
| 131 |
+
value=130, step=10,
|
| 132 |
+
label="Max Summary Length (tokens)"
|
| 133 |
+
)
|
| 134 |
+
min_length = gr.Slider(
|
| 135 |
+
minimum=10, maximum=100,
|
| 136 |
+
value=30, step=5,
|
| 137 |
+
label="Min Summary Length (tokens)"
|
| 138 |
+
)
|
| 139 |
+
bullet_mode = gr.Checkbox(
|
| 140 |
+
label="π΅ Bullet Point Mode",
|
| 141 |
+
value=False
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
gr.Markdown("""
|
| 145 |
---
|
| 146 |
**π‘ Tips:**
|
| 147 |
+
- Works best with **100β1000 word** inputs
|
| 148 |
+
- Articles, news, reports, essays
|
| 149 |
+
- Toggle **Bullet Mode** for point-wise output
|
| 150 |
+
- Adjust sliders to control summary length
|
| 151 |
|
| 152 |
+
---
|
| 153 |
+
**π Model:** `distilbart-cnn-12-6`
|
| 154 |
+
**π₯οΈ Device:** CPU
|
| 155 |
+
""")
|
| 156 |
|
| 157 |
+
# Events
|
| 158 |
+
submit_btn.click(
|
| 159 |
+
summarize_text,
|
| 160 |
+
inputs=[txt_input, state, max_length, min_length, bullet_mode],
|
| 161 |
+
outputs=[chatbot, state]
|
| 162 |
+
)
|
| 163 |
+
txt_input.submit(
|
| 164 |
+
summarize_text,
|
| 165 |
+
inputs=[txt_input, state, max_length, min_length, bullet_mode],
|
| 166 |
+
outputs=[chatbot, state]
|
| 167 |
+
)
|
| 168 |
+
clear_btn.click(clear_chat, outputs=[chatbot, state])
|
| 169 |
+
|
| 170 |
+
# ββ 4. Launch ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 171 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|