From b52d4b736bc6201cf6674f2f69d161f308c0c128 Mon Sep 17 00:00:00 2001
From: Caroline DE POURTALES <cdepourt@montana.irit.fr>
Date: Wed, 8 Jun 2022 14:00:31 +0200
Subject: [PATCH] cleaning instances

---
 callbacks.py | 60 ++++++++++++++++++++++++++++++++++++++++------------
 utils.py     |  2 ++
 2 files changed, 48 insertions(+), 14 deletions(-)

diff --git a/callbacks.py b/callbacks.py
index 5150142..ce52350 100644
--- a/callbacks.py
+++ b/callbacks.py
@@ -1,6 +1,8 @@
 import dash
 from dash.dependencies import Input, Output, State
 from dash.exceptions import PreventUpdate
+import dash_bootstrap_components as dbc
+from dash import Input, Output, State, html
 
 from utils import parse_contents_graph, parse_contents_instance, parse_contents_data
 
@@ -9,6 +11,27 @@ from pages.application.RandomForest.utils.xrf import *
 
 sys.modules['xrf'] = xrf
 
+####### Creates alerts when callback fails #########
+
+alert_selection_model = html.Div([dbc.Alert("You didn't choose a king of Machine Learning model first..",
+                                            is_open=True,
+                                            color='warning',
+                                            duration=10000, ), ])
+alert_network = html.Div([dbc.Alert("There was a problem while computing the graph, read the documentation. \
+                                    You might have forgotten to upload the data for Random Forest or you tried to upload an unknown format.",
+                                    is_open=True,
+                                    color='danger',
+                                    duration=10000, ), ])
+alert_explanation = html.Div([dbc.Alert(
+    "There was a problem while computing the explanation. Read the documentation to understand which king of format are accepted.",
+    is_open=True,
+    color='danger',
+    duration=10000, ), ])
+alert_version_model = []
+
+
+######################################################
+
 
 def register_callbacks(page_home, page_course, page_application, app):
     page_list = ['home', 'course', 'application']
@@ -100,18 +123,23 @@ def register_callbacks(page_home, page_course, page_application, app):
             # Choice of pkl pretrained model
             elif ihm_id == 'ml_pretrained_model_choice':
                 if model_application.ml_model is None:
-                    raise PreventUpdate
-                graph = parse_contents_graph(pretrained_model_contents, pretrained_model_filename)
-                model_application.update_pretrained_model(graph)
-                if not model_application.add_info:
-                    model_application.update_pretrained_model_layout()
-                    return model_application.component.network, None
+                    return alert_selection_model, None
                 else:
-                    if model_application.model_info is None:
-                        raise PreventUpdate
-                    model_application.update_pretrained_model_layout_with_info(model_application.model_info,
-                                                                               model_info_filename)
-                    return model_application.component.network, None
+                    try:
+                        graph = parse_contents_graph(pretrained_model_contents, pretrained_model_filename)
+                        model_application.update_pretrained_model(graph)
+                        if not model_application.add_info:
+                            model_application.update_pretrained_model_layout()
+                            return model_application.component.network, None
+                        else:
+                            if model_application.model_info is None:
+                                return alert_network, None
+                            else:
+                                model_application.update_pretrained_model_layout_with_info(model_application.model_info,
+                                                                                           model_info_filename)
+                                return model_application.component.network, None
+                    except:
+                        return alert_network, None
 
             # Choice of information for the model
             elif ihm_id == 'model_info_choice':
@@ -126,9 +154,13 @@ def register_callbacks(page_home, page_course, page_application, app):
             elif ihm_id == 'ml_instance_choice':
                 if model_application.ml_model is None or model_application.pretrained_model is None or model_application.enum <= 0 or model_application.xtype is None:
                     raise PreventUpdate
-                instance = parse_contents_instance(instance_contents, instance_filename)
-                model_application.update_instance(instance)
-                return model_application.component.network, model_application.component.explanation
+                try:
+                    instance = parse_contents_instance(instance_contents, instance_filename)
+                    model_application.update_instance(instance)
+                    return model_application.component.network, model_application.component.explanation
+                except:
+                    return alert_network, alert_explanation
+
 
             # Choice of number of expls
             elif ihm_id == 'number_explanations':
diff --git a/utils.py b/utils.py
index 67e9af7..248358b 100644
--- a/utils.py
+++ b/utils.py
@@ -22,6 +22,8 @@ def parse_contents_graph(contents, filename):
                 data = pickle.load(io.BytesIO(decoded))
         elif '.txt' in filename:
             data = decoded.decode('utf-8').strip()
+        elif '.dt' in filename:
+            data = decoded.decode('utf-8').strip()
     except Exception as e:
         print(e)
         return html.Div([
-- 
GitLab