Skip to content

Commit

Permalink
Merge pull request #32 from andreped/render-fix
Browse files Browse the repository at this point in the history
Added logging and sidebar widgets with clear/toggle methods
  • Loading branch information
andreped authored Oct 30, 2023
2 parents b6670af + 6f9d64a commit 2afcdda
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 59 deletions.
159 changes: 103 additions & 56 deletions demo/src/gui.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
import gradio as gr

from .inference import run_model
from .logger import flush_logs
from .logger import read_logs
from .logger import setup_logger
from .utils import load_ct_to_numpy
from .utils import load_pred_volume_to_numpy
from .utils import nifti_to_glb

# setup logging
LOGGER = setup_logger()


class WebUI:
def __init__(
Expand Down Expand Up @@ -53,14 +59,16 @@ def __init__(
).style(height=512)

def set_class_name(self, value):
print("Changed task to:", value)
LOGGER.info(f"Changed task to: {value}")
self.class_name = value

def combine_ct_and_seg(self, img, pred):
return (img, [(pred, self.class_name)])

def upload_file(self, file):
return file.name
out = file.name
LOGGER.info(f"File uploaded: {out}")
return out

def process(self, mesh_file_name):
path = mesh_file_name.name
Expand All @@ -70,9 +78,13 @@ def process(self, mesh_file_name):
task=self.class_names[self.class_name],
name=self.result_names[self.class_name],
)
LOGGER.info("Converting prediction NIfTI to GLB...")
nifti_to_glb("prediction.nii.gz")

LOGGER.info("Loading CT to numpy...")
self.images = load_ct_to_numpy(path)

LOGGER.info("Loading prediction volume to numpy..")
self.pred_images = load_pred_volume_to_numpy("./prediction.nii.gz")

return "./prediction.obj"
Expand All @@ -90,6 +102,10 @@ def get_img_pred_pair(self, k):
)
return out

def toggle_sidebar(self, state):
state = not state
return gr.update(visible=state), state

def run(self):
css = """
#model-3d {
Expand All @@ -100,69 +116,100 @@ def run(self):
margin: auto;
}
#upload {
height: 120px;
height: 160px;
}
"""
with gr.Blocks(css=css) as demo:
with gr.Row():
file_output = gr.File(file_count="single", elem_id="upload")
file_output.upload(self.upload_file, file_output, file_output)

model_selector = gr.Dropdown(
list(self.class_names.keys()),
label="Task",
info="Which task to perform",
multiselect=False,
size="sm",
)
model_selector.input(
fn=lambda x: self.set_class_name(x),
inputs=model_selector,
outputs=None,
)

run_btn = gr.Button("Run analysis").style(
full_width=False, size="lg"
)
run_btn.click(
fn=lambda x: self.process(x),
inputs=file_output,
outputs=self.volume_renderer,
)

with gr.Row():
gr.Examples(
examples=[
os.path.join(self.cwd, "test_thorax_CT.nii.gz"),
],
inputs=file_output,
outputs=file_output,
fn=self.upload_file,
cache_examples=True,
)

with gr.Row():
with gr.Box():
with gr.Column():
# create dummy image to be replaced by loaded images
t = gr.AnnotatedImage(
visible=True, elem_id="model-2d"
).style(
color_map={self.class_name: "#ffae00"},
height=512,
width=512,
with gr.Column(visible=True, scale=0.2) as sidebar_left:
# gr.Markdown("SideBar Left")
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,
)
demo.load(read_logs, None, logs, every=1)

with gr.Column():
with gr.Row():
file_output = gr.File(
file_count="single", elem_id="upload"
)
file_output.upload(
self.upload_file, file_output, file_output
)

self.slider.input(
self.get_img_pred_pair,
self.slider,
t,
model_selector = gr.Dropdown(
list(self.class_names.keys()),
label="Task",
info="Which task to perform",
multiselect=False,
size="sm",
)
model_selector.input(
fn=lambda x: self.set_class_name(x),
inputs=model_selector,
outputs=None,
)

self.slider.render()
with gr.Column():
run_btn = gr.Button("Run analysis").style(
full_width=False, size="lg"
)
run_btn.click(
fn=lambda x: self.process(x),
inputs=file_output,
outputs=self.volume_renderer,
)

sidebar_state = gr.State(True)

btn_toggle_sidebar = gr.Button("Toggle Sidebar")
btn_toggle_sidebar.click(
self.toggle_sidebar,
[sidebar_state],
[sidebar_left, sidebar_state],
)

btn_clear_logs = gr.Button("Clear logs")
btn_clear_logs.click(flush_logs, [], [])

with gr.Row():
gr.Examples(
examples=[
os.path.join(self.cwd, "test_thorax_CT.nii.gz"),
],
inputs=file_output,
outputs=file_output,
fn=self.upload_file,
cache_examples=True,
)

with gr.Box():
self.volume_renderer.render()
with gr.Row():
with gr.Box():
with gr.Column():
# create dummy image to be replaced by loaded images
t = gr.AnnotatedImage(
visible=True, elem_id="model-2d"
).style(
color_map={self.class_name: "#ffae00"},
height=512,
width=512,
)

self.slider.input(
self.get_img_pred_pair,
self.slider,
t,
)

self.slider.render()

with gr.Box():
self.volume_renderer.render()

# sharing app publicly -> share=True:
# https://gradio.app/sharing-your-app/
Expand Down
3 changes: 0 additions & 3 deletions demo/src/inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ def run_model(
task: str = "CT_Airways",
name: str = "Airways",
):
logging.basicConfig()
logging.getLogger().setLevel(logging.WARNING)

if verbose == "debug":
logging.getLogger().setLevel(logging.DEBUG)
elif verbose == "info":
Expand Down
37 changes: 37 additions & 0 deletions demo/src/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging
import sys


def get_logger():
return logging.getLogger(__name__)


def setup_logger():
# clear log
file_to_delete = open("log.txt", "w")
file_to_delete.close()

file_handler = logging.FileHandler(filename="log.txt")
stdout_handler = logging.StreamHandler(stream=sys.stdout)
handlers = [file_handler, stdout_handler]

logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] {%(filename)s:%(lineno)d} %(levelname)s - %(message)s",
handlers=handlers,
)

return get_logger()


def read_logs():
sys.stdout.flush()
with open("log.txt", "r") as f:
return f.read()


def flush_logs():
sys.stdout.flush()
# clear log
file_to_delete = open("log.txt", "w")
file_to_delete.close()

0 comments on commit 2afcdda

Please sign in to comment.