-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
85 lines (73 loc) · 2.35 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import json
import streamlit as st
import pickle
import string
from nltk.corpus import stopwords
import nltk
from nltk.stem.porter import PorterStemmer
from streamlit_lottie import st_lottie
import time
# Load NLTK resources
nltk.download('punkt')
nltk.download('stopwords')
# Create a PorterStemmer object
ps = PorterStemmer()
# Load the vectorizer and model from pickle files
tfidf = pickle.load(open('vectorizer.pkl', 'rb'))
model = pickle.load(open('model.pkl', 'rb'))
# Function to load Lottie file
def load_lottiefile(filepath: str):
with open(filepath, 'r') as f:
return json.load(f)
# Function to transform text
def transform_text(text: str) -> str:
text = text.lower()
text = nltk.word_tokenize(text)
text = [word for word in text if word.isalnum()]
text = [word for word in text if word not in stopwords.words('english') and word not in string.punctuation]
text = [ps.stem(word) for word in text]
return ' '.join(text)
# App layout
st.image("logo.svg")
st.title("Identify spam messages with a click of a button")
st.markdown('Protect yourself from *getting spammed* by using this service.')
# Columns for layout
col1, col2, col3 = st.columns((1.5, 0.5, 1))
with col1:
st.header("How it works")
st.markdown("This application uses **Multinomial Naive Bayes** to classify messages into Spam or Not Spam. "
"*The **precision** of the result is 100% and the **accuracy** is 97.2%.*")
with col2:
st.text("")
with col3:
lottie_spam = load_lottiefile("side_image.json")
st_lottie(
lottie_spam,
speed=0.5,
reverse=False,
loop=True,
quality="high",
key=None,
height=250,
width=250,
)
# Input message area
st.header("Email/SMS Spam Classifier")
input_msg = st.text_area("Enter the message")
# Predict button
if st.button('Predict'):
# Preprocess input message
transformed_msg = transform_text(input_msg)
# Vectorize input message
vector_input = tfidf.transform([transformed_msg])
# Predict spam or not spam
result = model.predict(vector_input)[0]
# Display result
if result == 1:
with st.spinner('Wait for it...'):
time.sleep(1)
st.error("This is a Spam message")
else:
with st.spinner('Wait for it...'):
time.sleep(1)
st.success("This is not a Spam Message")