File size: 16,832 Bytes
63aeef2
086dca7
112d8f5
e96bea6
112d8f5
9275fbf
63aeef2
112d8f5
63aeef2
9275fbf
 
4cc66ce
112d8f5
 
 
63aeef2
 
 
 
 
 
 
 
 
 
 
af455dd
 
 
 
 
 
 
 
 
63aeef2
 
 
9275fbf
 
 
 
af455dd
 
 
 
9275fbf
 
af455dd
 
 
9275fbf
 
 
af455dd
 
 
9275fbf
 
 
af455dd
 
 
9275fbf
af455dd
 
 
 
 
 
9275fbf
af455dd
 
 
9275fbf
af455dd
 
 
9275fbf
af455dd
 
 
9275fbf
af455dd
 
 
 
 
 
 
 
 
63aeef2
 
 
 
 
 
 
af455dd
63aeef2
 
af455dd
 
 
 
 
63aeef2
af455dd
63aeef2
af455dd
 
 
 
 
 
 
 
9275fbf
af455dd
 
 
 
 
 
 
 
 
 
 
 
 
 
63aeef2
af455dd
 
 
 
 
 
 
 
 
 
 
63aeef2
af455dd
 
9275fbf
112d8f5
af455dd
 
 
63aeef2
112d8f5
af455dd
112d8f5
af455dd
 
 
63aeef2
af455dd
 
 
 
 
63aeef2
af455dd
 
63aeef2
 
af455dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63aeef2
af455dd
 
 
 
 
63aeef2
af455dd
63aeef2
 
 
 
af455dd
 
 
63aeef2
af455dd
 
 
63aeef2
 
 
 
 
af455dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e96bea6
af455dd
 
 
e96bea6
63aeef2
 
 
 
e96bea6
63aeef2
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import sqlite3
import numpy as np
import pandas as pd
import gradio as gr
from pickle import load
from datetime import datetime
import sqlalchemy
from radiomics import featureextractor
from sqlalchemy.orm import sessionmaker
import nibabel as nib
from PIL import Image

extractor3D = featureextractor.RadiomicsFeatureExtractor("3DParams.yaml")
with open("model.pickle", "rb") as file:
    loaded_model = load(file)
    
def validation(username : str, password : str):
    if username == "" or password == "":
        return False
    table = pd.read_sql_table(table_name="Usuarios", con="sqlite:///database_test.db")
    row = table[table["Usuario"] == username]
    if row.empty:
        return False
    password_db = row["Contraseña"].to_numpy().tolist()[0]
    return password == password_db

with gr.Blocks(title="Historial de diagnósticos") as ViewingHistory:
    history_dataframe = gr.Dataframe(visible=False, type="pandas", wrap=True, interactive=False)
    
    def update_dataframe(history_dataframe):
        temp = pd.read_sql_table(table_name="Predicciones", con="sqlite:///database_test.db")
        return gr.Dataframe(value=temp, visible=True, type="pandas", wrap=True, interactive=False)
    
    ViewingHistory.load(fn=update_dataframe, inputs=[history_dataframe], outputs=[history_dataframe])

