Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,56 +1,113 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import pickle
|
3 |
-
import string
|
4 |
-
from nltk.corpus import stopwords
|
5 |
-
import nltk
|
6 |
-
from nltk.stem.porter import PorterStemmer
|
7 |
-
|
8 |
-
ps = PorterStemmer()
|
9 |
-
|
10 |
-
def transform_text(text):
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
tfidf = pickle.load(open('vectorizer.pkl', 'rb'))
|
35 |
-
model = pickle.load(open('model.pkl', 'rb'))
|
36 |
-
|
37 |
-
st.title("Email/SMS SPAM Classifier")
|
38 |
-
|
39 |
-
input_sms = st.text_area("Enter the message")
|
40 |
-
|
41 |
-
if st.button('Predict'):
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import streamlit as st
|
2 |
+
# import pickle
|
3 |
+
# import string
|
4 |
+
# from nltk.corpus import stopwords
|
5 |
+
# import nltk
|
6 |
+
# from nltk.stem.porter import PorterStemmer
|
7 |
+
|
8 |
+
# ps = PorterStemmer()
|
9 |
+
|
10 |
+
# def transform_text(text):
|
11 |
+
# text = text.lower()
|
12 |
+
# text = nltk.word_tokenize(text)
|
13 |
+
|
14 |
+
# y = []
|
15 |
+
# for i in text:
|
16 |
+
# if i.isalnum():
|
17 |
+
# y.append(i)
|
18 |
+
|
19 |
+
# text = y[:]
|
20 |
+
# y.clear()
|
21 |
+
|
22 |
+
# for i in text:
|
23 |
+
# if i not in stopwords.words('english') and i not in string.punctuation:
|
24 |
+
# y.append(i)
|
25 |
+
|
26 |
+
# text = y[:]
|
27 |
+
# y.clear()
|
28 |
+
|
29 |
+
# for i in text:
|
30 |
+
# y.append(ps.stem(i))
|
31 |
+
|
32 |
+
# return ' '.join(y)
|
33 |
+
|
34 |
+
# tfidf = pickle.load(open('vectorizer.pkl', 'rb'))
|
35 |
+
# model = pickle.load(open('model.pkl', 'rb'))
|
36 |
+
|
37 |
+
# st.title("Email/SMS SPAM Classifier")
|
38 |
+
|
39 |
+
# input_sms = st.text_area("Enter the message")
|
40 |
+
|
41 |
+
# if st.button('Predict'):
|
42 |
+
|
43 |
+
# # 1. preprocess
|
44 |
+
# transform_sms = transform_text(input_sms)
|
45 |
+
# # 2. vectorize
|
46 |
+
# vector_input = tfidf.transform([transform_sms])
|
47 |
+
# # 3. predict
|
48 |
+
# result = model.predict(vector_input)[0]
|
49 |
+
# # 4. Display
|
50 |
+
# if result == 1:
|
51 |
+
# st.header("Spam")
|
52 |
+
# else:
|
53 |
+
# st.header("Not Spam")
|
54 |
+
|
55 |
+
import streamlit as st
|
56 |
+
import pickle
|
57 |
+
import string
|
58 |
+
import nltk
|
59 |
+
from nltk.corpus import stopwords
|
60 |
+
from nltk.stem.porter import PorterStemmer
|
61 |
+
|
62 |
+
# Download NLTK resources if not already downloaded
|
63 |
+
nltk.download('punkt')
|
64 |
+
nltk.download('stopwords')
|
65 |
+
|
66 |
+
ps = PorterStemmer()
|
67 |
+
|
68 |
+
def transform_text(text):
|
69 |
+
text = text.lower()
|
70 |
+
text = nltk.word_tokenize(text)
|
71 |
+
|
72 |
+
y = []
|
73 |
+
for i in text:
|
74 |
+
if i.isalnum():
|
75 |
+
y.append(i)
|
76 |
+
|
77 |
+
text = y[:]
|
78 |
+
y.clear()
|
79 |
+
|
80 |
+
for i in text:
|
81 |
+
if i not in stopwords.words('english') and i not in string.punctuation:
|
82 |
+
y.append(i)
|
83 |
+
|
84 |
+
text = y[:]
|
85 |
+
y.clear()
|
86 |
+
|
87 |
+
for i in text:
|
88 |
+
y.append(ps.stem(i))
|
89 |
+
|
90 |
+
return ' '.join(y)
|
91 |
+
|
92 |
+
tfidf = pickle.load(open('vectorizer.pkl', 'rb'))
|
93 |
+
model = pickle.load(open('model.pkl', 'rb'))
|
94 |
+
|
95 |
+
st.title("Email/SMS SPAM Classifier")
|
96 |
+
|
97 |
+
input_sms = st.text_area("Enter the message")
|
98 |
+
|
99 |
+
if st.button('Predict'):
|
100 |
+
|
101 |
+
# 1. preprocess
|
102 |
+
transform_sms = transform_text(input_sms)
|
103 |
+
# 2. vectorize
|
104 |
+
vector_input = tfidf.transform([transform_sms])
|
105 |
+
# 3. predict
|
106 |
+
result = model.predict(vector_input)[0]
|
107 |
+
# 4. Display
|
108 |
+
if result == 1:
|
109 |
+
st.header("Spam")
|
110 |
+
else:
|
111 |
+
st.header("Not Spam")
|
112 |
+
|
113 |
+
|