Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
from app.model import predict, gradcam, CLASS_NAMES
|
| 4 |
+
|
| 5 |
+
def predict_fn(img):
|
| 6 |
+
label, confidence, probs = predict(img)
|
| 7 |
+
probs_sorted = {k: float(v) for k, v in sorted(probs.items(), key=lambda x: x[1], reverse=True)}
|
| 8 |
+
return {
|
| 9 |
+
"Predicted label": label,
|
| 10 |
+
"Confidence": round(confidence, 3),
|
| 11 |
+
"Class probabilities": probs_sorted
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
def gradcam_fn(img, interpolant):
|
| 15 |
+
heatmap = gradcam(img, interpolant=float(interpolant))
|
| 16 |
+
return Image.fromarray(heatmap)
|
| 17 |
+
|
| 18 |
+
with gr.Blocks(title="Brain Tumor MRI Classifier (InceptionV3 + Grad-CAM)") as demo:
|
| 19 |
+
gr.Markdown("# Brain Tumor MRI Classifier (InceptionV3 + Grad-CAM)")
|
| 20 |
+
gr.Markdown("Upload an MRI image to classify and visualize Grad-CAM explanation.")
|
| 21 |
+
|
| 22 |
+
with gr.Row():
|
| 23 |
+
with gr.Column():
|
| 24 |
+
input_img = gr.Image(type="pil", label="Upload MRI Image")
|
| 25 |
+
interpolant_slider = gr.Slider(0, 1, value=0.5, label="Grad-CAM Intensity (interpolant)")
|
| 26 |
+
submit_btn = gr.Button("Run Prediction + Grad-CAM")
|
| 27 |
+
|
| 28 |
+
with gr.Column():
|
| 29 |
+
output_json = gr.JSON(label="Prediction Results")
|
| 30 |
+
output_cam = gr.Image(label="Grad-CAM Overlay")
|
| 31 |
+
|
| 32 |
+
submit_btn.click(
|
| 33 |
+
fn=lambda img, interp: (predict_fn(img), gradcam_fn(img, interp)),
|
| 34 |
+
inputs=[input_img, interpolant_slider],
|
| 35 |
+
outputs=[output_json, output_cam]
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
demo.launch()
|