fill-mask · Chinese spelling correction · WASM · ~474 MB
Chinese typing errors are usually homophones: same sound, wrong character (天汽 instead of 天气). macbert4csc is a Chinese BERT fine-tuned to fix exactly these — it reads the whole sentence and predicts the most likely character at every position. This demo runs it on-device, marks every change, and shows how confident the model was.
| Model | shibing624/macbert4csc-base-chinese |
|---|---|
| Task | fill-mask (BERT MLM) — used here for Chinese spelling correction (CSC) |
| Architecture | MacBERT (BERT) — masked-language head fine-tuned for CSC; 102M params, 21,128-token Chinese vocab |
| Params / dtype | 102M · fp32 (only fp32 ONNX export exists — 474 MB single file) |
| Backend | WebAssembly (CPU) — no GPU required |
| Download | ~474 MB, cached after first load |
| License | Apache-2.0 |
| Web APIs | Web Workers, Cache Storage (via Transformers.js), WebAssembly |
改动明细(每处修正 + 置信度)
Unlike seq2seq models, this is a single forward pass: the sentence is tokenized, and the MLM head predicts the most likely character at every position at once, conditioned on the whole sentence. Each listed fix shows the model's confidence — the softmax probability of the corrected character. High confidence + a real error = a confident fix; low confidence can mean the model is unsure. Nothing is faked; a correct sentence usually comes back unchanged (try the last chip).
Chinese input methods make homophone typos inevitable, and spell-checking Chinese is a genuinely different problem from English (no spaces, no sound-alike dictionary lookup — the model has to understand context). On-device CSC means IMEs, editors, and chat apps can offer corrections with zero text leaving the device.
The model is a plain masked-LM; correction reads its per-position argmax:
import { AutoTokenizer, BertForMaskedLM } from "@huggingface/transformers";
const tokenizer = await AutoTokenizer.from_pretrained(MODEL, { subfolder: "onnx" });
const model = await BertForMaskedLM.from_pretrained(MODEL, { subfolder: "onnx", dtype: "fp32" });
const enc = await tokenizer("今天天汽很好");
const { logits } = await model({ input_ids: enc.input_ids, attention_mask: enc.attention_mask });
// for each position: argmax over the 21,128-token vocab = the corrected character
// diff against the input tokens → the changes shown on the page
One forward pass for the whole sentence; the argmax at every position is the model's real prediction. Runs in a Web Worker so the page stays responsive.