-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
54 lines (43 loc) · 1.19 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
import streamlit as st
import pickle
from nltk import word_tokenize as wt
from nltk.corpus import stopwords
import string
from nltk.stem.porter import PorterStemmer
import nltk
nltk.download('punkt')
nltk.download('stopwords')
st.title('SMS Spam Detector')
sms = st.text_area('Enter The Message')
tfidf = pickle.load(open('vectorizer.pkl','rb'))
model = pickle.load(open('model.pkl','rb'))
#preprocess
ps=PorterStemmer()
def transform_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) and (len(i)!=1) and (i.isdigit()==False):
y.append(i)
text = y[:]
y.clear()
for i in text:
y.append(ps.stem(i))
return ' '.join(y)
if st.button('Detect'):
trans_sms=transform_text(sms)
#vectorize
vector = tfidf.transform([trans_sms])
#predict
pred=model.predict(vector)
#display
if pred == 1:
st.markdown('<h2 style="color:red">Spam</h2>',unsafe_allow_html=True)
else:
st.markdown('<h2 style="color:green">Not Spam</h2>',unsafe_allow_html=True)