with gr.Blocks(title="Clasificación") as AIModel:
    with gr.Row():
        with gr.Column():
            image_file = gr.File(file_count="single", file_types=[".nii.gz", ".nii"], type="filepath", label="Imagen")
            segment_file = gr.File(file_count="single", file_types=[".nii.gz", ".nii"], type="filepath", label="Segmento")
            dropdown_navigator = gr.Dropdown(value="eje X", choices=["eje X", "eje Y", "eje Z"], filterable=True, type="value", label="Eje")
            
            slider = gr.Slider(visible=False)
            image_preview = gr.Image(visible=False)
            
            def preview_image(axis, image):
                brain_volume_data  = nib.load(image).get_fdata()
                if axis == "eje X":
                    middle_index = brain_volume_data.shape[0] // 2
                    max_index = brain_volume_data.shape[0] - 1
                    slice = brain_volume_data[middle_index, :, :]
                    image = Image.fromarray(slice)
                    image = image.rotate(90)
                elif axis == "eje Y":
                    middle_index = brain_volume_data.shape[1] // 2
                    max_index = brain_volume_data.shape[1] - 1
                    slice = brain_volume_data[:, middle_index, :]
                    image = Image.fromarray(slice)
                    image = image.rotate(90)
                else:
                    middle_index = brain_volume_data.shape[2] // 2
                    max_index = brain_volume_data.shape[2] - 1
                    slice = brain_volume_data[:, :, middle_index]
                    image = Image.fromarray(slice)
                    image = image.rotate(90)
                
                return (
                    gr.Slider(value=middle_index, minimum=0, maximum=max_index, visible=True),
                    gr.Image(value=image, label="Previsualización", type="pil", visible=True, interactive=False, show_download_button=True)
                )
            
            def slicing_image(axis, image, index):
                brain_volume_data  = nib.load(image).get_fdata()
                
                if axis == "eje X":
                    slice = brain_volume_data[index, :, :]
                    image = Image.fromarray(slice)
                    image = image.rotate(90)
                elif axis == "eje Y":
                    slice = brain_volume_data[:, index, :]
                    image = Image.fromarray(slice)
                    image = image.rotate(90)
                else:
                    slice = brain_volume_data[:, :, index]
                    image = Image.fromarray(slice)
                
                return (
                    gr.Image(value=image, label="Previsualización", type="pil", visible=True, interactive=False, show_download_button=True)
                )
            dropdown_navigator.change(fn=preview_image, inputs=[dropdown_navigator, image_file], outputs=[slider, image_preview])
            image_file.upload(fn=preview_image, inputs=[dropdown_navigator, image_file], outputs=[slider, image_preview])
            slider.change(fn=slicing_image, inputs=[dropdown_navigator, image_file, slider], outputs=[image_preview])
        with gr.Column():
          label_output = gr.Label(label="Resultado")
          comment_output = gr.Textbox(label="Observación", type="text", interactive=True)
    with gr.Row():
        with gr.Column():
            with gr.Row():
                with gr.Column():
                    clear_button = gr.ClearButton(value="Borrar", components=[image_file, segment_file, label_output, comment_output, dropdown_navigator])
                with gr.Column():
                    submit_button = gr.Button(value="Enviar", variant="primary")
                
                def clear_image_preview(image_preview, slider):
                    return (gr.Image(visible=False), gr.Slider(visible=False))
                
                clear_button.click(fn=clear_image_preview, inputs=[image_preview, slider], outputs=[image_preview, slider])    
        with gr.Column():
            flag_button = gr.Button(value="Guardar")

            def save_prediction(image, label_output, comment_output):
                grade1 = list(label_output.values())[0]
                grade2 = list(label_output.values())[1]
                engine = sqlalchemy.create_engine("sqlite:///database_test.db", echo=False)
                Session = sessionmaker(bind=engine)
                session = Session()
                metadata = sqlalchemy.MetaData()
                predictions_table = sqlalchemy.Table("Predicciones", metadata, autoload_with=engine)
        
                new_prediction = {
                    "Imagen": image,
                    "Grado 1":grade1,
                    "Grado 2": grade2,
                    "Observacion": comment_output,
                    "Usuario ID": 1,
                    "Ultima actualizacion": datetime.utcnow(),
                    "Creado el": datetime.utcnow()
                }
                
                stmt = predictions_table.insert().values(**new_prediction)
                session.execute(stmt)
                session.commit()
                return gr.Info("Se ha guardado exitosamente")
        
            def classify_image(image, segment):
                features3D = extractor3D.execute(imageFilepath=image, maskFilepath=segment)
                dict = {}
                for key, value in zip(features3D.keys(), features3D.values()):
                    if isinstance(value, np.ndarray):
                        dict[key] = [value.tolist()]
                    else:
                        dict[key] = [value]
                temp = pd.DataFrame(dict).select_dtypes(exclude=["object"]).to_numpy()
                prediction = loaded_model.predict_proba(temp).tolist()[0]
                return {"Grado 1": prediction[0], "Grado 2": prediction[1]}

    flag_button.click(fn=save_prediction, inputs=[image_file, label_output, comment_output]).success(update_dataframe, history_dataframe, history_dataframe) 
    submit_button.click(fn=classify_image, inputs=[image_file, segment_file], outputs=[label_output])
    
