nopp23 commited on
Commit
0281fa8
·
verified ·
1 Parent(s): 0d1a1cd

In-browser demo (Transformers.js) for the CodedLang detector

Browse files
Files changed (2) hide show
  1. README.md +15 -5
  2. index.html +137 -17
README.md CHANGED
@@ -1,10 +1,20 @@
1
  ---
2
- title: Codedlang Detector Demo
3
- emoji: 📈
4
- colorFrom: gray
5
- colorTo: yellow
6
  sdk: static
 
7
  pinned: false
 
 
 
 
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
1
  ---
2
+ title: CodedLang Coded-Language Detector
3
+ emoji: 🔎
4
+ colorFrom: blue
5
+ colorTo: gray
6
  sdk: static
7
+ app_file: index.html
8
  pinned: false
9
+ license: cc-by-4.0
10
+ models:
11
+ - nopp23/codedlang-coded-language-detector
12
+ datasets:
13
+ - RuyuanWan/CodedLang
14
+ short_description: Detect coded language in Chinese reviews, in-browser
15
  ---
16
 
17
+ In-browser demo (Transformers.js) for
18
+ [nopp23/codedlang-coded-language-detector](https://huggingface.co/nopp23/codedlang-coded-language-detector).
19
+ The model runs on the visitor's device; no text is sent to a server.
20
+ See the model card for metrics, charts, examples and limitations.
index.html CHANGED
@@ -1,19 +1,139 @@
1
  <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
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>CodedLang Detector Demo</title>
7
+ <style>
8
+ :root {
9
+ --bg: #fcfcfb; --fg: #0b0b0b; --muted: #52514e; --line: #e4e3df; --card: #ffffff;
10
+ --coded: #eb6834; --plain: #2a78d6; --btn: #0b0b0b; --btn-fg: #ffffff;
11
+ }
12
+ @media (prefers-color-scheme: dark) {
13
+ :root { --bg: #1a1a19; --fg: #f0efec; --muted: #c3c2b7; --line: #383835; --card: #222220;
14
+ --coded: #d95926; --plain: #3987e5; --btn: #f0efec; --btn-fg: #1a1a19; }
15
+ }
16
+ * { box-sizing: border-box; }
17
+ body { margin: 0; background: var(--bg); color: var(--fg);
18
+ font: 16px/1.55 system-ui, -apple-system, "Segoe UI", "PingFang SC", "Noto Sans SC", sans-serif; }
19
+ main { max-width: 760px; margin: 0 auto; padding: 32px 16px 64px; }
20
+ h1 { font-size: 1.5rem; margin: 0 0 4px; }
21
+ .sub { color: var(--muted); margin: 0 0 24px; }
22
+ textarea { width: 100%; min-height: 96px; padding: 12px; font: inherit; color: var(--fg);
23
+ background: var(--card); border: 1px solid var(--line); border-radius: 8px; resize: vertical; }
24
+ .row { display: flex; gap: 12px; align-items: center; margin: 12px 0 20px; flex-wrap: wrap; }
25
+ button { font: inherit; padding: 8px 18px; border-radius: 8px; border: 0; cursor: pointer;
26
+ background: var(--btn); color: var(--btn-fg); }
27
+ button:disabled { opacity: .45; cursor: default; }
28
+ #status { color: var(--muted); font-size: .9rem; }
29
+ .result { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 16px; min-height: 72px; }
30
+ .verdict { font-weight: 600; font-size: 1.1rem; }
31
+ .bar { height: 10px; background: var(--line); border-radius: 5px; overflow: hidden; margin: 10px 0 6px; }
32
+ .bar > div { height: 100%; width: 0; border-radius: 5px; transition: width .25s; }
33
+ .small { color: var(--muted); font-size: .88rem; }
34
+ h2 { font-size: 1.05rem; margin: 28px 0 8px; }
35
+ .examples { display: flex; flex-wrap: wrap; gap: 8px; }
36
+ .examples button { background: transparent; color: var(--fg); border: 1px solid var(--line);
37
+ padding: 6px 10px; font-size: .9rem; text-align: left; }
38
+ ul { padding-left: 20px; margin: 6px 0; }
39
+ li { margin: 4px 0; }
40
+ a { color: var(--plain); }
41
+ </style>
42
+ </head>
43
+ <body>
44
+ <main>
45
+ <h1>CodedLang coded-language detector</h1>
46
+ <p class="sub">Paste a <b>Chinese</b> review. The model estimates whether it hides meaning with homophones,
47
+ orthographic tricks, phonetic spellings or ciphers. It runs entirely in your browser; nothing you type is sent anywhere.</p>
48
+
49
+ <textarea id="text" placeholder="例如:垃圾公司,网络不稳定还尼玛卡"></textarea>
50
+ <div class="row">
51
+ <button id="go" disabled>Check</button>
52
+ <span id="status">Loading model (about 150 MB, first visit only)…</span>
53
+ </div>
54
+
55
+ <div class="result" id="result"><span class="small">The result will appear here.</span></div>
56
+
57
+ <h2>Try an example</h2>
58
+ <div class="examples" id="examples"></div>
59
+ <p class="small">From the dataset's held-out split. Some contain disguised profanity.</p>
60
+
61
+ <h2>Know its limits</h2>
62
+ <ul>
63
+ <li>Chinese only. Thai, English or other languages give meaningless scores.</li>
64
+ <li>It mostly recognises codes it saw in training. On codes it had never seen, it caught about half.</li>
65
+ <li>It reads surface forms, not context: 辣鸡汉堡 (spicy-chicken burger) is flagged because 辣鸡 is also slang for 垃圾 "garbage".</li>
66
+ <li>Scores are nearly always close to 0 or 1 and are not calibrated probabilities.</li>
67
+ <li>This page uses an 8-bit compressed copy of the model. On the 763-review held-out test it scored F1 0.891 vs 0.885 for the full model (1 review decided differently).</li>
68
+ <li>Research demo from a master's thesis. Do not use it to judge or moderate real people.</li>
69
+ </ul>
70
+ <p class="small">Model: <a href="https://huggingface.co/nopp23/codedlang-coded-language-detector">nopp23/codedlang-coded-language-detector</a>
71
+ (fine-tuned <code>hfl/chinese-roberta-wwm-ext</code>) · Data: <a href="https://huggingface.co/datasets/RuyuanWan/CodedLang">CodedLang</a>
72
+ (Wan, Li &amp; Huang, ACL 2026, CC BY 4.0).</p>
73
+ </main>
74
+
75
+ <script type="module">
76
+ import { AutoTokenizer, AutoModelForSequenceClassification }
77
+ from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.1.2";
78
+
79
+ const MODEL_ID = "nopp23/codedlang-coded-language-detector";
80
+ const THRESHOLD = 0.05;
81
+ const EXAMPLES = [
82
+ "垃圾公司,网络不稳定还尼玛卡",
83
+ "住在索尔万,只有一句话:谁nmb……过来high!驻唱帅哥,新鲜啤酒!",
84
+ "牛肉美味 份量大 值得一再前往",
85
+ "肉太老了!CP值低",
86
+ "一流的辣鸡汉堡 一流的服务",
87
+ "每小時八美金滴佛心停車場",
88
+ ];
89
+
90
+ const $ = (id) => document.getElementById(id);
91
+ const go = $("go"), status = $("status"), result = $("result"), text = $("text");
92
+
93
+ for (const ex of EXAMPLES) {
94
+ const b = document.createElement("button");
95
+ b.textContent = ex;
96
+ b.onclick = () => { text.value = ex; if (!go.disabled) run(); };
97
+ $("examples").appendChild(b);
98
+ }
99
+
100
+ let tok, model;
101
+ try {
102
+ tok = await AutoTokenizer.from_pretrained(MODEL_ID);
103
+ model = await AutoModelForSequenceClassification.from_pretrained(MODEL_ID, {
104
+ dtype: "q8",
105
+ progress_callback: (p) => {
106
+ if (p.status === "progress" && p.file && p.file.endsWith(".onnx")) {
107
+ status.textContent = `Loading model… ${Math.round(p.progress)}%`;
108
+ }
109
+ },
110
+ });
111
+ status.textContent = "Model ready.";
112
+ go.disabled = false;
113
+ } catch (e) {
114
+ status.textContent = "Could not load the model: " + e.message;
115
+ }
116
+
117
+ function softmax1(a, b) { const m = Math.max(a, b); const ea = Math.exp(a - m), eb = Math.exp(b - m); return eb / (ea + eb); }
118
+
119
+ async function run() {
120
+ const t = text.value.trim();
121
+ if (!t) { result.innerHTML = '<span class="small">Type or paste a Chinese review first.</span>'; return; }
122
+ go.disabled = true; status.textContent = "Checking…";
123
+ const inputs = await tok(t, { truncation: true, max_length: 256 });
124
+ const { logits } = await model(inputs);
125
+ const [l0, l1] = logits.data;
126
+ const p = softmax1(l0, l1);
127
+ const coded = p >= THRESHOLD;
128
+ const color = coded ? "var(--coded)" : "var(--plain)";
129
+ result.innerHTML = `
130
+ <div class="verdict">${coded ? "Coded language detected" : "No coded language detected"}</div>
131
+ <div class="bar"><div style="width:${(p * 100).toFixed(1)}%;background:${color}"></div></div>
132
+ <div class="small">P(coded) = ${p.toFixed(3)} · threshold ${THRESHOLD}</div>`;
133
+ status.textContent = "Model ready."; go.disabled = false;
134
+ }
135
+ go.onclick = run;
136
+ text.addEventListener("keydown", (e) => { if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) run(); });
137
+ </script>
138
+ </body>
139
  </html>