Views
No views yet
| Metric | Score |
|---|---|
| Accuracy | 98.65% |
| F1 Score (spam) | 94.88% |
| Precision (spam) | 96.53% |
| Recall (spam) | 93.29% |
Predicted Ham Predicted Spam
Actual Ham 961 5
Actual Spam 10 1391import pickle
2from huggingface_hub import hf_hub_download
3
4# Load model
5tfidf_path = hf_hub_download("anu56787ty/spam-detector-tfidf-lr", "tfidf_vectorizer.pkl")
6clf_path = hf_hub_download("anu56787ty/spam-detector-tfidf-lr", "logistic_regression.pkl")
7
8with open(tfidf_path, "rb") as f:
9 tfidf = pickle.load(f)
10with open(clf_path, "rb") as f:
11 clf = pickle.load(f)
12
13def predict(text):
14 vec = tfidf.transform([text])
15 pred = clf.predict(vec)[0]
16 proba = clf.predict_proba(vec)[0]
17 return {"label": "spam" if pred == 1 else "ham", "confidence": proba[pred]}
18
19# Try it!
20print(predict("Congratulations! You won a FREE iPhone! Click now!"))
21# → {'label': 'spam', 'confidence': 0.977}
22
23print(predict("Hey, what time are we meeting for lunch?"))
24# → {'label': 'ham', 'confidence': 0.986}Input text
↓
TF-IDF Vectorizer
• max_features=10,000
• ngram_range=(1,2) ← unigrams + bigrams
• sublinear_tf=True
↓
Logistic Regression
• C=5.0
• class_weight='balanced' ← handles class imbalance
↓
Output: ham / spam + confidence score| Feature | Value |
|---|---|
| 💰 Cost | Free — $0 |
| ⚡ Speed | < 1ms per prediction |
| 💾 Size | ~2 MB total |
| 🖥️ Hardware | CPU only |
| 📦 Dependencies | scikit-learn, huggingface_hub |
Dataset : ucirvine/sms_spam (5,574 messages)
Train : 4,459 messages
Test : 1,115 messages
Time : < 5 seconds on CPU1from transformers import AutoModelForCausalLM, AutoTokenizer
2
3model_id = 'anu56787ty/spam-detector-tfidf-lr'
4tokenizer = AutoTokenizer.from_pretrained(model_id)
5model = AutoModelForCausalLM.from_pretrained(model_id)AutoModelForCausalLM with the appropriate AutoModel class.