File size: 1,418 Bytes
3f365e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from PIL import Image
from app.model import predict, gradcam, CLASS_NAMES

def predict_fn(img):
    label, confidence, probs = predict(img)
    probs_sorted = {k: float(v) for k, v in sorted(probs.items(), key=lambda x: x[1], reverse=True)}
    return {
        "Predicted label": label,
        "Confidence": round(confidence, 3),
        "Class probabilities": probs_sorted
    }

def gradcam_fn(img, interpolant):
    heatmap = gradcam(img, interpolant=float(interpolant))
    return Image.fromarray(heatmap)

with gr.Blocks(title="Brain Tumor MRI Classifier (InceptionV3 + Grad-CAM)") as demo:
    gr.Markdown("# Brain Tumor MRI Classifier (InceptionV3 + Grad-CAM)")
    gr.Markdown("Upload an MRI image to classify and visualize Grad-CAM explanation.")

    with gr.Row():
        with gr.Column():
            input_img = gr.Image(type="pil", label="Upload MRI Image")
            interpolant_slider = gr.Slider(0, 1, value=0.5, label="Grad-CAM Intensity (interpolant)")
            submit_btn = gr.Button("Run Prediction + Grad-CAM")

        with gr.Column():
            output_json = gr.JSON(label="Prediction Results")
            output_cam = gr.Image(label="Grad-CAM Overlay")

    submit_btn.click(
        fn=lambda img, interp: (predict_fn(img), gradcam_fn(img, interp)),
        inputs=[input_img, interpolant_slider],
        outputs=[output_json, output_cam]
    )

demo.launch()