加载中…
加载中…
A compact TinyBERT (4-layer, 312 hidden) model fine-tuned to classify email text as spam or ham.
Trained on an Enron-derived CSV with light email-specific cleaning (e.g., removing quoted lines and base64-like blobs).
Optimized for low false positives by default; adjust the decision threshold if you want higher spam recall.
Labels:
ham(0) andspam(1)
from transformers import pipeline
clf = pipeline(
"text-classification",
model="prancyFox/tiny-bert-enron-spam",
truncation=True # recommended for long emails
)
clf("Congratulations! You won a FREE iPhone. Click here now!")
# [{'label': 'spam', 'score': 0.98}]
Batch inference
texts = [
"Meeting moved to 3pm, see agenda attached.",
"FREE gift card!!! Act now!",
]
preds = clf(texts, truncation=True)
Intended use
Out of scope / Limitations
Light normalization aimed at keeping semantic content:
> or |.Subject + "\n" + Message when available.(You can replicate similar cleaning in your serving pipeline for alignment.)
huawei-noah/TinyBERT_General_4L_312Dham=0, spam=1)2e-5 – 5e-5 (final run 3e-5)eval_f1Trainer script:
train/train_tinybert.py(TinyBERT-compatible, with legacy HF support shims).
Metrics below reflect a chunked evaluation pass (used for long emails), where the model sees up to 512 tokens per chunk with overlap. Threshold tuned to minimize false positives:
| Class | Precision | Recall | F1 |
|---|---|---|---|
| ham | 0.6875 | 0.9973 | 0.8139 |
| spam | 0.9954 | 0.5632 | 0.7194 |
| macro avg | 0.8414 | 0.7802 | 0.7666 |
Confusion matrix
[[16500 45]
[ 7500 9671]]
Interpretation: The model is conservative (very few false positives on ham). If you need to catch more spam, lower the decision threshold (e.g., from 0.5 → 0.35) or re-train with a spam-skewed class weight / focal loss.
Environment
transformers >= 4.40datasets >= 2.20evaluate >= 0.4.2torch >= 2.1Training command (example)
python train/train_tinybert.py \
--train data/enron.csv \
--text_col Message --label_col "Spam/Ham" \
--output_dir outputs/tiny-bert-enron-spam \
--epochs 4 --batch_size 32 --lr 3e-5 \
--max_length 256 --fp16
Serving (FastAPI example)
python spam_bert.py --serve \
--model prancyFox/tiny-bert-enron-spam \
--model-cache-dir ./models_cache
This repo should include:
config.jsonpytorch_model.bin or model.safetensorstokenizer.json and tokenizer_config.json (or vocab.txt etc.)README.md (this file)label_mapping.json with {"ham": 0, "spam": 1}If you use this model, please cite:
@software{tinybert_enron_spam_2025,
title = {TinyBERT Spam Classifier (Enron)},
author = {Ing. Daniel Eder},
year = {2025},
url = {https://huggingface.co/prancyFox/tiny-bert-enron-spam}
}
And the TinyBERT paper:
@article{jiao2020tinybert,
title={TinyBERT: Distilling BERT for Natural Language Understanding},
author={Jiao, Xiaoqi and Yin, Yichun and others},
journal={Findings of EMNLP},
year={2020}
}
--use_focal_loss or increase the spam class weight, then re-evaluate thresholds.