Spaces:
Runtime error
Runtime error
Update app.py
Browse files9 unmodified lines101112131413141516176 unmodified lines2425262727282930313233343435363713 unmodified lines51525354545556573 unmodified lines616263646465666768676869707172727374747576767778787980808182836 unmodified lines90919293949394952 unmodified lines989910010310110210310610410510610910710810911011111211211311411511611711811911912012112212312412512212312612712812913011 unmodified lines1421431441411451461471481491501511521 unmodified line1541551561491571581591601619 unmodified lines# =========================================================
app = FastAPI(
title="NeuroHealth EEG API",
version="6.0"
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
# INPUT SCHEMA
# =========================================================
class EEGRequest(BaseModel):
features: list
# =========================================================
# AD MODEL (conv style)
# AD MODEL (CNN - 19 channels)
# =========================================================
class EEG_CNN_AD(nn.Module):13 unmodified lines self.fc = nn.Linear(128, output_dim)
def forward(self, x):
x = x.view(x.size(0), 19, -1)
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 lines return self.fc(x)
# =========================================================
# PD MODEL (SEQUENTIAL net style - YOUR CHECKPOINT)
# PD MODEL (Dense Sequential)
# =========================================================
class EEG_CNN_PD(nn.Module):
def __init__(self, input_dim, output_dim):
class EEG_PD(nn.Module):
def __init__(self, output_dim):
super().__init__()
self.net = nn.Sequential(
nn.Linear(input_dim, 256), # net.0
nn.Linear(76, 256),
nn.ReLU(),
nn.BatchNorm1d(256), # net.2
nn.BatchNorm1d(256),
nn.Linear(256, 128), # net.4
nn.Linear(256, 128),
nn.ReLU(),
nn.BatchNorm1d(128), # net.6
nn.BatchNorm1d(128),
nn.Linear(128, output_dim) # net.7
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"
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("AD model loaded")
print("Loading PD model...")
pd_model = EEG_CNN_PD(INPUT_DIM, 2).to(DEVICE)
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 loaded")
print("PD model loaded")
print("System ready")
# =========================================================
# PREDICT
# PREDICTION ENGINE
# =========================================================
def predict(model, features, classes):
x = torch.tensor(features, dtype=torch.float32)
x = x.view(1, -1).to(DEVICE)
# AD expects 1444 (19x76), PD expects 76
if len(features) == 1444:
x = x.view(1, 19, 76)
else:
x = x.view(1, 76)
with torch.no_grad():
out = model(x)
probs = torch.softmax(out, dim=1).cpu().numpy()[0]
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"}
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) return predict(pd_model, req.features, PD_CLASSES)
@app.get("/health")
def health():
return {"status": "ok"}app.py+36-24