Spaces:
Runtime error
Runtime error
Update app.py
Browse files27 unmodified lines282930313132333435363737383940414041434243464445464849505147484953545556505152585960535455626356575859606162636465666768676970717273747374757677767778798078798081838485868782838485909186878889902 unmodified lines9394951009697981 unmodified line1001011021081091101111121031041051151061171181191201211071081091241101111123 unmodified lines1161171181341191361201211223 unmodified lines1261271281461291301491501511521311321331551341571351591361611371631381391403 unmodified lines14414514617314714817617717817914915015118215218415318615418815519015615715827 unmodified lines]
# =========================================================
# MODELS
# CONFIG
# =========================================================
INPUT_DIM = 95
# =========================================================
# MLP MODEL
# CNN MODEL (MATCHES SAVED WEIGHTS)
# =========================================================
class MLP(nn.Module):
class EEG_CNN(nn.Module):
def __init__(self, output_dim):
super().__init__()
self.network = nn.Sequential(
self.conv1 = nn.Conv1d(1, 32, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm1d(32)
nn.Linear(INPUT_DIM, 512),
nn.BatchNorm1d(512),
nn.GELU(),
nn.Dropout(0.3),
self.conv2 = nn.Conv1d(32, 64, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm1d(64)
nn.Linear(512, 256),
nn.BatchNorm1d(256),
nn.GELU(),
nn.Dropout(0.3),
self.conv3 = nn.Conv1d(64, 128, kernel_size=3, padding=1)
self.bn3 = nn.BatchNorm1d(128)
nn.Linear(256, 128),
nn.BatchNorm1d(128),
nn.GELU(),
# Global pooling removes dependence on sequence length
self.pool = nn.AdaptiveAvgPool1d(1)
nn.Linear(128, output_dim)
)
self.fc = nn.Linear(128, output_dim)
def forward(self, x):
# x shape: (batch, features)
x = x.unsqueeze(1) # (batch, 1, 95)
x = torch.relu(self.bn1(self.conv1(x)))
x = torch.relu(self.bn2(self.conv2(x)))
x = torch.relu(self.bn3(self.conv3(x)))
x = self.pool(x) # (batch, 128, 1)
x = x.squeeze(-1) # (batch, 128)
return self.network(x)
return self.fc(x)
# =========================================================
# LOAD MODELS
# =========================================================
ad_mlp = MLP(3).to(DEVICE)
pd_mlp = MLP(2).to(DEVICE)
ad_model = EEG_CNN(3).to(DEVICE)
pd_model = EEG_CNN(2).to(DEVICE)
ad_mlp.load_state_dict(
torch.load(
"AD_MLP.pt",
map_location=DEVICE
)
ad_model.load_state_dict(
torch.load("AD_MLP.pt", map_location=DEVICE)
)
pd_mlp.load_state_dict(
torch.load(
"PD_MLP.pt",
map_location=DEVICE
)
pd_model.load_state_dict(
torch.load("PD_MLP.pt", map_location=DEVICE)
)
ad_mlp.eval()
pd_mlp.eval()
ad_model.eval()
pd_model.eval()
print("Models Loaded Successfully")
2 unmodified lines# =========================================================
class EEGRequest(BaseModel):
features: list
# =========================================================1 unmodified line# =========================================================
def predict_model(model, features):
x = torch.tensor(
features,
dtype=torch.float32
).unsqueeze(0).to(DEVICE)
x = torch.tensor(features, dtype=torch.float32).unsqueeze(0).to(DEVICE)
with torch.no_grad():
outputs = model(x)
probs = torch.softmax(
outputs,
dim=1
).cpu().numpy()[0]
probs = torch.softmax(outputs, dim=1).cpu().numpy()[0]
pred = int(np.argmax(probs))
confidence = float(probs[pred])
return pred, confidence, probs.tolist()3 unmodified lines# =========================================================
@app.get("/")
def home():
return {
"message": "NeuroHealth EEG API Running"
}3 unmodified lines# =========================================================
@app.post("/predict/alzheimer")
def predict_alzheimer(request: EEGRequest):
pred, confidence, probs = predict_model(
ad_mlp,
request.features
)
pred, confidence, probs = predict_model(ad_model, request.features)
return {
"prediction": AD_CLASSES[pred],
"confidence": confidence,
"probabilities": {
AD_CLASSES[i]: float(probs[i])
for i in range(len(AD_CLASSES))
}
}3 unmodified lines# =========================================================
@app.post("/predict/parkinson")
def predict_parkinson(request: EEGRequest):
pred, confidence, probs = predict_model(
pd_mlp,
request.features
)
pred, confidence, probs = predict_model(pd_model, request.features)
return {
"prediction": PD_CLASSES[pred],
"confidence": confidence,
"probabilities": {
PD_CLASSES[i]: float(probs[i])
for i in range(len(PD_CLASSES))
}
}app.py+34-69