Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,8 +6,8 @@ import torch.nn as nn
|
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
app = FastAPI(
|
| 9 |
-
title="NeuroHealth
|
| 10 |
-
version="3.
|
| 11 |
)
|
| 12 |
|
| 13 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
@@ -20,14 +20,14 @@ AD_CLASSES = ["Alzheimer", "FTD", "Control"]
|
|
| 20 |
PD_CLASSES = ["Parkinson", "Control"]
|
| 21 |
|
| 22 |
# =========================================================
|
| 23 |
-
#
|
| 24 |
# =========================================================
|
| 25 |
|
| 26 |
class EEGRequest(BaseModel):
|
| 27 |
features: list
|
| 28 |
|
| 29 |
# =========================================================
|
| 30 |
-
# ✔
|
| 31 |
# =========================================================
|
| 32 |
|
| 33 |
class EEG_MLP(nn.Module):
|
|
@@ -35,17 +35,12 @@ class EEG_MLP(nn.Module):
|
|
| 35 |
super().__init__()
|
| 36 |
|
| 37 |
self.net = nn.Sequential(
|
| 38 |
-
nn.Linear(input_dim,
|
| 39 |
-
nn.BatchNorm1d(512),
|
| 40 |
-
nn.ReLU(),
|
| 41 |
-
nn.Dropout(0.3),
|
| 42 |
-
|
| 43 |
-
nn.Linear(512, 256),
|
| 44 |
nn.BatchNorm1d(256),
|
| 45 |
nn.ReLU(),
|
| 46 |
-
nn.Dropout(0.3),
|
| 47 |
|
| 48 |
nn.Linear(256, 128),
|
|
|
|
| 49 |
nn.ReLU(),
|
| 50 |
|
| 51 |
nn.Linear(128, output_dim)
|
|
@@ -55,70 +50,36 @@ class EEG_MLP(nn.Module):
|
|
| 55 |
return self.net(x)
|
| 56 |
|
| 57 |
# =========================================================
|
| 58 |
-
#
|
| 59 |
# =========================================================
|
| 60 |
|
| 61 |
-
|
| 62 |
-
def __init__(self, output_dim):
|
| 63 |
-
super().__init__()
|
| 64 |
-
|
| 65 |
-
self.conv1 = nn.Conv1d(19, 32, kernel_size=7)
|
| 66 |
-
self.bn1 = nn.BatchNorm1d(32)
|
| 67 |
-
|
| 68 |
-
self.conv2 = nn.Conv1d(32, 64, kernel_size=5)
|
| 69 |
-
self.bn2 = nn.BatchNorm1d(64)
|
| 70 |
-
|
| 71 |
-
self.conv3 = nn.Conv1d(64, 128, kernel_size=3)
|
| 72 |
-
self.bn3 = nn.BatchNorm1d(128)
|
| 73 |
|
| 74 |
-
|
| 75 |
-
self.fc = nn.Linear(128, output_dim)
|
| 76 |
-
|
| 77 |
-
def forward(self, x):
|
| 78 |
-
x = torch.relu(self.bn1(self.conv1(x)))
|
| 79 |
-
x = torch.relu(self.bn2(self.conv2(x)))
|
| 80 |
-
x = torch.relu(self.bn3(self.conv3(x)))
|
| 81 |
-
|
| 82 |
-
x = self.pool(x).squeeze(-1)
|
| 83 |
-
return self.fc(x)
|
| 84 |
-
|
| 85 |
-
# =========================================================
|
| 86 |
-
# LOAD MODELS (BASED ON YOUR REAL FILES)
|
| 87 |
-
# =========================================================
|
| 88 |
-
|
| 89 |
-
# ✔ THESE ARE MLP MODELS (CONFIRMED BY "net.*" KEYS)
|
| 90 |
-
|
| 91 |
-
ad_model = EEG_MLP(95, 3).to(DEVICE)
|
| 92 |
ad_model.load_state_dict(torch.load("AD_eeg_cnn.pth", map_location=DEVICE))
|
| 93 |
ad_model.eval()
|
| 94 |
|
| 95 |
-
pd_model = EEG_MLP(
|
| 96 |
pd_model.load_state_dict(torch.load("PD_eeg_cnn.pth", map_location=DEVICE))
|
| 97 |
pd_model.eval()
|
| 98 |
|
| 99 |
-
print("
|
| 100 |
|
| 101 |
# =========================================================
|
| 102 |
-
#
|
| 103 |
# =========================================================
|
| 104 |
|
| 105 |
-
def predict(model, features, classes
|
| 106 |
x = torch.tensor(features, dtype=torch.float32)
|
| 107 |
|
| 108 |
-
if x.numel() !=
|
| 109 |
-
raise ValueError("Expected
|
| 110 |
-
|
| 111 |
-
# ✔ MLP MODE: flat input
|
| 112 |
-
if model_type == "mlp":
|
| 113 |
-
x = x.unsqueeze(0).to(DEVICE)
|
| 114 |
|
| 115 |
-
|
| 116 |
-
elif model_type == "cnn":
|
| 117 |
-
x = x.view(19, 5).unsqueeze(0).to(DEVICE)
|
| 118 |
|
| 119 |
with torch.no_grad():
|
| 120 |
-
|
| 121 |
-
probs = torch.softmax(
|
| 122 |
|
| 123 |
pred = int(np.argmax(probs))
|
| 124 |
|
|
@@ -136,20 +97,15 @@ def predict(model, features, classes, model_type="mlp"):
|
|
| 136 |
|
| 137 |
@app.get("/")
|
| 138 |
def home():
|
| 139 |
-
return {
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
# =========================================================
|
| 144 |
|
| 145 |
@app.post("/predict/ad")
|
| 146 |
def predict_ad(req: EEGRequest):
|
| 147 |
-
return predict(ad_model, req.features, AD_CLASSES
|
| 148 |
-
|
| 149 |
-
# =========================================================
|
| 150 |
-
# PD PREDICTION
|
| 151 |
-
# =========================================================
|
| 152 |
|
| 153 |
@app.post("/predict/pd")
|
| 154 |
def predict_pd(req: EEGRequest):
|
| 155 |
-
return predict(pd_model, req.features, PD_CLASSES
|
|
|
|
| 6 |
import numpy as np
|
| 7 |
|
| 8 |
app = FastAPI(
|
| 9 |
+
title="NeuroHealth EEG API",
|
| 10 |
+
version="3.1"
|
| 11 |
)
|
| 12 |
|
| 13 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
| 20 |
PD_CLASSES = ["Parkinson", "Control"]
|
| 21 |
|
| 22 |
# =========================================================
|
| 23 |
+
# REQUEST MODEL
|
| 24 |
# =========================================================
|
| 25 |
|
| 26 |
class EEGRequest(BaseModel):
|
| 27 |
features: list
|
| 28 |
|
| 29 |
# =========================================================
|
| 30 |
+
# ✔ TRUE MODEL (MATCHES YOUR TRAINED STATE_DICT)
|
| 31 |
# =========================================================
|
| 32 |
|
| 33 |
class EEG_MLP(nn.Module):
|
|
|
|
| 35 |
super().__init__()
|
| 36 |
|
| 37 |
self.net = nn.Sequential(
|
| 38 |
+
nn.Linear(input_dim, 256),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
nn.BatchNorm1d(256),
|
| 40 |
nn.ReLU(),
|
|
|
|
| 41 |
|
| 42 |
nn.Linear(256, 128),
|
| 43 |
+
nn.BatchNorm1d(128),
|
| 44 |
nn.ReLU(),
|
| 45 |
|
| 46 |
nn.Linear(128, output_dim)
|
|
|
|
| 50 |
return self.net(x)
|
| 51 |
|
| 52 |
# =========================================================
|
| 53 |
+
# LOAD MODELS (CRITICAL: INPUT DIM = 76)
|
| 54 |
# =========================================================
|
| 55 |
|
| 56 |
+
INPUT_DIM = 76
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
ad_model = EEG_MLP(INPUT_DIM, 3).to(DEVICE)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
ad_model.load_state_dict(torch.load("AD_eeg_cnn.pth", map_location=DEVICE))
|
| 60 |
ad_model.eval()
|
| 61 |
|
| 62 |
+
pd_model = EEG_MLP(INPUT_DIM, 2).to(DEVICE)
|
| 63 |
pd_model.load_state_dict(torch.load("PD_eeg_cnn.pth", map_location=DEVICE))
|
| 64 |
pd_model.eval()
|
| 65 |
|
| 66 |
+
print("Models loaded successfully with correct architecture")
|
| 67 |
|
| 68 |
# =========================================================
|
| 69 |
+
# PREDICTION ENGINE
|
| 70 |
# =========================================================
|
| 71 |
|
| 72 |
+
def predict(model, features, classes):
|
| 73 |
x = torch.tensor(features, dtype=torch.float32)
|
| 74 |
|
| 75 |
+
if x.numel() != INPUT_DIM:
|
| 76 |
+
raise ValueError(f"Expected {INPUT_DIM} features, got {x.numel()}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
+
x = x.unsqueeze(0).to(DEVICE)
|
|
|
|
|
|
|
| 79 |
|
| 80 |
with torch.no_grad():
|
| 81 |
+
outputs = model(x)
|
| 82 |
+
probs = torch.softmax(outputs, dim=1).cpu().numpy()[0]
|
| 83 |
|
| 84 |
pred = int(np.argmax(probs))
|
| 85 |
|
|
|
|
| 97 |
|
| 98 |
@app.get("/")
|
| 99 |
def home():
|
| 100 |
+
return {
|
| 101 |
+
"message": "NeuroHealth EEG API Running",
|
| 102 |
+
"input_dim": INPUT_DIM
|
| 103 |
+
}
|
|
|
|
| 104 |
|
| 105 |
@app.post("/predict/ad")
|
| 106 |
def predict_ad(req: EEGRequest):
|
| 107 |
+
return predict(ad_model, req.features, AD_CLASSES)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
@app.post("/predict/pd")
|
| 110 |
def predict_pd(req: EEGRequest):
|
| 111 |
+
return predict(pd_model, req.features, PD_CLASSES)
|