Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added label file upload for text #495

Merged
merged 8 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dashboard/app_data/labels_text.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
negative
positive
52 changes: 37 additions & 15 deletions dashboard/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import utilities
from utilities import MovieReviewsModelRunner
from utilities import _create_html
from utilities import imagenet_class_name
import layouts


Expand All @@ -50,11 +49,6 @@
})
cache.clear()

# global variables # replace by generic label loader
class_name_mnist = ['digit 0', 'digit 1']
class_name_text = ["negative", "positive"]
class_names_imagenet = [imagenet_class_name(idx) for idx in range(1000)]

try:
spacy.load("en_core_web_sm")
except Exception: # If not present, we download
Expand Down Expand Up @@ -156,7 +150,6 @@ def upload_label(filename):
lnames = f.readlines()

labelnames = [item.rstrip() for item in lnames]
print (labelnames)
if labelnames is None or labelnames == ['']:
return html.Div(['Label file is empty, please upload a valid file']), labelnames
return html.Div([f'{filename} uploaded']), labelnames
Expand Down Expand Up @@ -447,13 +440,41 @@ def upload_model_text(contents, filename):
else:
raise PreventUpdate

# uploading labels for model
@app.callback(dash.dependencies.Output('output-label-text-upload', 'children'),
dash.dependencies.Output('upload-label-text', 'labelnames'),
dash.dependencies.Input('upload-label-text', 'filename'))
def upload_label_text(filename):
"""Take in the label file. Return a print statement about upload state."""
labelnames = []
if filename is not None:
try:
if 'txt' in filename:
with open(os.path.join(FOLDER_ON_SERVER, filename),'r',encoding="utf-8") as f:
lnames = f.readlines()

labelnames = [item.rstrip() for item in lnames]
if labelnames is None or labelnames == ['']:
return html.Div(['Label file is empty, please upload a valid file']), labelnames
return html.Div([f'{filename} uploaded']), labelnames

return html.Div([
html.P('File format error!'),
html.Br(),
html.P('Please upload labels in a .txt file.'), labelnames
]), []
except Exception as e:
print(e)
return html.Div(['There was an error processing this file.']), labelnames
else:
raise PreventUpdate

# perform expensive computations in this "global store"
# these computations are cached in a globally available
# redis memory store which is available across processes
# and for all time.
@cache.memoize()
def global_store_t(method_sel, model_runner, input_text,
def global_store_t(method_sel, model_runner, input_text, labelnames,
n_masks=1000, feature_res=6, p_keep=.1,
random_state=2):
"""Perform expersive computation.
Expand All @@ -462,9 +483,9 @@ def global_store_t(method_sel, model_runner, input_text,
return the explanations highlighted on the string itself.
"""
predictions = model_runner(input_text)
class_name = class_name_text
class_name = labelnames
pred_class = class_name[np.argmax(predictions)]
labels = tuple(class_name_text)
labels = tuple(labelnames)
pred_idx = labels.index(pred_class)

# expensive query
Expand Down Expand Up @@ -503,6 +524,7 @@ def select_method_t(method_sel):
dash.dependencies.State("signal_text", "data"),
dash.dependencies.State("upload-model-text", "filename"),
dash.dependencies.State("upload-text", "value"),
dash.dependencies.State('upload-label-text', 'labelnames'),
dash.dependencies.State("n_masks_text", "value"),
dash.dependencies.State("feature_res_text", "value"),
dash.dependencies.State("p_keep_text", "value"),
Expand All @@ -512,7 +534,7 @@ def select_method_t(method_sel):
)
# pylint: disable=too-many-locals
# pylint: disable=unused-argument
def update_multi_options_t(fn_m, input_text, sel_methods, new_model, new_text,
def update_multi_options_t(fn_m, input_text, sel_methods, new_model, new_text, labelnames,
n_masks=1000, feature_res=6, p_keep=0.1,
random_state=2, update_button_t=0, stop_button_t=0):
"""Take in the last model filename, text uploaded, and selected XAI method.
Expand All @@ -539,7 +561,7 @@ def update_multi_options_t(fn_m, input_text, sel_methods, new_model, new_text,
try:
input_tokens = tokenizer.tokenize(input_text)
predictions = model_runner(input_text)
class_name = class_name_text
class_name = labelnames
pred_class = class_name[np.argmax(predictions)]

fig_l = utilities.blank_fig()
Expand All @@ -548,7 +570,7 @@ def update_multi_options_t(fn_m, input_text, sel_methods, new_model, new_text,
for m in sel_methods:
if m == "LIME":
relevances_lime = global_store_t(
m, model_runner, input_text,
m, model_runner, input_text, class_name,
random_state=random_state)
output = _create_html(
input_tokens, relevances_lime[0], max_opacity=0.8)
Expand Down Expand Up @@ -582,7 +604,7 @@ def update_multi_options_t(fn_m, input_text, sel_methods, new_model, new_text,

elif m == "RISE":
relevances_rise = global_store_t(
m, model_runner, input_text,
m, model_runner, input_text, class_name,
n_masks=n_masks, feature_res=feature_res, p_keep=p_keep)
output = _create_html(
input_tokens, relevances_rise[0], max_opacity=0.8)
Expand Down Expand Up @@ -622,7 +644,7 @@ def update_multi_options_t(fn_m, input_text, sel_methods, new_model, new_text,
except Exception as e:
print(e)
return html.Div([
'There was an error running the model. Check either the test' +
'There was an error running the model. Check either the test ' +
'text or the model.'
]), utilities.blank_fig(), utilities.blank_fig()
else:
Expand Down
42 changes: 40 additions & 2 deletions dashboard/layouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,16 +642,54 @@ def get_uploads_text():
className = 'row',
),


# print selected model row
html.Div(id='output-model-text-upload',
className = 'row',
style = {
'height': '230px',
'height': '100px',
'margin-top': '20px',
'color' : colors['blue1']}
)
),

# select label row
html.Div([
dcc.Upload(
id='upload-label-text',
children=html.Div([
'Drag and Drop or ',
html.A('Select Label File')
]),
style={
'width': '80%',
'height': '40px',
'lineHeight': '40px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '3px',
'margin-left': '30px',
'margin-top': '20px',
'color' : colors['blue1']
},
# Allow multiple files to be uploaded
multiple=False
),
],
className = 'row',
),

# print selected model row
html.Div(id='output-label-text-upload',
className = 'row',
style = {
'height': '100px',
'margin-top': '20px',
#'margin-bottom': '50px',
#'fontSize': 8,
'color' : colors['blue1']}
)
],

className = 'three columns',
style = {
'textAlign': 'center',
Expand Down