Instructions to use Noor-Khan/spam-detector with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Scikit-learn
How to use Noor-Khan/spam-detector with Scikit-learn:
from huggingface_hub import hf_hub_download import joblib model = joblib.load( hf_hub_download("Noor-Khan/spam-detector", "sklearn_model.joblib") ) # only load pickle files from sources you trust # read more about it here https://skops.readthedocs.io/en/stable/persistence.html - Notebooks
- Google Colab
- Kaggle
Spam Detector β SMS Spam Classification (TF-IDF + Naive Bayes / Logistic Regression)
Binary spam classifier that labels an English text message as spam or ham. Built as a learning project by Noorullah Zamindar.
- Model type: TF-IDF vectorizer (max 5,000 features, 1β2 grams, min_df=2, sublinear TF) + two classifiers:
- Multinomial Naive Bayes (alpha=0.1) β the default model, best spam F1 on the test set
- Logistic Regression (max_iter=1000) β shipped for comparison
- Task: text classification / spam detection
- Language: English
- License: MIT
Training data
The UCI SMS Spam Collection (Tiago Almeida & JosΓ© Maria Hidalgo, 2011) β
5,572 real SMS messages: 4,825 ham / 747 spam (spam share 13.41%),
collected from the Grumbletext forum, the NUS SMS corpus, Caroline Tag's
thesis and the SMS Spam Corpus. The dataset file is shipped in this repo
(data/SMSSpamCollection, with its original data/readme); train.py
downloads it from the public UCI archive and SHA-256-verifies it if the
file is missing.
Split: 80/20 stratified, random_state=42 β 4,457 train / 1,115 test
(test: 149 spam, 966 ham). The split is fixed, so the numbers below
reproduce exactly β run evaluate.py in a clean copy of this repo to
check.
Evaluation
Measured on the 1,115-message held-out test split on 2026-11-27
(Python 3.12.3, scikit-learn 1.9.1). evaluate.py reproduces these
numbers standalone from the shipped artifacts β verified.
| Metric | Naive Bayes (default) | Logistic Regression |
|---|---|---|
| Accuracy | 0.9830 (1,096/1,115) | 0.9722 (1,084/1,115) |
| Spam precision | 0.9851 | 1.0000 |
| Spam recall | 0.8859 | 0.7919 |
| Spam F1 | 0.9329 | 0.8839 |
| ROC-AUC | 0.9871 | 0.9902 |
Confusion matrices β rows are true labels, columns are predictions
[[ham ok, ham flagged], [spam missed, spam caught]]:
- Naive Bayes:
[[964, 2], [17, 132]]β 2 ham messages wrongly flagged, 17 spam missed. - Logistic Regression:
[[966, 0], [31, 118]]β no ham wrongly flagged, but 31 spam missed.
Hand-written examples (not from the dataset)
| Message | Expected | Naive Bayes P(spam) | Logistic Regression P(spam) |
|---|---|---|---|
| "Congratulations! You have WON a FREE 1000 cash prize. Call 09001234567 now to claim your reward!" | spam | 1.0000 β spam | 0.9578 β spam |
| "Hey, are we still meeting for lunch tomorrow? Let me know what time works for you." | ham | 0.0001 β ham | 0.0273 β ham |
| "URGENT: Your account will be closed today unless you verify your details at http://bit.ly/verify-now" | spam | 0.5219 β spam | 0.3056 β ham |
| "Ok lar, see you at 7. Bring the notes from class." | ham | 0.0001 β ham | 0.0349 β ham |
See samples/examples.json for the full results.
Limitations β read before using
- SMS only, English only. Trained on short SMS messages (ham averages 72 characters, spam 139). It is not an email spam filter and was not tested on emails, long texts, or other languages.
- The two models disagree on unseen phishing-style text. The "URGENT account closed" example above is caught by Naive Bayes (barely, P=0.52) and missed by Logistic Regression (P=0.31). Out-of-distribution phrasing is the weakest point of both models.
- Duplicates in the dataset. The collection contains 403 exact duplicate rows, kept as-is because that is the canonical dataset β so a few test messages may also appear in training, which can flatter the scores slightly.
- Empty or punctuation-only input does not crash; it scores near the ham prior (Naive Bayes P(spam) = 0.1342, the dataset's spam share) and is labelled ham.
- This is a word-frequency model, not a language model. It cannot read intent, links, or sender reputation, and spammers who reword their messages can evade it. Do not use it as the only line of defence.
How to use
pip install -r requirements.txt
python spam_detector.py "Free entry in 2 a wkly comp to win FA Cup final tkts"
# [SPAM] P(spam)=0.9998 Free entry in 2 a wkly comp to win FA Cup final tkts
python spam_detector.py --model lr "Some message text" # Logistic Regression instead
python spam_detector.py --file messages.txt # one message per line
python spam_detector.py --interactive # type messages, Ctrl-D to finish
Or load the artifacts directly:
import joblib
vectorizer = joblib.load("tfidf_vectorizer.pkl")
model = joblib.load("spam_model_nb.pkl") # default; spam_model_lr.pkl for Logistic Regression
text = "WINNER!! You have won a free holiday. Call now to claim."
proba = model.predict_proba(vectorizer.transform([text]))[0, 1]
print("spam" if proba >= 0.5 else "ham", f"P(spam)={proba:.4f}")
Reproduce the results
python train.py # retrains both models from data/SMSSpamCollection, rewrites metrics.json
python evaluate.py # re-scores the shipped models on the held-out split; must match metrics.json
Files
README.mdβ this model cardspam_model_nb.pklβ Multinomial Naive Bayes (default model)spam_model_lr.pklβ Logistic Regression (comparison model)tfidf_vectorizer.pklβ TF-IDF vectorizer (5,000 features)spam_detector.pyβ prediction module + CLItrain.pyβ training script (reproduces the models and metrics.json)evaluate.pyβ standalone evaluation (reproduces the metrics from the shipped files)metrics.jsonβ full measured metricsrequirements.txtβ pinned environmentdata/SMSSpamCollectionβ the UCI dataset (5,572 messages), plus its originalreadmesamples/examples.jsonβ hand-written example predictions and edge cases
- Downloads last month
- -