scam-sms-classifier-mini
Built by Suman Mukherjee. A small text classifier that scores how much a short message (SMS, WhatsApp, email) reads like spam or a scam, from 0 to 1. TF-IDF word and character n-grams with logistic regression, trained with scikit-learn on CPU and exported to a 128 KB JSON file so it runs in plain JavaScript in a browser. It is the "model" half of the Is this message a scam? Space, where it is combined with hand-written, explainable rules. It is a small demonstration, not a production fraud system.
Intended use
- A second opinion on a single message, next to explainable rules, for people checking messages they received. In the Space, a score of 0.46 or more adds 2 points to the rule score; it can raise No common scam signs found to Be careful, or Be careful to Looks like a scam, and it never lowers a rule result.
- Teaching and experimentation: a fully reproducible, browser-sized text classifier with an honest, once-only held-out evaluation.
- Not for: blocking or deleting messages automatically, judging people, deciding claims, payments or account actions, or any use where a wrong answer harms someone without a human check. Do not describe a low score as "safe".
Training data
| Split | Messages | Spam or scam | From UCI | Synthetic |
|---|---|---|---|---|
| train | 4,255 | 982 | 3,095 | 1,160 |
| validation (tuning only) | 1,493 | 407 | 1,032 | 461 |
| heldout (scored once) | 1,468 | 377 | 1,032 | 436 |
- SMS Spam Collection (Tiago A. Almeida and José María Gómez Hidalgo), UCI Machine Learning Repository, CC BY 4.0, doi:10.24432/C5CC84. 5,574 English SMS: about 87% "ham" (mostly from the NUS SMS Corpus, Singapore) and 13% "spam" (mostly UK, from around 2012).
train.pydownloads it from UCI (it is not copied into this repo), checks the zip's SHA-256, removes 415 duplicate texts and makes a stratified 60/20/20 split with seed 20261114.data/uci_split.csvlists each original line number and its split; it contains no message text. The same collection is on the Hub asucirvine/sms_spam, which is why that id is in the metadata. Spam is not the same as scam: UCI "spam" includes ordinary promotions, ringtone offers and dating services, not only fraud. - Synthetic India-style messages (
data/synthetic.jsonl, 2,057 messages, 1,124 scam and 933 genuine), generated bygenerate_synthetic.pyfrom 208 templates in 23 categories with a fixed seed. Scam types: KYC/PAN/Aadhaar updates, OTP requests, UPI "enter PIN to receive", prizes and lotteries, parcel and customs fees, electricity cut-off, "digital arrest" and police threats, task and part-time jobs, investment and crypto groups, refunds and money "sent by mistake", APK and screen-sharing apps, family "new number" requests, loan fees. Genuine types include OTP messages that say "do not share", bank alerts, deliveries, bills, appointments, chats, work messages, bank service messages and insurer messages. Some templates are in Hinglish. Every bank, shop and person is fictional (for example "Corvane Bank", "Example Express"); links use reserved example domains (*.example,example.com/.net/.org) and documentation IP ranges; phone numbers start with 00000 or are 1800 000 0000 style. Templates, not messages, are assigned to splits, so validation and held-out messages use wording never seen in training.
Evaluation
Settings were chosen on the validation split only: vocabulary sizes, C and class weights by average precision (preferring the simplest setting within 0.003 of the best), then the score threshold (0.46) by F1. The combination with the rules (combine.json) was also tuned on validation. The held-out split was then scored once, by evaluate.mjs, using the same JavaScript the web page runs. "Flagged" means the Space would show Looks like a scam or Be careful. Full report: EVALUATION.md and heldout_report.json.
Held-out, all 1,468 messages (377 spam or scam, 1,091 genuine):
| Method | Precision | Recall | F1 | Spam or scam flagged | Genuine flagged |
|---|---|---|---|---|---|
| Rules only | 99.6% | 72.9% | 84.2% | 275 of 377 | 1 of 1,091 |
| This model only (score ≥ 0.46) | 92.7% | 97.9% | 95.2% | 369 of 377 | 29 of 1,091 |
| Rules + model (the Space) | 92.5% | 98.4% | 95.4% | 371 of 377 | 30 of 1,091 |
By part (held-out):
| Part | Method | Precision | Recall | F1 | Flagged | Genuine flagged |
|---|---|---|---|---|---|---|
| UCI, 1,032 real SMS | Model only | 99.2% | 95.3% | 97.2% | 122 of 128 | 1 of 904 |
| UCI | Rules + model | 98.4% | 95.3% | 96.8% | 122 of 128 | 2 of 904 |
| Synthetic, 436 messages | Model only | 89.8% | 99.2% | 94.3% | 247 of 249 | 28 of 187 |
| Synthetic | Rules + model | 89.9% | 100.0% | 94.7% | 249 of 249 | 28 of 187 |
What this shows: the model is good at the spam it learned from (UCI), but on new synthetic wording it flags many friendly chats (12 of 16) and refund or claim confirmations (12 of 24) as spam-like, because words such as "free", "call", "claim" and "pay" are common in UCI spam. In the Space those 28 messages get Be careful, never Looks like a scam. Validation results (used for tuning, so optimistic): model only precision 98.7%, recall 93.4%, F1 96.0%.
The JavaScript port (js/scam_model.js) matches scikit-learn's probabilities to within 0.0000026 on all 1,493 validation messages.
Limitations
- Small and partly synthetic. 1,468 held-out messages, 436 of them synthetic. The synthetic messages, their templates and the Space's rules were all written by one person, so synthetic results flatter both. Real-world accuracy on today's messages is unknown.
- Old, non-Indian real data. The only real messages are English SMS from around 2012, mostly UK spam and Singapore personal messages. Indian scam SMS in the real world look different, change fast and are often in Hindi or other scripts, which this model does not handle (non-ASCII characters are dropped).
- Spam is not scam. The UCI labels treat promotions as spam, so the model will call many genuine marketing messages spam-like, and it can mistake friendly messages with words like "free" or "call" for spam.
- Bag of n-grams. It has no understanding of meaning, sender or context; a scammer who avoids typical words will score low, and a genuine message that uses them will score high. It cannot check links or phone numbers.
- Process notes, for honesty: while building, the rules were adjusted after looking at validation and training errors, and 28 genuine templates were added to the generator after the model flagged genuine validation messages. All of this happened before the held-out split was scored. Afterwards only the wording of the rules' plain-English explanations changed; a check on all 7,276 messages confirmed identical scores and verdicts.
- No fairness analysis by language variety or region was possible with this data.
How to use
In a browser module (no dependencies; in Node, read model.json with fs instead of fetch):
import { loadModel } from "./js/scam_model.js";
const json = await (await fetch("model.json")).json();
const model = loadModel(json);
const { p, top } = model.predict("Your parcel is on hold. Pay a redelivery fee of Rs 25 today to release it.");
console.log(p.toFixed(2), p >= json.threshold ? "spam-like" : "not spam-like", top);
p is the probability of spam or scam; top lists word features that raised the score. The live Space uses it together with js/rules.js and combine.json.
Files
| File | What it is |
|---|---|
model.json |
Vocabulary, IDF weights, coefficients, intercept, threshold and training metadata |
combine.json |
How the Space combines the model with the rules (tuned on validation) |
train.py |
Downloads the UCI data, splits, tunes on validation, trains and exports (seed 20261114) |
generate_synthetic.py |
Writes data/synthetic.jsonl (seed 20261114) |
evaluate.mjs |
node evaluate.mjs validation tunes the combination and checks JS/Python parity; node evaluate.mjs heldout scores the held-out split once and refuses to run again |
js/scam_model.js, js/rules.js |
The same JavaScript files the Space runs |
EVALUATION.md, heldout_report.json, validation_report.json |
Results |
data/uci_split.csv, data/synthetic.jsonl |
Split assignment for UCI lines; the synthetic messages |
Reproduce (about 20 seconds on a laptop CPU; results are byte-identical):
pip install -r requirements.txt
python generate_synthetic.py
python train.py # caches the UCI download and split files in ~/.cache/scam-sms-classifier-mini
node evaluate.mjs validation # Node 20 or later
Citation and credits
Model and synthetic data: Suman Mukherjee, scam-sms-classifier-mini, Hugging Face, 2026. Training data: Almeida, T. & Hidalgo, J. (2011). SMS Spam Collection [Dataset]. UCI Machine Learning Repository. https://doi.org/10.24432/C5CC84, and Almeida, T.A., Gómez Hidalgo, J.M., Yamakami, A. Contributions to the Study of SMS Spam Filtering: New Collection and Results. DOCENG'11, 2011. Built with scikit-learn 1.9.1, NumPy and SciPy (BSD-3-Clause).
Licence
Apache-2.0 for the code, model weights and synthetic data. The model was trained partly on the SMS Spam Collection (CC BY 4.0); see THIRD_PARTY_NOTICES.md.