Spaces:
Running
Running
Added logging widget and logic
Browse files- demo/src/gui.py +70 -53
- demo/src/inference.py +2 -1
- demo/src/logger.py +30 -0
demo/src/gui.py
CHANGED
|
@@ -3,11 +3,16 @@ import os
|
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
from .inference import run_model
|
|
|
|
| 6 |
from .utils import load_ct_to_numpy
|
| 7 |
from .utils import load_pred_volume_to_numpy
|
| 8 |
from .utils import nifti_to_glb
|
| 9 |
|
| 10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
class WebUI:
|
| 12 |
def __init__(
|
| 13 |
self,
|
|
@@ -102,67 +107,79 @@ class WebUI:
|
|
| 102 |
#upload {
|
| 103 |
height: 120px;
|
| 104 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 105 |
"""
|
| 106 |
with gr.Blocks(css=css) as demo:
|
| 107 |
with gr.Row():
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
run_btn = gr.Button("Run analysis").style(
|
| 125 |
-
full_width=False, size="lg"
|
| 126 |
-
)
|
| 127 |
-
run_btn.click(
|
| 128 |
-
fn=lambda x: self.process(x),
|
| 129 |
-
inputs=file_output,
|
| 130 |
-
outputs=self.volume_renderer,
|
| 131 |
-
)
|
| 132 |
-
|
| 133 |
-
with gr.Row():
|
| 134 |
-
gr.Examples(
|
| 135 |
-
examples=[
|
| 136 |
-
os.path.join(self.cwd, "test_thorax_CT.nii.gz"),
|
| 137 |
-
],
|
| 138 |
-
inputs=file_output,
|
| 139 |
-
outputs=file_output,
|
| 140 |
-
fn=self.upload_file,
|
| 141 |
-
cache_examples=True,
|
| 142 |
-
)
|
| 143 |
-
|
| 144 |
-
with gr.Row():
|
| 145 |
-
with gr.Box():
|
| 146 |
-
with gr.Column():
|
| 147 |
-
# create dummy image to be replaced by loaded images
|
| 148 |
-
t = gr.AnnotatedImage(
|
| 149 |
-
visible=True, elem_id="model-2d"
|
| 150 |
-
).style(
|
| 151 |
-
color_map={self.class_name: "#ffae00"},
|
| 152 |
-
height=512,
|
| 153 |
-
width=512,
|
| 154 |
)
|
| 155 |
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
| 160 |
)
|
| 161 |
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
|
| 164 |
-
|
| 165 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
|
| 167 |
# sharing app publicly -> share=True:
|
| 168 |
# 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
|
| 7 |
from .utils import load_ct_to_numpy
|
| 8 |
from .utils import load_pred_volume_to_numpy
|
| 9 |
from .utils import nifti_to_glb
|
| 10 |
|
| 11 |
|
| 12 |
+
# setup logging
|
| 13 |
+
LOGGER = setup_logger()
|
| 14 |
+
|
| 15 |
+
|
| 16 |
class WebUI:
|
| 17 |
def __init__(
|
| 18 |
self,
|
|
|
|
| 107 |
#upload {
|
| 108 |
height: 120px;
|
| 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")
|
| 120 |
+
file_output.upload(self.upload_file, file_output, file_output)
|
| 121 |
+
|
| 122 |
+
model_selector = gr.Dropdown(
|
| 123 |
+
list(self.class_names.keys()),
|
| 124 |
+
label="Task",
|
| 125 |
+
info="Which task to perform",
|
| 126 |
+
multiselect=False,
|
| 127 |
+
size="sm",
|
| 128 |
+
)
|
| 129 |
+
model_selector.input(
|
| 130 |
+
fn=lambda x: self.set_class_name(x),
|
| 131 |
+
inputs=model_selector,
|
| 132 |
+
outputs=None,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
)
|
| 134 |
|
| 135 |
+
run_btn = gr.Button("Run analysis").style(
|
| 136 |
+
full_width=False, size="lg"
|
| 137 |
+
)
|
| 138 |
+
run_btn.click(
|
| 139 |
+
fn=lambda x: self.process(x),
|
| 140 |
+
inputs=file_output,
|
| 141 |
+
outputs=self.volume_renderer,
|
| 142 |
)
|
| 143 |
|
| 144 |
+
with gr.Row():
|
| 145 |
+
gr.Examples(
|
| 146 |
+
examples=[
|
| 147 |
+
os.path.join(self.cwd, "test_thorax_CT.nii.gz"),
|
| 148 |
+
],
|
| 149 |
+
inputs=file_output,
|
| 150 |
+
outputs=file_output,
|
| 151 |
+
fn=self.upload_file,
|
| 152 |
+
cache_examples=True,
|
| 153 |
+
)
|
| 154 |
|
| 155 |
+
with gr.Row():
|
| 156 |
+
with gr.Box():
|
| 157 |
+
with gr.Column():
|
| 158 |
+
# create dummy image to be replaced by loaded images
|
| 159 |
+
t = gr.AnnotatedImage(
|
| 160 |
+
visible=True, elem_id="model-2d"
|
| 161 |
+
).style(
|
| 162 |
+
color_map={self.class_name: "#ffae00"},
|
| 163 |
+
height=512,
|
| 164 |
+
width=512,
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
self.slider.input(
|
| 168 |
+
self.get_img_pred_pair,
|
| 169 |
+
self.slider,
|
| 170 |
+
t,
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
self.slider.render()
|
| 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/
|
demo/src/inference.py
CHANGED
|
@@ -4,6 +4,8 @@ import os
|
|
| 4 |
import shutil
|
| 5 |
import traceback
|
| 6 |
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def run_model(
|
| 9 |
input_path: str,
|
|
@@ -12,7 +14,6 @@ def run_model(
|
|
| 12 |
task: str = "CT_Airways",
|
| 13 |
name: str = "Airways",
|
| 14 |
):
|
| 15 |
-
logging.basicConfig()
|
| 16 |
logging.getLogger().setLevel(logging.WARNING)
|
| 17 |
|
| 18 |
if verbose == "debug":
|
|
|
|
| 4 |
import shutil
|
| 5 |
import traceback
|
| 6 |
|
| 7 |
+
from .logger import get_logger
|
| 8 |
+
|
| 9 |
|
| 10 |
def run_model(
|
| 11 |
input_path: str,
|
|
|
|
| 14 |
task: str = "CT_Airways",
|
| 15 |
name: str = "Airways",
|
| 16 |
):
|
|
|
|
| 17 |
logging.getLogger().setLevel(logging.WARNING)
|
| 18 |
|
| 19 |
if verbose == "debug":
|
demo/src/logger.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import logging
|
| 2 |
+
import sys
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def get_logger():
|
| 6 |
+
logging.getLogger(__name__)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def setup_logger():
|
| 10 |
+
# clear log
|
| 11 |
+
file_to_delete = open("log.txt", "w")
|
| 12 |
+
file_to_delete.close()
|
| 13 |
+
|
| 14 |
+
file_handler = logging.FileHandler(filename="log.txt")
|
| 15 |
+
stdout_handler = logging.StreamHandler(stream=sys.stdout)
|
| 16 |
+
handlers = [file_handler, stdout_handler]
|
| 17 |
+
|
| 18 |
+
logging.basicConfig(
|
| 19 |
+
level=logging.INFO,
|
| 20 |
+
format='[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s',
|
| 21 |
+
handlers=handlers,
|
| 22 |
+
)
|
| 23 |
+
|
| 24 |
+
return get_logger()
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def read_logs():
|
| 28 |
+
sys.stdout.flush()
|
| 29 |
+
with open("log.txt", "r") as f:
|
| 30 |
+
return f.read()
|