gatekeeper / app.py
Siyad798's picture
Update app.py
45d275a verified
Raw History Blame Contribute Delete
9.32 kB
import os
import shutil
import json
import torch
import torch.nn as nn
import torchvision
import numpy as np
import scipy.ndimage as ndimage
from skimage.measure import label, regionprops
import gradio as gr
import nibabel as nib
from monai.transforms import (
Compose, LoadImaged, EnsureChannelFirstd,
Resized, ScaleIntensityd, ToTensord, Lambdad,
Orientationd, Spacingd, ScaleIntensityRangePercentilesd
)
# ==========================================
# 1. MODEL ARCHITECTURE
# ==========================================
def get_pretrained_gatekeeper(num_classes):
model = torchvision.models.video.r3d_18(weights=None)
original_stem = model.stem[0]
new_stem = nn.Conv3d(1, original_stem.out_channels,
kernel_size=original_stem.kernel_size,
stride=original_stem.stride,
padding=original_stem.padding, bias=False)
model.stem[0] = new_stem
model.fc = nn.Linear(model.fc.in_features, num_classes)
return model
# ==========================================
# 2. INTELLIGENT AGENT SERVICE
# ==========================================
class GatekeeperService:
def __init__(self, model_path):
self.device = torch.device("cpu")
self.classes = ["normal", "tumor", "ischemic"]
print(f"--- 🔄 Initializing Gatekeeper Agent ---")
self.model = get_pretrained_gatekeeper(num_classes=3)
if os.path.exists(model_path):
state_dict = torch.load(model_path, map_location=self.device)
self.model.load_state_dict(state_dict)
self.model.to(self.device)
self.model.eval()
print("✅ Model Loaded Successfully.")
else:
print(f"⚠️ Model file '{model_path}' not found.")
# --- AI PIPELINE ---
# Robust scaling for the AI to handle artifacts/skull
self.transforms = Compose([
LoadImaged(keys=["image"]),
EnsureChannelFirstd(keys=["image"]),
Orientationd(keys=["image"], axcodes="RAS"),
Spacingd(keys=["image"], pixdim=(1.5, 1.5, 1.5), mode=("bilinear")),
Lambdad(keys=["image"], func=lambda x: x[0:1]),
ScaleIntensityRangePercentilesd(
keys=["image"], lower=1, upper=99,
b_min=0.0, b_max=1.0, clip=True
),
Resized(keys=["image"], spatial_size=(96, 96, 96)),
ToTensord(keys=["image"])
])
def identify_sequences(self, file_paths):
flair_path = None
swi_path = None
debug_log = {}
if not file_paths: return None, None, {}
for path in file_paths:
filename = os.path.basename(path).lower()
debug_log[filename] = "Ignored"
if "flair" in filename or "t2_flair" in filename:
flair_path = path
debug_log[filename] = "Matched: FLAIR"
elif "swi" in filename or "gre" in filename or "hemo" in filename:
swi_path = path
debug_log[filename] = "Matched: SWI"
return flair_path, swi_path, debug_log
def physics_check_hemorrhage(self, swi_path):
# (Physics logic remains unchanged as it was working correctly)
if not swi_path: return False, 0.0, "N/A"
try:
img_obj = nib.load(swi_path)
vol = img_obj.get_fdata()
zooms = img_obj.header.get_zooms()
voxel_vol = zooms[0] * zooms[1] * zooms[2]
threshold = np.mean(vol) * 0.1
brain_mask = vol > threshold
brain_center = ndimage.center_of_mass(brain_mask)
brain_pixels = vol[brain_mask]
if len(brain_pixels) > 0:
p99 = np.percentile(brain_pixels, 99)
vol = np.clip(vol / p99, 0, 1)
dark_mask = (vol < 0.05) & brain_mask
labeled_vol = label(dark_mask)
regions = regionprops(labeled_vol)
largest_bleed = 0.0
bleed_type = "Clean"
for region in regions:
size_mm3 = region.area * voxel_vol
if 2000 < size_mm3 < 300000:
centroid = region.centroid
dist = np.linalg.norm(np.array(centroid) - np.array(brain_center))
min_r, min_c, min_d, max_r, max_c, max_d = region.bbox
dims = sorted([max_r-min_r, max_c-min_c, max_d-min_d])
elongation = dims[2] / (dims[0] + 1e-5)
if elongation < 3.0:
if size_mm3 > largest_bleed:
largest_bleed = size_mm3
bleed_type = f"Deep Lesion (Vol: {size_mm3/1000:.1f}mL)"
if largest_bleed > 0:
return True, largest_bleed, bleed_type
return False, 0.0, "Artifacts Filtered"
except Exception as e:
print(f"Physics Error: {e}")
return False, 0.0, "Error"
def process_batch(self, file_list):
flair_path, swi_path, file_log = self.identify_sequences(file_list)
response = {
"status": "PROCESSING",
"action": "WAIT",
"findings": [],
"debug_files": file_log
}
if not flair_path:
response["status"] = "FAILED"
response["findings"].append("Error: No FLAIR sequence found.")
return response
# --- STEP 1: SAFETY GATE (Physics) ---
if swi_path:
is_bleed, size, shape = self.physics_check_hemorrhage(swi_path)
if is_bleed:
response["status"] = "COMPLETED"
response["action"] = "CONSULT_REPORT"
response["findings"].append(f"SAFETY STOP: Suspected Hemorrhage ({shape}) on SWI.")
return response
else:
response["findings"].append("Safety Check: No active hemorrhage detected.")
# --- STEP 2: TRIAGE (AI) ---
try:
input_dict = [{"image": flair_path}]
tensor_data = self.transforms(input_dict)[0]["image"].unsqueeze(0).to(self.device)
with torch.no_grad():
output = self.model(tensor_data)
probs = torch.softmax(output, dim=1)
# Probabilities
# 0: Normal, 1: Tumor, 2: Ischemic
p_tumor = probs[0, 1].item()
p_ischemic = probs[0, 2].item()
pred_idx = torch.argmax(probs, dim=1).item()
diagnosis = self.classes[pred_idx]
# --- RESTORED FILTERING LOGIC ---
# 1. Clear Stroke -> Go
if p_ischemic > 0.35:
response["status"] = "COMPLETED"
response["action"] = "ACTIVATE_VISION_AGENT"
response["findings"].append(f"Triage: Ischemic Stroke Candidate (Confidence {p_ischemic:.2%}).")
# 2. Clear Tumor -> Stop (THIS IS THE FIX)
# If the model is >80% sure it's a Tumor, we trust it and stop the pipeline.
elif diagnosis == "tumor" and p_tumor > 0.80:
response["status"] = "COMPLETED"
response["action"] = "CONSULT_REPORT" # <--- NOW IT STOPS
response["findings"].append(f"Triage: High Confidence Tumor ({p_tumor:.2%}). Oncology referral suggested. Vision Agent Skipped.")
# 3. Ambiguous Tumor -> Go (Safety)
# If it's a Tumor but confidence is low (60%), we double check with Vision Agent.
elif diagnosis == "tumor":
response["status"] = "COMPLETED"
response["action"] = "ACTIVATE_VISION_AGENT"
response["findings"].append(f"Triage: Ambiguous Mass ({p_tumor:.2%}). Routing to Vision Agent to verify if Ischemic.")
# 4. Normal/Other -> Stop
else:
response["status"] = "COMPLETED"
response["action"] = "CONSULT_REPORT"
response["findings"].append("Triage: No acute findings.")
except Exception as e:
response["status"] = "FAILED"
response["findings"].append(f"AI Error: {str(e)}")
return response
# ==========================================
# 3. INITIALIZATION & UI
# ==========================================
MODEL_FILE = "gatekeeper_hybrid_trained.pth"
agent = GatekeeperService(MODEL_FILE)
def predict_mri_protocol(file_list):
return agent.process_batch(file_list)
with gr.Blocks() as demo:
gr.Markdown("# 🧠 Neuro-Triage: Gatekeeper Node")
with gr.Row():
with gr.Column():
file_input = gr.File(label="Upload MRI Protocol", file_count="multiple", type="filepath")
submit_btn = gr.Button("Triaging Patient", variant="primary")
with gr.Column():
json_output = gr.JSON(label="Orchestrator Routing Instructions")
submit_btn.click(predict_mri_protocol, inputs=[file_input], outputs=[json_output])
if __name__ == "__main__":
demo.launch()