Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import tempfile
|
| 3 |
+
import nibabel as nib
|
| 4 |
+
import numpy as np
|
| 5 |
+
import gdown
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
# nnU-Net imports
|
| 10 |
+
from nnunetv2.inference.predict_from_raw_data import nnUNetPredictor
|
| 11 |
+
|
| 12 |
+
# -------------------------------
|
| 13 |
+
# STEP 1 β Download model if needed
|
| 14 |
+
# -------------------------------
|
| 15 |
+
MODEL_DIR = "models/nnUNet_trained"
|
| 16 |
+
os.makedirs(MODEL_DIR, exist_ok=True)
|
| 17 |
+
|
| 18 |
+
FOLDER_URL = "https://drive.google.com/drive/folders/163zOL8NmdYqhRCAGNOWG7Ak85_awF4Xv"
|
| 19 |
+
|
| 20 |
+
if not os.path.exists(os.path.join(MODEL_DIR, "fold_0")):
|
| 21 |
+
print("β¬ Downloading nnU-Net model from Google Drive ...")
|
| 22 |
+
gdown.download_folder(FOLDER_URL, output=MODEL_DIR, quiet=False, use_cookies=False)
|
| 23 |
+
print("β
Model downloaded.")
|
| 24 |
+
else:
|
| 25 |
+
print("β
Model already cached, skipping download.")
|
| 26 |
+
|
| 27 |
+
# -------------------------------
|
| 28 |
+
# STEP 2 β Initialize predictor
|
| 29 |
+
# -------------------------------
|
| 30 |
+
print("π§ Initializing nnU-Net predictor ...")
|
| 31 |
+
predictor = nnUNetPredictor(
|
| 32 |
+
model_training_output_dir=MODEL_DIR,
|
| 33 |
+
use_folds=[0, 1, 2, 3, 4], # ensemble of 5 folds
|
| 34 |
+
checkpoint_name="checkpoint_final.pth",
|
| 35 |
+
device=torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 36 |
+
)
|
| 37 |
+
predictor.initialize_from_trained_model_folder()
|
| 38 |
+
print("β
Predictor ready.")
|
| 39 |
+
|
| 40 |
+
# -------------------------------
|
| 41 |
+
# STEP 3 β Define inference function
|
| 42 |
+
# -------------------------------
|
| 43 |
+
def run_inference(nii_file):
|
| 44 |
+
# Save uploaded file to a temporary path
|
| 45 |
+
with tempfile.TemporaryDirectory() as tmpdir:
|
| 46 |
+
input_path = os.path.join(tmpdir, "input.nii.gz")
|
| 47 |
+
nii_file.save(input_path)
|
| 48 |
+
|
| 49 |
+
# Output directory
|
| 50 |
+
output_dir = os.path.join(tmpdir, "output")
|
| 51 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 52 |
+
|
| 53 |
+
# Run prediction
|
| 54 |
+
predictor.predict_from_files(
|
| 55 |
+
[input_path],
|
| 56 |
+
output_dir=output_dir,
|
| 57 |
+
save_probabilities=False
|
| 58 |
+
)
|
| 59 |
+
|
| 60 |
+
# Get the result file
|
| 61 |
+
output_files = [f for f in os.listdir(output_dir) if f.endswith(".nii.gz")]
|
| 62 |
+
if not output_files:
|
| 63 |
+
raise RuntimeError("Prediction failed: No output NIfTI file found.")
|
| 64 |
+
|
| 65 |
+
result_path = os.path.join(output_dir, output_files[0])
|
| 66 |
+
|
| 67 |
+
# Load and overlay for preview
|
| 68 |
+
img = nib.load(input_path).get_fdata()
|
| 69 |
+
seg = nib.load(result_path).get_fdata()
|
| 70 |
+
|
| 71 |
+
# Simple 2D preview (middle slice)
|
| 72 |
+
mid_slice = img.shape[2] // 2
|
| 73 |
+
overlay = np.stack([
|
| 74 |
+
img[:, :, mid_slice] / np.max(img), # base grayscale
|
| 75 |
+
seg[:, :, mid_slice] / np.max(seg) if np.max(seg) > 0 else np.zeros_like(seg[:, :, mid_slice]),
|
| 76 |
+
np.zeros_like(seg[:, :, mid_slice])
|
| 77 |
+
], axis=-1)
|
| 78 |
+
|
| 79 |
+
return (overlay, result_path)
|
| 80 |
+
|
| 81 |
+
# -------------------------------
|
| 82 |
+
# STEP 4 β Gradio Interface
|
| 83 |
+
# -------------------------------
|
| 84 |
+
title = "π§ Physics-Informed nnU-Net (Fisher PDE) for Glioblastoma MRI Segmentation"
|
| 85 |
+
description = """
|
| 86 |
+
Upload a **.nii** or **.nii.gz** MRI volume to run inference using the Physics-Informed nnU-Net model.
|
| 87 |
+
The model integrates a Fisher diffusion-reaction equation for enhanced tumor boundary accuracy.
|
| 88 |
+
"""
|
| 89 |
+
|
| 90 |
+
iface = gr.Interface(
|
| 91 |
+
fn=run_inference,
|
| 92 |
+
inputs=gr.File(label="Upload MRI NIfTI (.nii / .nii.gz)"),
|
| 93 |
+
outputs=[
|
| 94 |
+
gr.Image(label="Segmentation Overlay (mid-slice)"),
|
| 95 |
+
gr.File(label="Download Full 3D Segmentation (.nii.gz)")
|
| 96 |
+
],
|
| 97 |
+
title=title,
|
| 98 |
+
description=description,
|
| 99 |
+
allow_flagging="never"
|
| 100 |
+
)
|
| 101 |
+
|
| 102 |
+
if __name__ == "__main__":
|
| 103 |
+
iface.launch()
|