Spaces:
Sleeping
Sleeping
Added UI methods to clear logs and toggle sidebar
Browse files- demo/src/gui.py +36 -22
- demo/src/inference.py +0 -4
demo/src/gui.py
CHANGED
@@ -3,7 +3,7 @@ import os
|
|
3 |
import gradio as gr
|
4 |
|
5 |
from .inference import run_model
|
6 |
-
from .logger import setup_logger, read_logs
|
7 |
from .utils import load_ct_to_numpy
|
8 |
from .utils import load_pred_volume_to_numpy
|
9 |
from .utils import nifti_to_glb
|
@@ -58,14 +58,16 @@ class WebUI:
|
|
58 |
).style(height=512)
|
59 |
|
60 |
def set_class_name(self, value):
|
61 |
-
|
62 |
self.class_name = value
|
63 |
|
64 |
def combine_ct_and_seg(self, img, pred):
|
65 |
return (img, [(pred, self.class_name)])
|
66 |
|
67 |
def upload_file(self, file):
|
68 |
-
|
|
|
|
|
69 |
|
70 |
def process(self, mesh_file_name):
|
71 |
path = mesh_file_name.name
|
@@ -75,9 +77,13 @@ class WebUI:
|
|
75 |
task=self.class_names[self.class_name],
|
76 |
name=self.result_names[self.class_name],
|
77 |
)
|
|
|
78 |
nifti_to_glb("prediction.nii.gz")
|
79 |
|
|
|
80 |
self.images = load_ct_to_numpy(path)
|
|
|
|
|
81 |
self.pred_images = load_pred_volume_to_numpy("./prediction.nii.gz")
|
82 |
|
83 |
return "./prediction.obj"
|
@@ -94,6 +100,10 @@ class WebUI:
|
|
94 |
width=512,
|
95 |
)
|
96 |
return out
|
|
|
|
|
|
|
|
|
97 |
|
98 |
def run(self):
|
99 |
css = """
|
@@ -105,15 +115,16 @@ class WebUI:
|
|
105 |
margin: auto;
|
106 |
}
|
107 |
#upload {
|
108 |
-
height:
|
109 |
-
}
|
110 |
-
.logs {
|
111 |
-
width: 120px;
|
112 |
-
margin: auto;
|
113 |
}
|
114 |
"""
|
115 |
with gr.Blocks(css=css) as demo:
|
116 |
with gr.Row():
|
|
|
|
|
|
|
|
|
|
|
117 |
with gr.Column():
|
118 |
with gr.Row():
|
119 |
file_output = gr.File(file_count="single", elem_id="upload")
|
@@ -132,14 +143,23 @@ class WebUI:
|
|
132 |
outputs=None,
|
133 |
)
|
134 |
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
143 |
|
144 |
with gr.Row():
|
145 |
gr.Examples(
|
@@ -174,12 +194,6 @@ class WebUI:
|
|
174 |
|
175 |
with gr.Box():
|
176 |
self.volume_renderer.render()
|
177 |
-
|
178 |
-
with gr.Column(visible=True, style="logs", scale=0.2) as sidebar_left:
|
179 |
-
gr.Markdown("SideBar Right")
|
180 |
-
|
181 |
-
logs = gr.Textbox(label="Logs", info="Verbose from inference will be displayed below.", max_lines=16, autoscroll=True, elem_id="logs")
|
182 |
-
demo.load(read_logs, None, logs, every=1)
|
183 |
|
184 |
# sharing app publicly -> share=True:
|
185 |
# https://gradio.app/sharing-your-app/
|
|
|
3 |
import gradio as gr
|
4 |
|
5 |
from .inference import run_model
|
6 |
+
from .logger import setup_logger, read_logs, flush_logs
|
7 |
from .utils import load_ct_to_numpy
|
8 |
from .utils import load_pred_volume_to_numpy
|
9 |
from .utils import nifti_to_glb
|
|
|
58 |
).style(height=512)
|
59 |
|
60 |
def set_class_name(self, value):
|
61 |
+
LOGGER.info(f"Changed task to: {value}")
|
62 |
self.class_name = value
|
63 |
|
64 |
def combine_ct_and_seg(self, img, pred):
|
65 |
return (img, [(pred, self.class_name)])
|
66 |
|
67 |
def upload_file(self, file):
|
68 |
+
out = file.name
|
69 |
+
LOGGER.info(f"File uploaded: {out}")
|
70 |
+
return out
|
71 |
|
72 |
def process(self, mesh_file_name):
|
73 |
path = mesh_file_name.name
|
|
|
77 |
task=self.class_names[self.class_name],
|
78 |
name=self.result_names[self.class_name],
|
79 |
)
|
80 |
+
LOGGER.info("Converting prediction NIfTI to GLB...")
|
81 |
nifti_to_glb("prediction.nii.gz")
|
82 |
|
83 |
+
LOGGER.info("Loading CT to numpy...")
|
84 |
self.images = load_ct_to_numpy(path)
|
85 |
+
|
86 |
+
LOGGER.info("Loading prediction volume to numpy..")
|
87 |
self.pred_images = load_pred_volume_to_numpy("./prediction.nii.gz")
|
88 |
|
89 |
return "./prediction.obj"
|
|
|
100 |
width=512,
|
101 |
)
|
102 |
return out
|
103 |
+
|
104 |
+
def toggle_sidebar(self, state):
|
105 |
+
state = not state
|
106 |
+
return gr.update(visible=state), state
|
107 |
|
108 |
def run(self):
|
109 |
css = """
|
|
|
115 |
margin: auto;
|
116 |
}
|
117 |
#upload {
|
118 |
+
height: 160px;
|
|
|
|
|
|
|
|
|
119 |
}
|
120 |
"""
|
121 |
with gr.Blocks(css=css) as demo:
|
122 |
with gr.Row():
|
123 |
+
with gr.Column(visible=True, scale=0.2) as sidebar_left:
|
124 |
+
# gr.Markdown("SideBar Left")
|
125 |
+
logs = gr.Textbox(label="Logs", info="Verbose from inference will be displayed below.", max_lines=16, autoscroll=True, elem_id="logs", show_copy_button=True)
|
126 |
+
demo.load(read_logs, None, logs, every=1)
|
127 |
+
|
128 |
with gr.Column():
|
129 |
with gr.Row():
|
130 |
file_output = gr.File(file_count="single", elem_id="upload")
|
|
|
143 |
outputs=None,
|
144 |
)
|
145 |
|
146 |
+
with gr.Column():
|
147 |
+
run_btn = gr.Button("Run analysis").style(
|
148 |
+
full_width=False, size="lg"
|
149 |
+
)
|
150 |
+
run_btn.click(
|
151 |
+
fn=lambda x: self.process(x),
|
152 |
+
inputs=file_output,
|
153 |
+
outputs=self.volume_renderer,
|
154 |
+
)
|
155 |
+
|
156 |
+
sidebar_state = gr.State(True)
|
157 |
+
|
158 |
+
btn_toggle_sidebar = gr.Button("Toggle Sidebar")
|
159 |
+
btn_toggle_sidebar.click(self.toggle_sidebar, [sidebar_state], [sidebar_left, sidebar_state])
|
160 |
+
|
161 |
+
btn_clear_logs = gr.Button("Clear logs")
|
162 |
+
btn_clear_logs.click(flush_logs, [], [])
|
163 |
|
164 |
with gr.Row():
|
165 |
gr.Examples(
|
|
|
194 |
|
195 |
with gr.Box():
|
196 |
self.volume_renderer.render()
|
|
|
|
|
|
|
|
|
|
|
|
|
197 |
|
198 |
# sharing app publicly -> share=True:
|
199 |
# https://gradio.app/sharing-your-app/
|
demo/src/inference.py
CHANGED
@@ -4,8 +4,6 @@ import os
|
|
4 |
import shutil
|
5 |
import traceback
|
6 |
|
7 |
-
from .logger import get_logger
|
8 |
-
|
9 |
|
10 |
def run_model(
|
11 |
input_path: str,
|
@@ -14,8 +12,6 @@ def run_model(
|
|
14 |
task: str = "CT_Airways",
|
15 |
name: str = "Airways",
|
16 |
):
|
17 |
-
logging.getLogger().setLevel(logging.WARNING)
|
18 |
-
|
19 |
if verbose == "debug":
|
20 |
logging.getLogger().setLevel(logging.DEBUG)
|
21 |
elif verbose == "info":
|
|
|
4 |
import shutil
|
5 |
import traceback
|
6 |
|
|
|
|
|
7 |
|
8 |
def run_model(
|
9 |
input_path: str,
|
|
|
12 |
task: str = "CT_Airways",
|
13 |
name: str = "Airways",
|
14 |
):
|
|
|
|
|
15 |
if verbose == "debug":
|
16 |
logging.getLogger().setLevel(logging.DEBUG)
|
17 |
elif verbose == "info":
|