-
Notifications
You must be signed in to change notification settings - Fork 1
/
spam_detector_taipy.py
78 lines (63 loc) · 1.83 KB
/
spam_detector_taipy.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
from taipy.gui import Gui, notify
import pickle
import string
from nltk.corpus import stopwords
import nltk
from nltk.stem import WordNetLemmatizer
lemma = WordNetLemmatizer()
def transform_text(text):
text = text.lower()
text = nltk.word_tokenize(text)
y = []
for i in text:
if i.isalnum():
y.append(i)
text = y[:]
y.clear()
for i in text:
if i not in stopwords.words('english') and i not in string.punctuation:
y.append(i)
text = y[:]
y.clear()
for i in text:
y.append(lemma.lemmatize(i))
return " ".join(y)
tfidf = pickle.load(open('vectorizer.pkl','rb'))
model = pickle.load(open('model.pkl','rb'))
intro = "Enter the message:"
dummy = " "
message = " "
h_text = "Click to classify the spam or ham mail/sms"
page = """
<|text-center|
#
# Email/SMS **Spam**{:.color-primary} Detector
|>
<|{intro}|>
#
<|{dummy}|input|multiline|class_name=fullwidth|label=Hi there! Rohan Sharma this side!|>
#
<|text-center|
<|Predict|button|on_action=on_button_action|hover_text = Click to classify the spam or ham mail/sms|center|>
#
<|{message}|input|not active|label= Know the results here!|>
######Made with **LOVE**{:.color-primary} by Rohan Sharma
|>
"""
def on_button_action(state):
notify(state, 'info', f'Results may be inaccurate due to limited dataset. Thanks for using this Application :p')
state.text = "Rendering results"
def on_change(state):
# 1. preprocess
transformed_sms = transform_text(intro)
# 2. vectorize
vector_input = tfidf.transform([transformed_sms])
# 3. predict
result = model.predict(vector_input)[0]
# 4. Display
if result == 1:
state.message = "Spam"
else:
state.message ="Not-Spam"
if __name__ == "__main__":
app = Gui(page).run(watermark="""Taipy Inside :)""",use_reloader=True)