with gr.Blocks(title="Base de datos") as Database:
    with gr.Row():
        table_dropdown = gr.Dropdown(value="Usuarios", choices=["Usuarios", "Predicciones"],
        filterable=True, label="Tabla")
    with gr.Row():
        with gr.Column():
            action_dropdown = gr.Dropdown(choices=["Eliminar", "Descargar"], filterable=True, label="Acciones")
        with gr.Column():
            id_dropdown = gr.Dropdown(visible=False, filterable=True, label="Identificador (ID)")
        with gr.Column():
            action_button = gr.Button(visible=False)
    with gr.Row():
        database_dataframe = gr.Dataframe(visible=False, type="pandas", wrap=True, interactive=False)

    def on_table_dropdown_change(table_dropdown, database_dataframe, id_dropdown):
        temp = pd.read_sql_table(table_dropdown, "sqlite:///database_test.db")
        ids = temp["ID"].values.flatten().tolist()        
        return (
            gr.Dataframe(value=temp, visible=True, type="pandas", wrap=True, interactive=False),
            gr.Dropdown(choices=ids, visible=True, filterable=True, label="Identificador (ID)"),
        )
    
    def on_database_load(table_dropdown, database_dataframe):
        temp = pd.read_sql_table(table_dropdown, "sqlite:///database_test.db")
        return gr.Dataframe(value=temp, visible=True, type="pandas", wrap=True, interactive=False)
    
    def on_action_dropdown_change(action_dropdown):
        return gr.Button(value=action_dropdown, visible=True, variant="primary")
    
    def on_action_button_click(table_dropdown, action_dropdown, id_dropdown, database_dataframe):
        if action_dropdown == "Eliminar":
            engine = sqlalchemy.create_engine("sqlite:///database_test.db", echo=False)
            Session = sessionmaker(bind=engine)
            session = Session()
            metadata = sqlalchemy.MetaData()
            table = sqlalchemy.Table(table_dropdown, metadata, autoload_with=engine)
            stmt = sqlalchemy.delete(table).where(table.c.ID == id_dropdown)
            session.execute(stmt)
            session.commit()
            return gr.Info(f"Se ha eliminado el registro #{id_dropdown}")
        elif action_dropdown == "Descargar":
            if table_dropdown == "Predicciones":
                file_path = database_dataframe["Imagen"].to_numpy().tolist()[0]
                print(file_path)
                return gr.DownloadButton(value=file_path)
                #return gr.Info(f"Se ha descargado el registro #{id_dropdown}")
    
    table_dropdown.change(fn=on_table_dropdown_change, inputs=[table_dropdown, database_dataframe, id_dropdown], outputs=[database_dataframe, id_dropdown])
    Database.load(fn=on_database_load, inputs=[table_dropdown, database_dataframe], outputs=[database_dataframe]).success(fn=on_table_dropdown_change, inputs=[table_dropdown, database_dataframe, id_dropdown], outputs=[database_dataframe, id_dropdown])
    action_dropdown.change(fn=on_action_dropdown_change, inputs=[action_dropdown], outputs=[action_button])
    action_button.click(fn=on_action_button_click, inputs=[table_dropdown, action_dropdown, id_dropdown, database_dataframe], outputs=[action_dropdown])        
                
    
