File size: 1,827 Bytes
721f12c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import streamlit as st
import pickle
import nltk
import string
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer

# Initialize PorterStemmer
ps = PorterStemmer()

# Load the pre-trained model and TF-IDF vectorizer

with open('model.pkl', 'rb') as fil:
    model = pickle.load(fil)
with open('vectorized.pkl', 'rb') as file:
    tfidf = pickle.load(file)


# Define the text preprocessing function
def update_text(text):
    text = text.lower()
    text = nltk.word_tokenize(text)
    y = []
    for i in text:
        if i.isalnum():
            y.append(i)
    text = y[:]
    y.clear()
    for i in text:
        if i not in stopwords.words('english') and i not in string.punctuation:
            y.append(i)
    text = y[:]
    y.clear()
    for i in text:
        y.append(ps.stem(i))

    return " ".join(y)


# Streamlit application title
st.title("Email/SMS Spam Classifier")

# Collecting the SMS text with a larger text area
input_sms = st.text_area("Write the Message", height=150)

# Add a button to trigger the prediction
if st.button("Predict"):
    # Preprocessing the text
    transformed_sms = update_text(input_sms)

    # Ensure the transformed SMS is not empty before vectorizing
    if transformed_sms.strip():
        # Vectorizing the SMS
        vectorized_input = tfidf.transform([transformed_sms])

        # Convert the sparse matrix to a dense format
        vectorized_input_dense = vectorized_input.toarray()

        # Predicting
        try:
            result = model.predict(vectorized_input_dense)[0]
            if result == 1:
                st.header("Spam")
            else:
                st.header("Not Spam")
        except Exception as e:
            st.error(f"Error during prediction: {e}")
    else:
        st.warning("Please enter a valid message.")