rajeshree2 commited on
Commit
3820c85
Β·
verified Β·
1 Parent(s): 6870c65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +62 -56
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, history, max_len, min_len, bullet_mode):
18
  user_input = user_input.strip()
19
 
20
  if not user_input:
21
- history = history + [
22
- {"role": "assistant", "content": "⚠️ Please enter some text to summarize."}
23
- ]
24
- return history, history
25
 
26
  if len(user_input.split()) < 30:
27
- history = history + [
28
- {"role": "user", "content": user_input},
29
- {"role": "assistant", "content": (
30
- "πŸ‘‹ Hello! I'm your Text Summarizer.\n\n"
31
- "Paste any long article or document (30+ words) and I'll summarize it.\n\n"
32
- "Use the settings below to adjust length and format."
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 = "\n".join(
60
- f"β€’ {s.strip().capitalize()}"
61
  for s in sentences if s.strip()
62
  )
63
- out = f"πŸ“Œ Summary (Bullet Points):\n\n{bullets}"
64
  else:
65
- out = f"πŸ“Œ Summary:\n\n{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"\n\n---"
72
- f"\nπŸ“Š Original: {orig_words} words β†’ "
73
  f"Summary: {summ_words} words | "
74
- f"Reduced by {reduction}%"
75
  )
76
 
77
- label = user_input[:80] + "..." if len(user_input) > 80 else user_input
78
- history = history + [
79
- {"role": "user", "content": label},
80
- {"role": "assistant", "content": out}
81
- ]
82
 
83
  except Exception as e:
84
- history = history + [
85
- {"role": "user", "content": user_input[:60] + "..."},
86
- {"role": "assistant", "content": f"❌ Error: {str(e)}"}
87
- ]
88
 
89
- return history, history
 
 
 
 
 
 
 
 
 
 
 
 
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
- chatbot = gr.Chatbot(
109
- height=450,
110
- type="messages" # βœ… works in gradio 4.44.2
 
 
 
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
  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 to 1000 word inputs
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, state, max_length, min_length, bullet_mode],
155
- outputs=[chatbot, state]
156
  )
157
  txt_input.submit(
158
  summarize_text,
159
- inputs=[txt_input, state, max_length, min_length, bullet_mode],
160
- outputs=[chatbot, state]
161
  )
162
- clear_btn.click(clear_chat, outputs=[chatbot, state])
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)