with gr.Blocks(title="Información de usuario") as AdminInformation:   
    with gr.Row():
        with gr.Column():
            input_profile_image = gr.Image(interactive=False)
        with gr.Column():
            input_first_names = gr.Textbox(label="Nombres", interactive=False, type="text", max_lines=1)
            input_username = gr.Textbox(label="Usuario", interactive=False, type="text", max_lines=1)
            input_is_admin = gr.Textbox(label="Rol", interactive=False, type="text", max_lines=1)
        with gr.Column():
            input_last_names = gr.Textbox(label="Apellidos", interactive=False, type="text", max_lines=1)
            input_email = gr.Textbox(label="Correo electrónico", interactive=False, type="email", max_lines=1)
            input_phone = gr.Textbox(label="Número de teléfono", interactive=False, type="text", max_lines=1)
    with gr.Row():
        with gr.Row():
            edit_button = gr.Button(value="Editar")
            save_button = gr.Button(value="Guardar", variant="primary")
    
    def FillFields(input_username, input_first_names, input_last_names, input_email, input_phone, input_is_admin):
        
        table = pd.read_sql_table(table_name="Usuarios", con="sqlite:///database_test.db")
        row = table[table["ID"] == 1]

        if not row.empty:
            username_db = row["Usuario"].to_numpy().tolist()[0]
            first_names_db = row["Nombres"].to_numpy().tolist()[0]
            last_names_db = row["Apellidos"].to_numpy().tolist()[0]
            email_db = row["Correo electronico"].to_numpy().tolist()[0]
            phone_db = row["Telefono"].to_numpy().tolist()[0]
            is_admin_db = row["Es Administrador"].to_numpy().tolist()[0]
        else:
            username_db = ""
            first_names_db = ""
            last_names_db = ""
            email_db = ""
            phone_db = ""
            is_admin_db = False
        
        return (
            gr.Textbox(value=username_db, label="Usuario", interactive=False, max_lines=1),
            gr.Textbox(value=first_names_db, label="Nombres", interactive=False, max_lines=1),
            gr.Textbox(value=last_names_db, label="Apellidos", interactive=False, max_lines=1),
            gr.Textbox(value=email_db, label="Correo electrónico", interactive=False, max_lines=1),
            gr.Textbox(value=phone_db, label="Número de teléfono", interactive=False, max_lines=1),
            gr.Textbox(value="Administrador" if is_admin_db else "Doctor", label="Rol", interactive=False, type="text", max_lines=1)
        )
    
    def make_editable(edit_button, input_username, input_first_names, input_last_names, input_email, input_phone):
        
        if edit_button == "Editar":
            return (
                gr.Button(value="Cancelar"),
                gr.Textbox(value=input_username, interactive=True),
                gr.Textbox(value=input_first_names, interactive=True),
                gr.Textbox(value=input_last_names, interactive=True),
                gr.Textbox(value=input_email, interactive=True),
                gr.Textbox(value=input_phone, interactive=True),
            )
        else:
            return (
                gr.Button(value="Editar"),
                gr.Textbox(value=input_username, interactive=True),
                gr.Textbox(value=input_first_names, interactive=True),
                gr.Textbox(value=input_last_names, interactive=True),
                gr.Textbox(value=input_email, interactive=True),
                gr.Textbox(value=input_phone, interactive=True),
            )
    
    def save_values(input_username, input_first_names, input_last_names, input_email, input_phone):
        #connection = sqlite3.connect("database_test.db")
        #cursor = connection.cursor()
        #data = cursor.execute(f'''UPDATE Usuarios SET Nombres = '{input_first_names}', Usuario = '{input_username}', Apellidos = '{input_last_names}', "Correo electronico" = '{input_email}', Telefono = '{input_phone}'  WHERE ID==1;''')
        #connection.commit()
        #connection.close()
        engine = sqlalchemy.create_engine("sqlite:///database_test.db", echo=False)
        Session = sessionmaker(bind=engine)
        session = Session()
        metadata = sqlalchemy.MetaData()
        table = sqlalchemy.Table("Usuarios", metadata, autoload_with=engine)
        new_user = {
            "Usuario": input_username,
            "Nombres": input_first_names,
            "Apellidos": input_last_names,
            "Correo electronico": input_email,
            "Telefono": input_phone
        }
        stmt = sqlalchemy.update(table).where(table.c.ID == 1).values(**new_user)
        session.execute(stmt)
        session.commit()
        return (
            gr.Textbox(value=input_username, interactive=False),
            gr.Textbox(value=input_first_names, interactive=False),
            gr.Textbox(value=input_last_names, interactive=False),
            gr.Textbox(value=input_email, interactive=False),
            gr.Textbox(value=input_phone, interactive=False),
        )
    
    AdminInformation.load(fn=FillFields, inputs=[input_username, input_first_names, input_last_names, input_email, input_phone, input_is_admin], outputs=[input_username, input_first_names, input_last_names, input_email, input_phone, input_is_admin])
    edit_button.click(fn=make_editable, inputs=[edit_button, input_username, input_first_names, input_last_names, input_email, input_phone], outputs=[edit_button, input_username, input_first_names, input_last_names, input_email, input_phone])
    save_button.click(fn=save_values, inputs=[input_username, input_first_names, input_last_names, input_email, input_phone], outputs=[input_username, input_first_names, input_last_names, input_email, input_phone])

Demo = gr.TabbedInterface(
  interface_list=[AIModel, ViewingHistory, Database, AdminInformation],
  tab_names=["Aplicación", "Historial", "Base de datos", "Administrador"],
)

if __name__ == "__main__":
    Demo.launch(
        share=True,
        debug=True,
        inbrowser=True,
        auth=validation
    )