-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
51 lines (37 loc) · 1.27 KB
/
app.py
File metadata and controls
51 lines (37 loc) · 1.27 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
import streamlit as st
import sklearn
import pickle
import string
from nltk.corpus import stopwords
import nltk, re
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
def transform_message(text):
text = text.lower()
def clean_text(text):
rgx = r"[^A-Za-z0-9\s\.]"
return re.sub(rgx, '', text)
def remove_stop_words(text):
tokens = text.split()
tokens = [word for word in tokens if word not in stopwords.words('english')]
return ' '.join(tokens)
lemmatizer = WordNetLemmatizer()
def lemmatize_text(text):
return ' '.join([lemmatizer.lemmatize(word) for word in text.split()])
return lemmatize_text(remove_stop_words(clean_text(text)))
tfidf = pickle.load(open('vectorizer.pkl', 'rb'))
model = pickle.load(open('model.pkl', 'rb'))
st.title('Email Spam Classifier')
input_sms = st.text_area("Enter the message")
if st.button('Predict'):
# 1. preprocess
transformed_sms = transform_message(input_sms)
# 2. vectorizer
vector_input = tfidf.transform([transformed_sms])
# 3. predict
result = model.predict(vector_input)[0]
# 4. Display
if result == 1:
st.header('SPAM')
else:
st.header('NOT SPAM')