Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -14,25 +14,19 @@ model = model.to(device)
|
|
| 14 |
print("Model ready β
")
|
| 15 |
|
| 16 |
# ββ 2. Summarization Function ββββββββββββββββββββββββββββββββββββββ
|
| 17 |
-
def summarize_text(user_input,
|
| 18 |
user_input = user_input.strip()
|
| 19 |
|
| 20 |
if not user_input:
|
| 21 |
-
|
| 22 |
-
{"role": "assistant", "content": "β οΈ Please enter some text to summarize."}
|
| 23 |
-
]
|
| 24 |
-
return history, history
|
| 25 |
|
| 26 |
if len(user_input.split()) < 30:
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
)}
|
| 34 |
-
]
|
| 35 |
-
return history, history
|
| 36 |
|
| 37 |
try:
|
| 38 |
inputs = tokenizer(
|
|
@@ -56,41 +50,50 @@ def summarize_text(user_input, history, max_len, min_len, bullet_mode):
|
|
| 56 |
|
| 57 |
if bullet_mode:
|
| 58 |
sentences = summary.replace("?", ".").replace("!", ".").split(". ")
|
| 59 |
-
bullets = "
|
| 60 |
-
f"
|
| 61 |
for s in sentences if s.strip()
|
| 62 |
)
|
| 63 |
-
out = f"π Summary (Bullet Points):
|
| 64 |
else:
|
| 65 |
-
out = f"π Summary:
|
| 66 |
|
| 67 |
orig_words = len(user_input.split())
|
| 68 |
summ_words = len(summary.split())
|
| 69 |
reduction = round((1 - summ_words / orig_words) * 100, 1)
|
| 70 |
out += (
|
| 71 |
-
f"
|
| 72 |
-
f"
|
| 73 |
f"Summary: {summ_words} words | "
|
| 74 |
-
f"Reduced by {reduction}%"
|
| 75 |
)
|
| 76 |
|
| 77 |
-
label = user_input[:
|
| 78 |
-
|
| 79 |
-
{"role": "user", "content": label},
|
| 80 |
-
{"role": "assistant", "content": out}
|
| 81 |
-
]
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
|
| 89 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 90 |
|
| 91 |
|
| 92 |
def clear_chat():
|
| 93 |
-
return
|
| 94 |
|
| 95 |
|
| 96 |
# ββ 3. Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
@@ -104,26 +107,29 @@ with gr.Blocks(title="Text Summarizer") as demo:
|
|
| 104 |
|
| 105 |
with gr.Row():
|
| 106 |
|
|
|
|
| 107 |
with gr.Column(scale=7):
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
| 111 |
)
|
| 112 |
-
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
with gr.Row():
|
| 115 |
-
|
| 116 |
-
|
| 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 |
with gr.Column(scale=3):
|
| 126 |
-
gr.Markdown("### Settings")
|
| 127 |
max_length = gr.Slider(
|
| 128 |
minimum=50, maximum=300,
|
| 129 |
value=130, step=10,
|
|
@@ -135,31 +141,31 @@ with gr.Blocks(title="Text Summarizer") as demo:
|
|
| 135 |
label="Min Summary Length (tokens)"
|
| 136 |
)
|
| 137 |
bullet_mode = gr.Checkbox(
|
| 138 |
-
label="Bullet Point Mode",
|
| 139 |
value=False
|
| 140 |
)
|
| 141 |
gr.Markdown("""
|
| 142 |
---
|
| 143 |
-
Tips:
|
| 144 |
-
- Works best with 100
|
| 145 |
- Articles, news, reports, essays
|
| 146 |
- Toggle Bullet Mode for point wise output
|
| 147 |
---
|
| 148 |
-
Model: distilbart-cnn-12-6
|
| 149 |
-
Device: CPU
|
| 150 |
""")
|
| 151 |
|
| 152 |
submit_btn.click(
|
| 153 |
summarize_text,
|
| 154 |
-
inputs=[txt_input,
|
| 155 |
-
outputs=[
|
| 156 |
)
|
| 157 |
txt_input.submit(
|
| 158 |
summarize_text,
|
| 159 |
-
inputs=[txt_input,
|
| 160 |
-
outputs=[
|
| 161 |
)
|
| 162 |
-
clear_btn.click(clear_chat, outputs=[
|
| 163 |
|
| 164 |
# ββ 4. Launch ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 165 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 14 |
print("Model ready β
")
|
| 15 |
|
| 16 |
# ββ 2. Summarization Function ββββββββββββββββββββββββββββββββββββββ
|
| 17 |
+
def summarize_text(user_input, max_len, min_len, bullet_mode, chat_html):
|
| 18 |
user_input = user_input.strip()
|
| 19 |
|
| 20 |
if not user_input:
|
| 21 |
+
return chat_html, ""
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
if len(user_input.split()) < 30:
|
| 24 |
+
bot_msg = (
|
| 25 |
+
"π Hello! I'm your Text Summarizer. "
|
| 26 |
+
"Paste any long article or document (30+ words) and I'll summarize it."
|
| 27 |
+
)
|
| 28 |
+
new_html = chat_html + build_message(user_input, bot_msg)
|
| 29 |
+
return new_html, ""
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
try:
|
| 32 |
inputs = tokenizer(
|
|
|
|
| 50 |
|
| 51 |
if bullet_mode:
|
| 52 |
sentences = summary.replace("?", ".").replace("!", ".").split(". ")
|
| 53 |
+
bullets = "".join(
|
| 54 |
+
f"<li>{s.strip().capitalize()}</li>"
|
| 55 |
for s in sentences if s.strip()
|
| 56 |
)
|
| 57 |
+
out = f"π <b>Summary (Bullet Points):</b><br><ul>{bullets}</ul>"
|
| 58 |
else:
|
| 59 |
+
out = f"π <b>Summary:</b><br><br>{summary}"
|
| 60 |
|
| 61 |
orig_words = len(user_input.split())
|
| 62 |
summ_words = len(summary.split())
|
| 63 |
reduction = round((1 - summ_words / orig_words) * 100, 1)
|
| 64 |
out += (
|
| 65 |
+
f"<br><br><hr>"
|
| 66 |
+
f"<small>π Original: {orig_words} words β "
|
| 67 |
f"Summary: {summ_words} words | "
|
| 68 |
+
f"Reduced by {reduction}%</small>"
|
| 69 |
)
|
| 70 |
|
| 71 |
+
label = user_input[:100] + "..." if len(user_input) > 100 else user_input
|
| 72 |
+
new_html = chat_html + build_message(label, out)
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
except Exception as e:
|
| 75 |
+
new_html = chat_html + build_message(user_input[:60], f"β Error: {str(e)}")
|
| 76 |
+
|
| 77 |
+
return new_html, ""
|
| 78 |
+
|
| 79 |
|
| 80 |
+
def build_message(user_text, bot_text):
|
| 81 |
+
return f"""
|
| 82 |
+
<div style='margin:10px 0; padding:10px; background:#1e1e2e; border-radius:8px;'>
|
| 83 |
+
<div style='background:#2a2a3e; padding:8px 12px; border-radius:6px; margin-bottom:8px;'>
|
| 84 |
+
<b style='color:#a0a0ff;'>You:</b><br>
|
| 85 |
+
<span style='color:#e0e0e0;'>{user_text}</span>
|
| 86 |
+
</div>
|
| 87 |
+
<div style='background:#1a3a2a; padding:8px 12px; border-radius:6px;'>
|
| 88 |
+
<b style='color:#80ff80;'>Summarizer:</b><br>
|
| 89 |
+
<span style='color:#e0e0e0;'>{bot_text}</span>
|
| 90 |
+
</div>
|
| 91 |
+
</div>
|
| 92 |
+
"""
|
| 93 |
|
| 94 |
|
| 95 |
def clear_chat():
|
| 96 |
+
return "", ""
|
| 97 |
|
| 98 |
|
| 99 |
# ββ 3. Gradio UI βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
|
|
|
| 107 |
|
| 108 |
with gr.Row():
|
| 109 |
|
| 110 |
+
# Left β Chat
|
| 111 |
with gr.Column(scale=7):
|
| 112 |
+
|
| 113 |
+
# Chat display using HTML (works in ALL gradio versions)
|
| 114 |
+
chat_display = gr.HTML(
|
| 115 |
+
value="<div style='height:400px; overflow-y:auto; padding:10px; "
|
| 116 |
+
"background:#12121f; border-radius:8px; color:#ccc;'>"
|
| 117 |
+
"π¬ Your summaries will appear here...</div>"
|
| 118 |
)
|
| 119 |
+
chat_state = gr.State("")
|
| 120 |
|
| 121 |
+
txt_input = gr.Textbox(
|
| 122 |
+
placeholder="Paste your article, report, or any long text here...",
|
| 123 |
+
show_label=False,
|
| 124 |
+
lines=4
|
| 125 |
+
)
|
| 126 |
with gr.Row():
|
| 127 |
+
submit_btn = gr.Button("β¨ Summarize", variant="primary")
|
| 128 |
+
clear_btn = gr.Button("ποΈ Clear", variant="secondary")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
# Right β Settings
|
| 131 |
with gr.Column(scale=3):
|
| 132 |
+
gr.Markdown("### βοΈ Settings")
|
| 133 |
max_length = gr.Slider(
|
| 134 |
minimum=50, maximum=300,
|
| 135 |
value=130, step=10,
|
|
|
|
| 141 |
label="Min Summary Length (tokens)"
|
| 142 |
)
|
| 143 |
bullet_mode = gr.Checkbox(
|
| 144 |
+
label="π΅ Bullet Point Mode",
|
| 145 |
value=False
|
| 146 |
)
|
| 147 |
gr.Markdown("""
|
| 148 |
---
|
| 149 |
+
**π‘ Tips:**
|
| 150 |
+
- Works best with 100β1000 word inputs
|
| 151 |
- Articles, news, reports, essays
|
| 152 |
- Toggle Bullet Mode for point wise output
|
| 153 |
---
|
| 154 |
+
**Model:** distilbart-cnn-12-6
|
| 155 |
+
**Device:** CPU
|
| 156 |
""")
|
| 157 |
|
| 158 |
submit_btn.click(
|
| 159 |
summarize_text,
|
| 160 |
+
inputs=[txt_input, max_length, min_length, bullet_mode, chat_state],
|
| 161 |
+
outputs=[chat_display, txt_input]
|
| 162 |
)
|
| 163 |
txt_input.submit(
|
| 164 |
summarize_text,
|
| 165 |
+
inputs=[txt_input, max_length, min_length, bullet_mode, chat_state],
|
| 166 |
+
outputs=[chat_display, txt_input]
|
| 167 |
)
|
| 168 |
+
clear_btn.click(clear_chat, outputs=[chat_display, chat_state])
|
| 169 |
|
| 170 |
# ββ 4. Launch ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 171 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|