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

Refactor dashboard #502

Merged
merged 4 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ venv
venv3

.python-version

cache/
dashboard/cache/
60 changes: 28 additions & 32 deletions dashboard/callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@
import base64
import os
import warnings
import dash
import numpy as np
# Onnx
import onnx
# Plotly
import dash
import plotly.graph_objects as go
from plotly.subplots import make_subplots
import plotly.express as px
import plotly.graph_objects as go
import spacy
import utilities
from dash import html
from dash.exceptions import PreventUpdate
from flask_caching import Cache
from html2image import Html2Image
# Dash&Flask
from jupyter_dash import JupyterDash
from layouts.fig import blank_fig
from layouts.styles import COLORS
from onnx_tf.backend import prepare
# Others
from PIL import Image
import dianna
from dianna.utils.tokenizers import SpacyTokenizer
import utilities
from plotly.subplots import make_subplots
from utilities import MovieReviewsModelRunner
from utilities import _create_html
import layouts
import dianna
from dianna.utils.tokenizers import SpacyTokenizer


warnings.filterwarnings('ignore') # disable warnings relateds to tf versions
Expand Down Expand Up @@ -84,27 +81,27 @@ def upload_image(contents, filename):
height=300,
title=f"{filename[0]} uploaded",
title_x=0.5,
title_font_color=layouts.colors['blue1'])
title_font_color=COLORS['blue1'])

fig.update_xaxes(showgrid=False, showticklabels=False,
zeroline=False)
fig.update_yaxes(showgrid=False, showticklabels=False,
zeroline=False)

fig.layout.paper_bgcolor = layouts.colors['blue4']
fig.layout.paper_bgcolor = COLORS['blue4']

return fig

return utilities.blank_fig(
return blank_fig(
text='File format error! <br><br>Please upload only images' +
'in one of the following formats:' + extensions)

except Exception as e:
print(e)
return utilities.blank_fig(
return blank_fig(
text='There was an error processing this file.')
else:
return utilities.blank_fig()
return blank_fig()


# uploading model for image
Expand Down Expand Up @@ -263,7 +260,7 @@ def update_multi_options_i(fn_m, fn_i, sel_methods, new_model, new_image, label
ctx = dash.callback_context
if ctx.triggered[0]["prop_id"] == "stop_button.n_clicks":
return (html.Div(['Explanation stopped.'],
style={'margin-top': '60px'}), utilities.blank_fig())
style={'margin-top': '60px'}), blank_fig())

# update graph
if (fn_m and fn_i) is not None and (sel_methods != []):
Expand Down Expand Up @@ -361,7 +358,7 @@ def update_multi_options_i(fn_m, fn_i, sel_methods, new_model, new_image, label
fig.update_layout(
width=650,
height=(200*n_rows+50),
paper_bgcolor=layouts.colors['blue4'])
paper_bgcolor=COLORS['blue4'])

fig.update_xaxes(showgrid=False, showticklabels=False,
zeroline=False)
Expand All @@ -383,11 +380,11 @@ def update_multi_options_i(fn_m, fn_i, sel_methods, new_model, new_image, label
return (html.Div(
['There was an error running the model. Check ' +
'either the test image or the model.']),
utilities.blank_fig())
blank_fig())
else:
return (html.Div(['Missing model, image or XAI method.'],
style={'margin-top': '60px'}),
utilities.blank_fig())
blank_fig())

###################################################################

Expand Down Expand Up @@ -546,7 +543,7 @@ def update_multi_options_t(fn_m, input_text, sel_methods, new_model, new_text, l
if ctx.triggered[0]["prop_id"] == "stop_button_t.n_clicks":
return (
html.Div(['Explanation stopped.'], style={'margin-top': '60px'}),
utilities.blank_fig(), utilities.blank_fig())
blank_fig(), blank_fig())

# update text explanations
if (fn_m and input_text) is not None and (sel_methods != []):
Expand All @@ -564,8 +561,8 @@ def update_multi_options_t(fn_m, input_text, sel_methods, new_model, new_text, l
class_name = labelnames
pred_class = class_name[np.argmax(predictions)]

fig_l = utilities.blank_fig()
fig_r = utilities.blank_fig()
fig_l = blank_fig()
fig_r = blank_fig()

for m in sel_methods:
if m == "LIME":
Expand All @@ -591,9 +588,9 @@ def update_multi_options_t(fn_m, input_text, sel_methods, new_model, new_text, l
showticklabels=False, zeroline=False)
fig_l.update_layout(
title='LIME explanation:',
title_font_color=layouts.colors['blue1'],
paper_bgcolor=layouts.colors['blue4'],
plot_bgcolor=layouts.colors['blue4'],
title_font_color=COLORS['blue1'],
paper_bgcolor=COLORS['blue4'],
plot_bgcolor=COLORS['blue4'],
height=200,
width=500,
margin_b=40,
Expand Down Expand Up @@ -622,9 +619,9 @@ def update_multi_options_t(fn_m, input_text, sel_methods, new_model, new_text, l
showticklabels=False, zeroline=False)
fig_r.update_layout(
title='RISE explanation:',
title_font_color=layouts.colors['blue1'],
paper_bgcolor=layouts.colors['blue4'],
plot_bgcolor=layouts.colors['blue4'],
title_font_color=COLORS['blue1'],
paper_bgcolor=COLORS['blue4'],
plot_bgcolor=COLORS['blue4'],
height=200,
width=500,
margin_b=50,
Expand All @@ -646,9 +643,8 @@ def update_multi_options_t(fn_m, input_text, sel_methods, new_model, new_text, l
return html.Div([
'There was an error running the model. Check either the test ' +
'text or the model.'
]), utilities.blank_fig(), utilities.blank_fig()
]), blank_fig(), blank_fig()
else:
return (html.Div(['Missing model, input text or XAI method.']),
utilities.blank_fig(), utilities.blank_fig())
blank_fig(), blank_fig())

###################################################################
11 changes: 4 additions & 7 deletions dashboard/dashboard.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
"""Start dashboard."""
# Others
import warnings
# debug
from importlib import reload
# Dash&Flask
import dash
from dash import dcc
from dash import html
# Custom libraries
from dianna import utils # pylint: disable=unused-import
import layouts
import utilities
from callbacks import app
from dash import dcc
from dash import html
from layouts import images_page
from layouts import text_page
from dianna import utils # pylint: disable=unused-import


warnings.filterwarnings('ignore') # disable warnings related to versions of tf

Expand Down
Loading