cutterdalton-webai commited on
Commit
cd56fd4
·
verified ·
1 Parent(s): c11fdf5

fix: restore scroll and token-limit fixes on top of the trace toggle

Browse files

c11fdf5 was based on an older app.py and reverted 14119b5 and 05f3ee9. This re-applies them via a three-way merge, keeping the thinking-trace visibility control.

Files changed (1) hide show
  1. app.py +83 -41
app.py CHANGED
@@ -24,6 +24,8 @@ import gradio as gr
24
 
25
  MODEL_ID = "webAI-Official/TwIL-LM3-Pro"
26
  MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "8192"))
 
 
27
  ASSETS = Path(__file__).resolve().parent / "brand"
28
  AVATAR = ASSETS / "avatar.svg"
29
 
@@ -188,14 +190,29 @@ html, body, #root, .gradio-container, .gradio-container > .main,
188
  .gradio-container .wrap.default {
189
  background: transparent !important;
190
  }
191
- html, body, .gradio-container, .gradio-container > .main, .fillable,
 
 
 
 
 
 
 
 
 
 
 
 
192
  .contain, .app {
193
- height: 100% !important;
 
 
 
194
  min-height: 100vh !important;
195
- max-height: 100vh !important;
196
  max-width: none !important;
197
  margin: 0 !important;
198
- overflow: hidden !important;
199
  }
200
  .gradio-container, .main.fillable, .contain {
201
  display: flex !important;
@@ -212,15 +229,15 @@ footer, .footer, .built-with, .settings, .settings-bar, .show-api,
212
  }
213
 
214
  #app-shell {
215
- flex: 1 1 auto !important;
216
- height: 100% !important;
217
- max-height: 100% !important;
218
- min-height: 0 !important;
219
  display: flex !important;
220
  flex-direction: column !important;
221
- padding: 16px 24px 12px !important;
222
  box-sizing: border-box !important;
223
- overflow: hidden !important;
224
  background: var(--weba-canvas) !important;
225
  }
226
  #app-shell > .gap, #app-shell > div {
@@ -232,8 +249,8 @@ footer, .footer, .built-with, .settings, .settings-bar, .show-api,
232
  #app-shell > #twil-chat,
233
  #app-shell > .block:has(#twil-chat),
234
  #app-shell > div:has(#twil-chat) {
235
- flex: 1 1 auto !important;
236
- min-height: 0 !important;
237
  height: auto !important;
238
  }
239
 
@@ -322,30 +339,30 @@ footer, .footer, .built-with, .settings, .settings-bar, .show-api,
322
  #twil-chat {
323
  max-width: 768px !important;
324
  width: 100% !important;
325
- height: 100% !important;
 
326
  margin: 0 auto !important;
327
- flex: 1 1 auto !important;
328
  display: flex !important;
329
  flex-direction: column !important;
330
- overflow: hidden !important;
331
  }
332
  #twil-chat > .wrapper {
333
- flex: 1 1 auto !important;
334
- height: 100% !important;
 
335
  min-height: 0 !important;
336
  display: flex !important;
337
  flex-direction: column !important;
338
- overflow: hidden !important;
339
  }
340
  #twil-chat .bubble-wrap {
341
- flex: 1 1 auto !important;
342
- height: 100% !important;
343
- min-height: 0 !important;
 
344
  overflow-x: hidden !important;
345
- overflow-y: auto !important;
346
- overscroll-behavior: contain;
347
- scrollbar-gutter: stable;
348
- scroll-behavior: smooth;
349
  padding-right: 8px !important;
350
  }
351
  #twil-chat .bubble-wrap::-webkit-scrollbar {
@@ -552,12 +569,15 @@ footer, .footer, .built-with, .settings, .settings-bar, .show-api,
552
  flex-shrink: 0;
553
  }
554
 
555
- /* Tabs shell: keep the chat layout filling the viewport */
556
  #twil-tabs {
557
- flex: 1 1 auto !important;
558
- min-height: 0 !important;
 
 
559
  display: flex !important;
560
  flex-direction: column !important;
 
561
  background: transparent !important;
562
  border: none !important;
563
  box-shadow: none !important;
@@ -588,9 +608,11 @@ footer, .footer, .built-with, .settings, .settings-bar, .show-api,
588
  border-bottom-color: var(--weba-fg) !important;
