Spaces:
Runtime error
Runtime error
Update app.py
Browse files10 unmodified lines1112131415161713 unmodified lines3132333435363723 unmodified lines616263646566671 unmodified line6970717273747576777879808182838485868788899018 unmodified lines10911011111211311411511611711811912012112212312412512617 unmodified lines14414514614714814915015115215315415515615715815916016110 unmodified linesapp = FastAPI(title="NeuroHealth EEG System",version="7.0")DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")13 unmodified linesfeatures: list# =========================================================# AD MODEL (CNN - 19 channels)# =========================================================class EEG_CNN_AD(nn.Module):23 unmodified linesreturn self.fc(x)# =========================================================# PD MODEL (Dense Sequential)# =========================================================class EEG_PD(nn.Module):1 unmodified linesuper().__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):return self.net(x)# =========================================================# PATHS# =========================================================AD_MODEL_PATH = "AD_eeg_cnn_ad_ftd_cn.pt"18 unmodified linesprint("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)17 unmodified linesdef home():return {"status": "running","ad_input": "19x76 CNN","pd_input": "76 Dense"}@app.post("/predict/ad")def predict_ad(req: EEGRequest):return predict(ad_model, req.features, AD_CLASSES)@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"}10 unmodified lines1112131415161713 unmodified lines3132333435363723 unmodified lines616263646566671 unmodified line6970717273747576777879808182838485868788899018 unmodified lines10911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813917 unmodified lines15715815916016116216316416516616716816917017117217317410 unmodified linesapp = FastAPI(title="NeuroHealth EEG System",version="7.1")DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")13 unmodified linesfeatures: list# =========================================================# AD MODEL (CNN - REAL ARCHITECTURE)# =========================================================class EEG_CNN_AD(nn.Module):23 unmodified linesreturn self.fc(x)# =========================================================# PD MODEL (FULLY CONNECTED NETWORK - EXACT MATCH)# =========================================================class EEG_PD(nn.Module):1 unmodified linesuper().__init__()self.net = nn.Sequential(nn.Linear(76, 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):return self.net(x)# =========================================================# MODEL PATHS# =========================================================AD_MODEL_PATH = "AD_eeg_cnn_ad_ftd_cn.pt"18 unmodified linesprint("System ready")# =========================================================# INPUT PREPARATION# =========================================================def prepare_input(features, model_type):x = torch.tensor(features, dtype=torch.float32)if model_type == "ad":# CNN expects 19 x 76return x.view(1, 19, 76)elif model_type == "pd":# Dense expects 76 onlyreturn x.view(1, 76)else:raise ValueError("Unknown model type")# =========================================================# PREDICTION ENGINE# =========================================================def predict(model, features, classes, model_type):x = prepare_input(features, model_type).to(DEVICE)with torch.no_grad():logits = model(x)17 unmodified linesdef home():return {"status": "running","ad_model": "CNN (19x76)","pd_model": "Dense (76)"}@app.get("/health")def health():return {"status": "ok"}@app.post("/predict/ad")def predict_ad(req: EEGRequest):return predict(ad_model, req.features, AD_CLASSES, "ad")@app.post("/predict/pd")def predict_pd(req: EEGRequest):return predict(pd_model, req.features, PD_CLASSES, "pd")
app.py+36-23