Spaces:
Runtime error
Runtime error
Update app.py
Browse files27 unmodified lines282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293942 unmodified lines9798991001011021031 unmodified line1051061071081091101111121131141151161171181191201211221231241251261273 unmodified lines1311321331341351361371381393 unmodified lines1431441451461471481491501511521531541551561571581591601611621631641651663 unmodified lines17017117217317417517617717817918018118218318418518618718818919019119219327 unmodified lines]# =========================================================# MODELS# =========================================================INPUT_DIM = 95# =========================================================# MLP MODEL# =========================================================class MLP(nn.Module):def __init__(self, output_dim):super().__init__()self.network = nn.Sequential(nn.Linear(INPUT_DIM, 512),nn.BatchNorm1d(512),nn.GELU(),nn.Dropout(0.3),nn.Linear(512, 256),nn.BatchNorm1d(256),nn.GELU(),nn.Dropout(0.3),nn.Linear(256, 128),nn.BatchNorm1d(128),nn.GELU(),nn.Linear(128, output_dim))def forward(self, x):return self.network(x)# =========================================================# LOAD MODELS# =========================================================ad_mlp = MLP(3).to(DEVICE)pd_mlp = MLP(2).to(DEVICE)ad_mlp.load_state_dict(torch.load("AD_MLP.pt",map_location=DEVICE))pd_mlp.load_state_dict(torch.load("PD_MLP.pt",map_location=DEVICE))ad_mlp.eval()pd_mlp.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)with torch.no_grad():outputs = model(x)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)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)return {"prediction": PD_CLASSES[pred],"confidence": confidence,"probabilities": {PD_CLASSES[i]: float(probs[i])for i in range(len(PD_CLASSES))}}27 unmodified lines2829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889902 unmodified lines9394959697981 unmodified line1001011021031041051061071081091101111123 unmodified lines1161171181191201211223 unmodified lines1261271281291301311321331341351361371381391403 unmodified lines14414514614714814915015115215315415515615715827 unmodified lines]# =========================================================# CONFIG# =========================================================INPUT_DIM = 95# =========================================================# CNN MODEL (MATCHES SAVED WEIGHTS)# =========================================================class EEG_CNN(nn.Module):def __init__(self, output_dim):super().__init__()self.conv1 = nn.Conv1d(1, 32, kernel_size=3, padding=1)self.bn1 = nn.BatchNorm1d(32)self.conv2 = nn.Conv1d(32, 64, kernel_size=3, padding=1)self.bn2 = nn.BatchNorm1d(64)self.conv3 = nn.Conv1d(64, 128, kernel_size=3, padding=1)self.bn3 = nn.BatchNorm1d(128)# Global pooling removes dependence on sequence lengthself.pool = nn.AdaptiveAvgPool1d(1)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.fc(x)# =========================================================# LOAD MODELS# =========================================================ad_model = EEG_CNN(3).to(DEVICE)pd_model = EEG_CNN(2).to(DEVICE)ad_model.load_state_dict(torch.load("AD_MLP.pt", map_location=DEVICE))pd_model.load_state_dict(torch.load("PD_MLP.pt", map_location=DEVICE))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)with torch.no_grad():outputs = model(x)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_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_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