Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn.functional as F
|
| 4 |
+
from torchvision import transforms
|
| 5 |
+
from PIL import Image
|
| 6 |
+
from model import load_model
|
| 7 |
+
|
| 8 |
+
# Load models once (inference)
|
| 9 |
+
models = {v: load_model(v) for v in ["f", "c", "q"]}
|
| 10 |
+
|
| 11 |
+
# Define 2D image preprocessing
|
| 12 |
+
transform = transforms.Compose([
|
| 13 |
+
transforms.Resize((448, 448)),
|
| 14 |
+
transforms.ToTensor(),
|
| 15 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 16 |
+
std=[0.229, 0.224, 0.225])
|
| 17 |
+
])
|
| 18 |
+
|
| 19 |
+
def predict(img: Image.Image):
|
| 20 |
+
"""
|
| 21 |
+
Gradio prediction function for a single 2D image.
|
| 22 |
+
Returns a dict of model-version to positive-class probability.
|
| 23 |
+
"""
|
| 24 |
+
# Preprocess image and add batch dimension
|
| 25 |
+
x = transform(img).unsqueeze(0)
|
| 26 |
+
|
| 27 |
+
# Run inference and collect probabilities
|
| 28 |
+
results = {}
|
| 29 |
+
with torch.no_grad():
|
| 30 |
+
for version, model in models.items():
|
| 31 |
+
output = model(x)
|
| 32 |
+
# assume binary classification: index 1 is positive class
|
| 33 |
+
prob = F.softmax(output, dim=1)[0, 1].item()
|
| 34 |
+
results[version] = prob
|
| 35 |
+
return results
|
| 36 |
+
|
| 37 |
+
# Build the Gradio interface
|
| 38 |
+
iface = gr.Interface(
|
| 39 |
+
fn=predict,
|
| 40 |
+
inputs=gr.Image(type="pil", label="Brain Slice Image (2D)"),
|
| 41 |
+
outputs=gr.Label(num_top_classes=3, label="Alzheimer Risk Probabilities"),
|
| 42 |
+
title="Vbai-DPA 2.2c 2D Alzheimer Risk Classification",
|
| 43 |
+
description="Yüklediğiniz 2D beyin görüntüsü dilimi üzerinden f, c, q modellerinin pozitif sınıf ihtimallerini gösterir."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
iface.launch()
|