Update app.py
Browse files
app.py
CHANGED
|
@@ -1,128 +1,130 @@
|
|
| 1 |
-
import streamlit as st
|
| 2 |
-
import pickle
|
| 3 |
-
import string
|
| 4 |
-
from nltk.corpus import stopwords
|
| 5 |
-
import nltk
|
| 6 |
-
from nltk.stem.porter import PorterStemmer
|
| 7 |
-
|
| 8 |
-
ps = PorterStemmer()
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
st.
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
background-
|
| 23 |
-
background-
|
| 24 |
-
background-
|
| 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 |
-
st.
|
| 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 |
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pickle
|
| 3 |
+
import string
|
| 4 |
+
from nltk.corpus import stopwords
|
| 5 |
+
import nltk
|
| 6 |
+
from nltk.stem.porter import PorterStemmer
|
| 7 |
+
|
| 8 |
+
ps = PorterStemmer()
|
| 9 |
+
|
| 10 |
+
nltk.download('punkt')
|
| 11 |
+
|
| 12 |
+
tfidf = pickle.load(open("vectorizer.pkl", "rb"))
|
| 13 |
+
model = pickle.load(open("model.pkl", "rb"))
|
| 14 |
+
|
| 15 |
+
#st.title("EMail/SMS SPAM CLASSIFIER")
|
| 16 |
+
#input_sms = st.text_area("Enter the Message", height=200)
|
| 17 |
+
|
| 18 |
+
st.markdown(
|
| 19 |
+
f"""
|
| 20 |
+
<style>
|
| 21 |
+
body {{
|
| 22 |
+
background-image: url('D:\machine learning projects\email sms spam classifier\background.jpg') !important;
|
| 23 |
+
background-size: cover !important;
|
| 24 |
+
background-position: center !important;
|
| 25 |
+
background-repeat: no-repeat !important;
|
| 26 |
+
background-attachment: fixed !important;
|
| 27 |
+
}}
|
| 28 |
+
</style>
|
| 29 |
+
""",
|
| 30 |
+
unsafe_allow_html=True
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
st.markdown(
|
| 34 |
+
"""
|
| 35 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-icons/1.10.5/font/bootstrap-icons.min.css">
|
| 36 |
+
<h1 style="color: #3498db; font-weight: bold; text-align: center; font-size: 36px;">
|
| 37 |
+
<i class="bi bi-envelope-fill" style="vertical-align: middle; font-size: 36px; margin-right: 10px;"></i>
|
| 38 |
+
EMail/SMS SPAM DETECTOR
|
| 39 |
+
|
| 40 |
+
<h1 style="color: #008000; font-weight: normal; text-align: center; font-size: 15px;">
|
| 41 |
+
Protect Your Inbox from Spam
|
| 42 |
+
|
| 43 |
+
</h1>
|
| 44 |
+
""",
|
| 45 |
+
unsafe_allow_html=True
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
if 'input_sms' not in st.session_state:
|
| 49 |
+
st.session_state.input_sms = ""
|
| 50 |
+
|
| 51 |
+
# Create a text area with the current text in session state
|
| 52 |
+
st.session_state.input_sms = st.text_area("Enter your text here:", key="textarea", placeholder="Type here...",
|
| 53 |
+
value=st.session_state.input_sms, height=200)
|
| 54 |
+
|
| 55 |
+
st.markdown(
|
| 56 |
+
"""
|
| 57 |
+
<style>
|
| 58 |
+
textarea {
|
| 59 |
+
background-color: #2b2b2b !important;
|
| 60 |
+
color: white !important;
|
| 61 |
+
border-radius: 10px !important;
|
| 62 |
+
border: 2px solid #4CAF50 !important;
|
| 63 |
+
font-size: 16px !important;
|
| 64 |
+
padding: 10px !important;
|
| 65 |
+
}
|
| 66 |
+
</style>
|
| 67 |
+
""",
|
| 68 |
+
unsafe_allow_html=True,
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
# Create a "Clear Text" button
|
| 72 |
+
col1, col2, col3, col4 = st.columns(4)
|
| 73 |
+
|
| 74 |
+
with col3:
|
| 75 |
+
st.write("Double click")
|
| 76 |
+
if st.button("Clear Text"):
|
| 77 |
+
st.session_state.input_sms = "" # Clear the text
|
| 78 |
+
|
| 79 |
+
# Display the current text (optional)
|
| 80 |
+
#st.write("Current text:", st.session_state.input_sms)
|
| 81 |
+
|
| 82 |
+
with col4:
|
| 83 |
+
st.write(f"You wrote {len(st.session_state.input_sms)} characters.")
|
| 84 |
+
|
| 85 |
+
# 1. Preprocess the text
|
| 86 |
+
|
| 87 |
+
def transformed_text(text):
|
| 88 |
+
text = text.lower()
|
| 89 |
+
text = nltk.word_tokenize(text)
|
| 90 |
+
|
| 91 |
+
y = []
|
| 92 |
+
for i in text:
|
| 93 |
+
if i.isalnum():
|
| 94 |
+
y.append(i)
|
| 95 |
+
|
| 96 |
+
text = y[:]
|
| 97 |
+
y.clear()
|
| 98 |
+
|
| 99 |
+
for i in text:
|
| 100 |
+
if i not in stopwords.words("English") and i not in string.punctuation:
|
| 101 |
+
y.append(i)
|
| 102 |
+
|
| 103 |
+
text = y[:]
|
| 104 |
+
y.clear()
|
| 105 |
+
|
| 106 |
+
for i in text:
|
| 107 |
+
y.append(ps.stem(i))
|
| 108 |
+
|
| 109 |
+
return " ".join(y)
|
| 110 |
+
|
| 111 |
+
transformed_sms = transformed_text(st.session_state.input_sms)
|
| 112 |
+
|
| 113 |
+
# 2. Vectorize the text
|
| 114 |
+
|
| 115 |
+
vectorize_input = tfidf.transform([transformed_sms])
|
| 116 |
+
|
| 117 |
+
# 3. Predict
|
| 118 |
+
|
| 119 |
+
result = model.predict(vectorize_input)[0]
|
| 120 |
+
|
| 121 |
+
# 4. Check the output
|
| 122 |
+
with col2:
|
| 123 |
+
st.write("Check result")
|
| 124 |
+
if st.button("Predict"):
|
| 125 |
+
if result == 1:
|
| 126 |
+
st.subheader(":red[SPAM]")
|
| 127 |
+
|
| 128 |
+
else:
|
| 129 |
+
st.subheader(":green[NOT SPAM]")
|
| 130 |
|