niplinig commited on
Commit
9275fbf
·
verified ·
1 Parent(s): 0f8041d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -7
app.py CHANGED
@@ -3,9 +3,12 @@ import numpy as np
3
  import pandas as pd
4
  import gradio as gr
5
  from pickle import load
 
6
  import sqlalchemy
7
  from radiomics import featureextractor
8
  from sqlalchemy.orm import sessionmaker
 
 
9
 
10
  extractor3D = featureextractor.RadiomicsFeatureExtractor("3DParams.yaml")
11
  with open("model.pickle", "rb") as file:
@@ -24,8 +27,41 @@ def validation(username : str, password : str):
24
  with gr.Blocks(title="Clasificación") as AIModel:
25
  with gr.Row():
26
  with gr.Column():
27
- image_file = gr.File(file_count="single", file_types=[".nii.gz", ".nii"], type="filepath", label="Imagen")
28
- segment_file = gr.File(file_count="single", file_types=[".nii.gz", ".nii"], type="filepath", label="Segmento")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  with gr.Column():
30
  label_output = gr.Label(label="Resultado")
31
  comment_output = gr.Textbox(label="Observación", type="text", interactive=True)
@@ -37,22 +73,28 @@ with gr.Blocks(title="Clasificación") as AIModel:
37
  with gr.Column():
38
  submit_button = gr.Button(value="Enviar", variant="primary")
39
  with gr.Column():
40
- flag_button = gr.Button(value="Marcar")
41
 
42
  def make_prediction(image, label_output, comment_output):
43
  grade1 = list(label_output.values())[0]
44
  grade2 = list(label_output.values())[1]
45
- engine = sqlalchemy.create_engine("sqlite:///database.db", echo=False)
46
  Session = sessionmaker(bind=engine)
47
  session = Session()
 
 
 
48
  new_prediction = {
49
  "Imagen": image,
50
- "Grado 1": grade1,
51
  "Grado 2": grade2,
52
  "Observacion": comment_output,
53
  "Usuario ID": 1,
 
 
54
  }
55
- stmt = predictions.insert().values(**new_prediction)
 
56
  session.execute(stmt)
57
  session.commit()
58
 
@@ -70,6 +112,7 @@ with gr.Blocks(title="Clasificación") as AIModel:
70
 
71
  flag_button.click(fn=make_prediction, inputs=[image_file, label_output, comment_output])
72
  submit_button.click(fn=image_classifier, inputs=[image_file, segment_file], outputs=[label_output])
 
73
 
74
  with gr.Blocks(title="Historial de diagnósticos") as ViewingHistory:
75
  temp = pd.read_sql_table("Predicciones", "sqlite:///database_test.db")
@@ -92,7 +135,7 @@ with gr.Blocks(title="Base de datos") as Database:
92
 
93
  with gr.Blocks(title="Información de usuario", delete_cache=[60, 120]) as AdminInformation:
94
 
95
- username : str = ""
96
  first_names : str = ""
97
  last_names : str = ""
98
  email : str = ""
 
3
  import pandas as pd
4
  import gradio as gr
5
  from pickle import load
6
+ from datetime import datetime
7
  import sqlalchemy
8
  from radiomics import featureextractor
9
  from sqlalchemy.orm import sessionmaker
10
+ import nibabel as nib
11
+ from PIL import Image
12
 
13
  extractor3D = featureextractor.RadiomicsFeatureExtractor("3DParams.yaml")
14
  with open("model.pickle", "rb") as file:
 
27
  with gr.Blocks(title="Clasificación") as AIModel:
28
  with gr.Row():
29
  with gr.Column():
