-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathapp.py
More file actions
59 lines (44 loc) · 1.72 KB
/
Copy pathapp.py
File metadata and controls
59 lines (44 loc) · 1.72 KB
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
import streamlit as st
import sklearn
import pickle
import string
import re
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize, sent_tokenize
nltk.download('stopwords')
nltk.download('punkt')
nltk.download('punkt_tab')
from nltk.stem import PorterStemmer
port_stemmer = PorterStemmer()
tfidf = pickle.load(open('vectorizer.pkl', 'rb'))
model = pickle.load(open('model.pkl', 'rb'))
# Create a function to generate cleaned data from raw text
def clean_text(text):
text = word_tokenize(text) # Create tokens
text= " ".join(text) # Join tokens
text = [char for char in text if char not in string.punctuation] # Remove punctuations
text = ''.join(text) # Join the leters
text = [char for char in text if char not in re.findall(r"[0-9]", text)] # Remove Numbers
text = ''.join(text) # Join the leters
text = [word.lower() for word in text.split() if word.lower() not in set(stopwords.words('english'))] # Remove common english words (I, you, we,...)
text = ' '.join(text) # Join the leters
text = list(map(lambda x: port_stemmer.stem(x), text.split()))
return " ".join(text) # error word
st.title('SMS Spam Classifier')
input_sms = st.text_input("Enter the Message")
if st.button('Predict'):
if input_sms == "":
st.header('Please Enter Your Message !!!')
else:
# 1. Preprocess
transform_text = clean_text(input_sms)
# 2. Vectorize
vector_input = tfidf.transform([transform_text])
# 3. Prediction
result = model.predict(vector_input)
# 4. Display
if result == 1:
st.header("Spam")
else:
st.header("Not Spam")