Mikecode123 commited on
Commit
5a5c73c
·
verified ·
1 Parent(s): 86290ef

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -24
app.py CHANGED
@@ -10,8 +10,8 @@ import numpy as np
10
  # =========================================================
11
 
12
  app = FastAPI(
13
- title="NeuroHealth EEG API",
14
- version="6.0"
15
  )
16
 
17
  DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -24,14 +24,14 @@ AD_CLASSES = ["Alzheimer", "FTD", "Control"]
24
  PD_CLASSES = ["Parkinson", "Control"]
25
 
26
  # =========================================================
27
- # INPUT
28
  # =========================================================
29
 
30
  class EEGRequest(BaseModel):
31
  features: list
32
 
33
  # =========================================================
34
- # AD MODEL (conv style)
35
  # =========================================================
36
 
37
  class EEG_CNN_AD(nn.Module):
@@ -51,7 +51,7 @@ class EEG_CNN_AD(nn.Module):
51
  self.fc = nn.Linear(128, output_dim)
52
 
53
  def forward(self, x):
54
- x = x.view(x.size(0), 19, -1)
55
 
56
  x = torch.relu(self.bn1(self.conv1(x)))
57
  x = torch.relu(self.bn2(self.conv2(x)))
@@ -61,23 +61,23 @@ class EEG_CNN_AD(nn.Module):
61
  return self.fc(x)
62
 
63
  # =========================================================
64
- # PD MODEL (SEQUENTIAL net style - YOUR CHECKPOINT)
65
  # =========================================================
66
 
67
- class EEG_CNN_PD(nn.Module):
68
- def __init__(self, input_dim, output_dim):
69
  super().__init__()
70
 
71
  self.net = nn.Sequential(
72
- nn.Linear(input_dim, 256), # net.0
73
  nn.ReLU(),
74
- nn.BatchNorm1d(256), # net.2
75
 
76
- nn.Linear(256, 128), # net.4
77
  nn.ReLU(),
78
- nn.BatchNorm1d(128), # net.6
79
 
80
- nn.Linear(128, output_dim) # net.7
81
  )
82
 
83
  def forward(self, x):
@@ -90,8 +90,6 @@ class EEG_CNN_PD(nn.Module):
90
  AD_MODEL_PATH = "AD_eeg_cnn_ad_ftd_cn.pt"
91
  PD_MODEL_PATH = "PD_eeg_cnn.pth"
92
 
93
- INPUT_DIM = 19 * 76
94
-
95
  # =========================================================
96
  # LOAD MODELS
97
  # =========================================================
@@ -100,27 +98,33 @@ print("Loading AD model...")
100
  ad_model = EEG_CNN_AD(3).to(DEVICE)
101
  ad_model.load_state_dict(torch.load(AD_MODEL_PATH, map_location=DEVICE))
102
  ad_model.eval()
103
- print("AD loaded")
104
 
105
  print("Loading PD model...")
106
- pd_model = EEG_CNN_PD(INPUT_DIM, 2).to(DEVICE)
107
  pd_model.load_state_dict(torch.load(PD_MODEL_PATH, map_location=DEVICE))
108
  pd_model.eval()
109
- print("PD loaded")
 
 
110
 
111
  # =========================================================
112
- # PREDICT
113
  # =========================================================
114
 
115
  def predict(model, features, classes):
116
 
117
  x = torch.tensor(features, dtype=torch.float32)
118
 
119
- x = x.view(1, -1).to(DEVICE)
 
 
 
 
120
 
121
  with torch.no_grad():
122
- out = model(x)
123
- probs = torch.softmax(out, dim=1).cpu().numpy()[0]
124
 
125
  pred = int(np.argmax(probs))
126
 
@@ -138,7 +142,11 @@ def predict(model, features, classes):
138
 
139
  @app.get("/")
140
  def home():
141
- return {"status": "running"}
 
 
 
 
142
 
143
  @app.post("/predict/ad")
144
  def predict_ad(req: EEGRequest):
