-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamlit_app.py
52 lines (37 loc) · 1.43 KB
/
streamlit_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
import re
import joblib
import numpy as np
import streamlit as st
import streamlit.components.v1 as components
from lime.lime_text import LimeTextExplainer
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.neural_network import MLPClassifier
# create a header
st.write("# Spam Detection Engine")
# adding text input
message_text = st.text_input("Enter a message for spam evaluation")
# loading the model
def preprocessor(text):
text = re.sub("<[^>]*>", "", text)
emoticons = re.findall("(?::|;|=)(?:-)?(?:\)|\(|D|P)", text)
text = re.sub("[\W]+", " ", text.lower()) + " ".join(emoticons).replace("-", "")
return text
model = joblib.load("spam_classifier.joblib")
class_names = list(model.classes_)
# Generating and Displaying Predictions
def classify_message(model, message):
label = model.predict([message])[0]
probs = model.predict_proba([message])
return {"label": label, "probability": probs[0][class_names.index(label)]}
if message_text != "":
result = classify_message(model, message_text)
st.write(result)
# explaining predictions with lime
explain_pred = st.button("Explain Predictions")
if explain_pred:
with st.spinner("Generating explanations"):
explainer = LimeTextExplainer(class_names=class_names)
exp = explainer.explain_instance(
message_text, model.predict_proba, num_features=10
)
components.html(exp.as_html(), height=800)