Reads an email (subject + body) and returns the probability that it is spam or ham (a legitimate email). It is DistilBERT (uncased) with a new one-logit head. The last 2 of its 6 transformer blocks and the head were fine-tuned on ~23.6k emails from the Enron-Spam corpus. On the held-out 5,050-email test split it makes 32 mistakes (F1 0.9938, accuracy 0.9937).
| Architecture | distilbert/distilbert-base-uncased encoder (6 blocks, dim 768) -> [CLS] hidden state -> Dropout(0.3) -> Linear(768, 1); sigmoid = P(spam) |
| Parameters | 66,363,649 total, of which 14,176,513 (the last 2 blocks + the head) were trained |
| Input | one string, ideally "Subject: <subject>\n\n<body>" |
| Preprocessing | model.prepare_text: drop a leading Subject: header, then model.clean_text (HTML unescape, strip tags and URLs, lowercase, keep only a-z 0-9 . , ! ? $ % ' -, collapse whitespace). Same function as in training. WordPiece tokenizer, max 256 tokens (longer emails are truncated) |
| Output | {"spam": p, "ham": 1 - p}; Predictor.label() says spam when p >= 0.5 |
| Files | model.safetensors (weights), config.json (backbone_config, dropout, max_len, threshold, labels), tokenizer.json + tokenizer_config.json, model.py (architecture + cleaning + load() + Predictor), handler.py (Inference Endpoint) |
config.json stores the full DistilBERT config, so model.load() rebuilds the encoder with
AutoModel.from_config (pretrained=False) and then loads model.safetensors. Inference never downloads
the base model.
from huggingface_hub import snapshot_download
import sys
path = snapshot_download("shalev396/email-spam-classifier")
sys.path.insert(0, path)
import model
predictor = model.load(path, device="cpu") # or "cuda"
print(predictor.predict("Subject: Notes from this morning\n\nHi team, attached are the notes. Thanks, Sarah"))
# {'spam': 1.2e-06, 'ham': 0.999999}
print(predictor.predict(model.compose_email("You won a $1,000 gift card", "Click here to claim your prize now!")))
# {'spam': 0.9998, 'ham': 0.0002}
print(predictor.predict_proba(["email one ...", "email two ..."])) # batch -> [P(spam), ...]
Requirements: torch, transformers>=5, tokenizers, huggingface_hub, safetensors.
POST /gradio_api/call/predict with {"data": ["Subject: ...\n\n..."]}.handler.py accepts
{"inputs": "Subject: ...\n\n..."}, a list of such strings, or {"inputs": {"subject": ..., "body": ...}},
and uses a GPU when the endpoint has one.BCEWithLogitsLoss with
pos_weight = n_ham / n_spam = 0.966. AdamW with differential learning rates (head 5e-4, encoder 2e-5,
weight decay 0.01), 10% linear warmup then linear decay, batch 32, max 256 tokens, up to 5 epochs with
early stopping on validation loss (patience 2), seed 42.model.SpamClassifier (all 102 tensors, strict=True) and saved as safetensors. It was then re-evaluated on CPU
through model.Predictor, the code path the Space uses. The confusion matrix matches the original run's log exactly.
Training time was not recorded.Full code: training/ · Colab. The notebook reproduces this recipe step by step (data -> model -> training -> evaluation -> inference -> export).
| test split (5,050 emails) | accuracy | precision | recall | F1 | ROC-AUC | errors |
|---|---|---|---|---|---|---|
| DistilBERT fine-tuned, last 2 blocks (deployed) | 0.9937 | 0.9930 | 0.9945 | 0.9938 | 0.9998 | 32 (18 ham -> spam, 14 spam -> ham) |
| TF-IDF 1-2-grams + logistic regression (baseline) | 0.9913 | 0.9880 | 0.9949 | 0.9915 | 0.9992 | 44 (31 ham -> spam, 13 spam -> ham) |
Both rows are in metrics.json (comparison). The baseline (50k TF-IDF features, C=10, class-balanced) trains in
about 25 s on a CPU and is already strong on this corpus. DistilBERT's gain is mostly fewer false alarms: it sends
18 legitimate emails to spam instead of 31. Recall is about the same for both.
| metric (test) | value |
|---|---|
| accuracy | 0.9937 |
| precision | 0.9930 |
| recall | 0.9945 |
| f1 | 0.9938 |
| roc_auc | 0.9998 |
| average_precision | 0.9998 |
Test split, threshold 0.5. Precision, recall and F1 are for the spam class. average_precision is the area under the
precision-recall curve.
distilbert/distilbert-base-uncased