musk12 commited on
Commit
aec580f
·
verified ·
1 Parent(s): 7005bf0

Update app.py

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