File size: 3,344 Bytes
19842cd
25b6de6
 
 
ab78d06
25b6de6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19842cd
25b6de6
19842cd
25b6de6
 
19842cd
9b9dc60
 
 
52822e3
19842cd
25b6de6
19842cd
52822e3
 
 
 
 
 
 
 
 
 
 
 
25b6de6
ab78d06
 
25b6de6
52822e3
 
 
 
 
9b9dc60
52822e3
 
 
 
 
 
 
 
 
 
 
 
 
 
9b9dc60
0ffb846
 
9b9dc60
 
 
52822e3
9b9dc60
 
 
52822e3
 
 
 
 
9b9dc60
0ffb846
582999d
52822e3
 
 
 
 
0ffb846
9b9dc60
 
 
 
 
 
 
 
25b6de6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import spaces
import gradio as gr
import torch
from model import MedSegNet
from inference import preprocess_volume, run_inference, make_slice_figure, make_combined_figure
from report import build_pdf_report

device = "cuda" if torch.cuda.is_available() else "cpu"

model = MedSegNet(
    in_channels=4,
    sr_ratios=[4, 2, 1, 1],
    embed_dims=[32, 64, 160, 256],
    patch_kernel_size=[7, 3, 3, 3],
    patch_stride=[4, 2, 2, 2],
    patch_padding=[3, 1, 1, 1],
    mlp_ratios=[4, 4, 4, 4],
    num_heads=[1, 2, 5, 8],
    depths=[2, 2, 2, 2],
    decoder_head_embedding_dim=256,
    num_classes=3,
    decoder_dropout=0.0,
)

state_dict = torch.load("best_model.pt", map_location="cpu")
model.load_state_dict(state_dict)
model.eval()


@spaces.GPU
def run(flair_file, t1_file, t1ce_file, t2_file):
    if not all([flair_file, t1_file, t1ce_file, t2_file]):
        raise gr.Error("Please upload all 4 modalities: FLAIR, T1, T1ce, and T2.")

    model.to("cuda")
    volume, affine = preprocess_volume(t1_file, t1ce_file, t2_file, flair_file)
    pred = run_inference(model, volume, "cuda")

    tc_voxels = int(pred[0].sum())
    wt_voxels = int(pred[1].sum())
    et_voxels = int(pred[2].sum())

    stats_md = (
        f"### Predicted Region Volumes (voxels)\n"
        f"- **Whole Tumor (WT):** {wt_voxels:,}\n"
        f"- **Tumor Core (TC):** {tc_voxels:,}\n"
        f"- **Enhancing Tumor (ET):** {et_voxels:,}\n"
    )

    fig_path = make_slice_figure(volume, pred)
    combined_path = make_combined_figure(volume, pred)
    pdf_path = build_pdf_report(volume, pred, fig_path, combined_path)

    return combined_path, fig_path, stats_md, pdf_path


def load_example():
    return (
        "examples/BRATS_001_0000.nii.gz",  # FLAIR
        "examples/BRATS_001_0001.nii.gz",  # T1
        "examples/BRATS_001_0002.nii.gz",  # T1ce
        "examples/BRATS_001_0003.nii.gz",  # T2
    )


theme = gr.themes.Soft(
    primary_hue="blue",
    secondary_hue="cyan",
).set(
    body_background_fill="*neutral_50",
)

with gr.Blocks(title="MedSegNet — 3D Brain Tumor Segmentation", theme=theme) as demo:
    gr.Markdown("# MedSegNet")

    with gr.Row():
        with gr.Column(scale=1):
            gr.Markdown("#### 1. Upload MRI Volumes")
            gr.Markdown("*(.nii or .nii.gz format, all 4 modalities required)*")
            flair = gr.File(label="FLAIR")
            t1 = gr.File(label="T1")
            t1ce = gr.File(label="T1ce")
            t2 = gr.File(label="T2")

            with gr.Row():
                example_btn = gr.Button("📁 Load Example Case", variant="secondary")
                submit_btn = gr.Button("▶ Run Segmentation", variant="primary")

            pdf_output = gr.DownloadButton("📄 Download PDF Report", variant="secondary")

        with gr.Column(scale=1):
            gr.Markdown("#### 2. Segmentation Result")
            combined_image = gr.Image(label="Combined Segmentation Overlay")
            stats_output = gr.Markdown()
            gr.Markdown("#### Region Breakdown")
            output_image = gr.Image(label="FLAIR / WT / TC / ET")

    example_btn.click(fn=load_example, outputs=[flair, t1, t1ce, t2])

    submit_btn.click(
        fn=run,
        inputs=[flair, t1, t1ce, t2],
        outputs=[combined_image, output_image, stats_output, pdf_output],
    )

demo.launch()