-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
42 lines (35 loc) · 1.53 KB
/
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
import streamlit as st
import streamlit.components.v1 as components
from lime_explainer import explainer, tokenizer, METHODS
def format_dropdown_labels(val):
return METHODS[val]['name']
# Define page settings
st.beta_set_page_config(
page_title='LIME explainer app for classification models',
# layout="wide"
)
# Build app
title_text = 'LIME Explainer Dashboard for Fine-grained Sentiment'
subheader_text = '''1: Strongly Negative   2: Weakly Negative   3: Neutral   4: Weakly Positive   5: Strongly Positive'''
st.markdown(f"<h2 style='text-align: center;'><b>{title_text}</b></h2>", unsafe_allow_html=True)
st.markdown(f"<h5 style='text-align: center;'>{subheader_text}</h5>", unsafe_allow_html=True)
st.text("")
input_text = st.text_input('Enter your text:', "")
n_samples = st.text_input('Number of samples to generate for LIME explainer: (For really long input text, go up to 5000)', value=1000)
method_list = tuple(label for label, val in METHODS.items())
method = st.selectbox(
'Choose classifier:',
method_list,
index=4,
format_func=format_dropdown_labels,
)
if st.button("Explain Results"):
with st.spinner('Calculating...'):
text = tokenizer(input_text)
exp = explainer(method,
path_to_file=METHODS[method]['file'],
text=text,
lowercase=METHODS[method]['lowercase'],
num_samples=int(n_samples))
# Display explainer HTML object
components.html(exp.as_html(), height=800)