Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import nltk
|
2 |
+
nltk.download('punkt')
|
3 |
+
nltk.download('stopwords')
|
4 |
+
|
5 |
+
import streamlit as st
|
6 |
+
import pickle
|
7 |
+
import string
|
8 |
+
from nltk.corpus import stopwords
|
9 |
+
from nltk.stem.porter import PorterStemmer
|
10 |
+
|
11 |
+
ps = PorterStemmer()
|
12 |
+
|
13 |
+
|
14 |
+
def transform_text(text):
|
15 |
+
text = text.lower()
|
16 |
+
text = nltk.word_tokenize(text)
|
17 |
+
|
18 |
+
y = []
|
19 |
+
for i in text:
|
20 |
+
if i.isalnum():
|
21 |
+
y.append(i)
|
22 |
+
|
23 |
+
text = y[:]
|
24 |
+
y.clear()
|
25 |
+
|
26 |
+
for i in text:
|
27 |
+
if i not in stopwords.words('english') and i not in string.punctuation:
|
28 |
+
y.append(i)
|
29 |
+
|
30 |
+
text = y[:]
|
31 |
+
y.clear()
|
32 |
+
|
33 |
+
for i in text:
|
34 |
+
y.append(ps.stem(i))
|
35 |
+
|
36 |
+
return " ".join(y)
|
37 |
+
|
38 |
+
|
39 |
+
tk = pickle.load(open("vectorizer.pkl", 'rb'))
|
40 |
+
model = pickle.load(open("model.pkl", 'rb'))
|
41 |
+
|
42 |
+
st.title("SMS Spam Detection Model")
|
43 |
+
st.write("*Made with ❤️🔥 by Shrudex👨🏻💻*")
|
44 |
+
|
45 |
+
|
46 |
+
input_sms = st.text_input("Enter the SMS")
|
47 |
+
|
48 |
+
if st.button('Predict'):
|
49 |
+
|
50 |
+
# 1. preprocess
|
51 |
+
transformed_sms = transform_text(input_sms)
|
52 |
+
# 2. vectorize
|
53 |
+
vector_input = tk.transform([transformed_sms])
|
54 |
+
# 3. predict
|
55 |
+
result = model.predict(vector_input)[0]
|
56 |
+
# 4. Display
|
57 |
+
if result == 1:
|
58 |
+
st.header("Spam")
|
59 |
+
else:
|
60 |
+
st.header("Not Spam")
|