SagarVelamuri commited on
Commit
1e32b99
·
verified ·
1 Parent(s): bfbbfb4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +92 -150
app.py CHANGED
@@ -55,34 +55,25 @@ def _ensure_vocab_consistency(md, tok):
55
  actual_vocab = None
56
  if actual_vocab is not None:
57
  md.config.vocab_size = actual_vocab
58
- try:
59
- md.generation_config.vocab_size = actual_vocab
60
- except Exception:
61
- pass
62
  else:
63
  fallback_size = getattr(tok, "vocab_size", None)
64
  if fallback_size is None:
65
- try:
66
- fallback_size = len(tok)
67
- except Exception:
68
- fallback_size = 64000
69
  md.config.vocab_size = fallback_size
70
- try:
71
- md.generation_config.vocab_size = fallback_size
72
- except Exception:
73
- pass
74
  if not hasattr(md.config, "get_text_config") or not callable(getattr(md.config, "get_text_config", None)):
75
  def _get_text_config(self): return self
76
  md.config.get_text_config = types.MethodType(_get_text_config, md.config)
77
-
78
  _ensure_vocab_consistency(model, tokenizer)
79
 
80
  # Disable KV cache to avoid custom decoder's past_key_values shape bug
81
  for obj in (model.config, model.generation_config):
82
- try:
83
- setattr(obj, "use_cache", False)
84
- except Exception:
85
- pass
86
 
87
  ip = IndicProcessor(inference=True)
88
 
@@ -99,9 +90,7 @@ def _translate_to_lang(text: str, tgt_code: str, num_beams: int, max_new_tokens:
99
  return_tensors="pt",
100
  return_attention_mask=True,
101
  ).to(device)
102
-
103
  do_sample = (temperature is not None) and (float(temperature) > 0)
