Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,17 +12,24 @@ 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 |
if not user_input:
|
| 16 |
-
history.append(
|
| 17 |
return history, history
|
| 18 |
|
| 19 |
if len(user_input.split()) < 30:
|
| 20 |
-
history.append(
|
|
|
|
| 21 |
return history, history
|
| 22 |
|
| 23 |
try:
|
| 24 |
-
inputs = tokenizer(
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
ids = model.generate(
|
| 27 |
inputs["input_ids"],
|
| 28 |
max_new_tokens=int(max_len),
|
|
@@ -32,10 +39,11 @@ def summarize_text(user_input, history, max_len, min_len, bullet_mode):
|
|
| 32 |
early_stopping=True,
|
| 33 |
no_repeat_ngram_size=3
|
| 34 |
)
|
|
|
|
| 35 |
summary = tokenizer.decode(ids[0], skip_special_tokens=True)
|
| 36 |
|
| 37 |
if bullet_mode:
|
| 38 |
-
sentences = summary.replace("?",".").replace("!",".").split(". ")
|
| 39 |
body = "\n".join(f"β’ {s.strip().capitalize()}" for s in sentences if s.strip())
|
| 40 |
out = f"π **Summary (Bullets)**:\n\n{body}"
|
| 41 |
else:
|
|
@@ -43,36 +51,57 @@ def summarize_text(user_input, history, max_len, min_len, bullet_mode):
|
|
| 43 |
|
| 44 |
orig = len(user_input.split())
|
| 45 |
summ = len(summary.split())
|
| 46 |
-
out += f"\n\n---\nπ *{orig} words β {summ} words | Reduced by {round((1-summ/orig)*100,1)}%*"
|
| 47 |
-
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
except Exception as e:
|
| 50 |
-
history.append(
|
|
|
|
| 51 |
|
| 52 |
return history, history
|
| 53 |
|
|
|
|
| 54 |
def clear_chat():
|
| 55 |
return [], []
|
| 56 |
|
|
|
|
| 57 |
with gr.Blocks(title="Text Summarizer", theme=gr.themes.Soft()) as demo:
|
|
|
|
| 58 |
gr.Markdown("# π Text Summarization Chatbox\n### Powered by `facebook/bart-large-cnn`")
|
|
|
|
| 59 |
with gr.Row():
|
| 60 |
with gr.Column(scale=7):
|
| 61 |
-
chatbot = gr.Chatbot(height=450,
|
| 62 |
state = gr.State([])
|
|
|
|
| 63 |
with gr.Row():
|
| 64 |
-
txt = gr.Textbox(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
with gr.Column(scale=2):
|
| 66 |
btn = gr.Button("β¨ Summarize", variant="primary")
|
| 67 |
-
clear = gr.Button("ποΈ Clear",
|
|
|
|
| 68 |
with gr.Column(scale=3):
|
| 69 |
gr.Markdown("### βοΈ Settings")
|
| 70 |
-
max_l = gr.Slider(50,
|
| 71 |
-
min_l = gr.Slider(10,
|
| 72 |
bullet = gr.Checkbox(label="π΅ Bullet Point Mode", value=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
btn.click(summarize_text,
|
| 75 |
-
txt.submit(summarize_text,
|
| 76 |
clear.click(clear_chat, outputs=[chatbot, state])
|
| 77 |
|
| 78 |
demo.launch()
|
|
|
|
| 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": "π Please paste longer text (30+ words) to summarize."})
|
| 23 |
return history, history
|
| 24 |
|
| 25 |
try:
|
| 26 |
+
inputs = tokenizer(
|
| 27 |
+
user_input,
|
| 28 |
+
return_tensors="pt",
|
| 29 |
+
max_length=1024,
|
| 30 |
+
truncation=True
|
| 31 |
+
).to(device)
|
| 32 |
+
|
| 33 |
ids = model.generate(
|
| 34 |
inputs["input_ids"],
|
| 35 |
max_new_tokens=int(max_len),
|
|
|
|
| 39 |
early_stopping=True,
|
| 40 |
no_repeat_ngram_size=3
|
| 41 |
)
|
| 42 |
+
|
| 43 |
summary = tokenizer.decode(ids[0], skip_special_tokens=True)
|
| 44 |
|
| 45 |
if bullet_mode:
|
| 46 |
+
sentences = summary.replace("?", ".").replace("!", ".").split(". ")
|
| 47 |
body = "\n".join(f"β’ {s.strip().capitalize()}" for s in sentences if s.strip())
|
| 48 |
out = f"π **Summary (Bullets)**:\n\n{body}"
|
| 49 |
else:
|
|
|
|
| 51 |
|
| 52 |
orig = len(user_input.split())
|
| 53 |
summ = len(summary.split())
|
| 54 |
+
out += f"\n\n---\nπ *{orig} words β {summ} words | Reduced by {round((1 - summ/orig)*100, 1)}%*"
|
| 55 |
+
|
| 56 |
+
label = user_input[:80] + "..." if len(user_input) > 80 else user_input
|
| 57 |
+
history.append({"role": "user", "content": label})
|
| 58 |
+
history.append({"role": "assistant", "content": out})
|
| 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 |
|
| 66 |
+
|
| 67 |
def clear_chat():
|
| 68 |
return [], []
|
| 69 |
|
| 70 |
+
|
| 71 |
with gr.Blocks(title="Text Summarizer", theme=gr.themes.Soft()) as demo:
|
| 72 |
+
|
| 73 |
gr.Markdown("# π Text Summarization Chatbox\n### Powered by `facebook/bart-large-cnn`")
|
| 74 |
+
|
| 75 |
with gr.Row():
|
| 76 |
with gr.Column(scale=7):
|
| 77 |
+
chatbot = gr.Chatbot(height=450, type="messages") # β
Gradio 6.x format
|
| 78 |
state = gr.State([])
|
| 79 |
+
|
| 80 |
with gr.Row():
|
| 81 |
+
txt = gr.Textbox(
|
| 82 |
+
placeholder="Paste text here...",
|
| 83 |
+
lines=3,
|
| 84 |
+
show_label=False,
|
| 85 |
+
scale=8
|
| 86 |
+
)
|
| 87 |
with gr.Column(scale=2):
|
| 88 |
btn = gr.Button("β¨ Summarize", variant="primary")
|
| 89 |
+
clear = gr.Button("ποΈ Clear", variant="secondary")
|
| 90 |
+
|
| 91 |
with gr.Column(scale=3):
|
| 92 |
gr.Markdown("### βοΈ Settings")
|
| 93 |
+
max_l = gr.Slider(50, 500, value=150, step=10, label="Max Length")
|
| 94 |
+
min_l = gr.Slider(10, 200, value=40, step=5, label="Min Length")
|
| 95 |
bullet = gr.Checkbox(label="π΅ Bullet Point Mode", value=False)
|
| 96 |
+
gr.Markdown("""
|
| 97 |
+
---
|
| 98 |
+
**π‘ Tips:**
|
| 99 |
+
- Works best with 100β1000 word inputs
|
| 100 |
+
- Toggle Bullet Mode for point-wise output
|
| 101 |
+
""")
|
| 102 |
|
| 103 |
+
btn.click(summarize_text, inputs=[txt, state, max_l, min_l, bullet], outputs=[chatbot, state])
|
| 104 |
+
txt.submit(summarize_text, inputs=[txt, state, max_l, min_l, bullet], outputs=[chatbot, state])
|
| 105 |
clear.click(clear_chat, outputs=[chatbot, state])
|
| 106 |
|
| 107 |
demo.launch()
|