Jellyfish042 commited on
Commit
b91f21d
·
1 Parent(s): 9245c1d
Files changed (3) hide show
  1. app.py +28 -14
  2. tests/visual_smoke.py +61 -3
  3. visualization/assets/app.css +4 -3
app.py CHANGED
@@ -165,9 +165,6 @@ def initialize_models():
165
 
166
  print("Initializing models...")
167
 
168
- # Load precomputed example first
169
- load_precomputed_example()
170
-
171
  # Download RWKV model if needed
172
  print("Checking RWKV7 model...")
173
  _rwkv_model_path = download_rwkv_model()
@@ -195,10 +192,18 @@ def wrap_html_in_iframe(html: str) -> str:
195
  escaped = escape_attr(html)
196
  onload_js = (
197
  "(function(f){"
198
- "function r(){try{var d=f.contentWindow.document;"
199
- "if(!d)return;var h=Math.max(d.body.scrollHeight,d.documentElement.scrollHeight);"
200
- "f.style.height=(h+2)+'px';}catch(e){}}"
201
- "r();setTimeout(r,50);setTimeout(r,200);"
 
 
 
 
 
 
 
 
202
  "})(this)"
203
  )
204
  return f"""
@@ -323,8 +328,14 @@ APP_CSS = "\n".join((_ASSETS_DIR / name).read_text(encoding="utf-8") for name in
323
  # shared theme's media queries cannot reach the document. Keep them global.
324
  APP_HEAD = f"<style>{APP_CSS}</style>"
325
 
 
 
 
 
 
326
  with gr.Blocks(
327
  title="Compression-Lens: RWKV-7 vs Qwen3",
 
328
  ) as demo:
329
  gr.HTML(
330
  f"""
