luancy1208 commited on
Commit
5d19eec
·
verified ·
1 Parent(s): 2db6ea6

Delete chip-space/app.py

Browse files
Files changed (1) hide show
  1. chip-space/app.py +0 -665
chip-space/app.py DELETED
@@ -1,665 +0,0 @@
1
- """
2
- app.py — CHIP HuggingFace Space 入口(美观版)
3
-
4
- 部署: https://huggingface.co/spaces/<user>/CHIP
5
- """
6
- from __future__ import annotations
7
-
8
- import os
9
- import sys
10
- from pathlib import Path
11
-
12
- sys.path.insert(0, str(Path(__file__).parent))
13
-
14
- import gradio as gr
15
- import tiktoken
16
-
17
- from chip import Compressor
18
-
19
- # ============================================================
20
- # Tokenizer 计数器
21
- # ============================================================
22
- TOKENIZERS = {}
23
- try:
24
- TOKENIZERS["GPT-4 (cl100k)"] = tiktoken.get_encoding("cl100k_base")
25
- TOKENIZERS["GPT-4o (o200k)"] = tiktoken.get_encoding("o200k_base")
26
- except Exception as e:
27
- print(f"[warn] tiktoken load failed: {e}")
28
-
29
-
30
- def _lazy_load_qwen():
31
- from transformers import AutoTokenizer
32
- return AutoTokenizer.from_pretrained("Qwen/Qwen2.5-7B", trust_remote_code=True)
33
-
34
-
35
- def _lazy_load_deepseek():
36
- from transformers import AutoTokenizer
37
- return AutoTokenizer.from_pretrained("deepseek-ai/DeepSeek-V2-Lite", trust_remote_code=True)
38
-
39
-
40
- _LAZY = {
41
- "Qwen2.5": _lazy_load_qwen,
42
- "DeepSeek-V2/V3": _lazy_load_deepseek,
43
- }
44
-
45
-
46
- def count_tokens(text: str, tokenizer_name: str) -> int:
47
- if tokenizer_name in TOKENIZERS:
48
- enc = TOKENIZERS[tokenizer_name]
49
- elif tokenizer_name in _LAZY:
50
- try:
51
- tok = _LAZY[tokenizer_name]()
52
- TOKENIZERS[tokenizer_name] = tok
53
- enc = tok
54
- except Exception:
55
- return -1
56
- else:
57
- return -1
58
-
59
- if hasattr(enc, "encode") and not hasattr(enc, "tokenize"):
60
- return len(enc.encode(text))
61
- return len(enc.encode(text, add_special_tokens=False))
62
-
63
-
64
- # ============================================================
65
- # 主压缩函数
66
- # ============================================================
67
- def run(text: str, target: str, use_l1: bool, use_l2: bool, use_l3: bool,
68
- use_l4: bool, tokenizer_name: str, use_jieba: bool):
69
- if not text.strip():
70
- empty_card = "<div class='stat-card empty'>👈 请在左侧输入 prompt</div>"
71
- return "", empty_card, "", ""
72
-
73
- layers = []
74
- for flag, name in [(use_l1, "L1"), (use_l2, "L2"), (use_l3, "L3"), (use_l4, "L4")]:
75
- if flag:
76
- layers.append(name)
77
- if not layers:
78
- layers = ["L1"]
79
-
80
- if use_jieba:
81
- os.environ["CHIP_USE_JIEBA"] = "1"
82
- else:
83
- os.environ.pop("CHIP_USE_JIEBA", None)
84
- import chip.compressor as cc
85
- cc._default_compressor = None
86
-
87
- compressor = Compressor(target=target, layers=layers)
88
- result = compressor.compress(text)
89
-
90
- n_orig = count_tokens(text, tokenizer_name)
91
- n_comp = count_tokens(result.compressed, tokenizer_name)
92
-
93
- # ---- 漂亮的统计卡片 ----
94
- if n_orig > 0 and n_comp > 0:
95
- saving_pct = (1 - n_comp / n_orig) * 100
96
- n_diff = n_orig - n_comp
97
- if saving_pct > 0:
98
- badge_class = "saving-badge good"
99
- arrow = "↓"
100
- verb = "节省"
101
- elif saving_pct < 0:
102
- badge_class = "saving-badge bad"
103
- arrow = "↑"
104
- verb = "增加"
105
- else:
106
- badge_class = "saving-badge neutral"
107
- arrow = "→"
108
- verb = "持平"
109
-
110
- char_orig, char_comp = len(text), len(result.compressed)
111
- char_pct = (1 - char_comp / max(char_orig, 1)) * 100
112
-
113
- stats_html = f"""
114
- <div class="stats-grid">
115
- <div class="stat-card primary">
116
- <div class="stat-label">Token 节省</div>
117
- <div class="stat-value {badge_class}">
118
- <span class="arrow">{arrow}</span> {abs(saving_pct):.1f}%
119
- </div>
120
- <div class="stat-sub">{verb} {abs(n_diff)} token · 在 {tokenizer_name}</div>
121
- </div>
122
- <div class="stat-card">
123
- <div class="stat-label">原文</div>
124
- <div class="stat-value muted">{n_orig}</div>
125
- <div class="stat-sub">token · {char_orig} 字符</div>
126
- </div>
127
- <div class="stat-card">
128
- <div class="stat-label">压缩后</div>
129
- <div class="stat-value emphasis">{n_comp}</div>
130
- <div class="stat-sub">token · {char_comp} 字符</div>
131
- </div>
132
- <div class="stat-card">
133
- <div class="stat-label">字符压缩</div>
134
- <div class="stat-value muted">{char_pct:+.0f}%</div>
135
- <div class="stat-sub">字符数变化(供参考,token 才重要)</div>
136
- </div>
137
- </div>"""
138
- else:
139
- stats_html = (
140
- "<div class='stat-card empty'>"
141
- "⚠️ Token 计数不可用(可能是 tokenizer 加载失败)"
142
- "</div>"
143
- )
144
-
145
- # ---- 漂亮的规则面板 ----
146
- if result.applied_rules:
147
- rules_items = []
148
- for r in result.applied_rules:
149
- # 解析 rule id 和命中次数
150
- parts = r.split("×")
151
- rid = parts[0]
152
- count = parts[1] if len(parts) > 1 else "1"
153
- # 按层着色
154
- layer_color = {"L1": "#3B7DD8", "L2": "#E6883C", "L3": "#7C3AED", "L4": "#10B981"}
155
- color = "#888"
156
- for l, c in layer_color.items():
157
- if l in rid:
158
- color = c
159
- break
160
- badge = (
161
- f"<span class='rule-pill' style='--rule-color:{color}'>"
162
- f"<span class='rule-id'>{rid}</span>"
163
- f"<span class='rule-count'>×{count}</span>"
164
- f"</span>"
165
- )
166
- rules_items.append(badge)
167
- rules_html = (
168
- "<div class='rules-header'>🎯 命中规则 "
169
- f"<span class='rules-count'>{len(result.applied_rules)} 条</span></div>"
170
- "<div class='rules-pills'>" + " ".join(rules_items) + "</div>"
171
- )
172
- else:
173
- rules_html = (
174
- "<div class='rules-header empty-rules'>"
175
- "📭 没有规则匹配 — 这段 prompt 已经够紧凑了"
176
- "</div>"
177
- )
178
-
179
- return result.compressed, stats_html, rules_html, result.compressed
180
-
181
-
182
- # ============================================================
183
- # 示例
184
- # ============================================================
185
- EXAMPLES = [
186
- [
187
- "请你帮我对下面这段文字进行一个全面的分析,如果可以的话麻烦你给出一些改进的建议",
188
- "qwen2.5", True, True, False, True, "GPT-4 (cl100k)", False,
189
- ],
190
- [
191
- "请你扮演一位资深 Python 工程师,对下面的代码进行 code review,并以 JSON 格式输出结果",
192
- "qwen2.5", True, True, False, True, "GPT-4 (cl100k)", True,
193
- ],
194
- [
195
- "因为最近在下雨,所以路面变得很滑,因此开车的时候需要特别注意安全",
196
- "qwen2.5", True, True, False, True, "GPT-4 (cl100k)", False,
197
- ],
198
- [
199
- "Role: 资深产品经理\n任务: 评估这个需求,大家都知道产品决策需要数据支持",
200
- "deepseek_v3", True, True, True, True, "GPT-4 (cl100k)", False,
201
- ],
202
- ]
203
-
204
-
205
- # ============================================================
206
- # 自定义 CSS — 让 demo 看起来不像默认的 gradio
207
- # ============================================================
208
- CUSTOM_CSS = """
209
- /* ===== 全局 ===== */
210
- .gradio-container {
211
- max-width: 1280px !important;
212
- margin: 0 auto !important;
213
- }
214
-
215
- /* ===== Hero 区 ===== */
216
- .hero {
217
- text-align: center;
218
- padding: 36px 16px 12px;
219
- background: linear-gradient(135deg, rgba(59,125,216,0.08) 0%, rgba(230,136,60,0.08) 100%);
220
- border-radius: 16px;
221
- margin-bottom: 24px;
222
- border: 1px solid rgba(0,0,0,0.05);
223
- }
224
- .hero h1 {
225
- font-size: 2.4em !important;
226
- margin: 0 !important;
227
- background: linear-gradient(135deg, #3B7DD8, #E6883C);
228
- -webkit-background-clip: text;
229
- -webkit-text-fill-color: transparent;
230
- background-clip: text;
231
- font-weight: 700 !important;
232
- letter-spacing: -0.02em;
233
- }
234
- .hero .tagline {
235
- font-size: 1.1em;
236
- color: #555;
237
- margin-top: 8px;
238
- }
239
- .hero .subtitle {
240
- font-size: 0.95em;
241
- color: #777;
242
- max-width: 760px;
243
- margin: 12px auto 0;
244
- line-height: 1.6;
245
- }
246
- .hero .badges {
247
- margin-top: 16px;
248
- display: flex;
249
- gap: 8px;
250
- justify-content: center;
251
- flex-wrap: wrap;
252
- }
253
- .hero .badge {
254
- display: inline-flex;
255
- align-items: center;
256
- padding: 4px 10px;
257
- background: rgba(255,255,255,0.7);
258
- border: 1px solid rgba(0,0,0,0.08);
259
- border-radius: 6px;
260
- font-size: 0.85em;
261
- color: #444;
262
- text-decoration: none;
263
- transition: all 0.2s;
264
- }
265
- .hero .badge:hover {
266
- transform: translateY(-1px);
267
- box-shadow: 0 2px 8px rgba(0,0,0,0.08);
268
- }
269
-
270
- /* ===== Key facts ===== */
271
- .key-facts {
272
- display: grid;
273
- grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
274
- gap: 12px;
275
- margin: 24px 0;
276
- }
277
- .fact {
278
- padding: 14px 18px;
279
- background: white;
280
- border-radius: 10px;
281
- border-left: 3px solid var(--fact-color, #3B7DD8);
282
- box-shadow: 0 1px 3px rgba(0,0,0,0.05);
283
- }
284
- .fact-label {
285
- font-size: 0.75em;
286
- text-transform: uppercase;
287
- letter-spacing: 0.06em;
288
- color: #888;
289
- font-weight: 600;
290
- }
291
- .fact-value {
292
- font-size: 1.5em;
293
- font-weight: 700;
294
- color: var(--fact-color, #3B7DD8);
295
- margin-top: 4px;
296
- }
297
- .fact-detail {
298
- font-size: 0.85em;
299
- color: #666;
300
- margin-top: 2px;
301
- }
302
-
303
- /* ===== 统计卡片 ===== */
304
- .stats-grid {
305
- display: grid;
306
- grid-template-columns: 2fr 1fr 1fr 1fr;
307
- gap: 12px;
308
- margin: 8px 0;
309
- }
310
- .stat-card {
311
- background: white;
312
- border-radius: 10px;
313
- padding: 14px 16px;
314
- border: 1px solid rgba(0,0,0,0.06);
315
- transition: all 0.2s;
316
- }
317
- .stat-card:hover {
318
- box-shadow: 0 2px 12px rgba(0,0,0,0.08);
319
- transform: translateY(-1px);
320
- }
321
- .stat-card.primary {
322
- background: linear-gradient(135deg, #FAFBFF, #F5F7FB);
323
- border: 1px solid #DDE5F0;
324
- }
325
- .stat-card.empty {
326
- text-align: center;
327
- padding: 28px;
328
- background: #F8F9FB;
329
- color: #888;
330
- border: 1px dashed #DDD;
331
- font-size: 0.95em;
332
- }
333
- .stat-label {
334
- font-size: 0.75em;
335
- text-transform: uppercase;
336
- letter-spacing: 0.06em;
337
- color: #888;
338
- font-weight: 600;
339
- }
340
- .stat-value {
341
- font-size: 1.6em;
342
- font-weight: 700;
343
- margin-top: 4px;
344
- display: flex;
345
- align-items: baseline;
346
- gap: 6px;
347
- }
348
- .stat-value.muted { color: #999; font-size: 1.4em; }
349
- .stat-value.emphasis { color: #10B981; font-size: 1.8em; }
350
- .saving-badge.good { color: #10B981; }
351
- .saving-badge.bad { color: #E6883C; }
352
- .saving-badge.neutral { color: #888; }
353
- .arrow { font-weight: 700; }
354
- .stat-sub {
355
- font-size: 0.78em;
356
- color: #777;
357
- margin-top: 2px;
358
- }
359
-
360
- /* ===== 规则面板 ===== */
361
- .rules-header {
362
- font-size: 0.95em;
363
- font-weight: 600;
364
- color: #444;
365
- margin: 16px 0 8px;
366
- display: flex;
367
- align-items: center;
368
- gap: 8px;
369
- }
370
- .rules-count {
371
- font-size: 0.78em;
372
- background: #EFF2F7;
373
- padding: 2px 10px;
374
- border-radius: 10px;
375
- color: #555;
376
- font-weight: 500;
377
- }
378
- .rules-pills {
379
- display: flex;
380
- gap: 6px;
381
- flex-wrap: wrap;
382
- padding: 4px 0;
383
- }
384
- .rule-pill {
385
- display: inline-flex;
386
- align-items: center;
387
- gap: 4px;
388
- padding: 4px 10px;
389
- background: white;
390
- border: 1px solid var(--rule-color);
391
- color: var(--rule-color);
392
- border-radius: 999px;
393
- font-size: 0.82em;
394
- font-family: 'JetBrains Mono', 'Fira Code', monospace;
395
- font-weight: 500;
396
- transition: all 0.2s;
397
- }
398
- .rule-pill:hover {
399
- background: var(--rule-color);
400
- color: white;
401
- }
402
- .rule-count {
403
- font-size: 0.85em;
404
- opacity: 0.8;
405
- }
406
- .empty-rules {
407
- font-style: italic;
408
- color: #888;
409
- background: #F8F9FB;
410
- padding: 14px;
411
- border-radius: 8px;
412
- border: 1px dashed #DDD;
413
- text-align: center;
414
- }
415
-
416
- /* ===== 主操作按钮 ===== */
417
- button.primary {
418
- background: linear-gradient(135deg, #3B7DD8 0%, #5B9BE5 100%) !important;
419
- border: none !important;
420
- font-size: 1.05em !important;
421
- letter-spacing: 0.02em !important;
422
- transition: all 0.2s !important;
423
- }
424
- button.primary:hover {
425
- transform: translateY(-1px);
426
- box-shadow: 0 4px 12px rgba(59,125,216,0.3) !important;
427
- }
428
-
429
- /* ===== Footer ===== */
430
- .footer {
431
- margin-top: 36px;
432
- padding: 20px 0;
433
- border-top: 1px solid rgba(0,0,0,0.06);
434
- color: #777;
435
- font-size: 0.88em;
436
- text-align: center;
437
- }
438
- .footer a { color: #3B7DD8; text-decoration: none; }
439
- .footer a:hover { text-decoration: underline; }
440
-
441
- /* ===== 暗色模式适配 ===== */
442
- .dark .hero {
443
- background: linear-gradient(135deg, rgba(59,125,216,0.15), rgba(230,136,60,0.15));
444
- border-color: rgba(255,255,255,0.05);
445
- }
446
- .dark .hero .tagline { color: #BBB; }
447
- .dark .hero .subtitle { color: #999; }
448
- .dark .hero .badge {
449
- background: rgba(255,255,255,0.05);
450
- border-color: rgba(255,255,255,0.1);
451
- color: #CCC;
452
- }
453
- .dark .stat-card { background: rgba(255,255,255,0.03); border-color: rgba(255,255,255,0.08); }
454
- .dark .stat-card.primary { background: rgba(59,125,216,0.08); }
455
- .dark .stat-card.empty { background: rgba(255,255,255,0.03); color: #888; border-color: rgba(255,255,255,0.1); }
456
- .dark .stat-sub { color: #888; }
457
- .dark .fact { background: rgba(255,255,255,0.03); }
458
- .dark .fact-detail { color: #888; }
459
- .dark .rules-count { background: rgba(255,255,255,0.06); color: #BBB; }
460
- .dark .rule-pill { background: rgba(255,255,255,0.03); }
461
- .dark .empty-rules { background: rgba(255,255,255,0.03); border-color: rgba(255,255,255,0.1); }
462
-
463
- /* ===== 响应式 ===== */
464
- @media (max-width: 768px) {
465
- .stats-grid { grid-template-columns: 1fr 1fr; }
466
- .key-facts { grid-template-columns: 1fr; }
467
- .hero h1 { font-size: 1.8em !important; }
468
- }
469
- """
470
-
471
-
472
- # ============================================================
473
- # UI
474
- # ============================================================
475
- HERO_HTML = """
476
- <div class="hero">
477
- <h1>🀄 CHIP</h1>
478
- <div class="tagline">Chinese High-density Instruction Protocol · 中文高密度提示协议</div>
479
- <div class="subtitle">
480
- 把啰嗦的中文 prompt 自动压成结构化高密度形式 — <strong>数据驱动,不是品味</strong>
481
- </div>
482
- <div class="badges">
483
- <a class="badge" href="https://github.com/luancy1208/CHIP" target="_blank">⭐ GitHub</a>
484
- <a class="badge" href="https://github.com/luancy1208/CHIP/blob/main/SPEC.md" target="_blank">📄 SPEC</a>
485
- <a class="badge" href="https://github.com/luancy1208/CHIP/tree/main/results" target="_blank">📊 实测数据</a>
486
- <span class="badge">Apache-2.0</span>
487
- <span class="badge">v0.2 · 23 tests passing</span>
488
- </div>
489
- </div>
490
-
491
- <div class="key-facts">
492
- <div class="fact" style="--fact-color:#3B7DD8">
493
- <div class="fact-label">实测数据</div>
494
- <div class="fact-value">1800+ 行</div>
495
- <div class="fact-detail">9 tokenizer × 200 句 FLORES-200</div>
496
- </div>
497
- <div class="fact" style="--fact-color:#10B981">
498
- <div class="fact-label">国产模型上中文</div>
499
- <div class="fact-value">省 12.5%</div>
500
- <div class="fact-detail">Baichuan2 token / 等价英文</div>
501
- </div>
502
- <div class="fact" style="--fact-color:#E6883C">
503
- <div class="fact-label">cl100k 上中文</div>
504
- <div class="fact-value">贵 73%</div>
505
- <div class="fact-detail">所以 CHIP 的主战场是国产模型</div>
506
- </div>
507
- <div class="fact" style="--fact-color:#7C3AED">
508
- <div class="fact-label">### 标签</div>
509
- <div class="fact-value">1 token</div>
510
- <div class="fact-detail">在所有 9 个 tokenizer 上 — 完爆方括号</div>
511
- </div>
512
- </div>
513
- """
514
-
515
-
516
- FOOTER_HTML = """
517
- <div class="footer">
518
- <p>
519
- 🀄 CHIP v0.2 · Built with care for Chinese prompt engineering ·
520
- <a href="https://github.com/luancy1208/CHIP" target="_blank">GitHub</a> ·
521
- <a href="https://github.com/luancy1208/CHIP/issues" target="_blank">反馈 Issue</a>
522
- </p>
523
- <p style="margin-top:8px; font-size:0.82em; opacity:0.7">
524
- 数据来源: FLORES-200 dev (n=200) · 200 个 HSK 5/6 高频成语 · 45 个常见标记符号 · 实测于 9 个 tokenizer
525
- </p>
526
- </div>
527
- """
528
-
529
-
530
- with gr.Blocks(
531
- title="CHIP — 中文高密度提示协议",
532
- theme=gr.themes.Soft(
533
- primary_hue="blue",
534
- secondary_hue="orange",
535
- neutral_hue="slate",
536
- font=["system-ui", "-apple-system", "Segoe UI", "Helvetica Neue", "sans-serif"],
537
- ),
538
- css=CUSTOM_CSS,
539
- ) as demo:
540
- gr.HTML(HERO_HTML)
541
-
542
- with gr.Row(equal_height=True):
543
- # ------- 输入侧 -------
544
- with gr.Column(scale=1):
545
- gr.Markdown("### 📝 输入 prompt")
546
- inp = gr.Textbox(
547
- show_label=False,
548
- lines=10,
549
- placeholder="把啰嗦的中文 prompt 粘贴进来,看看 CHIP 能压到多紧...\n\n💡 试试:'请你帮我对下面这段文字进行一个全面的分析,如果可以的话麻烦你给出一些建议'",
550
- container=False,
551
- )
552
-
553
- with gr.Accordion("⚙️ 高级设置", open=False):
554
- with gr.Row():
555
- target = gr.Dropdown(
556
- ["qwen2.5", "deepseek_v3", "glm4", "cl100k", "o200k"],
557
- value="qwen2.5",
558
- label="🎯 目标模型",
559
- info="影响 L3 成语压缩等 target-aware 决策",
560
- )
561
- tokenizer_name = gr.Dropdown(
562
- list(TOKENIZERS.keys()) + list(_LAZY.keys()),
563
- value="GPT-4 (cl100k)",
564
- label="🔢 计数 tokenizer",
565
- info="决定 token 数怎么算(国产 tokenizer 首次需下载)",
566
- )
567
-
568
- gr.Markdown("**压缩层** — 默认 L1+L2+L4(保险);L3 仅在国产模型上有意义")
569
- with gr.Row():
570
- use_l1 = gr.Checkbox(value=True, label="L1 词法",
571
- info="套话剪枝 (~1.3-1.5×)")
572
- use_l2 = gr.Checkbox(value=True, label="L2 句法",
573
- info="模式重排 (~2-3×)")
574
- use_l3 = gr.Checkbox(value=False, label="L3 成语",
575
- info="长描述→成语")
576
- use_l4 = gr.Checkbox(value=True, label="L4 协议",
577
- info="### 标签归一化")
578
-
579
- use_jieba = gr.Checkbox(
580
- value=False,
581
- label="🚀 jieba NP 增强模式",
582
- info="复杂角色提取场景效果更好(默认关闭)",
583
- )
584
-
585
- btn = gr.Button("🔥 压缩", variant="primary", size="lg",
586
- elem_classes="primary")
587
-
588
- # ------- 输出侧 -------
589
- with gr.Column(scale=1):
590
- gr.Markdown("### ✨ 压缩结果")
591
- try:
592
- out = gr.Textbox(
593
- show_label=False,
594
- lines=10,
595
- interactive=False,
596
- show_copy_button=True,
597
- container=False,
598
- )
599
- except TypeError:
600
- out = gr.Textbox(show_label=False, lines=10,
601
- interactive=False, container=False)
602
- stats_panel = gr.HTML(
603
- "<div class='stat-card empty'>👈 在左侧输入并点击压缩按钮</div>"
604
- )
605
- rules_panel = gr.HTML()
606
-
607
- # 隐藏的 textbox 用于触发示例(因为示例需要更新所有 input)
608
- hidden_dup = gr.Textbox(visible=False)
609
-
610
- gr.Examples(
611
- examples=EXAMPLES,
612
- inputs=[inp, target, use_l1, use_l2, use_l3, use_l4, tokenizer_name, use_jieba],
613
- label="📌 试试这些示例(点击自动填充并运行)",
614
- examples_per_page=4,
615
- )
616
-
617
- with gr.Accordion("📖 关于 CHIP / 协议设计要点", open=False):
618
- gr.Markdown("""
619
- **核心发现(基于 1800 行实测数据)**
620
-
621
- | Tokenizer | ZH/EN ratio | 中文相对英文 |
622
- |---|---|---|
623
- | **baichuan2** 🟦 | 0.875 | 中文省 12.5% |
624
- | **deepseek_v3** 🟦 | 0.916 | 中文省 8.4% |
625
- | **glm4** 🟦 | 0.924 | 中文省 7.6% |
626
- | qwen2.5/3 🟦 | 0.988 | 持平 |
627
- | **o200k** (GPT-4o) 🟧 | 1.163 | 中文贵 16.3% |
628
- | **cl100k** (GPT-4) 🟧 | 1.731 | 中文贵 73.1% |
629
-
630
- 🟦 = 国产 tokenizer · 🟧 = OpenAI tokenizer
631
-
632
- ---
633
-
634
- **4 层压缩架构**
635
-
636
- - **L1 词法层** · 啰嗦套话 → 紧凑动宾,纯正则,~1.3-1.5×
637
- - **L2 句法层** · 模式重排 + 列表化,~2-3×
638
- - **L3 成语层** · 长描��� → 成语(实测白名单),仅国产模型默认关闭
639
- - **L4 协议层** · 标签归一化为 `### 标题`(实测全 tokenizer 1 token)
640
-
641
- **不主张的事**(诚实声明)
642
-
643
- - ❌ 不主张"中文因为信息密度高所以更省 token" — Mythbuster 2026 在 SWE-bench 上证伪
644
- - ❌ 不主张"中文 prompt 让模型更聪明" — 在英文中心模型上常常相反
645
- - ❌ 不主张"全程使用文言文压缩" — LLM 对生僻文言虚词理解不稳定
646
-
647
- CHIP 主张的是:**在符合中文训练分布的国产模型上,通过协议化压缩可在 token 经济性、可读性、可审计性三个维度同时提供工程价值。**
648
- """)
649
-
650
- gr.HTML(FOOTER_HTML)
651
-
652
- # 事件
653
- btn.click(
654
- run,
655
- inputs=[inp, target, use_l1, use_l2, use_l3, use_l4, tokenizer_name, use_jieba],
656
- outputs=[out, stats_panel, rules_panel, hidden_dup],
657
- )
658
-
659
-
660
- if __name__ == "__main__":
661
- demo.launch(
662
- server_name=os.getenv("HOST", "0.0.0.0"),
663
- server_port=int(os.getenv("PORT", 7860)),
664
- share=os.getenv("SHARE") == "1",
665
- )