nopp23's picture
In-browser demo (Transformers.js) for the CodedLang detector
0281fa8 verified
Raw History Blame Contribute Delete
6.78 kB
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CodedLang Detector Demo</title>
<style>
:root {
--bg: #fcfcfb; --fg: #0b0b0b; --muted: #52514e; --line: #e4e3df; --card: #ffffff;
--coded: #eb6834; --plain: #2a78d6; --btn: #0b0b0b; --btn-fg: #ffffff;
}
@media (prefers-color-scheme: dark) {
:root { --bg: #1a1a19; --fg: #f0efec; --muted: #c3c2b7; --line: #383835; --card: #222220;
--coded: #d95926; --plain: #3987e5; --btn: #f0efec; --btn-fg: #1a1a19; }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--fg);
font: 16px/1.55 system-ui, -apple-system, "Segoe UI", "PingFang SC", "Noto Sans SC", sans-serif; }
main { max-width: 760px; margin: 0 auto; padding: 32px 16px 64px; }
h1 { font-size: 1.5rem; margin: 0 0 4px; }
.sub { color: var(--muted); margin: 0 0 24px; }
textarea { width: 100%; min-height: 96px; padding: 12px; font: inherit; color: var(--fg);
background: var(--card); border: 1px solid var(--line); border-radius: 8px; resize: vertical; }
.row { display: flex; gap: 12px; align-items: center; margin: 12px 0 20px; flex-wrap: wrap; }
button { font: inherit; padding: 8px 18px; border-radius: 8px; border: 0; cursor: pointer;
background: var(--btn); color: var(--btn-fg); }
button:disabled { opacity: .45; cursor: default; }
#status { color: var(--muted); font-size: .9rem; }
.result { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 16px; min-height: 72px; }
.verdict { font-weight: 600; font-size: 1.1rem; }
.bar { height: 10px; background: var(--line); border-radius: 5px; overflow: hidden; margin: 10px 0 6px; }
.bar > div { height: 100%; width: 0; border-radius: 5px; transition: width .25s; }
.small { color: var(--muted); font-size: .88rem; }
h2 { font-size: 1.05rem; margin: 28px 0 8px; }
.examples { display: flex; flex-wrap: wrap; gap: 8px; }
.examples button { background: transparent; color: var(--fg); border: 1px solid var(--line);
padding: 6px 10px; font-size: .9rem; text-align: left; }
ul { padding-left: 20px; margin: 6px 0; }
li { margin: 4px 0; }
a { color: var(--plain); }
</style>
</head>
<body>
<main>
<h1>CodedLang coded-language detector</h1>
<p class="sub">Paste a <b>Chinese</b> review. The model estimates whether it hides meaning with homophones,
orthographic tricks, phonetic spellings or ciphers. It runs entirely in your browser; nothing you type is sent anywhere.</p>
<textarea id="text" placeholder="例如:垃圾公司,网络不稳定还尼玛卡"></textarea>
<div class="row">
<button id="go" disabled>Check</button>
<span id="status">Loading model (about 150 MB, first visit only)…</span>
</div>
<div class="result" id="result"><span class="small">The result will appear here.</span></div>
<h2>Try an example</h2>
<div class="examples" id="examples"></div>
<p class="small">From the dataset's held-out split. Some contain disguised profanity.</p>
<h2>Know its limits</h2>
<ul>
<li>Chinese only. Thai, English or other languages give meaningless scores.</li>
<li>It mostly recognises codes it saw in training. On codes it had never seen, it caught about half.</li>
<li>It reads surface forms, not context: 辣鸡汉堡 (spicy-chicken burger) is flagged because 辣鸡 is also slang for 垃圾 "garbage".</li>
<li>Scores are nearly always close to 0 or 1 and are not calibrated probabilities.</li>
<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>
<li>Research demo from a master's thesis. Do not use it to judge or moderate real people.</li>
</ul>
<p class="small">Model: <a href="https://huggingface.co/nopp23/codedlang-coded-language-detector">nopp23/codedlang-coded-language-detector</a>
(fine-tuned <code>hfl/chinese-roberta-wwm-ext</code>) · Data: <a href="https://huggingface.co/datasets/RuyuanWan/CodedLang">CodedLang</a>
(Wan, Li &amp; Huang, ACL 2026, CC BY 4.0).</p>
</main>
<script type="module">
import { AutoTokenizer, AutoModelForSequenceClassification }
from "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.1.2";
const MODEL_ID = "nopp23/codedlang-coded-language-detector";
const THRESHOLD = 0.05;
const EXAMPLES = [
"垃圾公司,网络不稳定还尼玛卡",
"住在索尔万,只有一句话:谁nmb……过来high!驻唱帅哥,新鲜啤酒!",
"牛肉美味 份量大 值得一再前往",
"肉太老了!CP值低",
"一流的辣鸡汉堡 一流的服务",
"每小時八美金滴佛心停車場",
];
const $ = (id) => document.getElementById(id);
const go = $("go"), status = $("status"), result = $("result"), text = $("text");
for (const ex of EXAMPLES) {
const b = document.createElement("button");
b.textContent = ex;
b.onclick = () => { text.value = ex; if (!go.disabled) run(); };
$("examples").appendChild(b);
}
let tok, model;
try {
tok = await AutoTokenizer.from_pretrained(MODEL_ID);
model = await AutoModelForSequenceClassification.from_pretrained(MODEL_ID, {
dtype: "q8",
progress_callback: (p) => {
if (p.status === "progress" && p.file && p.file.endsWith(".onnx")) {
status.textContent = `Loading model… ${Math.round(p.progress)}%`;
}
},
});
status.textContent = "Model ready.";
go.disabled = false;
} catch (e) {
status.textContent = "Could not load the model: " + e.message;
}
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); }
async function run() {
const t = text.value.trim();
if (!t) { result.innerHTML = '<span class="small">Type or paste a Chinese review first.</span>'; return; }
go.disabled = true; status.textContent = "Checking…";
const inputs = await tok(t, { truncation: true, max_length: 256 });
const { logits } = await model(inputs);
const [l0, l1] = logits.data;
const p = softmax1(l0, l1);
const coded = p >= THRESHOLD;
const color = coded ? "var(--coded)" : "var(--plain)";
result.innerHTML = `
<div class="verdict">${coded ? "Coded language detected" : "No coded language detected"}</div>
<div class="bar"><div style="width:${(p * 100).toFixed(1)}%;background:${color}"></div></div>
<div class="small">P(coded) = ${p.toFixed(3)} · threshold ${THRESHOLD}</div>`;
status.textContent = "Model ready."; go.disabled = false;
}
go.onclick = run;
text.addEventListener("keydown", (e) => { if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) run(); });
</script>
</body>
</html>