@@ -349,22 +360,26 @@ with gr.Blocks(
349
  example_btn = gr.Button("Load example", elem_id="load-example", scale=0, min_width=120)
350
 
351
  text_input = gr.Textbox(
 
352
  label="Input",
353
  show_label=False,
354
  container=False,
355
  placeholder=f"Enter text to compare (max {MAX_TEXT_LENGTH:,} characters)…",
356
- lines=12,
357
- max_lines=30,
358
  autoscroll=False,
359
  elem_id="input-text",
360
  )
361
  with gr.Row(elem_id="input-actions"):
362
  with gr.Column(scale=1, min_width=100):
363
- input_count = gr.HTML(f'<span class="count">0 / {MAX_TEXT_LENGTH:,} characters</span>', elem_id="input-count")
 
 
 
364
  clear_btn = gr.Button("Clear", elem_id="clear-button", scale=0, min_width=72)
365
  run_btn = gr.Button("Run Comparison", variant="primary", elem_id="run-button", scale=0, min_width=146)
366
 
367
- output_html = gr.HTML(label="Comparison", elem_id="comparison-output")
368
 
369
  with gr.Accordion("How to calculate compression rate?", open=False, elem_id="compression-metric"):
370
  gr.Markdown(
@@ -397,10 +412,9 @@ $$
397
  js="(text) => '<span class=\"count\">' + Array.from(text || '').length.toLocaleString('en-US') + ' / 16,384 characters</span>'",
398
  queue=False,
399
  )
400
- clear_btn.click(fn=clear_inputs, outputs=[text_input, output_html])
401
- example_btn.click(fn=get_default_example, outputs=[text_input, output_html])
402
  run_btn.click(fn=run_evaluation, inputs=[text_input], outputs=[output_html])
403
- demo.load(fn=get_default_example, outputs=[text_input, output_html])
404
 
405
 
406
  if __name__ == "__main__":
 
165
 
166
  print("Initializing models...")
167
 
 
 
 
168
  # Download RWKV model if needed
169
  print("Checking RWKV7 model...")
170
  _rwkv_model_path = download_rwkv_model()
 
192
  escaped = escape_attr(html)
193
  onload_js = (
194
  "(function(f){"
195
+ "var w=f.contentWindow,d=w.document;"
196
+ # scrollHeight includes the iframe's current viewport, so it cannot
197
+ # shrink after a narrow layout or a font change. Measure content only.
198
+ "function r(){if(!d.body)return;"
199
+ "var h=Math.ceil(d.body.getBoundingClientRect().height)+2;"
200
+ "if(Math.abs(f.getBoundingClientRect().height-h)>1)f.style.height=h+'px';}"
201
+ "r();"
202
+ "if(w.ResizeObserver&&d.body){"
203
+ "f._contentResizeObserver=new w.ResizeObserver(r);"
204
+ "f._contentResizeObserver.observe(d.body);}"
205
+ "w.addEventListener('resize',r);"
206
+ "if(d.fonts)d.fonts.ready.then(r);"
207
  "})(this)"
208
  )
209
  return f"""
 
328
  # shared theme's media queries cannot reach the document. Keep them global.
329
  APP_HEAD = f"<style>{APP_CSS}</style>"
330
 
331
+ # Populate the initial page from disk, including when a host imports this app.
332
+ # Sending this through demo.load() unnecessarily queues a large cached result.
333
+ load_precomputed_example()
334
+ _default_text, _default_html = get_default_example()
335
+
336
  with gr.Blocks(
337
  title="Compression-Lens: RWKV-7 vs Qwen3",
338
+ fill_width=True,
339
  ) as demo:
340
  gr.HTML(
341
  f"""
 
360
  example_btn = gr.Button("Load example", elem_id="load-example", scale=0, min_width=120)
361
 
362
  text_input = gr.Textbox(
363
+ value=_default_text,
364
  label="Input",
365
  show_label=False,
366
  container=False,
367
  placeholder=f"Enter text to compare (max {MAX_TEXT_LENGTH:,} characters)…",
368
+ lines=16,
369
+ max_lines=16,
370
  autoscroll=False,
371
  elem_id="input-text",
372
  )
373
  with gr.Row(elem_id="input-actions"):
374
  with gr.Column(scale=1, min_width=100):
375
+ input_count = gr.HTML(
376
+ f'<span class="count">{len(_default_text):,} / {MAX_TEXT_LENGTH:,} characters</span>',
377
+ elem_id="input-count",
378
+ )
379
  clear_btn = gr.Button("Clear", elem_id="clear-button", scale=0, min_width=72)
380
  run_btn = gr.Button("Run Comparison", variant="primary", elem_id="run-button", scale=0, min_width=146)
381
 
382
+ output_html = gr.HTML(value=_default_html, label="Comparison", elem_id="comparison-output")
383
 
384
  with gr.Accordion("How to calculate compression rate?", open=False, elem_id="compression-metric"):
385
  gr.Markdown(
 
412
  js="(text) => '<span class=\"count\">' + Array.from(text || '').length.toLocaleString('en-US') + ' / 16,384 characters</span>'",
413
  queue=False,
414
  )
415
+ clear_btn.click(fn=clear_inputs, outputs=[text_input, output_html], queue=False, show_progress="hidden")
416
+ example_btn.click(fn=get_default_example, outputs=[text_input, output_html], queue=False, show_progress="hidden")
417
  run_btn.click(fn=run_evaluation, inputs=[text_input], outputs=[output_html])
 
418
 
419
 
420
  if __name__ == "__main__":
tests/visual_smoke.py CHANGED
@@ -19,10 +19,48 @@ def check_comparison(page, report, expect) -> None:
19
  assert download.value.suggested_filename.endswith(".html")
20
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def check_app(page, expect) -> None:
23
  text = page.locator("#input-text textarea")
24
  expect(text).not_to_have_value("")
25
  assert text.evaluate("el => el.scrollTop") == 0, "Example scrolled to its end"
 
 
 
26
  # The Group wrapper previously painted a grey rectangle inside this panel.
27
  assert page.locator("#input-panel > .styler").count() == 0
28
  for row in ("#input-heading", "#input-actions"):
@@ -43,8 +81,7 @@ def check_app(page, expect) -> None:
43
  page.locator("#load-example").click()
44
  expect(text).to_have_value(re.compile(r"The Bitter Lesson"))
45
  assert text.evaluate("el => el.scrollTop") == 0
46
- page.get_by_text("How to calculate compression rate?", exact=True).click()
47
- expect(page.locator("#compression-metric .katex").first).to_be_visible()
48
  # Wait for the accordion animation and math layout to settle.
49
  page.wait_for_function(
50
  "document.documentElement.scrollWidth <= window.innerWidth + 1"
@@ -68,21 +105,42 @@ def main() -> None:
68
  with sync_playwright() as playwright:
69
  browser = playwright.chromium.launch()
70
  for theme in ("light", "dark"):
71
- for width in (1280, 390):
72
  page = browser.new_page(
73
  viewport={"width": width, "height": 1000}, color_scheme=theme,
74
  )
75
  errors = []
76
  page.on("pageerror", lambda error: errors.append(str(error)))
 
 
 
 
 
 
 
 
 
 
 
 
 
77
  page.goto(args.url or args.html.resolve().as_uri())
78
  target = page.locator("#input-panel" if args.url else "body")
79
  try:
80
  if args.url:
 
 
 
 
81
  check_app(page, expect)
82
  else:
83
  check_comparison(page, page, expect)
84
  finally:
85
  target.screenshot(path=str(args.out / f"ui-{theme}-{width}.png"))
 
 
 
 
86
  assert not errors, errors
87
  print(f"Passed: {theme}, {width}px")
88
  page.close()
 
19
  assert download.value.suggested_filename.endswith(".html")
20
 
21
 
22
+ def check_report_height(page) -> None:
23
+ page.wait_for_function("""() => {
24
+ const frame = document.querySelector('#comparison-output iframe');
25
+ const body = frame?.contentDocument?.body;
26
+ const accordion = document.querySelector('#compression-metric');
27
+ if (!body || !accordion) return false;
28
+ const contentBottom = frame.getBoundingClientRect().top + body.getBoundingClientRect().height;
29
+ const gap = accordion.getBoundingClientRect().top - contentBottom;
30
+ return Math.abs(frame.clientHeight - body.getBoundingClientRect().height) < 12
31
+ && gap >= 0 && gap < 64;
32
+ }""")
33
+
34
+
35
+ def check_accordion(page, expect) -> None:
36
+ accordion = page.locator("#compression-metric")
37
+ content = accordion.locator('[data-testid="accordion-content"]')
38
+ label = page.get_by_text("How to calculate compression rate?", exact=True)
39
+ expect(content).to_be_hidden()
40
+ for _ in range(2):
41
+ label.click()
42
+ expect(content).to_be_visible()
43
+ expect(content.locator(".katex").first).to_be_visible()
44
+ expect(content).to_contain_text("12.5")
45
+ label.click()
46
+ expect(content).to_be_hidden()
47
+ assert accordion.bounding_box()["height"] < 90, "Closed accordion kept its expanded height"
48
+ original_size = page.viewport_size
49
+ for width in (390, 1920, original_size["width"]):
50
+ page.set_viewport_size({"width": width, "height": original_size["height"]})
51
+ check_report_height(page)
52
+ label.click()
53
+ expect(content).to_be_visible()
54
+ check_report_height(page)
55
+
56
+
57
  def check_app(page, expect) -> None:
58
  text = page.locator("#input-text textarea")
59
  expect(text).not_to_have_value("")
60
  assert text.evaluate("el => el.scrollTop") == 0, "Example scrolled to its end"
61
+ assert text.evaluate("el => el.clientHeight") >= 350, "Input was flattened"
62
+ if page.viewport_size["width"] >= 1280:
63
+ assert page.locator("#input-panel").bounding_box()["width"] >= 1100, "App was constrained to Gradio's default narrow width"
64
  # The Group wrapper previously painted a grey rectangle inside this panel.
65
  assert page.locator("#input-panel > .styler").count() == 0
66
  for row in ("#input-heading", "#input-actions"):
 
81
  page.locator("#load-example").click()
82
  expect(text).to_have_value(re.compile(r"The Bitter Lesson"))
83
  assert text.evaluate("el => el.scrollTop") == 0
84
+ check_accordion(page, expect)
 
85
  # Wait for the accordion animation and math layout to settle.
86
  page.wait_for_function(
87
  "document.documentElement.scrollWidth <= window.innerWidth + 1"
 
105
  with sync_playwright() as playwright:
106
  browser = playwright.chromium.launch()
107
  for theme in ("light", "dark"):
108
+ for width in (1920, 390):
109
  page = browser.new_page(
110
  viewport={"width": width, "height": 1000}, color_scheme=theme,
111
  )
112
  errors = []
113
  page.on("pageerror", lambda error: errors.append(str(error)))
114
+ blocked_callbacks = []
115
+
116
+ def block_callbacks(route):
117
+ if route.request.method == "POST":
118
+ blocked_callbacks.append(route.request.url)
119
+ route.abort()
120
+ else:
121
+ route.continue_()
122
+
123
+ if args.url:
124
+ # Initial cached results must render without a load callback,
125
+ # a model inference, or a working Gradio event queue.
126
+ page.route("**/gradio_api/**", block_callbacks)
127
  page.goto(args.url or args.html.resolve().as_uri())
128
  target = page.locator("#input-panel" if args.url else "body")
129
  try:
130
  if args.url:
131
+ expect(page.locator("#input-text textarea")).not_to_have_value("")
132
+ expect(page.frame_locator("#comparison-output iframe").locator(".content .token").first).to_be_visible()
133
+ assert not blocked_callbacks, blocked_callbacks
134
+ page.unroute("**/gradio_api/**", block_callbacks)
135
  check_app(page, expect)
136
  else:
137
  check_comparison(page, page, expect)
138
  finally:
139
  target.screenshot(path=str(args.out / f"ui-{theme}-{width}.png"))
140
+ if args.url:
141
+ page.locator("#compression-metric").screenshot(
142
+ path=str(args.out / f"accordion-{theme}-{width}.png")
143
+ )
144
  assert not errors, errors
145
  print(f"Passed: {theme}, {width}px")
146
  page.close()
visualization/assets/app.css CHANGED
@@ -1,13 +1,15 @@
1
  body { background:var(--page)!important; }
2
  .gradio-container {
 
3
  max-width:1200px!important;
 
4
  margin-inline:auto!important;
5
  padding:0 24px 32px!important;
6
  background:transparent!important;
7
  color:var(--text)!important;
8
  font-family:var(--sans)!important;
9
  }
10
- .gradio-container .main { gap:20px!important; }
11
  .gradio-container footer { display:none!important; }
12
  #page-header { padding:0!important; background:transparent!important; border:0!important; }
13
  .project-header { padding-top:28px; text-align:center; }
@@ -23,7 +25,7 @@ body { background:var(--page)!important; }
23
  #input-heading .html-container, #input-count .html-container { padding:0; }
24
  #input-heading h2 { margin:0; color:var(--secondary); font-size:12px; font-weight:650; letter-spacing:.06em; text-transform:uppercase; }
25
  #input-text { padding:0!important; border:0!important; background:transparent!important; }
26
- #input-text textarea { height:272px!important; min-height:272px!important; max-height:none!important; resize:vertical!important; padding:12px 14px!important; font:13.5px/1.65 var(--mono)!important; color:var(--text)!important; background:var(--raised)!important; border:1px solid var(--strong)!important; border-radius:8px!important; }
27
  #input-text textarea:focus { border-color:var(--blue)!important; box-shadow:0 0 0 3px var(--focus)!important; }
28
  #input-actions { margin-top:12px; }
29
  #input-count { padding:0!important; border:0!important; }
@@ -41,7 +43,6 @@ body { background:var(--page)!important; }
41
  #compression-metric { padding:14px 20px!important; background:var(--surface)!important; border:1px solid var(--border)!important; border-radius:14px!important; box-shadow:var(--shadow); }
42
  #compression-metric .label-wrap { color:var(--secondary); font-size:12px; }
43
  #compression-metric .prose { color:var(--secondary); font-size:12px; line-height:1.8; }
44
- #compression-metric, #compression-metric .gr-accordion-content, #compression-metric .gr-accordion-content > div, #compression-metric .prose, #compression-metric .markdown, #compression-metric .md { max-height:none!important; height:auto!important; overflow:visible!important; }
45
  #compression-metric .katex-display { overflow-x:auto!important; overflow-y:hidden!important; }
46
  @media(max-width:760px) {
47
  .gradio-container { padding-inline:14px!important; }
 
1
  body { background:var(--page)!important; }
2
  .gradio-container {
3
+ width:100%!important;
4
  max-width:1200px!important;
5
+ box-sizing:border-box;
6
  margin-inline:auto!important;
7
  padding:0 24px 32px!important;
8
  background:transparent!important;
9
  color:var(--text)!important;
10
  font-family:var(--sans)!important;
11
  }
12
+ .gradio-container .main { gap:20px!important; padding:0!important; }
13
  .gradio-container footer { display:none!important; }
14
  #page-header { padding:0!important; background:transparent!important; border:0!important; }
15
  .project-header { padding-top:28px; text-align:center; }
 
25
  #input-heading .html-container, #input-count .html-container { padding:0; }
26
  #input-heading h2 { margin:0; color:var(--secondary); font-size:12px; font-weight:650; letter-spacing:.06em; text-transform:uppercase; }
27
  #input-text { padding:0!important; border:0!important; background:transparent!important; }
28
+ #input-text textarea { min-height:360px!important; max-height:none!important; resize:vertical!important; overflow-y:auto!important; padding:12px 14px!important; font:13.5px/1.65 var(--mono)!important; color:var(--text)!important; background:var(--raised)!important; border:1px solid var(--strong)!important; border-radius:8px!important; }
29
  #input-text textarea:focus { border-color:var(--blue)!important; box-shadow:0 0 0 3px var(--focus)!important; }
30
  #input-actions { margin-top:12px; }
31
  #input-count { padding:0!important; border:0!important; }
 
43
  #compression-metric { padding:14px 20px!important; background:var(--surface)!important; border:1px solid var(--border)!important; border-radius:14px!important; box-shadow:var(--shadow); }
44
  #compression-metric .label-wrap { color:var(--secondary); font-size:12px; }
45
  #compression-metric .prose { color:var(--secondary); font-size:12px; line-height:1.8; }
 
46
  #compression-metric .katex-display { overflow-x:auto!important; overflow-y:hidden!important; }
47
  @media(max-width:760px) {
48
  .gradio-container { padding-inline:14px!important; }