Asia/Kolkata
Posts

Zero-Python Name Gender Classification in Node.js With a 632 KB ONNX Model

November 28, 2026
When an app greets a user or cleans up a contact list, calling a cloud LLM just to guess if "Supratim Mondal" or "Sayantika Biswas" is male or female makes little sense. It adds lag, API cost, and privacy risk. To solve that locally, I built CompactGenderClassifier (GitHub). It shares the same 532K-weight core as my name model, weighs ~632 KB as an INT8 ONNX file, and runs in 0.475 ms per call straight inside Node.js with zero Python installed.

Why Last Names Fool Naive Models

If you feed a full name like Dr. Rahul Verma or Sayantika Biswas straight into a raw net, the title and last name get in the way. Last names like Mondal, Verma, or Smith carry no gender clue. Worse, if one gender shows up more with a certain last name in training data, the net learns false links. My fix runs in the text parser before a single token hits the net:
  1. Strip titles: Prefix words like Mr, Mrs, Dr, Prof, Sri, Smt, Md, and Adv get dropped up front.
  2. Pull out the first name: Only the given name goes into the model (Supratim from Supratim Mondal). Last names come back in the output JSON, yet never skew the score.

Reading Endings Without a Huge Word List

Across South Asian and Western names, gender clues often sit at the end of a first name. Endings like -a, -i, -ya, -ini, and -wati lean female. Endings like -an, -ar, -esh, and -kumar lean male. A 4,000-token uncased WordPiece list with a 20-token window catches those word parts cleanly. Inside the net, Masked Dual Pooling joins Mean Pool (128) and Max Pool (128) into one 256-dim vector. Mean Pool sums up the whole name, while Max Pool boosts sharp word endings anywhere in the string.

Data Scale and Benchmark Numbers

I trained CompactGenderClassifier from scratch on 976,267 clean first-name rows (889,912 train and 86,355 val). The split balances 485k female and 491k male names across Indian and global roots. | Metric | Old Pruned Baseline | CompactGenderClassifier | | :--- | :--- | :--- | | INT8 ONNX Size | 4.30 MB | ~632 KB | | Total Parameters | 4,380,000 | 532,482 | | Single-Thread CPU Latency | ~1.20 ms | 0.475 ms | | Node.js Throughput | ~800 qps | 2,105 qps | | Validation Accuracy / F1 | 84.79% | 84.44% / 84.44% | | Test Suite Benchmark | 55.6% (10/18) | 100.0% (18/18) | Even with 88% fewer weights than the old baseline, parsing out titles and last names pushed the test suite score from 55.6% to a clean 18/18.

Running Live Lookups in Node.js

You do not need Python, PyTorch, or a GPU box to run this in a Next.js API route. With onnxruntime-node and the pure-JS WordPiece code in scripts/classifier.mjs, setup takes three lines: The Node wrapper sets intraOpNumThreads: 1 and sequential mode so short lookups do not waste time spinning up thread pools. You can grab the INT8 ONNX files from Hugging Face or check the code on GitHub.