Spaces:
Running
Running
dbouget
commited on
Commit
·
b5d0029
1
Parent(s):
79725b7
Upgrade to gradio 5
Browse files- .gitignore +1 -0
- Dockerfile +5 -5
- demo/requirements.txt +2 -2
- demo/src/gui.py +100 -124
- demo/src/inference.py +36 -24
.gitignore
CHANGED
|
@@ -13,3 +13,4 @@ venv/
|
|
| 13 |
*.obj
|
| 14 |
*.zip
|
| 15 |
*.txt
|
|
|
|
|
|
| 13 |
*.obj
|
| 14 |
*.zip
|
| 15 |
*.txt
|
| 16 |
+
*.idea/
|
Dockerfile
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
# you will also find guides on how best to write your Dockerfile
|
| 3 |
-
FROM python:3.
|
| 4 |
|
| 5 |
# set language, format and stuff
|
| 6 |
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
|
|
@@ -50,10 +50,10 @@ WORKDIR $HOME/app
|
|
| 50 |
COPY --chown=user . $HOME/app
|
| 51 |
|
| 52 |
# Download pretrained models
|
| 53 |
-
RUN wget "https://github.com/raidionics/Raidionics-models/releases/download/
|
| 54 |
-
unzip "Raidionics-CT_Airways-
|
| 55 |
-
RUN wget "https://github.com/raidionics/Raidionics-models/releases/download/
|
| 56 |
-
unzip "Raidionics-CT_Lungs-
|
| 57 |
|
| 58 |
RUN rm -r *.zip
|
| 59 |
|
|
|
|
| 1 |
# read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
|
| 2 |
# you will also find guides on how best to write your Dockerfile
|
| 3 |
+
FROM python:3.10-slim
|
| 4 |
|
| 5 |
# set language, format and stuff
|
| 6 |
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
|
|
|
|
| 50 |
COPY --chown=user . $HOME/app
|
| 51 |
|
| 52 |
# Download pretrained models
|
| 53 |
+
RUN wget "https://github.com/raidionics/Raidionics-models/releases/download/v1.3.0-rc/Raidionics-CT_Airways-v13.zip" && \
|
| 54 |
+
unzip "Raidionics-CT_Airways-v13.zip" && mkdir -p resources/models/ && mv CT_Airways/ resources/models/CT_Airways/
|
| 55 |
+
RUN wget "https://github.com/raidionics/Raidionics-models/releases/download/v1.3.0-rc/Raidionics-CT_Lungs-v13.zip" && \
|
| 56 |
+
unzip "Raidionics-CT_Lungs-v13.zip" && mv CT_Lungs/ resources/models/CT_Lungs/
|
| 57 |
|
| 58 |
RUN rm -r *.zip
|
| 59 |
|
demo/requirements.txt
CHANGED
|
@@ -1,2 +1,2 @@
|
|
| 1 |
-
raidionicsrads
|
| 2 |
-
gradio
|
|
|
|
| 1 |
+
raidionicsrads
|
| 2 |
+
gradio
|
demo/src/gui.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
| 5 |
from .convert import nifti_to_obj
|
| 6 |
from .css_style import css
|
|
@@ -22,48 +24,39 @@ class WebUI:
|
|
| 22 |
cwd: str = "/home/user/app/",
|
| 23 |
share: int = 1,
|
| 24 |
):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
# global states
|
| 26 |
self.images = []
|
| 27 |
self.pred_images = []
|
| 28 |
|
| 29 |
-
# @TODO: This should be dynamically set based on chosen volume size
|
| 30 |
-
self.nb_slider_items = 820
|
| 31 |
-
|
| 32 |
self.model_name = model_name
|
| 33 |
self.cwd = cwd
|
| 34 |
self.share = share
|
| 35 |
|
| 36 |
-
self.
|
| 37 |
-
self.extension = None
|
| 38 |
-
|
| 39 |
-
self.class_name = "airways" # default
|
| 40 |
self.class_names = {
|
| 41 |
-
"
|
| 42 |
-
"lungs": "CT_Lungs",
|
| 43 |
}
|
| 44 |
|
| 45 |
self.result_names = {
|
| 46 |
-
"
|
| 47 |
-
"lungs": "Lungs",
|
| 48 |
}
|
| 49 |
|
| 50 |
-
# define widgets not to be rendered immediantly, but later on
|
| 51 |
-
self.slider = gr.Slider(
|
| 52 |
-
minimum=1,
|
| 53 |
-
maximum=self.nb_slider_items,
|
| 54 |
-
value=1,
|
| 55 |
-
step=1,
|
| 56 |
-
label="Which 2D slice to show",
|
| 57 |
-
)
|
| 58 |
self.volume_renderer = gr.Model3D(
|
| 59 |
clear_color=[0.0, 0.0, 0.0, 0.0],
|
| 60 |
label="3D Model",
|
| 61 |
-
show_label=True,
|
| 62 |
visible=True,
|
| 63 |
elem_id="model-3d",
|
| 64 |
-
camera_position=[90, 180, 768],
|
| 65 |
height=512,
|
| 66 |
)
|
|
|
|
| 67 |
|
| 68 |
def set_class_name(self, value):
|
| 69 |
LOGGER.info(f"Changed task to: {value}")
|
|
@@ -79,75 +72,107 @@ class WebUI:
|
|
| 79 |
|
| 80 |
def process(self, mesh_file_name):
|
| 81 |
path = mesh_file_name.name
|
| 82 |
-
curr = path.split("/")[-1]
|
| 83 |
-
self.extension = ".".join(curr.split(".")[1:])
|
| 84 |
-
self.filename = (
|
| 85 |
-
curr.split(".")[0] + "-" + self.class_names[self.class_name]
|
| 86 |
-
)
|
| 87 |
run_model(
|
| 88 |
path,
|
| 89 |
model_path=os.path.join(self.cwd, "resources/models/"),
|
| 90 |
task=self.class_names[self.class_name],
|
| 91 |
name=self.result_names[self.class_name],
|
| 92 |
-
output_filename=self.filename + "." + self.extension,
|
| 93 |
)
|
| 94 |
LOGGER.info("Converting prediction NIfTI to OBJ...")
|
| 95 |
-
nifti_to_obj(
|
| 96 |
|
| 97 |
LOGGER.info("Loading CT to numpy...")
|
| 98 |
self.images = load_ct_to_numpy(path)
|
| 99 |
|
| 100 |
LOGGER.info("Loading prediction volume to numpy..")
|
| 101 |
-
self.pred_images = load_pred_volume_to_numpy(
|
| 102 |
-
self.filename + "." + self.extension
|
| 103 |
-
)
|
| 104 |
|
| 105 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 106 |
|
| 107 |
-
|
| 108 |
-
if (self.filename is None) or (self.extension is None):
|
| 109 |
-
LOGGER.error(
|
| 110 |
-
"The prediction is not available or ready to download. Wait until the result is available in the 3D viewer."
|
| 111 |
-
)
|
| 112 |
-
raise ValueError("Run inference before downloading!")
|
| 113 |
-
return self.filename + "." + self.extension
|
| 114 |
|
| 115 |
def get_img_pred_pair(self, k):
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
color_map={self.class_name: "#ffae00"},
|
| 122 |
-
height=512,
|
| 123 |
-
width=512,
|
| 124 |
-
)
|
| 125 |
-
return out
|
| 126 |
|
| 127 |
def toggle_sidebar(self, state):
|
| 128 |
state = not state
|
| 129 |
return gr.update(visible=state), state
|
| 130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
def run(self):
|
| 132 |
with gr.Blocks(css=css) as demo:
|
| 133 |
with gr.Row():
|
| 134 |
-
with gr.Column(
|
| 135 |
logs = gr.Textbox(
|
| 136 |
placeholder="\n" * 16,
|
| 137 |
label="Logs",
|
| 138 |
info="Verbose from inference will be displayed below.",
|
| 139 |
-
lines=
|
| 140 |
-
max_lines=
|
| 141 |
autoscroll=True,
|
| 142 |
elem_id="logs",
|
| 143 |
show_copy_button=True,
|
|
|
|
| 144 |
container=True,
|
|
|
|
| 145 |
)
|
| 146 |
-
|
|
|
|
|
|
|
| 147 |
|
| 148 |
-
with gr.Column():
|
| 149 |
with gr.Row():
|
| 150 |
-
with gr.Column(
|
| 151 |
sidebar_state = gr.State(True)
|
| 152 |
|
| 153 |
btn_toggle_sidebar = gr.Button(
|
|
@@ -160,66 +185,30 @@ class WebUI:
|
|
| 160 |
[sidebar_left, sidebar_state],
|
| 161 |
)
|
| 162 |
|
| 163 |
-
btn_clear_logs = gr.Button(
|
| 164 |
-
"Clear logs", elem_id="logs-button"
|
| 165 |
-
)
|
| 166 |
btn_clear_logs.click(flush_logs, [], [])
|
| 167 |
|
| 168 |
-
file_output = gr.File(
|
| 169 |
-
file_count="single",
|
| 170 |
-
elem_id="upload",
|
| 171 |
-
scale=3,
|
| 172 |
-
)
|
| 173 |
-
file_output.upload(
|
| 174 |
-
self.upload_file, file_output, file_output
|
| 175 |
-
)
|
| 176 |
|
| 177 |
-
model_selector = gr.Dropdown(
|
| 178 |
list(self.class_names.keys()),
|
| 179 |
label="Task",
|
| 180 |
info="Which structure to segment.",
|
| 181 |
multiselect=False,
|
| 182 |
-
scale=1,
|
| 183 |
-
)
|
| 184 |
-
model_selector.input(
|
| 185 |
-
fn=lambda x: self.set_class_name(x),
|
| 186 |
-
inputs=model_selector,
|
| 187 |
-
outputs=None,
|
| 188 |
)
|
| 189 |
|
| 190 |
-
with gr.Column(
|
| 191 |
-
run_btn = gr.Button(
|
| 192 |
-
"Run analysis",
|
| 193 |
-
variant="primary",
|
| 194 |
-
elem_id="run-button",
|
| 195 |
-
)
|
| 196 |
-
run_btn.click(
|
| 197 |
-
fn=lambda x: self.process(x),
|
| 198 |
-
inputs=file_output,
|
| 199 |
-
outputs=self.volume_renderer,
|
| 200 |
-
)
|
| 201 |
-
|
| 202 |
-
download_btn = gr.DownloadButton(
|
| 203 |
-
"Download prediction",
|
| 204 |
-
visible=True,
|
| 205 |
-
variant="secondary",
|
| 206 |
-
elem_id="download",
|
| 207 |
-
)
|
| 208 |
-
download_btn.click(
|
| 209 |
-
fn=self.download_prediction,
|
| 210 |
-
inputs=None,
|
| 211 |
-
outputs=download_btn,
|
| 212 |
-
)
|
| 213 |
|
| 214 |
with gr.Row():
|
| 215 |
gr.Examples(
|
| 216 |
examples=[
|
| 217 |
os.path.join(self.cwd, "test_thorax_CT.nii.gz"),
|
| 218 |
],
|
| 219 |
-
inputs=file_output,
|
| 220 |
-
outputs=file_output,
|
| 221 |
fn=self.upload_file,
|
| 222 |
-
cache_examples=
|
| 223 |
)
|
| 224 |
|
| 225 |
gr.Markdown(
|
|
@@ -229,32 +218,19 @@ class WebUI:
|
|
| 229 |
"""
|
| 230 |
)
|
| 231 |
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
self.slider.input(
|
| 244 |
-
self.get_img_pred_pair,
|
| 245 |
-
self.slider,
|
| 246 |
-
t,
|
| 247 |
-
)
|
| 248 |
-
|
| 249 |
-
self.slider.render()
|
| 250 |
-
|
| 251 |
-
with gr.Group(): # gr.Box():
|
| 252 |
-
self.volume_renderer.render()
|
| 253 |
-
|
| 254 |
# sharing app publicly -> share=True:
|
| 255 |
# https://gradio.app/sharing-your-app/
|
| 256 |
# inference times > 60 seconds -> need queue():
|
| 257 |
# https://github.com/tloen/alpaca-lora/issues/60#issuecomment-1510006062
|
| 258 |
-
demo.queue().launch(
|
| 259 |
-
server_name="0.0.0.0", server_port=7860, share=self.share
|
| 260 |
-
)
|
|
|
|
| 1 |
import os
|
| 2 |
|
| 3 |
import gradio as gr
|
| 4 |
+
from zipfile import ZipFile
|
| 5 |
+
from PIL import Image
|
| 6 |
|
| 7 |
from .convert import nifti_to_obj
|
| 8 |
from .css_style import css
|
|
|
|
| 24 |
cwd: str = "/home/user/app/",
|
| 25 |
share: int = 1,
|
| 26 |
):
|
| 27 |
+
self.file_output = None
|
| 28 |
+
self.model_selector = None
|
| 29 |
+
self.stripped_cb = None
|
| 30 |
+
self.registered_cb = None
|
| 31 |
+
self.run_btn = None
|
| 32 |
+
self.slider = None
|
| 33 |
+
self.download_file = None
|
| 34 |
+
|
| 35 |
# global states
|
| 36 |
self.images = []
|
| 37 |
self.pred_images = []
|
| 38 |
|
|
|
|
|
|
|
|
|
|
| 39 |
self.model_name = model_name
|
| 40 |
self.cwd = cwd
|
| 41 |
self.share = share
|
| 42 |
|
| 43 |
+
self.class_name = "Airways" # default
|
|
|
|
|
|
|
|
|
|
| 44 |
self.class_names = {
|
| 45 |
+
"Airways": "CT_Airways",
|
|
|
|
| 46 |
}
|
| 47 |
|
| 48 |
self.result_names = {
|
| 49 |
+
"Airways": "Airways",
|
|
|
|
| 50 |
}
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
self.volume_renderer = gr.Model3D(
|
| 53 |
clear_color=[0.0, 0.0, 0.0, 0.0],
|
| 54 |
label="3D Model",
|
|
|
|
| 55 |
visible=True,
|
| 56 |
elem_id="model-3d",
|
|
|
|
| 57 |
height=512,
|
| 58 |
)
|
| 59 |
+
# self.volume_renderer = ShinyModel3D()
|
| 60 |
|
| 61 |
def set_class_name(self, value):
|
| 62 |
LOGGER.info(f"Changed task to: {value}")
|
|
|
|
| 72 |
|
| 73 |
def process(self, mesh_file_name):
|
| 74 |
path = mesh_file_name.name
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
run_model(
|
| 76 |
path,
|
| 77 |
model_path=os.path.join(self.cwd, "resources/models/"),
|
| 78 |
task=self.class_names[self.class_name],
|
| 79 |
name=self.result_names[self.class_name],
|
|
|
|
| 80 |
)
|
| 81 |
LOGGER.info("Converting prediction NIfTI to OBJ...")
|
| 82 |
+
nifti_to_obj("prediction.nii.gz")
|
| 83 |
|
| 84 |
LOGGER.info("Loading CT to numpy...")
|
| 85 |
self.images = load_ct_to_numpy(path)
|
| 86 |
|
| 87 |
LOGGER.info("Loading prediction volume to numpy..")
|
| 88 |
+
self.pred_images = load_pred_volume_to_numpy("./prediction.nii.gz")
|
|
|
|
|
|
|
| 89 |
|
| 90 |
+
slider = gr.Slider(
|
| 91 |
+
minimum=0,
|
| 92 |
+
maximum=len(self.images) - 1,
|
| 93 |
+
value=int(len(self.images) / 2),
|
| 94 |
+
step=1,
|
| 95 |
+
label="Which 2D slice to show",
|
| 96 |
+
interactive=True,
|
| 97 |
+
)
|
| 98 |
|
| 99 |
+
return "./prediction.obj", slider
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
def get_img_pred_pair(self, k):
|
| 102 |
+
img = self.images[k]
|
| 103 |
+
img_pil = Image.fromarray(img)
|
| 104 |
+
seg_list = []
|
| 105 |
+
seg_list.append((self.pred_images[k], self.class_name))
|
| 106 |
+
return img_pil, seg_list
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
def toggle_sidebar(self, state):
|
| 109 |
state = not state
|
| 110 |
return gr.update(visible=state), state
|
| 111 |
|
| 112 |
+
def package_results(self):
|
| 113 |
+
"""Generates text files and zips them."""
|
| 114 |
+
output_dir = "temp_output"
|
| 115 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 116 |
+
|
| 117 |
+
zip_filename = os.path.join(output_dir, "generated_files.zip")
|
| 118 |
+
with ZipFile(zip_filename, 'w') as zf:
|
| 119 |
+
zf.write("./prediction.nii.gz")
|
| 120 |
+
|
| 121 |
+
return zip_filename
|
| 122 |
+
|
| 123 |
+
def setup_interface_outputs(self):
|
| 124 |
+
with gr.Row():
|
| 125 |
+
with gr.Group():
|
| 126 |
+
with gr.Column(scale=2):
|
| 127 |
+
t = gr.AnnotatedImage(
|
| 128 |
+
visible=True,
|
| 129 |
+
elem_id="model-2d",
|
| 130 |
+
color_map={self.class_name: "#ffae00"},
|
| 131 |
+
height=512,
|
| 132 |
+
width=512,
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
self.slider = gr.Slider(
|
| 136 |
+
minimum=0,
|
| 137 |
+
maximum=1,
|
| 138 |
+
value=0,
|
| 139 |
+
step=1,
|
| 140 |
+
label="Which 2D slice to show",
|
| 141 |
+
interactive=True,
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
self.slider.change(fn=self.get_img_pred_pair, inputs=self.slider, outputs=t)
|
| 145 |
+
|
| 146 |
+
with gr.Group():
|
| 147 |
+
self.volume_renderer.render()
|
| 148 |
+
self.download_btn = gr.DownloadButton(label="Download results", visible=False)
|
| 149 |
+
self.download_file = gr.File(label="Download Zip", interactive=True, visible=False)
|
| 150 |
+
|
| 151 |
+
|
| 152 |
def run(self):
|
| 153 |
with gr.Blocks(css=css) as demo:
|
| 154 |
with gr.Row():
|
| 155 |
+
with gr.Column(scale=1, visible=True) as sidebar_left:
|
| 156 |
logs = gr.Textbox(
|
| 157 |
placeholder="\n" * 16,
|
| 158 |
label="Logs",
|
| 159 |
info="Verbose from inference will be displayed below.",
|
| 160 |
+
lines=38,
|
| 161 |
+
max_lines=38,
|
| 162 |
autoscroll=True,
|
| 163 |
elem_id="logs",
|
| 164 |
show_copy_button=True,
|
| 165 |
+
# scroll_to_output=False,
|
| 166 |
container=True,
|
| 167 |
+
# line_breaks=True,
|
| 168 |
)
|
| 169 |
+
timer = gr.Timer(value=1, active=True)
|
| 170 |
+
timer.tick(fn=read_logs, inputs=None, outputs=logs)
|
| 171 |
+
# demo.load(read_logs, None, logs, every=0.5)
|
| 172 |
|
| 173 |
+
with gr.Column(scale=2):
|
| 174 |
with gr.Row():
|
| 175 |
+
with gr.Column(min_width=150):
|
| 176 |
sidebar_state = gr.State(True)
|
| 177 |
|
| 178 |
btn_toggle_sidebar = gr.Button(
|
|
|
|
| 185 |
[sidebar_left, sidebar_state],
|
| 186 |
)
|
| 187 |
|
| 188 |
+
btn_clear_logs = gr.Button("Clear logs", elem_id="logs-button")
|
|
|
|
|
|
|
| 189 |
btn_clear_logs.click(flush_logs, [], [])
|
| 190 |
|
| 191 |
+
self.file_output = gr.File(file_count="single", elem_id="upload")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
+
self.model_selector = gr.Dropdown(
|
| 194 |
list(self.class_names.keys()),
|
| 195 |
label="Task",
|
| 196 |
info="Which structure to segment.",
|
| 197 |
multiselect=False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 198 |
)
|
| 199 |
|
| 200 |
+
with gr.Column(min_width=150):
|
| 201 |
+
self.run_btn = gr.Button("Run segmentation", variant="primary", elem_id="run-button")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
|
| 203 |
with gr.Row():
|
| 204 |
gr.Examples(
|
| 205 |
examples=[
|
| 206 |
os.path.join(self.cwd, "test_thorax_CT.nii.gz"),
|
| 207 |
],
|
| 208 |
+
inputs=self.file_output,
|
| 209 |
+
outputs=self.file_output,
|
| 210 |
fn=self.upload_file,
|
| 211 |
+
cache_examples=False,
|
| 212 |
)
|
| 213 |
|
| 214 |
gr.Markdown(
|
|
|
|
| 218 |
"""
|
| 219 |
)
|
| 220 |
|
| 221 |
+
self.setup_interface_outputs()
|
| 222 |
+
|
| 223 |
+
# Define the signals/slots
|
| 224 |
+
self.file_output.upload(self.upload_file, self.file_output, self.file_output)
|
| 225 |
+
self.model_selector.input(fn=lambda x: self.set_class_name(x), inputs=self.model_selector, outputs=None)
|
| 226 |
+
self.run_btn.click(fn=self.process, inputs=[self.file_output],
|
| 227 |
+
outputs=[self.volume_renderer, self.slider]).then(fn=lambda:
|
| 228 |
+
gr.DownloadButton(visible=True), inputs=None, outputs=self.download_btn)
|
| 229 |
+
self.download_btn.click(fn=self.package_results, inputs=[], outputs=self.download_file).then(fn=lambda
|
| 230 |
+
file_path: gr.File(label="Download Zip", visible=True, value=file_path), inputs=self.download_file,
|
| 231 |
+
outputs=self.download_file)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
# sharing app publicly -> share=True:
|
| 233 |
# https://gradio.app/sharing-your-app/
|
| 234 |
# inference times > 60 seconds -> need queue():
|
| 235 |
# https://github.com/tloen/alpaca-lora/issues/60#issuecomment-1510006062
|
| 236 |
+
demo.queue().launch(server_name="0.0.0.0", server_port=7860, share=self.share)
|
|
|
|
|
|
demo/src/inference.py
CHANGED
|
@@ -3,6 +3,8 @@ import logging
|
|
| 3 |
import os
|
| 4 |
import shutil
|
| 5 |
import traceback
|
|
|
|
|
|
|
| 6 |
|
| 7 |
|
| 8 |
def run_model(
|
|
@@ -11,7 +13,6 @@ def run_model(
|
|
| 11 |
verbose: str = "info",
|
| 12 |
task: str = "CT_Airways",
|
| 13 |
name: str = "Airways",
|
| 14 |
-
output_filename: str = None,
|
| 15 |
):
|
| 16 |
if verbose == "debug":
|
| 17 |
logging.getLogger().setLevel(logging.DEBUG)
|
|
@@ -28,9 +29,6 @@ def run_model(
|
|
| 28 |
if os.path.exists("./result/"):
|
| 29 |
shutil.rmtree("./result/")
|
| 30 |
|
| 31 |
-
if output_filename is None:
|
| 32 |
-
raise ValueError("Please, set output_filename.")
|
| 33 |
-
|
| 34 |
patient_directory = ""
|
| 35 |
output_path = ""
|
| 36 |
try:
|
|
@@ -59,37 +57,51 @@ def run_model(
|
|
| 59 |
rads_config.set("System", "input_folder", patient_directory)
|
| 60 |
rads_config.set("System", "output_folder", output_path)
|
| 61 |
rads_config.set("System", "model_folder", model_path)
|
| 62 |
-
rads_config.set(
|
| 63 |
-
|
| 64 |
-
"pipeline_filename",
|
| 65 |
-
os.path.join(model_path, task, "pipeline.json"),
|
| 66 |
-
)
|
| 67 |
rads_config.add_section("Runtime")
|
| 68 |
-
rads_config.set(
|
| 69 |
-
"Runtime", "reconstruction_method", "thresholding"
|
| 70 |
-
) # thresholding, probabilities
|
| 71 |
rads_config.set("Runtime", "reconstruction_order", "resample_first")
|
| 72 |
rads_config.set("Runtime", "use_preprocessed_data", "False")
|
| 73 |
|
| 74 |
with open("rads_config.ini", "w") as f:
|
| 75 |
rads_config.write(f)
|
| 76 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
# finally, run inference
|
| 78 |
from raidionicsrads.compute import run_rads
|
| 79 |
-
|
| 80 |
run_rads(config_filename="rads_config.ini")
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
output_filename,
|
| 92 |
-
)
|
| 93 |
# Clean-up
|
| 94 |
if os.path.exists(patient_directory):
|
| 95 |
shutil.rmtree(patient_directory)
|
|
|
|
| 3 |
import os
|
| 4 |
import shutil
|
| 5 |
import traceback
|
| 6 |
+
import json
|
| 7 |
+
import fnmatch
|
| 8 |
|
| 9 |
|
| 10 |
def run_model(
|
|
|
|
| 13 |
verbose: str = "info",
|
| 14 |
task: str = "CT_Airways",
|
| 15 |
name: str = "Airways",
|
|
|
|
| 16 |
):
|
| 17 |
if verbose == "debug":
|
| 18 |
logging.getLogger().setLevel(logging.DEBUG)
|
|
|
|
| 29 |
if os.path.exists("./result/"):
|
| 30 |
shutil.rmtree("./result/")
|
| 31 |
|
|
|
|
|
|
|
|
|
|
| 32 |
patient_directory = ""
|
| 33 |
output_path = ""
|
| 34 |
try:
|
|
|
|
| 57 |
rads_config.set("System", "input_folder", patient_directory)
|
| 58 |
rads_config.set("System", "output_folder", output_path)
|
| 59 |
rads_config.set("System", "model_folder", model_path)
|
| 60 |
+
rads_config.set('System', 'pipeline_filename', os.path.join(output_path,
|
| 61 |
+
'test_pipeline.json'))
|
|
|
|
|
|
|
|
|
|
| 62 |
rads_config.add_section("Runtime")
|
| 63 |
+
rads_config.set("Runtime", "reconstruction_method", "thresholding") # thresholding, probabilities
|
|
|
|
|
|
|
| 64 |
rads_config.set("Runtime", "reconstruction_order", "resample_first")
|
| 65 |
rads_config.set("Runtime", "use_preprocessed_data", "False")
|
| 66 |
|
| 67 |
with open("rads_config.ini", "w") as f:
|
| 68 |
rads_config.write(f)
|
| 69 |
|
| 70 |
+
pip = {}
|
| 71 |
+
step_index = 1
|
| 72 |
+
pip_num = str(step_index)
|
| 73 |
+
pip[pip_num] = {}
|
| 74 |
+
pip[pip_num]["task"] = "Classification"
|
| 75 |
+
pip[pip_num]["inputs"] = {} # Empty input means running it on all existing data for the patient
|
| 76 |
+
pip[pip_num]["target"] = ["MRSequence"]
|
| 77 |
+
pip[pip_num]["model"] = "MRI_SequenceClassifier"
|
| 78 |
+
pip[pip_num]["description"] = "Classification of the MRI sequence type for all input scans."
|
| 79 |
+
|
| 80 |
+
step_index = step_index + 1
|
| 81 |
+
pip_num = str(step_index)
|
| 82 |
+
pip[pip_num] = {}
|
| 83 |
+
pip[pip_num]["task"] = 'Model selection'
|
| 84 |
+
pip[pip_num]["model"] = task
|
| 85 |
+
pip[pip_num]["timestamp"] = 0
|
| 86 |
+
pip[pip_num]["format"] = "thresholding"
|
| 87 |
+
pip[pip_num]["description"] = f"Identifying the best {task} segmentation model for existing inputs"
|
| 88 |
+
|
| 89 |
+
with open(os.path.join(output_path, 'test_pipeline.json'), 'w', newline='\n') as outfile:
|
| 90 |
+
json.dump(pip, outfile, indent=4, sort_keys=True)
|
| 91 |
+
|
| 92 |
# finally, run inference
|
| 93 |
from raidionicsrads.compute import run_rads
|
|
|
|
| 94 |
run_rads(config_filename="rads_config.ini")
|
| 95 |
|
| 96 |
+
logging.info(f"Looking for the following pattern: {task}")
|
| 97 |
+
patterns = [f"*-{name}.*"]
|
| 98 |
+
existing_files = os.listdir(os.path.join(output_path, "T0"))
|
| 99 |
+
logging.info(f"Existing files: {existing_files}")
|
| 100 |
+
fileName = str(os.path.join(output_path, "T0",
|
| 101 |
+
[x for x in existing_files if
|
| 102 |
+
any(fnmatch.fnmatch(x, pattern) for pattern in patterns)][0]))
|
| 103 |
+
os.rename(src=fileName, dst="./prediction.nii.gz")
|
| 104 |
+
|
|
|
|
|
|
|
| 105 |
# Clean-up
|
| 106 |
if os.path.exists(patient_directory):
|
| 107 |
shutil.rmtree(patient_directory)
|