-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
58 lines (41 loc) · 1.22 KB
/
app.py
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
import streamlit as st
import pickle
import string
import nltk
from nltk.corpus import stopwords
from nltk.stem.porter import PorterStemmer
# TRANSFORM FUNCTION FOR PREPROCESSING
ps = PorterStemmer()
def transform_mails(mails):
mails = mails.lower()
mails = nltk.word_tokenize(mails)
y = []
for i in mails:
if i.isalnum():
y.append(i)
mails = y[:]
y.clear()
for i in mails:
if i not in stopwords.words('english') and i not in string.punctuation:
y.append(i)
mails = y[:]
y.clear()
for i in mails:
y.append(ps.stem(i))
return " ".join(y)
tfidf = pickle.load(open('vectorizer.pkl', 'rb'))
model = pickle.load(open('model.pkl', 'rb'))
st.title("EMAIL/SMS Spam Classifier")
input_msg = st.text_area("Enter TEXT here")
if st.button('Predict'):
# 1. PREPROCESS TEXT
transformed_msg = transform_mails(input_msg)
# 2. VECTORIZE USING TFIDF
vector_msg = tfidf.transform([transformed_msg])
# 3. PREDICT USING MULTINOMIAL NAIVE BAYES MODEL
result = model.predict(vector_msg)[0]
# 4. DISPLAY THE OUTPUT AS HAM-SPAM TEXT
if result == 1:
st.header("SPAM TEXT")
else:
st.header("NOT A SPAM TEXT - HAM TEXT")