Jellyfish042 commited on
Commit
b828a40
·
1 Parent(s): d449498

Adopt Panda-inspired comparison interface

Browse files
README.md CHANGED
@@ -18,20 +18,23 @@ Compare the byte-level prediction performance between models.
18
  ## Features
19
 
20
  - **Byte-level analysis**: See exactly where each model performs better or worse
21
- - **Interactive visualization**: Hover over tokens to see detailed predictions
22
  - **Color-coded comparison**:
23
- - 🟢 Green = Qwen3 predicts better (lower loss)
24
- - 🔴 Red = RWKV7 predicts better (lower loss)
 
25
  - **Top-10 predictions**: View each model's top predictions for every token
26
  - **Word occurrence linking**: See how repeated words are predicted differently
27
 
28
  ## How to Use
29
 
30
- 1. Enter or paste your text (max 4000 characters)
31
  2. Click "Run Comparison"
32
  3. Explore the interactive visualization
33
  4. Download the HTML file for offline viewing
34
 
 
 
35
  ## Models
36
 
37
  | Model | Type | Parameters | Architecture |
 
18
  ## Features
19
 
20
  - **Byte-level analysis**: See exactly where each model performs better or worse
21
+ - **Interactive visualization**: Hover over tokens to see detailed predictions; left- or right-click a token to pin the popup. Click it again, click outside, or press Esc to close.
22
  - **Color-coded comparison**:
23
+ - 🟢 Green = RWKV7 predicts better (lower loss)
24
+ - 🔴 Red = Qwen3 predicts better (lower loss)
25
+ - Relative coloring compares the local difference with the average difference
26
  - **Top-10 predictions**: View each model's top predictions for every token
27
  - **Word occurrence linking**: See how repeated words are predicted differently
28
 
29
  ## How to Use
30
 
31
+ 1. Enter or paste your text (max 16,384 characters), or click "Load example"
32
  2. Click "Run Comparison"
33
  3. Explore the interactive visualization
34
  4. Download the HTML file for offline viewing
35
 
36
+ The interface follows the RWKV Panda style with light and dark themes, soft red/green highlights, and both models' Top-10 probability tables shown together in the hover popup. Browser context menus are suppressed only on tokens and the popup.
37
+
38
  ## Models
39
 
40
  | Model | Type | Parameters | Architecture |
app.py CHANGED
@@ -190,9 +190,9 @@ def initialize_models():
190
 
191
  def wrap_html_in_iframe(html: str) -> str:
192
  """Wrap HTML in an iframe for Gradio display."""
193
- # For srcdoc attribute, we only need to escape quotes
194
- # The HTML entities inside (like ", 
) should remain as-is
195
- escaped = html.replace('"', """)
196
  onload_js = (
197
  "(function(f){"
198
  "function r(){try{var d=f.contentWindow.document;"
@@ -202,9 +202,10 @@ def wrap_html_in_iframe(html: str) -> str:
202
  "})(this)"
203
  )
204
  return f"""
205
- <div style="width:100%;border:1px solid #ddd;border-radius:8px;overflow:hidden;">
206
  <iframe srcdoc="{escaped}"
207
- style="width:100%;border:none;height:400px;"
 
208
  sandbox="allow-scripts allow-same-origin"
209
  onload="{onload_js}"></iframe>
210
  </div>
@@ -316,79 +317,50 @@ def get_default_example():
316
 
317
 
318
  # Build Gradio UI
 
 
 
319
  with gr.Blocks(
320
  title="Compression-Lens: RWKV-7 vs Qwen3",
321
  theme=gr.themes.Soft(),
322
- css="""
323
- #input-text textarea {
324
- font-family: Consolas, 'Courier New', monospace;
325
- }
326
- .gr-accordion-content {
327
- max-height: none !important;
328
- height: auto !important;
329
- overflow: visible !important;
330
- }
331
- .gr-accordion-content > div {
332
- max-height: none !important;
333
- height: auto !important;
334
- overflow: visible !important;
335
- }
336
- .gr-accordion-content .prose,
337
- .gr-accordion-content .markdown,
338
- .gr-accordion-content .md {
339
- max-height: none !important;
340
- height: auto !important;
341
- overflow: visible !important;
342
- }
343
- #compression-metric .gr-accordion-content,
344
- #compression-metric .gr-accordion-content > div,
345
- #compression-metric .prose,
346
- #compression-metric .markdown,
347
- #compression-metric .md,
348
- #compression-metric * {
349
- max-height: none !important;
350
- overflow: visible !important;
351
- overflow-y: visible !important;
352
- overflow-x: visible !important;
353
- }
354
- """,
355
  ) as demo:
356
  gr.HTML(
357
  f"""
358
- <div style="text-align: center; margin-bottom: 20px;">
359
- <h1 style="margin-bottom: 10px;">🔬 Compression-Lens: RWKV-7 vs Qwen3 Byte-Level Comparison</h1>
360
- <p style="margin-bottom: 15px; color: #666;">Compare the byte-level prediction performance between <strong>{RWKV_MODEL_DISPLAY_NAME}</strong> and <strong>Qwen3-1.7B-Base</strong>.</p>
361
- <div style="display: flex; justify-content: center; align-items: center; gap: 10px;">
362
- <a href="https://github.com/Jellyfish042/uncheatable_eval" target="_blank" style="text-decoration: none;">
363
- <img src="https://img.shields.io/badge/GitHub-Project-181717?logo=github" alt="GitHub Project">
364
- </a>
365
- <a href="https://huggingface.co/spaces/Jellyfish042/UncheatableEval" target="_blank" style="text-decoration: none;">
366
- <img src="https://img.shields.io/badge/%F0%9F%8F%86%20Leaderboard-Gradio-ff7c00" alt="Leaderboard">
367
- </a>
368
- </div>
369
- </div>
370
- """
371
  )
372
 
373
- with gr.Row():
374
- with gr.Column(scale=1):
375
- text_input = gr.Textbox(
376
- label="Input Text",
377
- placeholder=f"Enter text to analyze (max {MAX_TEXT_LENGTH} characters)...",
378
- lines=10,
379
- max_lines=20,
380
- elem_id="input-text",
381
- )
382
-
383
- with gr.Row():
384
- clear_btn = gr.Button("Clear", variant="secondary")
385
- run_btn = gr.Button("▶ Run Comparison", variant="primary")
386
-
387
- gr.Markdown("---")
 
 
 
 
 
388
 
389
- with gr.Row():
390
- with gr.Column():
391
- output_html = gr.HTML(label="Visualization")
392
 
393
  with gr.Accordion("How to calculate compression rate?", open=False, elem_id="compression-metric"):
394
  gr.Markdown(
@@ -413,12 +385,17 @@ $$
413
  ],
414
  )
415
 
416
- # Event handlers
 
 
 
 
 
 
 