104
-
105
  outputs = model.generate(
106
  **enc,
107
  max_new_tokens=int(max_new_tokens),
@@ -114,10 +103,7 @@ def _translate_to_lang(text: str, tgt_code: str, num_beams: int, max_new_tokens:
114
  early_stopping=False,
115
  pad_token_id=model.generation_config.pad_token_id,
116
  )
117
-
118
- decoded = tokenizer.batch_decode(
119
- outputs, skip_special_tokens=True, clean_up_tokenization_spaces=True
120
- )
121
  final = ip.postprocess_batch(decoded, lang=tgt_code)
122
  return final[0].strip()
123
 
@@ -154,52 +140,52 @@ THEME = gr.themes.Soft(
154
  button_primary_text_color="#ffffff",
155
  )
156
 
157
- # -------------------- LAYOUT CSS (simple, robust) --------------------
158
  CUSTOM_CSS = """
159
- /* allow natural page scroll (prevents clipping on short screens) */
160
- html, body { background:#0b1220; }
161
- .gradio-container { max-width: 1280px; margin: 0 auto; padding: 16px; }
162
 
163
- /* header */
164
- #hdr { background:#162434; border:1px solid #223144; border-radius:12px; padding:12px; text-align:center; }
 
165
  #title { color:#ffffff; font-weight:900; font-size:20px; margin:0; letter-spacing:.2px; }
166
  #subtitle { color:#9fb4d1; font-size:12.5px; margin:0; }
167
 
168
- /* main two-column layout: left sidebar fixed width, right content flexible */
169
- #page {
170
- display:grid;
171
- grid-template-columns: 320px 1fr;
172
- gap: 16px;
173
- margin-top: 14px;
174
  }
175
 
176
- /* right column layout: top= 2 columns (EN + HI), bottom = TE full width */
177
- #right {
178
- display:grid;
179
- grid-template-rows: auto auto;
180
- gap: 16px;
181
- }
182
- #topRow {
183
- display:grid;
184
- grid-template-columns: 1fr 1fr;
185
- gap: 16px;
186
- }
187
-
188
- /* panel shell */
189
- .panel { border:1px solid #223144; border-radius:12px; background:#0f172a; overflow:hidden; }
190
  .panel-h {
191
  display:flex; align-items:center; justify-content:space-between;
192
  padding:10px 12px; background:#0b1b2b; border-bottom:1px solid #243244;
193
  color:#eaf2ff; font-weight:900; letter-spacing:.2px;
 
194
  }
195
- .panel-b { padding:12px; }
196
 
197
- /* maximize button */
198
- .max { font-weight:900; padding:4px 10px; border-radius:10px; border:1px solid #3c5a86;
199
- background:#122037; color:#ffffff; }
200
- .max:hover { border-color:#60a5fa; }
 
 
 
 
 
 
 
 
 
 
 
201
 
202
- /* inputs */
203
  textarea, textarea:focus {
204
  background:#0b1220 !important; color:#f9fbff !important;
205
  font-size: 17px !important; line-height:1.55 !important;
@@ -209,99 +195,77 @@ textarea::placeholder { color:#9fb4d1 !important; }
209
  textarea:hover { border-color:#6b8db6 !important; }
210
  textarea:focus { border-color:#60a5fa !important; outline:none !important; }
211
 
212
- label { color:#ffffff !important; font-weight:800 !important; }
 
 
213
 
214
- /* sidebar sections */
215
- .section { margin-bottom: 16px; }
216
- .section h3 { margin:0 0 8px 0; color:#eaf2ff; font-size:14px; font-weight:900; letter-spacing:.2px; }
217
- #examples { border:1px dashed #2b3d55; border-radius:8px; padding:8px; }
218
 
219
- /* modal */
220
  #modal { position: fixed; inset: 0; z-index: 9999; background: rgba(2,6,23,.88); display: none; align-items: center; justify-content: center; padding: 12px; }
221
  #modal[style*="display: block"] { display: flex !important; }
222
  .modal-card { width:min(1280px,96vw); height:min(92vh,900px); background:#0f172a; border:1px solid #335070; border-radius:14px; box-shadow:0 18px 40px rgba(2,6,23,.6); display:flex; flex-direction:column; gap:8px; padding:10px; }
223
  .modal-title { color:#ffffff; font-weight:800; font-size:18px; letter-spacing:.2px; margin:0; }
224
  #fs_box textarea { height: calc(100% - 52px) !important; }
225
  .modal-actions { display:flex; gap:8px; justify-content:flex-end; }
226
-
227
- /* footer */
228
- #ftr { text-align:center; color:#9fb4d1; font-size:12px; margin-top:14px; }
229
  """
230
 
231
  # -------------------- Build UI --------------------
232
  with gr.Blocks(theme=THEME, css=CUSTOM_CSS, title="EN→HI / EN→TE Translator") as demo:
233
- modal_state = gr.State(value="") # '', 'src', 'hi', 'te'
234
 
235
  # Header
236
  with gr.Column(elem_id="hdr"):
237
  gr.Markdown('<p id="title">English → Hindi & Telugu Translator</p>')
238
  gr.Markdown('<p id="subtitle">IndicTrans2 pipeline · law-ai/InLegalTrans-En2Indic-1B</p>')
239
 
240
- # Page grid
241
- with gr.Row(elem_id="page"):
242
- # LEFT SIDEBAR (fixed width)
243
- with gr.Column():
244
- # Advanced Settings
245
- with gr.Column(elem_classes=["panel","section"]):
246
- gr.Markdown('<div class="panel-h">Advanced Settings <span></span></div>')
247
- with gr.Column(elem_classes=["panel-b"]):
248
- num_beams = gr.Slider(1, 8, value=4, step=1, label="Beam search: num_beams")
249
- max_new = gr.Slider(16, 512, value=128, step=8, label="Max new tokens")
250
- temperature = gr.Slider(0.0, 1.5, value=0.0, step=0.05, label="Temperature (0 = deterministic)")
251
- top_p = gr.Slider(0.0, 1.0, value=1.0, step=0.01, label="Top-p")
252
- top_k = gr.Slider(0, 100, value=50, step=1, label="Top-k")
253
-
254
- # Actions
255
- with gr.Column(elem_classes=["panel","section"]):
256
- gr.Markdown('<div class="panel-h">Actions <span></span></div>')
257
- with gr.Column(elem_classes=["panel-b"]):
258
- with gr.Row():
259
- translate_btn = gr.Button("Translate", variant="primary")
260
- clear_btn = gr.Button("Clear", variant="secondary")
261
-
262
- # Examples
263
- with gr.Column(elem_classes=["panel","section"]):
264
- gr.Markdown('<div class="panel-h">Examples <span></span></div>')
265
- with gr.Column(elem_classes=["panel-b"], elem_id="examples"):
266
- gr.Examples(
267
- examples=[
268
- ["The Constitution guarantees fundamental rights to every citizen of India."],
269
- ["Maintenance proceedings shall commence within thirty days from the date of application."],
270
- ["The agreement shall remain in force unless terminated by mutual consent in writing."],
271
- ],
272
- inputs=[],
273
- outputs=[],
274
- fn=None # just displays; click-to-fill is not needed here
275
- )
276
-
277
- # RIGHT CONTENT (flex)
278
- with gr.Column(elem_id="right"):
279
- # Top row: English + Hindi
280
- with gr.Row(elem_id="topRow"):
281
- # English
282
- with gr.Column(elem_classes=["panel"]):
283
- gr.Markdown('<div class="panel-h">English Text <button class="max">⤢</button></div>')
284
- src = gr.Textbox(lines=12, placeholder="Type English here…", show_label=False)
285
-
286
- # Hindi
287
- with gr.Column(elem_classes=["panel"]):
288
- gr.Markdown('<div class="panel-h">Hindi (hin_Deva) <button class="max" id="max_hi">⤢</button></div>')
289
- hi_out = gr.Textbox(lines=12, show_copy_button=True, show_label=False)
290
-
291
- # Bottom row: Telugu full width
292
  with gr.Column(elem_classes=["panel"]):
293
- gr.Markdown('<div class="panel-h">Telugu (tel_Telu) <button class="max" id="max_te">⤢</button></div>')
294
- te_out = gr.Textbox(lines=12, show_copy_button=True, show_label=False)
 
 
295
 
296
- # Footer
297
- gr.Markdown('<div id="ftr">Model: law-ai/InLegalTrans-En2Indic-1B · Tokenizer: ai4bharat/indictrans2-en-indic-1B</div>')
 
 
 
 
 
 
 
 
 
 
 
 
298
 
299
  # ---- Fullscreen modal (hidden by default) ----
300
  with gr.Group(visible=False, elem_id="modal") as modal:
301
  modal_title = gr.Markdown('<div class="modal-title">Fullscreen</div>')
302
  fs_text = gr.Textbox(lines=22, elem_id="fs_box")
303
  with gr.Row(elem_classes=["modal-actions"]):
304
- fs_save = gr.Button("Save & Close", variant="primary")
305
  fs_close = gr.Button("Close", variant="secondary")
306
 
307
  # ---- Wiring ----
@@ -313,34 +277,12 @@ with gr.Blocks(theme=THEME, css=CUSTOM_CSS, title="EN→HI / EN→TE Translator"
313
  )
314
  clear_btn.click(lambda: ("", "", ""), outputs=[src, hi_out, te_out])
315
 
316
- # Maximize (open modal) use separate triggers so we know which pane
317
- def open_src(s): return gr.update(visible=True), "src", '<div class="modal-title">English (Fullscreen)</div>', s
318
- def open_hi(h): return gr.update(visible=True), "hi", '<div class="modal-title">Hindi (Fullscreen)</div>', h
319
- def open_te(t): return gr.update(visible=True), "te", '<div class="modal-title">Telugu (Fullscreen)</div>', t
320
-
321
- # Add tiny buttons for English too (we can’t attach JS to raw HTML buttons from Markdown),
322
- # so we add hidden Gradio buttons to trigger the modal.
323
- btn_src = gr.Button(visible=False)
324
- btn_hi = gr.Button(visible=False)
325
- btn_te = gr.Button(visible=False)
326
-
327
- btn_src.click(open_src, inputs=[src], outputs=[modal, modal_state, modal_title, fs_text])
328
- btn_hi.click(open_hi, inputs=[hi_out], outputs=[modal, modal_state, modal_title, fs_text])
329
- btn_te.click(open_te, inputs=[te_out], outputs=[modal, modal_state, modal_title, fs_text])
330
-
331
- # Save & Close / Close
332
- def save_and_close(which, fs_val, s, h, t):
333
- if which == "src": s = fs_val
334
- elif which == "hi": h = fs_val
335
- elif which == "te": t = fs_val
336
- return (gr.update(visible=False), "", s, h, t)
337
-
338
- def close_no_save():
339
- return (gr.update(visible=False), "")
340
-
341
- fs_save.click(save_and_close, inputs=[modal_state, fs_text, src, hi_out, te_out],
342
- outputs=[modal, modal_state, src, hi_out, te_out])
343
- fs_close.click(close_no_save, outputs=[modal, modal_state])
344
 
345
  # Keep queue to enable buffering; omit unsupported args on older Gradio
346
  demo.queue(max_size=48).launch()
 
55
  actual_vocab = None
56
  if actual_vocab is not None:
57
  md.config.vocab_size = actual_vocab
58
+ try: md.generation_config.vocab_size = actual_vocab
59
+ except Exception: pass
 
 
60
  else:
61
  fallback_size = getattr(tok, "vocab_size", None)
62
  if fallback_size is None:
63
+ try: fallback_size = len(tok)
64
+ except Exception: fallback_size = 64000
 
 
65
  md.config.vocab_size = fallback_size
66
+ try: md.generation_config.vocab_size = fallback_size
67
+ except Exception: pass
 
 
68
  if not hasattr(md.config, "get_text_config") or not callable(getattr(md.config, "get_text_config", None)):
69
  def _get_text_config(self): return self
70
  md.config.get_text_config = types.MethodType(_get_text_config, md.config)
 
71
  _ensure_vocab_consistency(model, tokenizer)
72
 
73
  # Disable KV cache to avoid custom decoder's past_key_values shape bug
74
  for obj in (model.config, model.generation_config):
75
+ try: setattr(obj, "use_cache", False)
76
+ except Exception: pass
 
 
77
 
78
  ip = IndicProcessor(inference=True)
79
 
 
90
  return_tensors="pt",
91
  return_attention_mask=True,
92
  ).to(device)
 
