Spaces:
Runtime error
Runtime error
Update app.py
Browse files9 unmodified lines10111213141516176 unmodified lines242526272829303132333435363713 unmodified lines515253545556573 unmodified lines61626364656667686970717273747576777879808182836 unmodified lines90919293949596972 unmodified lines10010110210310410510610710810911011111211311411511611711811912012112212312412512611 unmodified lines1381391401411421431441 unmodified line1461471481499 unmodified lines# =========================================================app = FastAPI(title="NeuroHealth EEG API",version="6.0")DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")6 unmodified linesPD_CLASSES = ["Parkinson", "Control"]# =========================================================# INPUT# =========================================================class EEGRequest(BaseModel):features: list# =========================================================# AD MODEL (conv style)# =========================================================class EEG_CNN_AD(nn.Module):13 unmodified linesself.fc = nn.Linear(128, output_dim)def forward(self, x):x = x.view(x.size(0), 19, -1)x = torch.relu(self.bn1(self.conv1(x)))x = torch.relu(self.bn2(self.conv2(x)))3 unmodified linesreturn self.fc(x)# =========================================================# PD MODEL (SEQUENTIAL net style - YOUR CHECKPOINT)# =========================================================class EEG_CNN_PD(nn.Module):def __init__(self, input_dim, output_dim):super().__init__()self.net = nn.Sequential(nn.Linear(input_dim, 256), # net.0nn.ReLU(),nn.BatchNorm1d(256), # net.2nn.Linear(256, 128), # net.4nn.ReLU(),nn.BatchNorm1d(128), # net.6nn.Linear(128, output_dim) # net.7)def forward(self, x):6 unmodified linesAD_MODEL_PATH = "AD_eeg_cnn_ad_ftd_cn.pt"PD_MODEL_PATH = "PD_eeg_cnn.pth"INPUT_DIM = 19 * 76# =========================================================# LOAD MODELS# =========================================================2 unmodified linesad_model = EEG_CNN_AD(3).to(DEVICE)ad_model.load_state_dict(torch.load(AD_MODEL_PATH, map_location=DEVICE))ad_model.eval()print("AD loaded")print("Loading PD model...")pd_model = EEG_CNN_PD(INPUT_DIM, 2).to(DEVICE)pd_model.load_state_dict(torch.load(PD_MODEL_PATH, map_location=DEVICE))pd_model.eval()print("PD loaded")# =========================================================# PREDICT# =========================================================def predict(model, features, classes):x = torch.tensor(features, dtype=torch.float32)x = x.view(1, -1).to(DEVICE)with torch.no_grad():out = model(x)probs = torch.softmax(out, dim=1).cpu().numpy()[0]pred = int(np.argmax(probs))11 unmodified lines@app.get("/")def home():return {"status": "running"}@app.post("/predict/ad")def predict_ad(req: EEGRequest):1 unmodified line@app.post("/predict/pd")def predict_pd(req: EEGRequest):return predict(pd_model, req.features, PD_CLASSES)9 unmodified lines10111213141516176 unmodified lines242526272829303132333435363713 unmodified lines515253545556573 unmodified lines61626364656667686970717273747576777879808182836 unmodified lines9091929394952 unmodified lines989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913011 unmodified lines1421431441451461471481491501511521 unmodified line1541551561571581591601619 unmodified lines# =========================================================app = FastAPI(title="NeuroHealth EEG System",version="7.0")DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")6 unmodified linesPD_CLASSES = ["Parkinson", "Control"]# =========================================================# INPUT SCHEMA# =========================================================class EEGRequest(BaseModel):features: list# =========================================================# AD MODEL (CNN - 19 channels)# =========================================================class EEG_CNN_AD(nn.Module):13 unmodified linesself.fc = nn.Linear(128, output_dim)def forward(self, x):x = x.view(x.size(0), 19, 76)x = torch.relu(self.bn1(self.conv1(x)))x = torch.relu(self.bn2(self.conv2(x)))3 unmodified linesreturn self.fc(x)# =========================================================# PD MODEL (Dense Sequential)# =========================================================class EEG_PD(nn.Module):def __init__(self, output_dim):super().__init__()self.net = nn.Sequential(nn.Linear(76, 256),nn.ReLU(),nn.BatchNorm1d(256),nn.Linear(256, 128),nn.ReLU(),nn.BatchNorm1d(128),nn.Linear(128, output_dim))def forward(self, x):6 unmodified linesAD_MODEL_PATH = "AD_eeg_cnn_ad_ftd_cn.pt"PD_MODEL_PATH = "PD_eeg_cnn.pth"# =========================================================# LOAD MODELS# =========================================================2 unmodified linesad_model = EEG_CNN_AD(3).to(DEVICE)ad_model.load_state_dict(torch.load(AD_MODEL_PATH, map_location=DEVICE))ad_model.eval()print("AD model loaded")print("Loading PD model...")pd_model = EEG_PD(2).to(DEVICE)pd_model.load_state_dict(torch.load(PD_MODEL_PATH, map_location=DEVICE))pd_model.eval()print("PD model loaded")print("System ready")# =========================================================# PREDICTION ENGINE# =========================================================def predict(model, features, classes):x = torch.tensor(features, dtype=torch.float32)# AD expects 1444 (19x76), PD expects 76if len(features) == 1444:x = x.view(1, 19, 76)else:x = x.view(1, 76)with torch.no_grad():logits = model(x)probs = torch.softmax(logits, dim=1).cpu().numpy()[0]pred = int(np.argmax(probs))11 unmodified lines@app.get("/")def home():return {"status": "running","ad_input": "19x76 CNN","pd_input": "76 Dense"}@app.post("/predict/ad")def predict_ad(req: EEGRequest):1 unmodified line@app.post("/predict/pd")def predict_pd(req: EEGRequest):return predict(pd_model, req.features, PD_CLASSES)@app.get("/health")def health():return {"status": "ok"}
app.py+36-24