417
  clear_btn.click(fn=clear_inputs, outputs=[text_input, output_html])
418
-
419
  run_btn.click(fn=run_evaluation, inputs=[text_input], outputs=[output_html])
420
-
421
- # Load default example on page load
422
  demo.load(fn=get_default_example, outputs=[text_input, output_html])
423
 
424
 
 
190
 
191
  def wrap_html_in_iframe(html: str) -> str:
192
  """Wrap HTML in an iframe for Gradio display."""
193
+ from core.escaping import escape_attr
194
+
195
+ escaped = escape_attr(html)
196
  onload_js = (
197
  "(function(f){"
198
  "function r(){try{var d=f.contentWindow.document;"
 
202
  "})(this)"
203
  )
204
  return f"""
205
+ <div class="comparison-frame">
206
  <iframe srcdoc="{escaped}"
207
+ title="Byte-level model comparison"
208
+ style="display:block;width:100%;border:none;height:400px;"
209
  sandbox="allow-scripts allow-same-origin"
210
  onload="{onload_js}"></iframe>
211
  </div>
 
317
 
318
 
319
  # Build Gradio UI
320
+ _ASSETS_DIR = SCRIPT_DIR / "visualization" / "assets"
321
+ APP_CSS = "\n".join((_ASSETS_DIR / name).read_text(encoding="utf-8") for name in ("theme.css", "app.css"))
322
+
323
  with gr.Blocks(
324
  title="Compression-Lens: RWKV-7 vs Qwen3",
325
  theme=gr.themes.Soft(),
326
+ css=APP_CSS,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
327
  ) as demo:
328
  gr.HTML(
329
  f"""