589
  }
590
  #twil-tabs .tabitem {
591
- flex: 1 1 auto !important;
592
- min-height: 0 !important;
593
- overflow: hidden !important;
 
 
594
  background: transparent !important;
595
  border: none !important;
596
  box-shadow: none !important;
@@ -598,8 +620,10 @@ footer, .footer, .built-with, .settings, .settings-bar, .show-api,
598
  }
599
  #tab-chat > .gap, #tab-chat > .column,
600
  #tab-chat, #tab-chat .column:has(> div > #twil-chat) {
601
- height: 100% !important;
 
602
  min-height: 0 !important;
 
603
  }
604
  #tab-chat > .gap, #tab-chat > .column {
605
  display: flex !important;
@@ -607,12 +631,15 @@ footer, .footer, .built-with, .settings, .settings-bar, .show-api,
607
  }
608
  #tab-chat .block:has(#twil-chat),
609
  #tab-chat div:has(> #twil-chat) {
610
- flex: 1 1 auto !important;
611
- min-height: 0 !important;
612
  height: auto !important;
 
613
  }
614
  #tab-showcase {
615
- overflow-y: auto !important;
 
 
616
  }
617
 
618
  /* Showcase cards */
@@ -757,6 +784,11 @@ def _strip_thinking(text: str) -> str:
757
  def _parse_think(raw: str) -> tuple[str, str, bool]:
758
  """Return (think, answer, think_open) from a possibly streaming reply."""
759
  open_idx = raw.find(THINK_OPEN)
 
 
 
 
 
760
  if open_idx == -1:
761
  # A partially streamed opening tag is not an answer yet.
762
  if raw.strip() and THINK_OPEN.startswith(raw.strip()):
@@ -780,8 +812,8 @@ def _parse_think(raw: str) -> tuple[str, str, bool]:
780
  def _gpu_seconds(history, max_new_tokens=2048, *args, **kwargs):
781
  # Scale the ZeroGPU reservation with the requested output budget. The
782
  # platform caps a single reservation at five minutes.
