File size: 1,700 Bytes
8f460b5
1840ab8
308314b
 
 
d481617
483b677
308314b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
483b677
308314b
 
 
 
 
f4ba322
308314b
 
 
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import streamlit as st
import pandas as pd
import joblib
import re
import string

# Page configuration
st.set_page_config(page_title="SMS Spam Detector", layout="centered")
st.title("📩 SMS Spam Detection App")
st.markdown("🔍 Enter a message below to check if it's **Spam** or **Not Spam (Ham)**")

# --- Load Model and Vectorizer ---
model = joblib.load("model/spam_model.pkl")           # Make sure path is correct
vectorizer = joblib.load("model/tfidf_vectorizer.pkl")  # Adjust as per your folder

# --- Text Cleaning Function ---
def clean_text(text):
    text = text.lower()
    text = re.sub(r"http\S+|www\S+|https\S+", '', text, flags=re.MULTILINE)
    text = re.sub(r'\@w+|\#','', text)
    text = re.sub(r'[^\w\s]', '', text)
    text = re.sub(r'\d+', '', text)
    text = text.translate(str.maketrans('', '', string.punctuation))
    return text.strip()

# --- Prediction Function ---
def predict_spam(message):
    cleaned = clean_text(message)
    vector = vectorizer.transform([cleaned])
    prediction = model.predict(vector)
    return "Spam" if prediction[0] == 1 else "Not Spam"

# --- Input Section ---
user_input = st.text_area("✉️ Enter your SMS message here:")

if st.button("Check Message"):
    if user_input.strip() == "":
        st.warning("⚠️ Please enter a valid message.")
    else:
        result = predict_spam(user_input)
        if result == "Spam":
            st.error("🚫 This message is classified as **SPAM**.")
        else:
            st.success("✅ This message is classified as **NOT SPAM (HAM)**.")

# Footer
st.markdown("---")
st.markdown("🔒 **Note**: This is a demo model and not intended for production use without proper testing.")