330
+ <header class="project-header">
331
+ <h1>🔬 Compression-Lens: RWKV-7 vs Qwen3 Byte-Level Comparison</h1>
332
+ <p>Compare the byte-level prediction performance between <strong>{RWKV_MODEL_DISPLAY_NAME}</strong> and <strong>Qwen3-1.7B-Base</strong>.</p>
333
+ <div class="project-links">
334
+ <a href="https://github.com/Jellyfish042/uncheatable_eval" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/badge/GitHub-Project-181717?logo=github" alt="GitHub Project"></a>
335
+ <a href="https://huggingface.co/spaces/Jellyfish042/UncheatableEval" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/badge/%F0%9F%8F%86%20Leaderboard-Gradio-ff7c00" alt="Leaderboard"></a>
336
+ </div>
337
+ </header>
338
+ """,
339
+ elem_id="page-header",
 
 
 
340
  )
341
 
342
+ with gr.Group(elem_id="input-panel"):
343
+ with gr.Row(elem_id="input-heading"):
344
+ with gr.Column(scale=1, min_width=0):
345
+ gr.HTML("<h2>Input</h2>")
346
+ example_btn = gr.Button("Load example", elem_id="load-example", scale=0, min_width=120)
347
+
348
+ text_input = gr.Textbox(
349
+ label="Input",
350
+ show_label=False,
351
+ container=False,
352
+ placeholder=f"Enter text to compare (max {MAX_TEXT_LENGTH:,} characters)…",
353
+ lines=12,
354
+ max_lines=30,
355
+ elem_id="input-text",
356
+ )
357
+ with gr.Row(elem_id="input-actions"):
358
+ with gr.Column(scale=1, min_width=100):
359
+ input_count = gr.HTML(f'<span class="count">0 / {MAX_TEXT_LENGTH:,} characters</span>', elem_id="input-count")
360
+ clear_btn = gr.Button("Clear", elem_id="clear-button", scale=0, min_width=72)
361
+ run_btn = gr.Button("Run Comparison", variant="primary", elem_id="run-button", scale=0, min_width=146)
362
 
363
+ output_html = gr.HTML(label="Comparison", elem_id="comparison-output")
 
 
364
 
365
  with gr.Accordion("How to calculate compression rate?", open=False, elem_id="compression-metric"):
366
  gr.Markdown(
 
385
  ],
386
  )
387
 
388
+ # Keep evaluation callbacks unchanged; the counter runs entirely in the browser.
389
+ text_input.change(
390
+ fn=None,
391
+ inputs=[text_input],
392
+ outputs=[input_count],
393
+ js="(text) => '<span class=\"count\">' + Array.from(text || '').length.toLocaleString('en-US') + ' / 16,384 characters</span>'",
394
+ queue=False,
395
+ )
396
  clear_btn.click(fn=clear_inputs, outputs=[text_input, output_html])
397
+ example_btn.click(fn=get_default_example, outputs=[text_input, output_html])
398
  run_btn.click(fn=run_evaluation, inputs=[text_input], outputs=[output_html])
 
 
399
  demo.load(fn=get_default_example, outputs=[text_input, output_html])
400
 
401
 
demos/compression-lens-demo.html ADDED
The diff for this file is too large to render. See raw diff
 
precomputed/example_visualization.html CHANGED
The diff for this file is too large to render. See raw diff
 
requirements.txt CHANGED
@@ -10,6 +10,6 @@ tqdm>=4.65.0
10
  packaging
11
  rwkv>=0.8.0
12
  requests
13
- huggingface_hub
14
  accelerate
15
  python-dotenv
 
10
  packaging
11
  rwkv>=0.8.0
12
  requests
13
+ huggingface_hub<1.0
14
  accelerate
15
  python-dotenv
visualization/assets/app.css ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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; }
14
+ .gradio-container .project-header h1 { margin:0!important; color:var(--text)!important; font-size:24px!important; line-height:1.4!important; font-weight:650!important; letter-spacing:-.4px; }
15
+ .gradio-container .project-header p { margin:10px 0 14px!important; color:var(--secondary)!important; font-size:13px; }
16
+ .project-links { display:flex; justify-content:center; align-items:center; flex-wrap:wrap; gap:10px; }
17
+ .project-links a { display:inline-flex; align-items:center; }
18
+ .project-links img { display:block; height:20px; max-width:100%; margin:0!important; }
19
+ #input-panel { padding:20px!important; border:1px solid var(--border)!important; border-radius:14px!important; background:var(--surface)!important; box-shadow:var(--shadow); }
20
+ #input-panel .form, #input-panel .block { background:transparent; box-shadow:none; }
21
+ #input-heading, #input-actions { align-items:center; gap:8px; }
22
+ #input-heading { margin-bottom:12px; }
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; }
30
+ #input-count .count { color:var(--muted); font-size:12px; font-variant-numeric:tabular-nums; }
31
+ #load-example, #clear-button, #run-button { height:34px; min-height:34px; padding:0 14px; font-size:13px; font-weight:500; border-radius:8px; box-shadow:none; }
32
+ #load-example { background:transparent; border-color:transparent; color:var(--secondary); }
33
+ #clear-button { background:var(--raised); border:1px solid var(--strong); color:var(--text); }
34
+ #run-button { background:#2a78d6; border:1px solid #2a78d6; color:#fff; }
35
+ #run-button:hover { background:#216bc4; }
36
+ #clear-button:hover, #load-example:hover { border-color:var(--blue); background:var(--sunken); }
37
+ #comparison-output { padding:0!important; border:0!important; background:transparent!important; }
38
+ #comparison-output .html-container { padding:0; }
39
+ .comparison-frame { width:100%; overflow:hidden; border-radius:14px; }
40
+ .comparison-frame iframe { display:block; width:100%; border:0; height:400px; }
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; }
48
+ .project-header { padding-top:20px; }
49
+ .gradio-container .project-header h1 { font-size:20px!important; }
50
+ #input-panel { padding:15px!important; }
51
+ #input-actions { flex-wrap:wrap; }
52
+ }
visualization/assets/main.css CHANGED
@@ -1,268 +1,128 @@
1
- body {
2
- font-family: Consolas, 'Courier New', monospace;
3
- margin: 0;
4
- padding: 0;
5
- background-color: #f5f5f5;
6
- }
7
- .header {
8
- background-color: #333;
9
- color: white;
10
- padding: 20px;
11
- position: sticky;
12
- top: 0;
13
- z-index: 100;
14
- }
15
- .header h1 {
16
- margin: 0 0 15px 0;
17
- font-size: 18px;
18
- }
19
- .meta {
20
- display: flex;
21
- flex-wrap: wrap;
22
- gap: 20px;
23
- font-size: 12px;
24
- color: #c8c8c8;
25
- }
26
- .legend {
27
- display: flex;
28
- flex-direction: column;
29
- gap: 6px;
30
- margin-top: 10px;
31
- }
32
- .legend-row {
33
- display: flex;
34
- gap: 15px;
35
- align-items: center;
36
- flex-wrap: wrap;
37
- }
38
- .legend-item {
39
- display: flex;
40
- align-items: center;
41
- gap: 5px;
42
- }
43
- .legend-toggle label {
44
- display: inline-flex;
45
- align-items: center;
46
- gap: 4px;
47
- color: #c8c8c8;
48
- font-size: 12px;
49
- }
50
- .legend-toggle input[type="radio"] {
51
- margin: 0 2px 0 6px;
52
- }
53
- .legend-box {
54
- width: 20px;
55
- height: 12px;
56
- border: 1px solid #666;
57
- }
58
- .content {
59
- background-color: white;
60
- margin: 10px;
61
- padding: 15px;
62
- border: 1px solid #ccc;
63
- font-size: 14px;
64
- line-height: 1.8;
65
- white-space: pre-wrap;
66
- overflow-wrap: anywhere;
67
- word-wrap: break-word;
68
- position: relative;
69
- }
70
- .content span {
71
- padding: 1px 0;
72
- }
73
- .word {
74
- cursor: pointer;
75
- position: relative;
76
- }
77
- .word:hover {
78
- outline: 2px solid #007bff;
79
- outline-offset: 1px;
80
- }
81
- .word.highlighted {
82
- outline: 2px solid #ff6b6b;
83
- outline-offset: 1px;
84
- }
85
- #svg-overlay {
86
- position: fixed;
87
- top: 0;
88
- left: 0;
89
- width: 100%;
90
- height: 100%;
91
- pointer-events: none;
92
- z-index: 1000;
93
- }
94
- .link-line {
95
- stroke: #007bff;
96
- stroke-width: 2;
97
- fill: none;
98
- opacity: 0.7;
99
- }
100
- .link-dot {
101
- fill: #007bff;
102
- opacity: 0.8;
103
- }
104
- .token {
105
- position: relative;
106
- cursor: help;
107
- }
108
- .token:hover {
109
- outline: 1px dashed #666;
110
- }
111
- .token-kind-control {
112
- color: #f59e0b;
113
- }
114
- .token-kind-raw {
115
- color: #fb7185;
116
- }
117
- #tooltip {
118
- position: fixed;
119
- background-color: rgba(0, 0, 0, 0.9);
120
- color: #e5e7eb;
121
- padding: 10px 14px;
122
- border-radius: 6px;
123
- font-size: 12px;
124
- max-width: none;
125
- width: max-content;
126
- z-index: 2000;
127
- pointer-events: none;
128
- display: none;
129
- line-height: 1.6;
130
- box-shadow: 0 2px 10px rgba(0,0,0,0.3);
131
- }
132
- #tooltip .label {
133
- color: #9ca3af;
134
- font-weight: bold;
135
- }
136
- #tooltip .bytes {
137
- color: #a5f3fc;
138
- font-family: monospace;
139
- }
140
- #tooltip .loss-a {
141
- color: #fbbf24;
142
- font-family: monospace;
143
- }
144
- #tooltip .loss-b {
145
- color: #60a5fa;
146
- font-family: monospace;
147
- }
148
- #tooltip .model-a {
149
- color: #fbbf24;
150
- }
151
- #tooltip .model-b {
152
- color: #60a5fa;
153
- }
154
- #tooltip .topk-section {
155
- margin-top: 8px;
156
- padding-top: 8px;
157
- border-top: 1px solid #555;
158
- }
159
- #tooltip .topk-container {
160
- display: flex;
161
- gap: 16px;
162
- align-items: flex-start;
163
- }
164
- #tooltip .topk-column {
165
- flex: 0 0 auto;
166
- min-width: 180px;
167
- }
168
- #tooltip .topk-title {
169
- color: #9ca3af;
170
- font-weight: bold;
171
- margin-bottom: 4px;
172
- font-size: 11px;
173
- }
174
- #tooltip .topk-title.model-a {
175
- color: #fbbf24;
176
- }
177
- #tooltip .topk-title.model-b {
178
- color: #60a5fa;
179
- }
180
- #tooltip .topk-list {
181
- font-size: 11px;
182
- }
183
- #tooltip .topk-item {
184
- display: flex;
185
- gap: 4px;
186
- padding: 1px 0;
187
- align-items: flex-start;
188
- }
189
- #tooltip .token-block {
190
- margin-top: 6px;
191
- display: flex;
192
- align-items: center;
193
- gap: 6px;
194
- white-space: nowrap;
195
- flex-wrap: nowrap;
196
- overflow-x: visible;
197
- }
198
- #tooltip .token-chips {
199
- display: flex;
200
- flex-wrap: nowrap;
201
- gap: 15px;
202
- align-items: center;
203
- flex: 0 0 auto;
204
- }
205
- #tooltip .token-chip-group {
206
- display: inline-flex;
207
- align-items: center;
208
- gap: 4px;
209
- flex: 0 0 auto;
210
- white-space: nowrap;
211
- }
212
- #tooltip .token-prob {
213
- color: #a7f3d0;
214
- font-family: monospace;
215
- font-size: 11px;
216
- white-space: nowrap;
217
- }
218
- #tooltip .token-id {
219
- color: #9ca3af;
220
- font-family: monospace;
221
- white-space: nowrap;
222
- }
223
- #tooltip .token-chip {
224
- max-width: 100%;
225
- }
226
- #tooltip .token-chip-group .topk-token {
227
- white-space: pre;
228
- overflow-wrap: normal;
229
- word-break: normal;
230
- }
231
- #tooltip .topk-rank {
232
- color: #6b7280;
233
- min-width: 18px;
234
- }
235
- #tooltip .topk-rank.hit {
236
- color: #22c55e;
237
- }
238
- #tooltip .topk-token {
239
- color: #e5e7eb;
240
- white-space: pre-wrap;
241
- overflow-wrap: anywhere;
242
- word-break: break-word;
243
- font-family: monospace;
244
- background-color: rgba(255, 255, 255, 0.08);
245
- padding: 0 4px;
246
- border-radius: 3px;
247
- display: inline-block;
248
- max-width: 100%;
249
- }
250
- #tooltip .esc-control {
251
- color: #fbbf24;
252
- }
253
- #tooltip .esc-raw {
254
- color: #fb7185;
255
- }
256
- #tooltip .topk-prob {
257
- color: #a7f3d0;
258
- min-width: 45px;
259
- text-align: right;
260
- }
261
- #tooltip .topk-hit {
262
- color: #22c55e;
263
- }
264
- #tooltip .topk-miss {
265
- color: #f87171;
266
- font-style: italic;
267
- }
268
-
 
1
+ * { box-sizing:border-box; }
2
+ html { scrollbar-gutter:stable; }
3
+ [hidden] { display:none!important; }
4
+ body { margin:0; background:var(--page); color:var(--text); font:14px/1.5 var(--sans); -webkit-font-smoothing:antialiased; }
5
+ button,input,textarea { font:inherit; }
6
+ button { height:34px; padding:0 14px; color:var(--text); font-size:13px; font-weight:500; background:var(--raised); border:1px solid var(--strong); border-radius:8px; cursor:pointer; }
7
+ button:hover { background:var(--sunken); border-color:var(--blue); }
8
+ button:focus-visible,a:focus-visible,summary:focus-visible { outline:3px solid var(--focus); outline-offset:2px; }
9
+ button.primary { color:#fff; background:#2a78d6; border-color:#2a78d6; min-width:146px; }
10
+ button.primary:hover { background:#216bc4; }
11
+ button.ghost { background:transparent; border-color:transparent; color:var(--secondary); }
12
+ a { color:var(--secondary); text-decoration:none; }
13
+ a:hover { color:var(--blue); }
14
+ .container { width:min(1200px,calc(100% - 48px)); margin-inline:auto; }
15
+ .project-header { padding-top:28px; text-align:center; }
16
+ .project-header h1 { margin:0; font-size:24px; line-height:1.4; font-weight:650; letter-spacing:-.4px; }
17
+ .project-header p { margin:10px 0 14px; color:var(--secondary); font-size:13px; }
18
+ .project-links { display:flex; justify-content:center; align-items:center; flex-wrap:wrap; gap:10px; }
19
+ .project-links a { display:inline-flex; align-items:center; }
20
+ .project-links img { display:block; height:20px; max-width:100%; }
21
+ .layout { display:flex; flex-direction:column; gap:20px; padding-block:24px 32px; }
22
+ .card { background:var(--surface); border:1px solid var(--border); border-radius:14px; box-shadow:var(--shadow); padding:20px; }
23
+ .card-head { display:flex; align-items:center; justify-content:space-between; gap:12px; margin-bottom:12px; }
24
+ .card-head h1,.card-head h2 { margin:0; font-size:12px; font-weight:650; letter-spacing:.06em; text-transform:uppercase; color:var(--secondary); }
25
+ #input-text { display:block; width:100%; min-height:272px; height:272px; resize:vertical; padding:12px 14px; font:13.5px/1.65 var(--mono); border:1px solid var(--strong); border-radius:8px; background:var(--raised); color:var(--text); }
26
+ #input-text:focus { outline:none; border-color:var(--blue); box-shadow:0 0 0 3px var(--focus); }
27
+ .prompt-actions { display:flex; align-items:center; gap:8px; margin-top:12px; }
28
+ .prompt-actions .count { margin-right:auto; color:var(--muted); font-size:12px; font-variant-numeric:tabular-nums; }
29
+ .output-actions { display:flex; align-items:center; gap:12px; }
30
+ .output-actions button { height:29px; padding:0 10px; font-size:12px; }
31
+ .preview-label { font-size:11px; color:var(--muted); }
32
+ .stats { display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:12px; margin-bottom:16px; }
33
+ .stat { padding:12px 14px; background:var(--raised); border:1px solid var(--border); border-radius:10px; }
34
+ .stat .label { font-size:12px; color:var(--secondary); }
35
+ .stat .value { font-size:22px; font-weight:600; letter-spacing:-.02em; margin-top:3px; font-variant-numeric:tabular-nums; }
36
+ .stat .value small { font-size:12px; font-weight:400; letter-spacing:0; color:var(--muted); margin-left:4px; }
37
+ .toolbar { display:flex; flex-wrap:wrap; align-items:center; gap:12px 20px; margin-bottom:16px; }
38
+ .control { display:flex; align-items:center; gap:9px; font-size:12px; color:var(--secondary); }
39
+ .segmented { display:inline-flex; gap:2px; padding:3px; border-radius:10px; background:var(--chip); }
40
+ .segmented label { position:relative; display:inline-flex; align-items:center; justify-content:center; min-height:28px; padding:3px 12px; border-radius:7px; font-size:12px; color:var(--secondary); cursor:pointer; }
41
+ .segmented input { position:absolute; opacity:0; width:1px; height:1px; }
42
+ .segmented label:has(input:checked) { background:var(--raised); color:var(--text); box-shadow:0 1px 2px #0000001a,0 0 0 1px var(--border); }
43
+ .segmented label:has(input:focus-visible) { outline:3px solid var(--focus); }
44
+ #color-range-slider { width:122px; accent-color:#2a78d6; height:18px; margin:0; }
45
+ #color-range-value { min-width:39px; font-size:11px; color:var(--muted); font-variant-numeric:tabular-nums; }
46
+ .legend { display:flex; align-items:center; flex-wrap:wrap; gap:7px 12px; color:var(--secondary); font-size:11px; margin-left:auto; }
47
+ .legend-item { display:inline-flex; align-items:center; gap:5px; }
48
+ .legend-box { width:11px; height:8px; border-radius:2px; border:1px solid var(--border); }
49
+ .legend-box.better { background:color-mix(in srgb,rgb(77,255,77) 60%,var(--raised)); }
50
+ .legend-box.equal { background:var(--raised); }
51
+ .legend-box.worse { background:color-mix(in srgb,rgb(255,77,77) 60%,var(--raised)); }
52
+ .doc-frame { background:var(--raised); border:1px solid var(--strong); border-radius:12px; overflow:hidden; }
53
+ .content { padding:18px 20px; min-height:220px; font:14.5px/2 var(--mono); white-space:pre-wrap; overflow-wrap:anywhere; position:relative; }
54
+ /* Soften the existing colors without changing how deltas are calculated. */
55
+ .content .token { position:relative; cursor:help; padding:2px 0; border-radius:3px; background-color:color-mix(in srgb,var(--heat,var(--raised)) 60%,var(--raised))!important; box-decoration-break:clone; -webkit-box-decoration-break:clone; }
56
+ .content .token:hover { outline:1.5px solid var(--blue); outline-offset:1px; }
57
+ .content .token.pinned-token { outline:2px solid var(--blue); outline-offset:1px; }
58
+ .content .word { cursor:pointer; }
59
+ .content .word.highlighted { outline:1px dashed var(--blue); outline-offset:1px; }
60
+ .content .word.highlighted:hover { outline:1.5px solid var(--blue); }
61
+ .token-kind-control { color:var(--orange); }
62
+ .token-kind-raw { color:var(--red); }
63
+ .doc-bar { display:flex; flex-wrap:wrap; align-items:center; justify-content:space-between; gap:8px; padding:10px 16px; color:var(--muted); background:var(--sunken); border-top:1px solid var(--border); font-size:11.5px; }
64
+ #svg-overlay { position:fixed; inset:0; width:100%; height:100%; pointer-events:none; z-index:1000; }
65
+ .link-line { stroke:var(--blue); stroke-width:1.4; fill:none; opacity:.65; }
66
+ .link-dot { fill:var(--blue); opacity:.8; }
67
+ /* Original hover-panel markup: both probability tables remain visible together. */
68
+ #tooltip { display:none; position:fixed; z-index:2000; pointer-events:none; width:max-content; max-width:min(900px,calc(100vw - 24px)); max-height:calc(100vh - 24px); overflow:auto; background:var(--raised); color:var(--text); border:1px solid var(--strong); border-radius:12px; box-shadow:var(--float-shadow); padding:12px 14px; font:12px/1.65 var(--sans); }
69
+ #tooltip>div:first-child { background:var(--sunken); border:1px solid var(--border); border-radius:6px; padding:5px 8px; margin-bottom:8px; overflow-wrap:anywhere; }
70
+ #tooltip.pinned { pointer-events:auto; border-color:var(--blue); }
71
+ #tooltip.pinned:after { content:"Pinned · Click the token again, click outside, or press Esc to close"; display:block; margin-top:10px; padding-top:8px; border-top:1px solid var(--grid); font:11px/1.5 var(--sans); color:var(--muted); }
72
+ #tooltip .label { color:var(--secondary); font-weight:500; }
73
+ #tooltip .bytes { color:var(--muted); font:11px var(--mono); }
74
+ #tooltip .loss-a,#tooltip .loss-b { font:11px/1.7 var(--mono); overflow-wrap:anywhere; }
75
+ #tooltip .loss-a,#tooltip .model-a { color:var(--blue); }
76
+ #tooltip .loss-b,#tooltip .model-b { color:var(--orange); }
77
+ #tooltip hr { border:0!important; border-top:1px solid var(--grid)!important; margin:9px 0!important; }
78
+ #tooltip .token-block { display:flex; align-items:baseline; flex-wrap:wrap; gap:6px; margin-top:5px; }
79
+ #tooltip .token-chips { display:flex; flex-wrap:wrap; gap:6px 13px; align-items:baseline; }
80
+ #tooltip .token-chip-group { display:inline-flex; flex-wrap:wrap; align-items:baseline; gap:4px; }
81
+ #tooltip .token-id { font:10px var(--mono); color:var(--muted); }
82
+ #tooltip .token-prob { font:11px var(--mono); color:var(--green); }
83
+ #tooltip .token-chip-group .topk-token { white-space:pre-wrap; }
84
+ #tooltip .topk-section { margin-top:10px; border-top:1px solid var(--grid); padding-top:10px; }
85
+ #tooltip .topk-container { display:flex; align-items:stretch; gap:24px; }
86
+ #tooltip .topk-column { position:relative; flex:1 1 0; min-width:220px; }
87
+ #tooltip .topk-column+.topk-column:before { content:""; position:absolute; top:0; bottom:0; left:-12px; width:1px; background:var(--grid); }
88
+ #tooltip .topk-title { font-size:11px; font-weight:600; margin-bottom:7px; }
89
+ #tooltip .topk-item { display:grid; grid-template-columns:23px minmax(65px,1fr) 66px 12px; align-items:baseline; gap:6px; padding:3px 0; font:11px/1.7 var(--mono); }
90
+ #tooltip .topk-rank { color:var(--muted); text-align:right; }
91
+ #tooltip .topk-token { color:var(--text); font-family:var(--mono); white-space:pre-wrap; overflow-wrap:anywhere; background:var(--chip); padding:1px 5px; border-radius:4px; justify-self:start; max-width:100%; }
92
+ #tooltip .topk-prob { text-align:right; color:var(--secondary); font-variant-numeric:tabular-nums; }
93
+ #tooltip .topk-hit,#tooltip .topk-rank.hit { color:var(--green); font-weight:600; }
94
+ #tooltip .topk-miss { display:block; color:var(--red); font-size:10px; font-style:italic; margin-top:4px; }
95
+ #tooltip .esc-control { color:var(--orange); }
96
+ #tooltip .esc-raw { color:var(--red); }
97
+ .method { padding-block:14px; }
98
+ .method summary { display:flex; align-items:center; justify-content:space-between; color:var(--secondary); font-size:12px; font-weight:500; cursor:pointer; list-style:none; }
99
+ .method summary::-webkit-details-marker { display:none; }
100
+ .method summary:after { content:"+"; font:16px var(--mono); color:var(--muted); }
101
+ .method[open] summary:after { content:"−"; }
102
+ .method p { color:var(--secondary); font-size:12px; line-height:1.8; margin:12px 0 0; }
103
+ .method .katex-display { overflow-x:auto; overflow-y:hidden; padding-block:8px; }
104
+ .method .katex { font-size:1.1em; }
105
+ #demo-status { color:var(--muted); font-size:12px; }
106
+ .footer { display:flex; justify-content:space-between; gap:12px; color:var(--muted); font-size:11px; padding-inline:2px; }
107
+ @media(max-width:1050px) { .legend { margin-left:0; flex-basis:100%; } }
108
+ @media(max-width:760px) {
109
+ .container { width:calc(100% - 28px); }
110
+ .project-header { padding-top:20px; }
111
+ .project-header h1 { font-size:20px; }
112
+ .layout { padding-top:16px; gap:16px; } .card { padding:15px; }
113
+ .stats { grid-template-columns:repeat(2,minmax(0,1fr)); gap:8px; }
114
+ .stat { padding:10px 12px; } .stat .value { font-size:20px; }
115
+ .stat:last-child { grid-column:1/-1; display:flex; justify-content:space-between; align-items:center; }
116
+ .stat:last-child .value { margin:0; font-size:18px; }
117
+ .toolbar { gap:10px 16px; } .content { padding:14px; font-size:13px; }
118
+ .doc-bar { padding:9px 12px; font-size:10.5px; }
119
+ .output-actions { gap:8px; } .preview-label { font-size:10px; }
120
+ .prompt-actions { flex-wrap:wrap; }
121
+ #tooltip { padding:10px; font-size:11px; }
122
+ #tooltip .topk-container { gap:16px; } #tooltip .topk-column { min-width:0; }
123
+ #tooltip .topk-column+.topk-column:before { left:-8px; }
124
+ #tooltip .topk-item { grid-template-columns:17px minmax(37px,1fr) 49px 8px; gap:3px; font-size:10px; }
125
+ #tooltip .topk-token { padding-inline:3px; } .footer { flex-wrap:wrap; }
126
+ }
127
+ .report { max-width:1200px; margin-inline:auto; }
128
+ .report-models { color:var(--muted); font-size:11px; margin:3px 0 0; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
visualization/assets/main.js CHANGED
@@ -1,4 +1,4 @@
1
- const svgOverlay = document.getElementById('svg-overlay');
2
  const content = document.querySelector('.content');
3
  const renderModelEl = document.getElementById('render-model');
4
  let renderModel = {};
@@ -181,17 +181,34 @@
181
  }
182
 
183
  words.forEach(word => {
184
- word.addEventListener('mouseenter', (e) => drawLines(word, e));
185
- word.addEventListener('mouseleave', clearLines);
186
  });
187
 
188
- window.addEventListener('scroll', clearLines);
189
 
190
  const tooltip = document.getElementById('tooltip');
191
  const tokenSpans = document.querySelectorAll('.token');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
192
 
193
  tokenSpans.forEach(token => {
194
- token.addEventListener('mouseenter', (e) => {
195
  const tokenIdx = parseInt(token.dataset.tokenIdx);
196
  const tokenInfo = (!isNaN(tokenIdx) && renderTokens[tokenIdx]) ? renderTokens[tokenIdx] : null;
197
  const bytes = (tokenInfo && tokenInfo.bytes_hex) ? tokenInfo.bytes_hex : '';
@@ -502,9 +519,9 @@
502
  }
503
 
504
  tooltip.style.display = 'block';
505
- });
506
 
507
- token.addEventListener('mousemove', (e) => {
508
  const tooltipRect = tooltip.getBoundingClientRect();
509
  const viewportWidth = window.innerWidth;
510
  const viewportHeight = window.innerHeight;
@@ -523,10 +540,32 @@
523
 
524
  tooltip.style.left = x + 'px';
525
  tooltip.style.top = y + 'px';
526
- });
 
 
 
527
 
528
  token.addEventListener('mouseleave', () => {
529
- tooltip.style.display = 'none';
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
530
  });
531
  });
532
 
@@ -614,6 +653,10 @@
614
  linebreakTokens.forEach(token => {
615
  token.style.backgroundColor = 'rgb(255, 255, 255)';
616
  });
 
 
 
 
617
  }
618
 
619
  function updateLegendText() {
@@ -649,4 +692,20 @@
649
  updateLegendText();
650
  // Apply default color range on page load
651
  updateColors(10);
652
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const svgOverlay = document.getElementById('svg-overlay');
2
  const content = document.querySelector('.content');
3
  const renderModelEl = document.getElementById('render-model');
4
  let renderModel = {};
 
181
  }
182
 
183
  words.forEach(word => {
184
+ word.addEventListener('mouseenter', (e) => { if (!pinnedToken) drawLines(word, e); });
185
+ word.addEventListener('mouseleave', () => { if (!pinnedToken) clearLines(); });
186
  });
187
 
188
+ window.addEventListener('scroll', () => { if (pinnedToken) drawLines(pinnedToken); else clearLines(); });
189
 
190
  const tooltip = document.getElementById('tooltip');
191
  const tokenSpans = document.querySelectorAll('.token');
192
+ let pinnedToken = null;
193
+
194
+ function dismissTooltip() {
195
+ if (pinnedToken) pinnedToken.classList.remove('pinned-token');
196
+ pinnedToken = null;
197
+ tooltip.classList.remove('pinned');
198
+ tooltip.style.display = 'none';
199
+ clearLines();
200
+ }
201
+
202
+ document.addEventListener('click', (e) => {
203
+ if (!tooltip.contains(e.target) && !e.target.closest('.content .token')) dismissTooltip();
204
+ });
205
+ document.addEventListener('keydown', (e) => { if (e.key === 'Escape') dismissTooltip(); });
206
+ document.addEventListener('contextmenu', (e) => {
207
+ if (e.target.closest('.content .token, #tooltip')) e.preventDefault();
208
+ });
209
 
210
  tokenSpans.forEach(token => {
211
+ const showTooltip = () => {
212
  const tokenIdx = parseInt(token.dataset.tokenIdx);
213
  const tokenInfo = (!isNaN(tokenIdx) && renderTokens[tokenIdx]) ? renderTokens[tokenIdx] : null;
214
  const bytes = (tokenInfo && tokenInfo.bytes_hex) ? tokenInfo.bytes_hex : '';
 
519
  }
520
 
521
  tooltip.style.display = 'block';
522
+ };
523
 
524
+ const positionTooltip = (e) => {
525
  const tooltipRect = tooltip.getBoundingClientRect();
526
  const viewportWidth = window.innerWidth;
527
  const viewportHeight = window.innerHeight;
 
540
 
541
  tooltip.style.left = x + 'px';
542
  tooltip.style.top = y + 'px';
543
+ };
544
+
545
+ token.addEventListener('mouseenter', () => { if (!pinnedToken) showTooltip(); });
546
+ token.addEventListener('mousemove', (e) => { if (!pinnedToken) positionTooltip(e); });
547
 
548
  token.addEventListener('mouseleave', () => {
549
+ if (!pinnedToken) tooltip.style.display = 'none';
550
+ });
551
+
552
+ const togglePin = (e) => {
553
+ if (pinnedToken === token) { dismissTooltip(); return; }
554
+ if (pinnedToken) pinnedToken.classList.remove('pinned-token');
555
+ showTooltip();
556
+ positionTooltip(e);
557
+ pinnedToken = token;
558
+ token.classList.add('pinned-token');
559
+ tooltip.classList.add('pinned');
560
+ drawLines(token, e);
561
+ };
562
+
563
+ token.addEventListener('click', (e) => {
564
+ if (e.button === 0) togglePin(e);
565
+ });
566
+ token.addEventListener('contextmenu', (e) => {
567
+ e.preventDefault();
568
+ togglePin(e);
569
  });
570
  });
571
 
 
653
  linebreakTokens.forEach(token => {
654
  token.style.backgroundColor = 'rgb(255, 255, 255)';
655
  });
656
+ tokenSpans.forEach(token => {
657
+ const color = token.style.backgroundColor;
658
+ token.style.setProperty('--heat', color === 'rgb(255, 255, 255)' ? 'var(--raised)' : color);
659
+ });
660
  }
661
 
662
  function updateLegendText() {
 
692
  updateLegendText();
693
  // Apply default color range on page load
694
  updateColors(10);
695
+
696
+ const downloadButton = document.getElementById('download-comparison');
697
+ if (downloadButton) downloadButton.addEventListener('click', () => {
698
+ const page = document.documentElement.cloneNode(true);
699
+ page.querySelector('.content').replaceChildren();
700
+ const popup = page.querySelector('#tooltip');
701
+ popup.replaceChildren(); popup.style.display = 'none'; popup.classList.remove('pinned');
702
+ page.querySelector('#svg-overlay').replaceChildren();
703
+ page.querySelector('input[value="relative"]').setAttribute('checked', '');
704
+ page.querySelector('input[value="absolute"]').removeAttribute('checked');
705
+ page.querySelector('#color-range-slider').setAttribute('value', '10');
706
+ const url = URL.createObjectURL(new Blob(['<!doctype html>\n' + page.outerHTML], {type:'text/html;charset=utf-8'}));
707
+ const link = document.createElement('a');
708
+ link.href = url; link.download = 'compression-lens-comparison.html'; link.click();
709
+ setTimeout(() => URL.revokeObjectURL(url), 1000);
710
+ });
711
+
visualization/assets/theme.css ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ :root {
2
+ color-scheme: light;
3
+ --page:#f4f4f1; --surface:#fcfcfb; --raised:#fff; --sunken:#f6f6f3;
4
+ --text:#0b0b0b; --secondary:#52514e; --muted:#77756f; --grid:#e1e0d9;
5
+ --border:rgba(11,11,11,.09); --strong:rgba(11,11,11,.16); --chip:rgba(11,11,11,.055);
6
+ --blue:#2a78d6; --orange:#bc512a; --green:#23834b; --red:#c14747; --focus:rgba(42,120,214,.23);
7
+ --shadow:0 1px 2px #0000000a,0 4px 18px #0000000a; --float-shadow:0 10px 34px #00000029,0 2px 6px #0000000f;
8
+ --sans:system-ui,-apple-system,"Segoe UI",sans-serif;
9
+ --mono:ui-monospace,"Cascadia Mono","JetBrains Mono",Consolas,monospace;
10
+ }
11
+ @media(prefers-color-scheme:dark) {
12
+ :root {
13
+ color-scheme:dark;
14
+ --page:#0d0d0d; --surface:#1a1a19; --raised:#232322; --sunken:#151514;
15
+ --text:#fff; --secondary:#c3c2b7; --muted:#a09e95; --grid:#343431;
16
+ --border:rgba(255,255,255,.09); --strong:rgba(255,255,255,.18); --chip:rgba(255,255,255,.08);
17
+ --blue:#75acee; --orange:#f1a57e; --green:#92d4a8; --red:#f4a0a0; --focus:rgba(57,135,229,.35);
18
+ --shadow:0 1px 2px #0006; --float-shadow:0 10px 34px #0009;
19
+ }
20
+ }
visualization/html_generator.py CHANGED
@@ -5,21 +5,16 @@ Generates interactive HTML visualizations comparing byte-level losses between tw
5
  """
