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="
|
| 15 |
)
|
| 16 |
|
| 17 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
@@ -24,18 +24,18 @@ AD_CLASSES = ["Alzheimer", "FTD", "Control"]
|
|
| 24 |
PD_CLASSES = ["Parkinson", "Control"]
|
| 25 |
|
| 26 |
# =========================================================
|
| 27 |
-
# INPUT
|
| 28 |
# =========================================================
|
| 29 |
|
| 30 |
class EEGRequest(BaseModel):
|
| 31 |
features: list
|
| 32 |
|
| 33 |
# =========================================================
|
| 34 |
-
# AD MODEL (CNN -
|
| 35 |
# =========================================================
|
| 36 |
|
| 37 |
class EEG_CNN_AD(nn.Module):
|
| 38 |
-
def __init__(self
|
| 39 |
super().__init__()
|
| 40 |
|
| 41 |
self.conv1 = nn.Conv1d(19, 32, 7, padding=3)
|
|
@@ -48,7 +48,7 @@ class EEG_CNN_AD(nn.Module):
|
|
| 48 |
self.bn3 = nn.BatchNorm1d(128)
|
| 49 |
|
| 50 |
self.pool = nn.AdaptiveAvgPool1d(1)
|
| 51 |
-
self.fc = nn.Linear(128,
|
| 52 |
|
| 53 |
def forward(self, x):
|
| 54 |
x = x.view(x.size(0), 19, 76)
|
|
@@ -61,30 +61,30 @@ class EEG_CNN_AD(nn.Module):
|
|
| 61 |
return self.fc(x)
|
| 62 |
|
| 63 |
# =========================================================
|
| 64 |
-
# PD MODEL (
|
| 65 |
# =========================================================
|
| 66 |
|
| 67 |
class EEG_PD(nn.Module):
|
| 68 |
-
def __init__(self
|
| 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,
|
| 81 |
)
|
| 82 |
|
| 83 |
def forward(self, x):
|
| 84 |
return self.net(x)
|
| 85 |
|
| 86 |
# =========================================================
|
| 87 |
-
#
|
| 88 |
# =========================================================
|
| 89 |
|
| 90 |
AD_MODEL_PATH = "AD_eeg_cnn_ad_ftd_cn.pt"
|
|
@@ -95,13 +95,13 @@ PD_MODEL_PATH = "PD_eeg_cnn.pth"
|
|
| 95 |
# =========================================================
|
| 96 |
|
| 97 |
print("Loading AD model...")
|
| 98 |
-
ad_model = EEG_CNN_AD(
|
| 99 |
ad_model.load_state_dict(torch.load(AD_MODEL_PATH, map_location=DEVICE))
|
| 100 |
ad_model.eval()
|
| 101 |
print("AD model loaded")
|
| 102 |
|
| 103 |
print("Loading PD model...")
|
| 104 |
-
pd_model = EEG_PD(
|
| 105 |
pd_model.load_state_dict(torch.load(PD_MODEL_PATH, map_location=DEVICE))
|
| 106 |
pd_model.eval()
|
| 107 |
print("PD model loaded")
|
|
@@ -109,35 +109,33 @@ print("PD model loaded")
|
|
| 109 |
print("System ready")
|
| 110 |
|
| 111 |
# =========================================================
|
| 112 |
-
# INPUT
|
| 113 |
# =========================================================
|
| 114 |
|
| 115 |
-
def
|
| 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("
|
| 129 |
|
| 130 |
# =========================================================
|
| 131 |
-
#
|
| 132 |
# =========================================================
|
| 133 |
|
| 134 |
def predict(model, features, classes, model_type):
|
| 135 |
|
| 136 |
-
x =
|
| 137 |
|
| 138 |
with torch.no_grad():
|
| 139 |
-
|
| 140 |
-
probs = torch.softmax(
|
| 141 |
|
| 142 |
pred = int(np.argmax(probs))
|
| 143 |
|
|
@@ -157,8 +155,9 @@ def predict(model, features, classes, model_type):
|
|
| 157 |
def home():
|
| 158 |
return {
|
| 159 |
"status": "running",
|
| 160 |
-
"
|
| 161 |
-
"
|
|
|
|
| 162 |
}
|
| 163 |
|
| 164 |
@app.get("/health")
|
|
|
|
| 11 |
|
| 12 |
app = FastAPI(
|
| 13 |
title="NeuroHealth EEG System",
|
| 14 |
+
version="8.0"
|
| 15 |
)
|
| 16 |
|
| 17 |
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 24 |
PD_CLASSES = ["Parkinson", "Control"]
|
| 25 |
|
| 26 |
# =========================================================
|
| 27 |
+
# INPUT
|
| 28 |
# =========================================================
|
| 29 |
|
| 30 |
class EEGRequest(BaseModel):
|
| 31 |
features: list
|
| 32 |
|
| 33 |
# =========================================================
|
| 34 |
+
# AD MODEL (CNN - MUST MATCH TRAINING)
|
| 35 |
# =========================================================
|
| 36 |
|
| 37 |
class EEG_CNN_AD(nn.Module):
|
| 38 |
+
def __init__(self):
|
| 39 |
super().__init__()
|
| 40 |
|
| 41 |
self.conv1 = nn.Conv1d(19, 32, 7, padding=3)
|
|
|
|
| 48 |
self.bn3 = nn.BatchNorm1d(128)
|
| 49 |
|
| 50 |
self.pool = nn.AdaptiveAvgPool1d(1)
|
| 51 |
+
self.fc = nn.Linear(128, 3)
|
| 52 |
|
| 53 |
def forward(self, x):
|
| 54 |
x = x.view(x.size(0), 19, 76)
|
|
|
|
| 61 |
return self.fc(x)
|
| 62 |
|
| 63 |
# =========================================================
|
| 64 |
+
# PD MODEL (EXACT MATCH TO CHECKPOINT)
|
| 65 |
# =========================================================
|
| 66 |
|
| 67 |
class EEG_PD(nn.Module):
|
| 68 |
+
def __init__(self):
|
| 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, 2)
|
| 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"
|
|
|
|
| 95 |
# =========================================================
|
| 96 |
|
| 97 |
print("Loading AD model...")
|
| 98 |
+
ad_model = EEG_CNN_AD().to(DEVICE)
|
| 99 |
ad_model.load_state_dict(torch.load(AD_MODEL_PATH, map_location=DEVICE))
|
| 100 |
ad_model.eval()
|
| 101 |
print("AD model loaded")
|
| 102 |
|
| 103 |
print("Loading PD model...")
|
| 104 |
+
pd_model = EEG_PD().to(DEVICE)
|
| 105 |
pd_model.load_state_dict(torch.load(PD_MODEL_PATH, map_location=DEVICE))
|
| 106 |
pd_model.eval()
|
| 107 |
print("PD model loaded")
|
|
|
|
| 109 |
print("System ready")
|
| 110 |
|
| 111 |
# =========================================================
|
| 112 |
+
# INPUT HANDLER
|
| 113 |
# =========================================================
|
| 114 |
|
| 115 |
+
def prepare(features, model_type):
|
| 116 |
|
| 117 |
x = torch.tensor(features, dtype=torch.float32)
|
| 118 |
|
| 119 |
if model_type == "ad":
|
|
|
|
| 120 |
return x.view(1, 19, 76)
|
| 121 |
|
| 122 |
elif model_type == "pd":
|
|
|
|
| 123 |
return x.view(1, 76)
|
| 124 |
|
| 125 |
else:
|
| 126 |
+
raise ValueError("Invalid model type")
|
| 127 |
|
| 128 |
# =========================================================
|
| 129 |
+
# PREDICT ENGINE
|
| 130 |
# =========================================================
|
| 131 |
|
| 132 |
def predict(model, features, classes, model_type):
|
| 133 |
|
| 134 |
+
x = prepare(features, model_type).to(DEVICE)
|
| 135 |
|
| 136 |
with torch.no_grad():
|
| 137 |
+
out = model(x)
|
| 138 |
+
probs = torch.softmax(out, dim=1).cpu().numpy()[0]
|
| 139 |
|
| 140 |
pred = int(np.argmax(probs))
|
| 141 |
|
|
|
|
| 155 |
def home():
|
| 156 |
return {
|
| 157 |
"status": "running",
|
| 158 |
+
"ad_input": "19x76 CNN",
|
| 159 |
+
"pd_input": "76 Dense",
|
| 160 |
+
"version": "8.0"
|
| 161 |
}
|
| 162 |
|
| 163 |
@app.get("/health")
|