783
- tokens = int(max_new_tokens or 2048)
784
- return min(300, max(30, 20 + tokens // 50))
785
 
786
 
787
  def _history_for_model(history: list) -> list[dict]:
@@ -868,12 +900,23 @@ def _stream_tokens(conversation: list[dict], max_new_tokens: int, temperature: f
868
  generate_kwargs["temperature"] = float(temperature)
869
  generate_kwargs["top_p"] = float(top_p)
870
 
 
 
 
871
  Thread(target=model.generate, kwargs=generate_kwargs, daemon=True).start()
872
- acc = ""
873
  for text in streamer:
874
  acc += text
875
  yield acc
876
 
 
 
 
 
 
 
 
 
 
877
 
878
  def queue_message(message, history):
879
  """Paint the user turn immediately so the GPU wait is not a blank screen."""
@@ -966,7 +1009,7 @@ def generate_reply(
966
  yield history
967
 
968
 
969
- with gr.Blocks(fill_height=True, fill_width=True, elem_id="app-root") as demo:
970
  with gr.Column(elem_id="app-shell"):
971
  gr.HTML(HEADER)
972
  with gr.Tabs(elem_id="twil-tabs") as tabs:
@@ -985,7 +1028,6 @@ with gr.Blocks(fill_height=True, fill_width=True, elem_id="app-root") as demo:
985
  line_breaks=False,
986
  elem_id="twil-chat",
987
  scale=1,
988
- height="100%",
989
  autoscroll=True,
990
  )
991
  with gr.Row(elem_id="chips-row") as chips_row:
@@ -1005,7 +1047,7 @@ with gr.Blocks(fill_height=True, fill_width=True, elem_id="app-root") as demo:
1005
  )
1006
  send = gr.Button("↑", elem_id="send-btn", scale=0)
1007
  with gr.Accordion("Parameters", open=False, elem_id="params-box"):
1008
- max_new_tokens = gr.Slider(256, 16384, value=2048, step=256, label="Max new tokens")
1009
  temperature = gr.Slider(0, 1.5, value=0, step=0.05, label="Temperature (0 = greedy)")
1010
  top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
1011
  enable_thinking = gr.Checkbox(value=True, label="Enable thinking")
 
24
 
25
  MODEL_ID = "webAI-Official/TwIL-LM3-Pro"
26
  MAX_INPUT_TOKEN_LENGTH = int(os.getenv("MAX_INPUT_TOKEN_LENGTH", "8192"))
27
+ # The reasoning trace alone often runs ~2k tokens, so leave room for the answer.
28
+ DEFAULT_MAX_NEW_TOKENS = 4096
29
  ASSETS = Path(__file__).resolve().parent / "brand"
30
  AVATAR = ASSETS / "avatar.svg"
31
 
 
190
  .gradio-container .wrap.default {
191
  background: transparent !important;
192
  }
193
+ /* Gradio's root is a flex column locked to the viewport. Make it a normal
194
+ document so content taller than the window extends the page and scrolls. */
195
+ html, body {
196
+ display: block !important;
197
+ height: auto !important;
198
+ min-height: 100% !important;
199
+ max-height: none !important;
200
+ max-width: none !important;
201
+ margin: 0 !important;
202
+ overflow-x: hidden !important;
203
+ overflow-y: auto !important;
204
+ }
205
+ .gradio-container, .gradio-container > .main, .fillable,
206
  .contain, .app {
207
+ display: flex !important;
208
+ flex-direction: column !important;
209
+ flex-grow: 0 !important;
210
+ height: auto !important;
211
  min-height: 100vh !important;
212
+ max-height: none !important;
213
  max-width: none !important;
214
  margin: 0 !important;
215
+ overflow: visible !important;
216
  }
217
  .gradio-container, .main.fillable, .contain {
218
  display: flex !important;
 
229
  }
230
 
231
  #app-shell {
232
+ flex: 0 0 auto !important;
233
+ height: auto !important;
234
+ max-height: none !important;
235
+ min-height: 100vh !important;
236
  display: flex !important;
237
  flex-direction: column !important;
238
+ padding: 16px 24px 24px !important;
239
  box-sizing: border-box !important;
240
+ overflow: visible !important;
241
  background: var(--weba-canvas) !important;
242
  }
243
  #app-shell > .gap, #app-shell > div {
 
249
  #app-shell > #twil-chat,
250
  #app-shell > .block:has(#twil-chat),
251
  #app-shell > div:has(#twil-chat) {
252
+ flex: 0 0 auto !important;
253
+ min-height: auto !important;
254
  height: auto !important;
255
  }
256
 
 
339
  #twil-chat {
340
  max-width: 768px !important;
341
  width: 100% !important;
342
+ height: auto !important;
343
+ max-height: none !important;
344
  margin: 0 auto !important;
345
+ flex: 0 0 auto !important;
346
  display: flex !important;
347
  flex-direction: column !important;
348
+ overflow: visible !important;
349
  }
350
  #twil-chat > .wrapper {
351
+ flex: 0 0 auto !important;
352
+ height: auto !important;
353
+ max-height: none !important;
354
  min-height: 0 !important;
355
  display: flex !important;
356
  flex-direction: column !important;
357
+ overflow: visible !important;
358
  }
359
  #twil-chat .bubble-wrap {
360
+ flex: 0 0 auto !important;
361
+ height: auto !important;
362
+ min-height: 280px !important;
363
+ max-height: none !important;
364
  overflow-x: hidden !important;
365
+ overflow-y: visible !important;
 
 
 
366
  padding-right: 8px !important;
367
  }
368
  #twil-chat .bubble-wrap::-webkit-scrollbar {
 
569
  flex-shrink: 0;
570
  }
571
 
572
+ /* Tabs grow with their content so the document can scroll. */
573
  #twil-tabs {
574
+ flex: 0 0 auto !important;
575
+ min-height: auto !important;
576
+ height: auto !important;
577
+ max-height: none !important;
578
  display: flex !important;
579
  flex-direction: column !important;
580
+ overflow: visible !important;
581
  background: transparent !important;
582
  border: none !important;
583
  box-shadow: none !important;
 
608
  border-bottom-color: var(--weba-fg) !important;
609
  }
610
  #twil-tabs .tabitem {
611
+ flex: 0 0 auto !important;
612
+ min-height: auto !important;
613
+ height: auto !important;
614
+ max-height: none !important;
615
+ overflow: visible !important;
616
  background: transparent !important;
617
  border: none !important;
618
  box-shadow: none !important;
 
620
  }
