rajeshree2 commited on
Commit
acbe06f
Β·
verified Β·
1 Parent(s): 090fcc4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -31
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
- MODEL_NAME = "facebook/bart-large-cnn"
 
7
  tokenizer = BartTokenizer.from_pretrained(MODEL_NAME)
8
  model = BartForConditionalGeneration.from_pretrained(MODEL_NAME)
9
- device = "cuda" if torch.cuda.is_available() else "cpu"
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": "πŸ‘‹ Please paste longer text (30+ words) to summarize."})
 
 
 
 
 
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
- ids = model.generate(
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(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:
50
  out = f"πŸ“Œ **Summary**:\n\n{summary}"
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})
@@ -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("# πŸ“ 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()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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)