← All models

fill-mask · text · WASM · ~103 MB

Chinese BERT — predict the missing character

Google's BERT, pre-trained on Chinese Wikipedia. Its one job here is masked language modelling: hide a character behind [MASK] and it ranks which Hànzì belongs there. Chinese BERT is character-level — every character is its own token, so it needs no word-segmentation step. Type a sentence with a mask and see its guesses, plus the exact logits it computed. Everything runs on your device.

At a glance

ModelXenova/bert-base-chinese (ONNX build of google-bert/bert-base-chinese)
Task / pipelinefill-mask — masked language modelling, loaded via AutoModelForMaskedLM for raw logits
OutputA probability over the whole 21,128-character vocabulary at each [MASK]
ArchitectureBERT-base (12 layers, ~102M params), character-level WordPiece — one token per Hànzì
LanguageChinese (Simplified & Traditional) — mask token is [MASK]
Quantizationq8 (8-bit) ONNX
BackendWebAssembly (runs anywhere; no WebGPU needed)
Download~103 MB, cached after first load
LicenseApache-2.0
Web APIsWebAssembly, Web Workers — all Baseline widely available

Run it

Write a Chinese sentence containing [MASK] (use a sample if you're not sure). Chinese BERT predicts the missing character. You can use more than one mask.

See inside — character tokens & logits

First, how the text is tokenised: Chinese BERT splits your sentence into one chip per character — no word segmentation, no sub-word merging. The ▢ chip is the position being predicted.

And the prediction itself: the model outputs a logit (a raw, unbounded score) for every one of its 21,128 vocabulary characters at the masked position. We softmax those into probabilities. The bars above show the probability; the logit beside each is the actual number the network produced. The – below is the entropy of the top-k — low means BERT is sure which character fits, high means it's hedging.

Why it matters

Chinese has no spaces between words, so most NLP starts with the hard problem of segmentation. A character-level masked model sidesteps that entirely: it reasons one Hànzì at a time, in context. Because it scores every character in context, you can ask it what fits, how strongly, and compare alternatives — all locally, with no prompt engineering and no API bill.

Use cases

Basics

One Chinese mask, ranked character predictions with probabilities and the raw logits.

Practical

A Chinese input assistant — predict the next character one at a time and build a sentence.

Wild

Chengyu challenge — mask a character in a four-character idiom and see if BERT knows it.

Multi-model

Fill the Chinese mask, then translate the completed sentence to English with a second model.

How the API works

The one-liner uses the fill-mask pipeline:

import { pipeline } from "@huggingface/transformers";

const unmask = await pipeline("fill-mask", "Xenova/bert-base-chinese");
const out = await unmask("北京是中国的[MASK]都。");
// → [{ token_str: "首", score: 0.82, sequence: "…" }, { token_str: "古", … }]  → 首都 = "capital"

For the "see inside" surface we go one level lower — AutoModelForMaskedLM — so we can read the exact logits tensor ([1, sequence, 21128]), slice the row at the [MASK] position, and softmax it ourselves. That's the only way to show the real logit values and the character-by-character tokenisation, not just the pipeline's rounded scores. All of it runs in a Web Worker so the page never janks.

References