93
  do_sample = (temperature is not None) and (float(temperature) > 0)
 
94
  outputs = model.generate(
95
  **enc,
96
  max_new_tokens=int(max_new_tokens),
 
103
  early_stopping=False,
104
  pad_token_id=model.generation_config.pad_token_id,
105
  )
106
+ decoded = tokenizer.batch_decode(outputs, skip_special_tokens=True, clean_up_tokenization_spaces=True)
 
 
 
107
  final = ip.postprocess_batch(decoded, lang=tgt_code)
108
  return final[0].strip()
109
 
 
140
  button_primary_text_color="#ffffff",
141
  )
142
 
143
+ # -------------------- LAYOUT CSS (20/40/40 exact split) --------------------
144
  CUSTOM_CSS = """
145
+ /* Full-bleed app */
146
+ html, body { height: 100%; background:#0b1220; }
147
+ .gradio-container { height: 100vh !important; width: 100vw !important; max-width: 100vw !important; margin: 0; padding: 0 8px 8px 8px; }
148
 
149
+ /* Header */
150
+ #hdr { height: 64px; display:flex; flex-direction:column; align-items:center; justify-content:center; gap:6px;
151
+ background:#162434; border:1px solid #223144; border-radius:12px; margin:8px 0; }
152
  #title { color:#ffffff; font-weight:900; font-size:20px; margin:0; letter-spacing:.2px; }
153
  #subtitle { color:#9fb4d1; font-size:12.5px; margin:0; }
154
 
155
+ /* Main grid: 20% / 40% / 40% widths; fills the rest of the viewport height */
156
+ #main {
157
+ height: calc(100vh - 64px - 16px); /* header + top/bottom margins */
158
+ display: grid;
159
+ grid-template-columns: 20% 40% 40%;
160
+ gap: 10px;
161
  }
162
 
163
+ /* Panels */
164
+ .panel { border:1px solid #223144; border-radius:12px; background:#0f172a; display:flex; flex-direction:column; min-height:0; }
 
 
 
 
 
 
 
 
 
 
 
 
165
  .panel-h {
166
  display:flex; align-items:center; justify-content:space-between;
167
  padding:10px 12px; background:#0b1b2b; border-bottom:1px solid #243244;
168
  color:#eaf2ff; font-weight:900; letter-spacing:.2px;
169
+ border-top-left-radius:12px; border-top-right-radius:12px;
170
  }
171
+ .panel-b { flex:1 1 auto; min-height:0; padding:10px 12px; }
172
 
173
+ /* Left column (Advanced) = 100% of main height; scroll inside if overflow */
174
+ #left { height: 100%; }
175
+ #adv-inner { height: 100%; overflow: auto; padding-right: 6px; }
176
+ label { color:#ffffff !important; font-weight:800 !important; }
177
+
178
+ /* Middle column splits 75% (input) / 25% (buttons) */
179
+ #middle { display:grid; grid-template-rows: 75% 25%; height:100%; gap:10px; }
180
+
181
+ /* Right column splits 50% / 50% */
182
+ #right { display:grid; grid-template-rows: 1fr 1fr; height:100%; gap:10px; }
183
+
184
+ /* Textareas fill their panel without overflow */
185
+ .textwrap { height:100%; min-height:0; display:flex; }
186
+ .textwrap > div { flex:1 1 auto; min-height:0; }
187
+ .textwrap textarea { height:100% !important; }
188
 
 
189
  textarea, textarea:focus {
190
  background:#0b1220 !important; color:#f9fbff !important;
191
  font-size: 17px !important; line-height:1.55 !important;
 
195
  textarea:hover { border-color:#6b8db6 !important; }
196
  textarea:focus { border-color:#60a5fa !important; outline:none !important; }
197
 
198
+ /* Buttons row */
199
+ #btnrow { display:flex; align-items:center; justify-content:center; gap:14px; height:100%; }
200
+ #btnrow > button { min-width: 160px; height: 46px; font-weight:800; border-radius:10px; }
201
 
202
+ /* Maximize button (Hindi/Telugu headers) */
203
+ .max { font-weight:900; padding:4px 10px; border-radius:10px; border:1px solid #3c5a86;
204
+ background:#122037; color:#ffffff; }
205
+ .max:hover { border-color:#60a5fa; }
206
 
207
+ /* Modal */
208
  #modal { position: fixed; inset: 0; z-index: 9999; background: rgba(2,6,23,.88); display: none; align-items: center; justify-content: center; padding: 12px; }
209
  #modal[style*="display: block"] { display: flex !important; }
210
  .modal-card { width:min(1280px,96vw); height:min(92vh,900px); background:#0f172a; border:1px solid #335070; border-radius:14px; box-shadow:0 18px 40px rgba(2,6,23,.6); display:flex; flex-direction:column; gap:8px; padding:10px; }
211
  .modal-title { color:#ffffff; font-weight:800; font-size:18px; letter-spacing:.2px; margin:0; }
212
  #fs_box textarea { height: calc(100% - 52px) !important; }
213
  .modal-actions { display:flex; gap:8px; justify-content:flex-end; }
 
 
 
214
  """
