-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstreamlit_app.py
60 lines (43 loc) · 1.87 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
53
54
55
56
57
58
59
60
import streamlit as st
import joblib
import pandas as pd
import model_tf_idf
import lime
from lime.lime_text import LimeTextExplainer
import streamlit.components.v1 as components
st.title('Hate Speech Recognition')
###################################################
stopwords = model_tf_idf.stopwords
sentence = st.text_input('Input your tweet here:')
if sentence:
clf = joblib.load('model_lr_tfidf.pkl')
count_vect = joblib.load('tf_idf_vect.pkl')
review_tweet = model_tf_idf.clean_text(sentence)
pred = clf.predict(count_vect.transform([review_tweet]))
if pred[0] == 0:
prediction = "Hate Tweet"
elif pred[0] == 1:
prediction = "Offensive"
else:
prediction = ' Neither hate nor Offensive'
st.write(prediction)
if st.checkbox('Explanation'):
try:
explainer = LimeTextExplainer(class_names=['Hate', 'Offensive', 'Normal tweet'])
def pred_fn(text):
text_transformed = count_vect.transform(text)
return clf.predict_proba(text_transformed)
explanation = explainer.explain_instance(sentence, classifier_fn=pred_fn, top_labels=3)
explanation.save_to_file('lime_report.html')
HtmlFile = open("lime_report.html", 'r', encoding='utf-8')
source_code = HtmlFile.read()
components.html(source_code, height=1000)
except:
st.write("Enter your tweet to obtain the explanation")
if st.checkbox('Show class labels'):
st.write(pd.DataFrame({
'class Label': ['Hate Tweet', 'Offensive', 'Neither'],
'Output': ['Hate speech is towards detecting the more prevalent forms of hate speech',
'Offensive Tweet consider words that are sexist and derogatory',
'Tweets with overall positive sentiment and are more like to belong to neither class', ]
}))