-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
201 lines (159 loc) · 4.79 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import streamlit as st
from streamlit_lottie import st_lottie
import pickle
import joblib
import numpy as np
import requests
from PIL import Image
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer,WordNetLemmatizer
import nltk
import string
nltk.download('stopwords')
nltk.download('punkt')
nltk.download('wordnet')
image = Image.open('./img/spam.png')
st.set_page_config(page_title='Spam Detection', page_icon=image)
def load_lottie(url):
r = requests.get(url)
if r.status_code != 200:
return None
return r.json()
# Initialize stemmer and lemmatizer
stemmer = PorterStemmer()
lemmatizer = WordNetLemmatizer()
# Define function to preprocess text
def preprocess_text(text):
# Convert to lowercase
text = text.lower()
# Remove punctuation
text = "".join([char for char in text if char not in string.punctuation])
# Remove stopwords
stop_words = set(stopwords.words('english'))
text = " ".join([word for word in text.split() if word not in stop_words])
# Tokenize
tokens = nltk.word_tokenize(text)
# Stem and lemmatize
lemmatized_tokens = [lemmatizer.lemmatize(token) for token in tokens]
stemmed_tokens = [stemmer.stem(token) for token in lemmatized_tokens]
# Join tokens back into text string
preprocessed_text = " ".join(stemmed_tokens)
return preprocessed_text
# load the saved objects
cv = pickle.load(open('count_vectorizer.pkl', 'rb'))
model = joblib.load(open('model_NB','rb'))
st.title("Spam Detection System")
st.write("\t\t\tV 1.0")
st.write('---')
animation_spam = load_lottie('https://assets7.lottiefiles.com/private_files/lf30_prqvme9e.json')
with st.container():
right_column, left_column = st.columns(2)
with right_column:
# Enter the message you want to classify
new_message = st.text_area('Enter your message :', height=100)
if st.button('Predict'):
# Doind preprocessing on the text I get
new_message = preprocess_text(new_message)
# transform the new message using the loaded CountVectorizer object
new_bow_features = cv.transform([new_message])
# Predict
result = model.predict(new_bow_features)
# Display
if result == 1:
st.error("Spam")
elif result == 0:
st.success("Ham 'Not Spam'")
with left_column:
st_lottie(animation_spam, speed=0.5, height=250, key="secoend")
st.write('---')
st.write('')
animation_contact = load_lottie("https://assets5.lottiefiles.com/packages/lf20_ejryvjev.json")
with st.container():
right_column, left_column = st.columns(2)
with right_column:
st.write('')
st.write('')
st.write('')
st.write('_For any issue contact me via :_')
'''
[![Github](https://skillicons.dev/icons?i=github)](https://github.com/aliabdallah7)
[![LinkedIn](https://skillicons.dev/icons?i=linkedin)](https://www.linkedin.com/in/ali-abdallah7/)
[![Gmail](https://img.icons8.com/color/70/gmail-new.png)](mailto:ali.abdallah43792@gmail.com)
'''
with left_column:
st_lottie(animation_contact, speed=1, height=200, key="third")
footer = """<style>
div.stButton > button:first-child {
padding: 10px 20px;
border: 2px solid #ff4d4d;
background-color: #ffcdd2;
color: #c62828;
font-size: 16px;
font-weight: bold;
border-radius: 5px;
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.5);
transition: all 0.2s ease;
}
div.stButton > button:hover:first-child {
background-color: #ff4d4d;
color: #fff;
box-shadow: 0px 2px 8px rgba(0, 0, 0, 0.8);
}
div.stButton > button:active:first-child {
background-color: #c62828;
color: #fff;
box-shadow: inset 0px 2px 5px rgba(0, 0, 0, 0.5);
}
header {visibility: hidden;}
/* Light mode styles */
.my-paragraph {
color: black;
}
/* Dark mode styles */
@media (prefers-color-scheme: dark) {
.my-paragraph {
color: white;
}
}
a:link , a:visited{
color: #5C5CFF;
background-color: transparent;
text-decoration: none;
}
a:hover, a:active {
color: red;
background-color: transparent;
text-decoration: underline;
}
:root {
--footer-bg-color: #333;
}
@media (prefers-color-scheme: dark) {
:root {
--footer-bg-color: rgb(14, 17, 23);
}
}
@media (prefers-color-scheme: light) {
:root {
--footer-bg-color: white;
}
}
.footer {
position: fixed;
left: 0;
bottom: 0;
width: 100%;
background-color: var(--footer-bg-color);
color: black;
text-align: center;
}
</style>
<div class="footer">
<p class="my-paragraph">© 2023 <a href="https://www.linkedin.com/in/ali-abdallah7/"> Ali Abdallah</a></p>
</div>
"""
st.markdown(footer,unsafe_allow_html=True)
st.markdown("<br>",unsafe_allow_html=True)