215
 
216
  # -------------------- Build UI --------------------
217
  with gr.Blocks(theme=THEME, css=CUSTOM_CSS, title="EN→HI / EN→TE Translator") as demo:
218
+ modal_state = gr.State(value="") # '', 'hi', 'te'
219
 
220
  # Header
221
  with gr.Column(elem_id="hdr"):
222
  gr.Markdown('<p id="title">English → Hindi & Telugu Translator</p>')
223
  gr.Markdown('<p id="subtitle">IndicTrans2 pipeline · law-ai/InLegalTrans-En2Indic-1B</p>')
224
 
225
+ # Main 20/40/40 layout
226
+ with gr.Row(elem_id="main"):
227
+ # LEFT: Advanced (20% width, 100% height)
228
+ with gr.Column(elem_id="left", elem_classes=["panel"]):
229
+ gr.Markdown('<div class="panel-h">Advanced Settings</div>')
230
+ with gr.Group(elem_id="adv-inner", elem_classes=["panel-b"]):
231
+ num_beams = gr.Slider(1, 8, value=4, step=1, label="Beam search: num_beams")
232
+ max_new = gr.Slider(16, 512, value=128, step=8, label="Max new tokens")
233
+ temperature = gr.Slider(0.0, 1.5, value=0.0, step=0.05, label="Temperature (0 = deterministic)")
234
+ top_p = gr.Slider(0.0, 1.0, value=1.0, step=0.01, label="Top-p")
235
+ top_k = gr.Slider(0, 100, value=50, step=1, label="Top-k")
236
+
237
+ # MIDDLE: English (40% width) → 75% input, 25% buttons
238
+ with gr.Column(elem_id="middle"):
239
+ with gr.Column(elem_classes=["panel"]):
240
+ gr.Markdown('<div class="panel-h">English Text</div>')
241
+ with gr.Group(elem_classes=["panel-b","textwrap"]):
242
+ src = gr.Textbox(placeholder="Type English here…", show_label=False, lines=14)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
243
  with gr.Column(elem_classes=["panel"]):
