Download app.py from EScO-V10/MedSegNet: direct link, hf CLI and curl.
- Browser
- Download file 3.34 kB
-
https://huggingface.co/spaces/EScO-V10/MedSegNet/resolve/main/app.py
- Command line
-
hf download hf://spaces/EScO-V10/MedSegNet/app.py
-
curl -L -o app.py https://huggingface.co/spaces/EScO-V10/MedSegNet/resolve/main/app.py
3.34 kB
| 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() | |
| 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() |