"""Native Gradio controls for the retained OCT model and B-scan viewer.""" from __future__ import annotations import html import os from pathlib import Path os.environ.setdefault("GRADIO_ANALYTICS_ENABLED", "False") import gradio as gr from PIL import Image from src.viewer import ( AUTO, ENGINE, FAMILIES, STORE, boundary_choices, compute_choices, legend, load_upload, render, resolve_family, volume_info, ) DEFAULT_SCANNER = "heidelberg_spectralis" EMPTY_MESSAGE = "Upload a structural OCT DICOM to begin." def download_result(volume, artifact, png=None): if artifact not in {"predictions", "overlay"}: raise gr.Error("Choose a supported export format.") label = "Download NPZ" if artifact == "predictions" else "Download PNG" path = None if volume and volume.prediction: path = volume.prediction.export if artifact == "predictions" else png ready = path is not None and Path(path).is_file() return gr.update(value=str(path) if ready else None, label=label, interactive=ready) def display_outputs( identity, frame, keys, overlay, opacity, width, labels, contrast, brightness, physical, artifact="predictions", ): if not identity: return ( None, None, "Upload a scan to browse B-scans.", download_result(None, artifact), ) volume = STORE.get(identity) index = max(0, min(volume.shape[0] - 1, int(frame) - 1)) volume.view_frame = index volume.view_settings = ( keys, overlay, opacity, width, labels, contrast, brightness, physical, artifact, ) raw, result = render( volume, index, keys, overlay, opacity, width, labels, contrast, brightness, physical, ) caption = f"B-scan {index + 1:,} / {volume.shape[0]:,}" png = None if result is not None: png = volume.directory / f"overlay-bscan-{index + 1:04d}.png" Image.fromarray(result).save(png) caption += " · " + ( "full-volume prediction" if volume.prediction.complete_volume else "single-slice preview" ) elif volume.prediction: caption += " · this slice has not been segmented" return raw, result, caption, download_result(volume, artifact, png) def on_upload( files, previous, scanner, overlay, opacity, width, labels, contrast, brightness, physical, artifact, ): volume = None try: volume = load_upload(files) identity = STORE.add(volume) frame = (volume.shape[0] - 1) // 2 + 1 family = volume.detected_family if scanner == AUTO else scanner choices = boundary_choices(family) keys = [value for _, value in choices] raw, result, caption, download = display_outputs( identity, frame, keys, overlay, opacity, width, labels, contrast, brightness, physical, artifact, ) if previous: STORE.remove(previous) message = "Scan loaded. Check the scanner family, then run inference." if family is None: message = "Scan loaded. Select its scanner family, then run inference." return ( identity, raw, result, gr.update( minimum=1, maximum=max(2, volume.shape[0]), value=frame, interactive=volume.shape[0] > 1, ), gr.update(choices=choices, value=keys), volume_info(volume, family), legend(keys), caption, download, message, gr.update(interactive=True), ) except Exception as exc: if volume is not None: STORE.remove(volume.identity) raise gr.Error(str(exc)) from exc def clear_upload(identity, artifact="predictions"): STORE.remove(identity) return ( None, None, None, gr.update(minimum=1, maximum=2, value=1, interactive=False), gr.update(choices=[], value=[]), "No scan loaded.", legend([]), "Upload a scan to browse B-scans.", download_result(None, artifact), EMPTY_MESSAGE, gr.update(interactive=False), ) def clear_volume(identity): return ( *clear_upload(identity), gr.update(value=None), DEFAULT_SCANNER, "predictions", ) def scanner_changed( identity, scanner, frame, overlay, opacity, width, labels, contrast, brightness, physical, artifact, ): if not identity: return (gr.skip(),) * 8 volume = STORE.get(identity) family = volume.detected_family if scanner == AUTO else scanner if volume.prediction is not None and volume.prediction.family != family: volume.prediction = None choices = boundary_choices(family) keys = [value for _, value in choices] outputs = display_outputs( identity, frame, keys, overlay, opacity, width, labels, contrast, brightness, physical, artifact, ) return ( gr.update(choices=choices, value=keys), volume_info(volume, family), legend(keys), *outputs, "Scanner selection updated. Run inference to apply it.", ) def move_frame(delta): def move(identity, frame, *settings): if not identity: return (gr.skip(),) * 5 volume = STORE.get(identity) next_frame = max(1, min(volume.shape[0], int(frame) + delta)) return gr.update(value=next_frame), *display_outputs( identity, next_frame, *settings ) return move def update_display(identity, frame, *settings): outputs = display_outputs(identity, frame, *settings) return *outputs, legend(settings[0]) if identity else legend([]) def segment( identity, frame, scanner, compute, scope, batch_size, *settings, progress=gr.Progress(), ): try: volume = STORE.get(identity) family = resolve_family(volume, scanner) if batch_size is None or not 1 <= int(batch_size) <= 16: raise ValueError("Enter a batch size from 1 to 16.") progress(0, desc="Loading the saved model") yield ( gr.skip(), None, "Loading model…", download_result(None, settings[-1]), "Loading the saved model. First use takes a little longer.", gr.update(interactive=False), ) for event in ENGINE.predict( volume, family, compute, int(batch_size), scope == "volume", int(frame) - 1 ): if "result" not in event: progress( event["completed"] / event["total"], desc=f"Segmenting B-scans · {event['completed']:,}/{event['total']:,}", ) message = ( f"Segmenting {event['completed']:,} / {event['total']:,} B-scans · " f"{event['device'].upper()} · {event['seconds']:.0f} s elapsed" ) yield ( gr.skip(), gr.skip(), gr.skip(), gr.skip(), message, gr.update(interactive=False), ) else: result = event["result"] outputs = display_outputs( identity, volume.view_frame + 1, *(volume.view_settings or settings) ) message = ( f"**Completed** · {FAMILIES[family]} · {result.compute.upper()} · " f"{len(result.frames):,} B-scans · {len(result.keys)} boundaries · " f"{result.seconds:.1f} s" ) if not result.complete_volume: message += " \nSlice preview omits smoothing between B-scans. Use Full volume for the saved pipeline." yield *outputs, message, gr.update(interactive=True) except Exception as exc: message = html.escape(str(exc)) if "out of memory" in str(exc).lower(): message += " Try batch size 1, or select CPU." yield ( gr.skip(), gr.skip(), gr.skip(), gr.skip(), f"**Error:** {message}", gr.update(interactive=True), ) def cancel(identity): if identity: STORE.get(identity).cancelled = True return ( "Stop requested. Inference stops after the current model batch.", gr.update(interactive=True), ) def build_app(): zoom_js = Path(__file__).with_name("viewer.js").read_text() with gr.Blocks( title="MYXOS · OCT Viewer", analytics_enabled=False, delete_cache=(7200, 7200) ) as demo: identity = gr.State(value=None, time_to_live=7200, delete_callback=STORE.remove) gr.Markdown("# OCT Boundary Explorer") with gr.Row(): with gr.Column(scale=1, min_width=300): gr.Markdown("### Configuration") upload = gr.File( label="OCT volume", file_types=[".dcm", ".dicom"], file_count="multiple", type="filepath", height=135, ) scanner = gr.Dropdown( choices=[ *[(name, key) for key, name in FAMILIES.items()], ("Detect from DICOM", AUTO), ], value=DEFAULT_SCANNER, label="Scanner family", ) compute = gr.Dropdown( choices=compute_choices(), value="auto", label="Processor", info="Auto uses an available GPU, otherwise CPU.", ) scope = gr.Radio( choices=[ ("Full volume", "volume"), ("Selected B-scan preview", "preview"), ], value="volume", label="Inference scope", ) with gr.Accordion("Performance settings", open=False): batch = gr.Number( value=1, minimum=1, maximum=16, step=1, precision=0, label="Batch size", info="Lower this if GPU memory is limited.", ) with gr.Row(): run = gr.Button( "Run inference", variant="primary", interactive=False, scale=3, min_width=135, ) stop = gr.Button("Stop", variant="stop", scale=1, min_width=70) clear = gr.Button("Clear volume", size="sm") scan_info = gr.Markdown("No scan loaded.") gr.Markdown( "Upload one volume, or its contiguous DICOM parts together." ) with gr.Column(scale=3, min_width=480): gr.Markdown("### Boundary viewer") messages = gr.Markdown(EMPTY_MESSAGE) with gr.Tabs(): viewers = [] for tab, label, viewer_id in [ ("Boundary overlay", "Predicted boundaries", "overlay-viewer"), ("Original OCT", "OCT B-scan", "source-viewer"), ]: with gr.Tab(tab): buttons = [] for text, action in [ ("Zoom in", "in"), ("Zoom out", "out"), ("Fit", "fit"), ]: button = gr.Button(text, size="sm", render=False) button.click( fn=None, js=f"() => ({zoom_js})('{viewer_id}', '{action}')", queue=False, api_name=False, ) buttons.append(button) viewers.append( gr.Image( label=label, type="numpy", format="png", interactive=False, height=480, buttons=[*buttons, "fullscreen"], elem_id=viewer_id, ) ) segmented, original = viewers gr.Markdown( "Use Zoom in / Zoom out, then drag to pan. Fit resets the view." ) with gr.Row(): previous = gr.Button("Previous", scale=0, min_width=85) frame = gr.Slider( 1, 2, value=1, step=1, label="B-scan", scale=6, interactive=False, ) following = gr.Button("Next", scale=0, min_width=85) caption = gr.Markdown("Upload a scan to browse B-scans.") colors = gr.Markdown(legend([])) with gr.Accordion("Visible boundaries", open=False): boundaries = gr.CheckboxGroup( choices=[], value=[], label="Overlay boundaries" ) with gr.Accordion("Display settings", open=False): with gr.Row(): show_overlay = gr.Checkbox(value=True, label="Show boundaries") show_labels = gr.Checkbox(value=False, label="Label curves") physical = gr.Checkbox( value=True, label="Use physical pixel aspect" ) with gr.Row(): opacity = gr.Slider( 0.1, 1.0, value=0.9, step=0.05, label="Overlay opacity" ) width = gr.Slider(1, 5, value=2, step=1, label="Line width") with gr.Row(): contrast = gr.Slider( 0.5, 3.0, value=1.0, step=0.05, label="Display contrast" ) brightness = gr.Slider( -0.4, 0.4, value=0.0, step=0.02, label="Display brightness" ) with gr.Row(): artifact = gr.Dropdown( choices=[ ("Boundary predictions · NPZ", "predictions"), ("Current B-scan overlay · PNG", "overlay"), ], value="predictions", label="Export format", scale=2, ) download = gr.DownloadButton( "Download selected output", interactive=False, scale=1, min_width=175, ) gr.Markdown( "Display settings change the view only. Predictions retain original OCT coordinates." ) display_settings = [ boundaries, show_overlay, opacity, width, show_labels, contrast, brightness, physical, artifact, ] settings_without_keys = display_settings[1:] display_widgets = [original, segmented, caption, download] upload_outputs = [ identity, original, segmented, frame, boundaries, scan_info, colors, caption, download, messages, run, ] upload.upload( on_upload, [upload, identity, scanner, *settings_without_keys], upload_outputs, api_name="load_oct", concurrency_id="viewer", concurrency_limit=1, ) upload.clear( clear_upload, [identity, artifact], upload_outputs, concurrency_id="viewer", concurrency_limit=1, ) clear.click( clear_volume, [identity], upload_outputs + [upload, scanner, artifact], api_name="clear_volume", concurrency_id="viewer", concurrency_limit=1, ) scanner.change( scanner_changed, [identity, scanner, frame, *settings_without_keys], [boundaries, scan_info, colors, *display_widgets, messages], concurrency_id="viewer", concurrency_limit=1, ) frame.release( display_outputs, [identity, frame, *display_settings], display_widgets, queue=False, api_name="view_bscan", ) for button, delta in [(previous, -1), (following, 1)]: button.click( move_frame(delta), [identity, frame, *display_settings], [frame, *display_widgets], queue=False, api_name=False, ) for control in (show_overlay, show_labels, physical, boundaries): control.input( update_display, [identity, frame, *display_settings], [*display_widgets, colors], queue=False, api_name=False, ) for control in (opacity, width, contrast, brightness): control.release( display_outputs, [identity, frame, *display_settings], display_widgets, queue=False, api_name=False, ) artifact.input( display_outputs, [identity, frame, *display_settings], display_widgets, queue=False, api_name="select_export", ) inference = run.click( segment, [identity, frame, scanner, compute, scope, batch, *display_settings], [*display_widgets, messages, run], api_name="segment_oct", show_progress="full", show_progress_on=[segmented, original], concurrency_id="viewer", concurrency_limit=1, ) stop.click( cancel, [identity], [messages, run], cancels=[inference], queue=False, api_name="stop_segmentation", ) return demo