@@ -146,4 +154,8 @@ def predict_ad(req: EEGRequest):
146
 
147
  @app.post("/predict/pd")
148
  def predict_pd(req: EEGRequest):
149
- return predict(pd_model, req.features, PD_CLASSES)
 
 
 
 
 
10
  # =========================================================
11
 
12
  app = FastAPI(
13
+ title="NeuroHealth EEG System",
14
+ version="7.0"
15
  )
16
 
17
  DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
24
  PD_CLASSES = ["Parkinson", "Control"]
25
 
26
  # =========================================================
27
+ # INPUT SCHEMA
28
  # =========================================================
29
 
30
  class EEGRequest(BaseModel):
31
  features: list
32
 
33
  # =========================================================
34
+ # AD MODEL (CNN - 19 channels)
35
  # =========================================================
36
 
37
  class EEG_CNN_AD(nn.Module):
 
51
  self.fc = nn.Linear(128, output_dim)
52
 
53
  def forward(self, x):
54
+ x = x.view(x.size(0), 19, 76)
55
 
56
  x = torch.relu(self.bn1(self.conv1(x)))
57
  x = torch.relu(self.bn2(self.conv2(x)))
 
61
  return self.fc(x)
62
 
63
  # =========================================================
64
+ # PD MODEL (Dense Sequential)
65
  # =========================================================
66
 
67
+ class EEG_PD(nn.Module):
68
+ def __init__(self, output_dim):
69
  super().__init__()
70
 
71
  self.net = nn.Sequential(
72
+ nn.Linear(76, 256),
73
  nn.ReLU(),
74
+ nn.BatchNorm1d(256),
75
 
76
+ nn.Linear(256, 128),
77
  nn.ReLU(),
78
+ nn.BatchNorm1d(128),
79
 
80
+ nn.Linear(128, output_dim)
81
  )
82
 
83
  def forward(self, x):
 
90
  AD_MODEL_PATH = "AD_eeg_cnn_ad_ftd_cn.pt"
91
  PD_MODEL_PATH = "PD_eeg_cnn.pth"
92
 
 
 
93
  # =========================================================
94
  # LOAD MODELS
95
  # =========================================================
 
98
  ad_model = EEG_CNN_AD(3).to(DEVICE)
99
  ad_model.load_state_dict(torch.load(AD_MODEL_PATH, map_location=DEVICE))
100
  ad_model.eval()
101
+ print("AD model loaded")
102
 
103
  print("Loading PD model...")
104
+ pd_model = EEG_PD(2).to(DEVICE)
105
  pd_model.load_state_dict(torch.load(PD_MODEL_PATH, map_location=DEVICE))
106
  pd_model.eval()
107
+ print("PD model loaded")
108
+
109
+ print("System ready")
110
 
111
  # =========================================================
112
+ # PREDICTION ENGINE
113
  # =========================================================
114
 
115
  def predict(model, features, classes):
116
 
117
  x = torch.tensor(features, dtype=torch.float32)
118
 
119
+ # AD expects 1444 (19x76), PD expects 76
120
+ if len(features) == 1444:
121
+ x = x.view(1, 19, 76)
122
+ else:
123
+ x = x.view(1, 76)
124
 
125
  with torch.no_grad():
126
+ logits = model(x)
127
+ probs = torch.softmax(logits, dim=1).cpu().numpy()[0]
128
 
129
  pred = int(np.argmax(probs))
130
 
 
142
 
143
  @app.get("/")
144
  def home():
145
+ return {
146
+ "status": "running",
147
+ "ad_input": "19x76 CNN",
148
+ "pd_input": "76 Dense"
149
+ }
150
 
151
  @app.post("/predict/ad")
152
  def predict_ad(req: EEGRequest):
 
154
 
155
  @app.post("/predict/pd")
156
  def predict_pd(req: EEGRequest):
157
+ return predict(pd_model, req.features, PD_CLASSES)
158
+
159
+ @app.get("/health")
160
+ def health():
161
+ return {"status": "ok"}