6
 
7
  import bisect
8
- import json
9
  import math
10
  import re
11
- from pathlib import Path
12
  from typing import List, Tuple, Optional, Set
13
 
14
  import numpy as np
15
 
16
- from core.escaping import escape_json_for_script
17
  from core.render_model import RenderModel, TokenInfo, build_display
18
- from visualization.render import render_page
19
  from core.helpers import TokenizerBytesConverter
20
 
21
- ASSETS_DIR = Path(__file__).resolve().parent / "assets"
22
-
23
 
24
  # Compression rate conversion factor
25
  COMPRESSION_RATE_FACTOR = (1.0 / math.log(2.0)) * 0.125 * 100.0
@@ -594,8 +589,6 @@ def generate_comparison_html(
594
  )
595
  )
596
 
597
- delta_color = "#64ff64" if avg_delta < 0 else "#ff6464"
598
-
599
  render_model = RenderModel(
600
  text=text,
601
  tokens=render_tokens,
@@ -610,62 +603,7 @@ def generate_comparison_html(
610
  "avg_delta_compression": avg_delta_compression,
611
  },
612
  )
613
- render_model_json = escape_json_for_script(render_model.to_dict())
614
-
615
- style_block = (ASSETS_DIR / "main.css").read_text(encoding="utf-8")
616
-
617
- header_html = f"""
618
- <div class="header">
619
- <div class="meta">
620
- <div>Model A: {model_a_name}</div>
621
- <div>Model B: {model_b_name}</div>
622
- <div>RWKV Compression: {avg_compression_a:.2f}%</div>
623
- <div>Qwen Compression: {avg_compression_b:.2f}%</div>
624
- <div style="color: {delta_color}">Avg Delta: {avg_delta_compression:+.2f}%</div>
625
- </div>
626
- <div class="legend">
627
- <div class="legend-row">
628
- <div class="legend-item legend-toggle">
629
- <span style="color: #aaa;">Coloring Mode:</span>
630
- <label><input type="radio" name="delta-mode" value="relative" checked> vs Avg Delta</label>
631
- <label><input type="radio" name="delta-mode" value="absolute"> Absolute</label>
632
- </div>
633
- <div class="legend-item">
634
- <span style="color: #aaa;">Color Range:</span>
635
- <input type="range" id="color-range-slider" min="0" max="100" value="10" step="0.1" style="width: 200px; vertical-align: middle;">
636
- <span id="color-range-value" style="color: #fff; min-width: 45px; display: inline-block;">10%</span>
637
- </div>
638
- </div>
639
- <div class="legend-row">
640
- <div class="legend-item">
641
- <div class="legend-box" style="background-color: rgb(77, 255, 77)"></div>
642
- <span id="legend-better">RWKV better than avg delta</span>
643
- </div>
644
- <div class="legend-item">
645
- <div class="legend-box" style="background-color: rgb(255, 255, 255)"></div>
646
- <span id="legend-equal">Equal to avg delta</span>
647
- </div>
648
- <div class="legend-item">
649
- <div class="legend-box" style="background-color: rgb(255, 77, 77)"></div>
650
- <span id="legend-worse">RWKV worse than avg delta</span>
651
- </div>
652
- </div>
653
- </div>
654
- </div>
655
- """.strip("\n")
656
-
657
- script_body = (ASSETS_DIR / "main.js").read_text(encoding="utf-8")
658
-
659
- html_doc = render_page(
660
- {
661
- "page_title": "Model Comparison",
662
- "style_block": style_block.strip("\n"),
663
- "header_html": header_html,
664
- "content_html": "",
665
- "render_model_json": render_model_json,
666
- "script_body": script_body.strip("\n"),
667
- }
668
- )
669
 
