Views
No views yet
spam_classifier_model.pkl - Trained classification modeltfidf_vectorizer.pkl - TF-IDF vectorizer (required for inference)1from huggingface_hub import hf_hub_download
2import pickle
3import re
4
5# Download model and vectorizer
6model_path = hf_hub_download(
7 repo_id="satyam2025/spam-email-classifier",
8 filename="spam_classifier_model.pkl"
9)
10vectorizer_path = hf_hub_download(
11 repo_id="satyam2025/spam-email-classifier",
12 filename="tfidf_vectorizer.pkl"
13)
14
15# Load files
16with open(model_path, 'rb') as f:
17 model = pickle.load(f)
18with open(vectorizer_path, 'rb') as f:
19 vectorizer = pickle.load(f)
20
21# Preprocessing function
22def clean_email_text(text):
23 text = text.lower()
24 text = re.sub(r'\S*@\S*\s?', '', text)
25 text = re.sub(r'http\S+|www\.\S+', '', text)
26 text = re.sub(r'<.*?>', '', text)
27 text = re.sub(r'[^a-zA-Z\s!?.]', ' ', text)
28 text = ' '.join(text.split())
29 return text
30
31# Predict function
32def predict_spam(email_text, threshold=0.8):
33 cleaned = clean_email_text(email_text)
34 features = vectorizer.transform([cleaned])
35 spam_probability = model.predict_proba(features)[0][1]
36 is_spam = spam_probability >= threshold
37 return {
38 'spam_probability': spam_probability,
39 'is_spam': is_spam,
40 'classification': 'SPAM' if is_spam else 'HAM'
41 }
42
43# Example
44email = "Congratulations! You won $1000. Click here now!"
45result = predict_spam(email)
46print(result)
47# Output: {'spam_probability': 0.966, 'is_spam': True, 'classification': 'SPAM'}.pkl files for inference1@misc{spam-classifier-2024,
2 author = {Satyam},
3 title = {Spam Email Classifier},
4 year = {2024},
5 publisher = {HuggingFace},
6 howpublished = {\url{https://huggingface.co/satyam2025/spam-email-classifier}}
7}