621
  #tab-chat > .gap, #tab-chat > .column,
622
  #tab-chat, #tab-chat .column:has(> div > #twil-chat) {
623
+ height: auto !important;
624
+ max-height: none !important;
625
  min-height: 0 !important;
626
+ overflow: visible !important;
627
  }
628
  #tab-chat > .gap, #tab-chat > .column {
629
  display: flex !important;
 
631
  }
632
  #tab-chat .block:has(#twil-chat),
633
  #tab-chat div:has(> #twil-chat) {
634
+ flex: 0 0 auto !important;
635
+ min-height: auto !important;
636
  height: auto !important;
637
+ overflow: visible !important;
638
  }
639
  #tab-showcase {
640
+ overflow: visible !important;
641
+ height: auto !important;
642
+ max-height: none !important;
643
  }
644
 
645
  /* Showcase cards */
 
784
  def _parse_think(raw: str) -> tuple[str, str, bool]:
785
  """Return (think, answer, think_open) from a possibly streaming reply."""
786
  open_idx = raw.find(THINK_OPEN)
787
+ if open_idx == -1 and THINK_CLOSE in raw:
788
+ # The chat template can emit the opening tag in the prompt, so the
789
+ # completion only carries the closing one.
790
+ raw = THINK_OPEN + raw
791
+ open_idx = 0
792
  if open_idx == -1:
793
  # A partially streamed opening tag is not an answer yet.
794
  if raw.strip() and THINK_OPEN.startswith(raw.strip()):
 
812
  def _gpu_seconds(history, max_new_tokens=2048, *args, **kwargs):
813
  # Scale the ZeroGPU reservation with the requested output budget. The
814
  # platform caps a single reservation at five minutes.
815
+ tokens = int(max_new_tokens or DEFAULT_MAX_NEW_TOKENS)
816
+ return min(300, max(30, 30 + tokens // 30))
817
 
818
 
819
  def _history_for_model(history: list) -> list[dict]:
 
900
  generate_kwargs["temperature"] = float(temperature)
901
  generate_kwargs["top_p"] = float(top_p)
902
 
903
+ prompt_tail = tokenizer.decode(input_ids[0, -8:], skip_special_tokens=False)
904
+ acc = THINK_OPEN if prompt_tail.rstrip().endswith(THINK_OPEN) else ""
905
+
906
  Thread(target=model.generate, kwargs=generate_kwargs, daemon=True).start()
 
907
  for text in streamer:
908
  acc += text
909
  yield acc
910
 
911
+ generated = len(tokenizer.encode(acc, add_special_tokens=False))
912
+ if generated >= int(max_new_tokens) - 4:
913
+ if THINK_OPEN in acc and THINK_CLOSE not in acc:
914
+ acc += THINK_CLOSE
915
+ yield acc + (
916
+ f"\n\n*Stopped at the {int(max_new_tokens):,}-token limit. "
917
+ "Raise **Max new tokens** under Parameters for a complete answer.*"
918
+ )
919
+
920
 
921
  def queue_message(message, history):
922
  """Paint the user turn immediately so the GPU wait is not a blank screen."""
 
1009
  yield history
1010
 
1011
 
1012
+ with gr.Blocks(fill_height=False, fill_width=True, elem_id="app-root") as demo:
1013
  with gr.Column(elem_id="app-shell"):
1014
  gr.HTML(HEADER)
1015
  with gr.Tabs(elem_id="twil-tabs") as tabs:
 
1028
  line_breaks=False,
1029
  elem_id="twil-chat",
1030
  scale=1,
 
1031
  autoscroll=True,
1032
  )
1033
  with gr.Row(elem_id="chips-row") as chips_row:
 
1047
  )
1048
  send = gr.Button("↑", elem_id="send-btn", scale=0)
1049
  with gr.Accordion("Parameters", open=False, elem_id="params-box"):
1050
+ max_new_tokens = gr.Slider(256, 16384, value=DEFAULT_MAX_NEW_TOKENS, step=256, label="Max new tokens")
1051
  temperature = gr.Slider(0, 1.5, value=0, step=0.05, label="Temperature (0 = greedy)")
1052
  top_p = gr.Slider(0.1, 1.0, value=0.95, step=0.05, label="Top-p")
1053
  enable_thinking = gr.Checkbox(value=True, label="Enable thinking")