File size: 964 Bytes
42bb626 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
import joblib
import re
import string
# 🧹 Reuse the same clean_text function
def clean_text(text):
text = text.lower()
text = re.sub(r'\d+', '', text) # remove numbers
text = text.translate(str.maketrans('', '', string.punctuation)) # remove punctuation
text = text.strip()
return text
# 💾 Load the saved model and vectorizer
model = joblib.load("spam_detection_model.pkl")
vectorizer = joblib.load("spam_detection_vectorizer.pkl")
# 💬 Function to predict a new message
def predict_message(msg):
msg_clean = clean_text(msg)
msg_vec = vectorizer.transform([msg_clean])
pred = model.predict(msg_vec)[0]
return "🚨 Spam" if pred == 1 else "✅ Not Spam"
# 🧪 Test with some examples
print(predict_message("Congratulations! You have won a car"))
print(predict_message("Hey, click here to claim your reward"))
print(predict_message("Exclusive offer! Click here to claim your reward now")) |