244
+ gr.Markdown('<div class="panel-h">Actions</div>')
245
+ with gr.Row(elem_id="btnrow", elem_classes=["panel-b"]):
246
+ translate_btn = gr.Button("Translate", variant="primary")
247
+ clear_btn = gr.Button("Clear", variant="secondary")
248
 
249
+ # RIGHT: outputs (40% width) → 50% Hindi, 50% Telugu
250
+ with gr.Column(elem_id="right"):
251
+ with gr.Column(elem_classes=["panel"]):
252
+ with gr.Row(elem_classes=["panel-h"]):
253
+ gr.Markdown("Hindi (hin_Deva)")
254
+ hi_max = gr.Button("⤢", elem_classes=["max"])
255
+ with gr.Group(elem_classes=["panel-b","textwrap"]):
256
+ hi_out = gr.Textbox(show_copy_button=True, show_label=False, lines=10)
257
+ with gr.Column(elem_classes=["panel"]):
258
+ with gr.Row(elem_classes=["panel-h"]):
259
+ gr.Markdown("Telugu (tel_Telu)")
260
+ te_max = gr.Button("⤢", elem_classes=["max"])
261
+ with gr.Group(elem_classes=["panel-b","textwrap"]):
262
+ te_out = gr.Textbox(show_copy_button=True, show_label=False, lines=10)
263
 
