Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,58 +3,54 @@ import torch
|
|
| 3 |
from torchvision import transforms
|
| 4 |
from PIL import Image
|
| 5 |
from model import load_model
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
|
| 10 |
models_cache = {}
|
| 11 |
|
| 12 |
class_names = [
|
| 13 |
-
'Alzheimer Disease',
|
| 14 |
-
'Mild Alzheimer Risk',
|
| 15 |
'Moderate Alzheimer Risk',
|
| 16 |
-
'Very Mild Alzheimer Risk',
|
| 17 |
-
'No Risk',
|
| 18 |
'Parkinson Disease'
|
| 19 |
]
|
| 20 |
|
| 21 |
transform = transforms.Compose([
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
models_cache[model_name] = model
|
| 32 |
-
else:
|
| 33 |
-
model = models_cache[model_name]
|
| 34 |
|
| 35 |
-
|
| 36 |
-
tensor = transform(
|
| 37 |
|
| 38 |
with torch.no_grad():
|
| 39 |
outputs = model(tensor)
|
| 40 |
probs = torch.nn.functional.softmax(outputs, dim=1)[0]
|
| 41 |
-
predicted = torch.argmax(probs).item()
|
| 42 |
|
| 43 |
return {class_names[i]: float(probs[i]) for i in range(len(class_names))}
|
| 44 |
|
| 45 |
-
|
| 46 |
model_options = [
|
| 47 |
"Vbai-DPA 2.1f",
|
| 48 |
"Vbai-DPA 2.1c",
|
| 49 |
"Vbai-DPA 2.1q"
|
| 50 |
]
|
| 51 |
|
|
|
|
| 52 |
with gr.Blocks() as demo:
|
| 53 |
-
gr.Markdown("Dementia and Parkinson Diagnosis 2.1 🧠")
|
| 54 |
-
gr.Markdown("Select model
|
| 55 |
|
| 56 |
with gr.Row():
|
| 57 |
-
|
| 58 |
image_input = gr.Image(type="pil", label="MRI/fMRI Image")
|
| 59 |
output_label = gr.Label(num_top_classes=3, label="Top Predictions")
|
| 60 |
|
|
|
|
| 3 |
from torchvision import transforms
|
| 4 |
from PIL import Image
|
| 5 |
from model import load_model
|
|
|
|
| 6 |
|
| 7 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 8 |
|
| 9 |
models_cache = {}
|
| 10 |
|
| 11 |
class_names = [
|
| 12 |
+
'Alzheimer Disease',
|
| 13 |
+
'Mild Alzheimer Risk',
|
| 14 |
'Moderate Alzheimer Risk',
|
| 15 |
+
'Very Mild Alzheimer Risk',
|
| 16 |
+
'No Risk',
|
| 17 |
'Parkinson Disease'
|
| 18 |
]
|
| 19 |
|
| 20 |
transform = transforms.Compose([
|
| 21 |
+
transforms.Resize((448, 448)),
|
| 22 |
+
transforms.ToTensor(),
|
| 23 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406],
|
| 24 |
+
std=[0.229, 0.224, 0.225])
|
| 25 |
+
])
|
| 26 |
|
| 27 |
+
def predict(version: str, image: Image.Image):
|
| 28 |
+
if version not in models_cache:
|
| 29 |
+
models_cache[version] = load_model(version, device)
|
| 30 |
+
model = models_cache[version]
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
img = image.convert("RGB")
|
| 33 |
+
tensor = transform(img).unsqueeze(0).to(device)
|
| 34 |
|
| 35 |
with torch.no_grad():
|
| 36 |
outputs = model(tensor)
|
| 37 |
probs = torch.nn.functional.softmax(outputs, dim=1)[0]
|
|
|
|
| 38 |
|
| 39 |
return {class_names[i]: float(probs[i]) for i in range(len(class_names))}
|
| 40 |
|
|
|
|
| 41 |
model_options = [
|
| 42 |
"Vbai-DPA 2.1f",
|
| 43 |
"Vbai-DPA 2.1c",
|
| 44 |
"Vbai-DPA 2.1q"
|
| 45 |
]
|
| 46 |
|
| 47 |
+
# Gradio arayüzü
|
| 48 |
with gr.Blocks() as demo:
|
| 49 |
+
gr.Markdown("# Dementia and Parkinson Diagnosis 2.1 🧠")
|
| 50 |
+
gr.Markdown("Select the model you want to choose (f, c or q) and upload a 2D brain slice.")
|
| 51 |
|
| 52 |
with gr.Row():
|
| 53 |
+
version_selector = gr.Radio(choices=model_options, value='f', label="Model Version")
|
| 54 |
image_input = gr.Image(type="pil", label="MRI/fMRI Image")
|
| 55 |
output_label = gr.Label(num_top_classes=3, label="Top Predictions")
|
| 56 |
|