30
+ image_file = gr.File(file_count="single", file_types=[".nii.gz", ".nii"], type="filepath", label="Imagen")
31
+ segment_file = gr.File(file_count="single", file_types=[".nii.gz", ".nii"], type="filepath", label="Segmento")
32
+ dropdown_navigator = gr.Dropdown(value="eje X", choices=["eje X", "eje Y", "eje Z"], filterable=True, type="value", label="Eje")
33
+
34
+ def change_slider(image, slider_value, axis):
35
+ brain_volume_data = nib.load(image).get_fdata()
36
+ if axis == "eje X":
37
+ slice = brain_volume_data[slider_value, :, :]
38
+ image = Image.fromarray(slice)
39
+ image = image.rotate(90)
40
+ return image
41
+ elif axis == "eje Y":
42
+ slice = brain_volume_data[:, slider_value, :]
43
+ image = Image.fromarray(slice)
44
+ image = image.rotate(90)
45
+ return image
46
+ else:
47
+ slice = brain_volume_data[:, :, slider_value]
48
+ image = Image.fromarray(slice)
49
+ return image
50
+
51
+ @gr.render(inputs=[image_file, dropdown_navigator])
52
+ def preview_image (image, axis):
53
+ if image == None or image == "":
54
+ return
55
+ if axis == "eje X":
56
+ slider = gr.Slider(value=int(nib.load(image).get_fdata().shape[0] // 2), minimum=0, maximum=nib.load(image).get_fdata().shape[0] - 1, label="Control deslizante")
57
+ elif axis == "eje Y":
58
+ slider = gr.Slider(value=int(nib.load(image).get_fdata().shape[1] // 2), minimum=0, maximum=nib.load(image).get_fdata().shape[1] - 1, label="Control deslizante")
59
+ else:
60
+ slider = gr.Slider(value=int(nib.load(image).get_fdata().shape[2] // 2), minimum=0, maximum=nib.load(image).get_fdata().shape[2] - 1, label="Control deslizante")
61
+
62
+ image_preview = gr.Image(label="Previsualización", interactive=False, type="pil")
63
+ slider.change(fn=change_slider, inputs=[image_file, slider, dropdown_navigator], outputs=[image_preview])
64
+
65
  with gr.Column():
66
  label_output = gr.Label(label="Resultado")
67
  comment_output = gr.Textbox(label="Observación", type="text", interactive=True)
 
73
  with gr.Column():
74
  submit_button = gr.Button(value="Enviar", variant="primary")
75
  with gr.Column():
76
+ flag_button = gr.Button(value="Marcar")
77
 
78
  def make_prediction(image, label_output, comment_output):
79
  grade1 = list(label_output.values())[0]
80
  grade2 = list(label_output.values())[1]
81
+ engine = sqlalchemy.create_engine("sqlite:///database_test.db", echo=False)
82
  Session = sessionmaker(bind=engine)
83
  session = Session()
84
+ metadata = sqlalchemy.MetaData()
85
+ predictions_table = sqlalchemy.Table("Predicciones", metadata, autoload_with=engine)
86
+
87
  new_prediction = {
88
  "Imagen": image,
89
+ "Grado 1":grade1,
90
  "Grado 2": grade2,
91
  "Observacion": comment_output,
92
  "Usuario ID": 1,
93
+ "Ultima actualizacion": datetime.utcnow(),
94
+ "Creado el": datetime.utcnow()
95
  }
96
+
97
+ stmt = predictions_table.insert().values(**new_prediction)
98
  session.execute(stmt)
99
  session.commit()
100
 
 
112
 
113
  flag_button.click(fn=make_prediction, inputs=[image_file, label_output, comment_output])
114
  submit_button.click(fn=image_classifier, inputs=[image_file, segment_file], outputs=[label_output])
115
+
116
 
117
  with gr.Blocks(title="Historial de diagnósticos") as ViewingHistory:
118
  temp = pd.read_sql_table("Predicciones", "sqlite:///database_test.db")
 
135
 
136
  with gr.Blocks(title="Información de usuario", delete_cache=[60, 120]) as AdminInformation:
137
 
138
+ username : str = ""
139
  first_names : str = ""
140
  last_names : str = ""
141
  email : str = ""