automatic-speech-recognition · audio · WASM (WebGPU when available) · ~244 MB
The English wav2vec2 demo speaks one language, the Russian XLSR demo another, and the French, Spanish and Italian demos more. This one takes the same cross-lingual cousin somewhere new: XLSR-53 was pre-trained on 53 languages (56,000 hours of multilingual audio) and then fine-tuned on Mandarin Chinese. Its output alphabet is not 26 Latin letters or 33 Cyrillic ones — it is a ~3,500-symbol vocabulary of Chinese characters (hanzi). Same CTC mechanics (label every frame, then collapse), but the model has to pick the right character for each run of sound, with no language model to lean on — so it substitutes homophones the way a phonetic listener would (加/迦, 大/打, 木/目). Pick a clip and watch the frames collapse into Chinese.
| Model | onnx-community/wav2vec2-large-xlsr-53-chinese-zh-cn-ONNX (base: jonatasgrosman/wav2vec2-large-xlsr-53-chinese-zh-cn) |
|---|---|
| Task / pipeline | automatic-speech-recognition — loaded as AutoModelForCTC so we can see the frames |
| Architecture | XLSR-53: wav2vec2-large cross-lingual encoder (24 transformer layers, ~315M params) pre-trained on 53 languages, fine-tuned for Mandarin. CTC head over a ~3,500-symbol Chinese character vocab (blank + "|" boundary + digits + Latin letters + ~3,470 hanzi). No decoder, no language model. |
| Language | Mandarin Chinese (zh-CN) — this is a language-specialised recogniser; other languages produce Mandarin-phonetic character gibberish |
| Quantization | q4 (4-bit) ONNX from the standard onnx/ folder (~244 MB; verified to produce coherent Mandarin — the full fp32 export is 1.28 GB, fp16 is 639 MB) |
| Vocab | the character vocab is ~3,500 entries, too large to hand-list, so we build the id→character map from the model's bundled vocab.json and CTC-decode from it |
| Backend | WebAssembly (WebGPU when a real adapter is present), honest fallback |
| Download | ~244 MB (4-bit encoder), cached after first load |
| License | Apache-2.0 (base model) — the ONNX-export repo inherits the base license |
| Web APIs | WebAssembly / WebGPU, Web Workers, Web Audio (decode + resample), MediaRecorder (mic) |
Choose Mandarin audio — record from your mic, upload a file, or pick the bundled sample. The page decodes it to 16 kHz mono, draws the waveform, and runs the CTC model in a Web Worker. XLSR-53 does best on clear Mandarin speech. First load is a ~244 MB 4-bit download, cached afterwards.
This context didn't grant mic access (common in headless / embedded views). The model still works — use the sample above or Upload audio. For live recording, open this in a normal browser tab and allow the microphone when prompted, then speak Mandarin.
Below is the model's raw per-frame argmax: one cell per ~20 ms frame, showing the
single character it thinks is most likely there. Most frames are blank (·) — the model
saying "nothing new here". Where a sound lands you get a run of the same character. The transcript is
produced by two mechanical rules, no decoder involved:
(1) merge consecutive duplicates, (2) drop the blanks. A run like
·· 中中 ·· 国国 国 ·· 人人 collapses to 中国人.
Raw per-frame argmax (– frames · ~– ms each):
↓ merge duplicates, drop blanks ↓
Mandarin has roughly a billion speakers, and its writing system makes it a great stress-test for speech AI: an ASR model can't spell out a hanzi phonetically the way it can a Latin word — it has to pick the right character from thousands. Cross-lingual self-supervised pre-training is how XLSR-53 gets there: it learns what human speech sounds like from 53 languages at once, so fine-tuning to Mandarin needs far fewer transcripts. That is what puts a usable Chinese recogniser inside a browser tab — private, offline once cached, no server, no key. And because it's CTC (frames, not generated tokens) it can't hallucinate an unspoken sentence, and every character is pinned to a moment in time.
One Mandarin clip in, Chinese text out — and the per-frame strip that produced it.
Per-character forced alignment: real per-hanzi timings, straight from the CTC frames.
Feed it English and watch cross-lingual transfer break down into Mandarin-phonetic hanzi.
Transcribe Mandarin with XLSR → OPUS-MT translates it to English, all on-device.
This repo ships the standard transformers.js layout, so we load the model directly — but as
AutoModelForCTC (not the ASR pipeline) so we can see the per-frame argmax. Because the
character vocab is ~3,500 entries, we build the id→character map from vocab.json and do
the CTC collapse ourselves (exactly what the pipeline does internally):
import { AutoModelForCTC, Tensor } from "@huggingface/transformers";
const model_id = "onnx-community/wav2vec2-large-xlsr-53-chinese-zh-cn-ONNX";
const model = await AutoModelForCTC.from_pretrained(model_id, {
device: "wasm", dtype: "q4", // 4-bit export (~244 MB) — coherent Mandarin; fp32 is 1.28 GB
});
// audio = 16 kHz mono Float32Array → normalise to zero mean / unit variance (wav2vec2 feature step)
const x = normalize(audio);
const { logits } = await model({ input_values: new Tensor("float32", x, [1, x.length]) });
// logits shape [1, frames, ~3503] ← one score per Chinese character per frame
// greedy CTC decode: argmax per frame, merge duplicates, drop the blank (id 0)
const ids = argmaxPerFrame(logits); // e.g. [0,0,1520,1520,0,880,880,...]
const text = collapse(ids, id2char); // "中国…" — no decoder involved
Inference runs in a Web Worker so the UI never janks. The worker returns the raw per-frame argmax and real per-character timings (from the frame each character was emitted at) — nothing is faked or pre-computed.