670
  if return_render_model:
671
  return html_doc, render_model.to_dict()
 
5
  """
6
 
7
  import bisect
 
8
  import math
9
  import re
 
10
  from typing import List, Tuple, Optional, Set
11
 
12
  import numpy as np
13
 
 
14
  from core.render_model import RenderModel, TokenInfo, build_display
15
+ from visualization.render import render_comparison_page
16
  from core.helpers import TokenizerBytesConverter
17
 
 
 
18
 
19
  # Compression rate conversion factor
20
  COMPRESSION_RATE_FACTOR = (1.0 / math.log(2.0)) * 0.125 * 100.0
 
589
  )
590
  )
591
 
 
 
592
  render_model = RenderModel(
593
  text=text,
594
  tokens=render_tokens,
 
603
  "avg_delta_compression": avg_delta_compression,
604
  },
605
  )
606
+ html_doc = render_comparison_page(render_model.to_dict())
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
607
 
608
  if return_render_model:
609
  return html_doc, render_model.to_dict()
visualization/render.py CHANGED
@@ -4,13 +4,35 @@ Page rendering using a lightweight template.
4
 
5
  from pathlib import Path
6
  from string import Template
7
- from typing import Dict
 
 
8
 
9
 
10
  _TEMPLATE_PATH = Path(__file__).resolve().parent / "templates" / "page.html.tmpl"
 
11
 
12
 
13
  def render_page(context: Dict[str, str]) -> str:
14
  template_text = _TEMPLATE_PATH.read_text(encoding="utf-8")
15
  template = Template(template_text)
16
  return template.safe_substitute(context)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
  from pathlib import Path
6
  from string import Template
7
+ from typing import Any, Dict
8
+
9
+ from core.escaping import escape_json_for_script, escape_text_node
10
 
11
 
12
  _TEMPLATE_PATH = Path(__file__).resolve().parent / "templates" / "page.html.tmpl"
13
+ _ASSETS_DIR = Path(__file__).resolve().parent / "assets"
14
 
15
 
16
  def render_page(context: Dict[str, str]) -> str:
17
  template_text = _TEMPLATE_PATH.read_text(encoding="utf-8")
18
  template = Template(template_text)
19
  return template.safe_substitute(context)
20
+
21
+
22
+ def render_comparison_page(model: Dict[str, Any]) -> str:
23
+ """Render the current presentation without changing evaluation data."""
24
+ meta = model["meta"]
25
+ return render_page(
26
+ {
27
+ "page_title": "Compression-Lens: Model Comparison",
28
+ "model_a": escape_text_node(meta["model_a"]),
29
+ "model_b": escape_text_node(meta["model_b"]),
30
+ "rwkv_compression": f'{meta["avg_compression"]["rwkv"]:.2f}',
31
+ "qwen_compression": f'{meta["avg_compression"]["qwen"]:.2f}',
32
+ "mean_difference": f'{meta["avg_delta_compression"]:+.2f}',
33
+ "document_meta": f'{len(model["text"].encode("utf-8")):,} bytes · {len(model["tokens"]):,} segments',
34
+ "style_block": "\n".join((_ASSETS_DIR / name).read_text(encoding="utf-8") for name in ("theme.css", "main.css")),
35
+ "render_model_json": escape_json_for_script(model),
36
+ "script_body": (_ASSETS_DIR / "main.js").read_text(encoding="utf-8"),
37
+ }
38
+ )
visualization/templates/page.html.tmpl CHANGED
@@ -1,17 +1,42 @@
1
- <!DOCTYPE html>
2
- <html>
3
  <head>
4
- <meta charset="UTF-8">
 
5
  <title>$page_title</title>
6
  <style>
7
  $style_block
8
  </style>
9
  </head>
10
  <body>
11
- <svg id="svg-overlay"></svg>
12
- <div id="tooltip"></div>
13
- $header_html
14
- <div class="content">$content_html</div>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  <script id="render-model" type="application/json">$render_model_json</script>
16
  <script>
17
  $script_body
 
1
+ <!doctype html>
2
+ <html lang="en">
3
  <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
  <title>$page_title</title>
7
  <style>
8
  $style_block
9
  </style>
10
  </head>
11
  <body>
12
+ <main class="report">
13
+ <section class="card comparison-card">
14
+ <div class="card-head">
15
+ <div><h2>Comparison</h2><p class="report-models">$model_a × $model_b</p></div>
16
+ <div class="output-actions"><button id="download-comparison">Download HTML</button></div>
17
+ </div>
18
+ <div class="stats">
19
+ <div class="stat"><div class="label">RWKV compression rate</div><div class="value">$rwkv_compression<small>%</small></div></div>
20
+ <div class="stat"><div class="label">Qwen compression rate</div><div class="value">$qwen_compression<small>%</small></div></div>
21
+ <div class="stat"><div class="label">Mean difference · RWKV − Qwen</div><div class="value">$mean_difference<small>pp</small></div></div>
22
+ </div>
23
+ <div class="toolbar">
24
+ <div class="control"><span>Color by</span><div class="segmented" role="group" aria-label="Coloring mode"><label><input type="radio" name="delta-mode" value="relative" checked>vs Avg Delta</label><label><input type="radio" name="delta-mode" value="absolute">Absolute</label></div></div>
25
+ <label class="control" for="color-range-slider">Color range<input type="range" id="color-range-slider" min="0" max="100" value="10" step="0.1"><span id="color-range-value">10.0%</span></label>
26
+ <div class="legend">
27
+ <span class="legend-item"><i class="legend-box better"></i><span id="legend-better"></span></span>
28
+ <span class="legend-item"><i class="legend-box equal"></i><span id="legend-equal"></span></span>
29
+ <span class="legend-item"><i class="legend-box worse"></i><span id="legend-worse"></span></span>
30
+ </div>
31
+ </div>
32
+ <div class="doc-frame">
33
+ <div class="content" aria-label="Byte-level comparison"></div>
34
+ <div class="doc-bar"><span>$document_meta</span><span>Hover for predictions · Click to pin</span></div>
35
+ </div>
36
+ </section>
37
+ </main>
38
+ <svg id="svg-overlay" aria-hidden="true"></svg>
39
+ <div id="tooltip" role="tooltip"></div>
40
  <script id="render-model" type="application/json">$render_model_json</script>
41
  <script>
42
  $script_body