264
  # ---- Fullscreen modal (hidden by default) ----
265
  with gr.Group(visible=False, elem_id="modal") as modal:
266
  modal_title = gr.Markdown('<div class="modal-title">Fullscreen</div>')
267
  fs_text = gr.Textbox(lines=22, elem_id="fs_box")
268
  with gr.Row(elem_classes=["modal-actions"]):
 
269
  fs_close = gr.Button("Close", variant="secondary")
270
 
271
  # ---- Wiring ----
 
277
  )
278
  clear_btn.click(lambda: ("", "", ""), outputs=[src, hi_out, te_out])
279
 
280
+ # Maximize handlers (read-only preview; no save-back)
281
+ def open_hi(h): return gr.update(visible=True), "hi", '<div class="modal-title">Hindi (Fullscreen)</div>', h
282
+ def open_te(t): return gr.update(visible=True), "te", '<div class="modal-title">Telugu (Fullscreen)</div>', t
283
+ hi_max.click(open_hi, inputs=[hi_out], outputs=[modal, modal_state, modal_title, fs_text])
284
+ te_max.click(open_te, inputs=[te_out], outputs=[modal, modal_state, modal_title, fs_text])
285
+ fs_close.click(lambda: (gr.update(visible=False), ""), outputs=[modal, modal_state])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
286
 
287
  # Keep queue to enable buffering; omit unsupported args on older Gradio
288
  demo.queue(max_size=48).launch()