Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ import numpy as np
|
|
| 11 |
|
| 12 |
app = FastAPI(
|
| 13 |
title="NeuroHealth EEG System",
|
| 14 |
-
version="7.
|
| 15 |
)
|
| 16 |
|
| 17 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
@@ -31,7 +31,7 @@ class EEGRequest(BaseModel):
|
|
| 31 |
features: list
|
| 32 |
|
| 33 |
# =========================================================
|
| 34 |
-
# AD MODEL (CNN -
|
| 35 |
# =========================================================
|
| 36 |
|
| 37 |
class EEG_CNN_AD(nn.Module):
|
|
@@ -61,7 +61,7 @@ class EEG_CNN_AD(nn.Module):
|
|
| 61 |
return self.fc(x)
|
| 62 |
|
| 63 |
# =========================================================
|
| 64 |
-
# PD MODEL (
|
| 65 |
# =========================================================
|
| 66 |
|
| 67 |
class EEG_PD(nn.Module):
|
|
@@ -69,22 +69,22 @@ class EEG_PD(nn.Module):
|
|
| 69 |
super().__init__()
|
| 70 |
|
| 71 |
self.net = nn.Sequential(
|
| 72 |
-
nn.Linear(76, 256),
|
| 73 |
nn.ReLU(),
|
| 74 |
-
nn.BatchNorm1d(256),
|
| 75 |
|
| 76 |
-
nn.Linear(256, 128),
|
| 77 |
nn.ReLU(),
|
| 78 |
-
nn.BatchNorm1d(128),
|
| 79 |
|
| 80 |
-
nn.Linear(128, output_dim)
|
| 81 |
)
|
| 82 |
|
| 83 |
def forward(self, x):
|
| 84 |
return self.net(x)
|
| 85 |
|
| 86 |
# =========================================================
|
| 87 |
-
# PATHS
|
| 88 |
# =========================================================
|
| 89 |
|
| 90 |
AD_MODEL_PATH = "AD_eeg_cnn_ad_ftd_cn.pt"
|
|
@@ -109,18 +109,31 @@ print("PD model loaded")
|
|
| 109 |
print("System ready")
|
| 110 |
|
| 111 |
# =========================================================
|
| 112 |
-
#
|
| 113 |
# =========================================================
|
| 114 |
|
| 115 |
-
def
|
| 116 |
|
| 117 |
x = torch.tensor(features, dtype=torch.float32)
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
else:
|
| 123 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 124 |
|
| 125 |
with torch.no_grad():
|
| 126 |
logits = model(x)
|
|
@@ -144,18 +157,18 @@ def predict(model, features, classes):
|
|
| 144 |
def home():
|
| 145 |
return {
|
| 146 |
"status": "running",
|
| 147 |
-
"
|
| 148 |
-
"
|
| 149 |
}
|
| 150 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
@app.post("/predict/ad")
|
| 152 |
def predict_ad(req: EEGRequest):
|
| 153 |
-
return predict(ad_model, req.features, AD_CLASSES)
|
| 154 |
|
| 155 |
@app.post("/predict/pd")
|
| 156 |
def predict_pd(req: EEGRequest):
|
| 157 |
-
return predict(pd_model, req.features, PD_CLASSES)
|
| 158 |
-
|
| 159 |
-
@app.get("/health")
|
| 160 |
-
def health():
|
| 161 |
-
return {"status": "ok"}
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI(
|
| 13 |
title="NeuroHealth EEG System",
|
| 14 |
+
version="7.1"
|
| 15 |
)
|
| 16 |
|
| 17 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 31 |
features: list
|
| 32 |
|
| 33 |
# =========================================================
|
| 34 |
+
# AD MODEL (CNN - REAL ARCHITECTURE)
|
| 35 |
# =========================================================
|
| 36 |
|
| 37 |
class EEG_CNN_AD(nn.Module):
|
|
|
|
| 61 |
return self.fc(x)
|
| 62 |
|
| 63 |
# =========================================================
|
| 64 |
+
# PD MODEL (FULLY CONNECTED NETWORK - EXACT MATCH)
|
| 65 |
# =========================================================
|
| 66 |
|
| 67 |
class EEG_PD(nn.Module):
|
|
|
|
| 69 |
super().__init__()
|
| 70 |
|
| 71 |
self.net = nn.Sequential(
|
| 72 |
+
nn.Linear(76, 256), # net.0
|
| 73 |
nn.ReLU(),
|
| 74 |
+
nn.BatchNorm1d(256), # net.2
|
| 75 |
|
| 76 |
+
nn.Linear(256, 128), # net.4
|
| 77 |
nn.ReLU(),
|
| 78 |
+
nn.BatchNorm1d(128), # net.6
|
| 79 |
|
| 80 |
+
nn.Linear(128, output_dim) # net.7
|
| 81 |
)
|
| 82 |
|
| 83 |
def forward(self, x):
|
| 84 |
return self.net(x)
|
| 85 |
|
| 86 |
# =========================================================
|
| 87 |
+
# MODEL PATHS
|
| 88 |
# =========================================================
|
| 89 |
|
| 90 |
AD_MODEL_PATH = "AD_eeg_cnn_ad_ftd_cn.pt"
|
|
|
|
| 109 |
print("System ready")
|
| 110 |
|
| 111 |
# =========================================================
|
| 112 |
+
# INPUT PREPARATION
|
| 113 |
# =========================================================
|
| 114 |
|
| 115 |
+
def prepare_input(features, model_type):
|
| 116 |
|
| 117 |
x = torch.tensor(features, dtype=torch.float32)
|
| 118 |
|
| 119 |
+
if model_type == "ad":
|
| 120 |
+
# CNN expects 19 x 76
|
| 121 |
+
return x.view(1, 19, 76)
|
| 122 |
+
|
| 123 |
+
elif model_type == "pd":
|
| 124 |
+
# Dense expects 76 only
|
| 125 |
+
return x.view(1, 76)
|
| 126 |
+
|
| 127 |
else:
|
| 128 |
+
raise ValueError("Unknown model type")
|
| 129 |
+
|
| 130 |
+
# =========================================================
|
| 131 |
+
# PREDICTION ENGINE
|
| 132 |
+
# =========================================================
|
| 133 |
+
|
| 134 |
+
def predict(model, features, classes, model_type):
|
| 135 |
+
|
| 136 |
+
x = prepare_input(features, model_type).to(DEVICE)
|
| 137 |
|
| 138 |
with torch.no_grad():
|
| 139 |
logits = model(x)
|
|
|
|
| 157 |
def home():
|
| 158 |
return {
|
| 159 |
"status": "running",
|
| 160 |
+
"ad_model": "CNN (19x76)",
|
| 161 |
+
"pd_model": "Dense (76)"
|
| 162 |
}
|
| 163 |
|
| 164 |
+
@app.get("/health")
|
| 165 |
+
def health():
|
| 166 |
+
return {"status": "ok"}
|
| 167 |
+
|
| 168 |
@app.post("/predict/ad")
|
| 169 |
def predict_ad(req: EEGRequest):
|
| 170 |
+
return predict(ad_model, req.features, AD_CLASSES, "ad")
|
| 171 |
|
| 172 |
@app.post("/predict/pd")
|
| 173 |
def predict_pd(req: EEGRequest):
|
| 174 |
+
return predict(pd_model, req.features, PD_CLASSES, "pd")
|
|
|
|
|
|
|
|
|
|
|
|