Mikecode123 commited on
Commit
2ec3d11
·
verified ·
1 Parent(s): 1f193d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -20
app.py CHANGED
@@ -10,8 +10,8 @@ import numpy as np
10
  # =========================================================
11
 
12
  app = FastAPI(
13
- title="NeuroHealth EEG System",
14
- version="8.0"
15
  )
16
 
17
  DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
@@ -31,7 +31,7 @@ class EEGRequest(BaseModel):
31
  features: list
32
 
33
  # =========================================================
34
- # AD MODEL (CNN - MUST MATCH TRAINING)
35
  # =========================================================
36
 
37
  class EEG_CNN_AD(nn.Module):
@@ -53,15 +53,20 @@ class EEG_CNN_AD(nn.Module):
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)))
58
- x = torch.relu(self.bn3(self.conv3(x)))
 
 
 
 
 
59
 
60
  x = self.pool(x).squeeze(-1)
61
  return self.fc(x)
62
 
63
  # =========================================================
64
- # PD MODEL (EXACT MATCH TO CHECKPOINT)
65
  # =========================================================
66
 
67
  class EEG_PD(nn.Module):
@@ -69,15 +74,17 @@ class EEG_PD(nn.Module):
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, 2)
 
 
81
  )
82
 
83
  def forward(self, x):
@@ -109,24 +116,22 @@ print("PD model loaded")
109
  print("System ready")
110
 
111
  # =========================================================
112
- # INPUT HANDLER
113
  # =========================================================
114
 
115
  def prepare(features, model_type):
116
-
117
  x = torch.tensor(features, dtype=torch.float32)
118
 
119
  if model_type == "ad":
120
  return x.view(1, 19, 76)
121
 
122
- elif model_type == "pd":
123
  return x.view(1, 76)
124
 
125
- else:
126
- raise ValueError("Invalid model type")
127
 
128
  # =========================================================
129
- # PREDICT ENGINE
130
  # =========================================================
131
 
132
  def predict(model, features, classes, model_type):
@@ -156,8 +161,8 @@ def home():
156
  return {
157
  "status": "running",
158
  "ad_input": "19x76 CNN",
159
- "pd_input": "76 Dense",
160
- "version": "8.0"
161
  }
162
 
163
  @app.get("/health")
 
10
  # =========================================================
11
 
12
  app = FastAPI(
13
+ title="NeuroHealth EEG API",
14
+ version="9.0"
15
  )
16
 
17
  DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
 
31
  features: list
32
 
33
  # =========================================================
34
+ # AD MODEL (CNN - EXACTLY FROM CHECKPOINT STRUCTURE)
35
  # =========================================================
36
 
37
  class EEG_CNN_AD(nn.Module):
 
53
  def forward(self, x):
54
  x = x.view(x.size(0), 19, 76)
55
 
56
+ x = torch.relu(self.conv1(x))
57
+ x = self.bn1(x)
58
+
59
+ x = torch.relu(self.conv2(x))
60
+ x = self.bn2(x)
61
+
62
+ x = torch.relu(self.conv3(x))
63
+ x = self.bn3(x)
64
 
65
  x = self.pool(x).squeeze(-1)
66
  return self.fc(x)
67
 
68
  # =========================================================
69
+ # PD MODEL (STRICT MATCH TO YOUR CHECKPOINT KEYS)
70
  # =========================================================
71
 
72
  class EEG_PD(nn.Module):
 
74
  super().__init__()
75
 
76
  self.net = nn.Sequential(
77
+ nn.Linear(76, 256), # net.0
78
  nn.ReLU(),
 
79
 
80
+ nn.BatchNorm1d(256), # net.2
81
+
82
+ nn.Linear(256, 128), # net.4
83
  nn.ReLU(),
 
84
 
85
+ nn.BatchNorm1d(128), # net.6
86
+
87
+ nn.Linear(128, 2) # net.7
88
  )
89
 
90
  def forward(self, x):
 
116
  print("System ready")
117
 
118
  # =========================================================
119
+ # PREPROCESSING
120
  # =========================================================
121
 
122
  def prepare(features, model_type):
 
123
  x = torch.tensor(features, dtype=torch.float32)
124
 
125
  if model_type == "ad":
126
  return x.view(1, 19, 76)
127
 
128
+ if model_type == "pd":
129
  return x.view(1, 76)
130
 
131
+ raise ValueError("Invalid model type")
 
132
 
133
  # =========================================================
134
+ # PREDICTION ENGINE
135
  # =========================================================
136
 
137
  def predict(model, features, classes, model_type):
 
161
  return {
162
  "status": "running",
163
  "ad_input": "19x76 CNN",
164
+ "pd_input": "76 features MLP",
165
+ "version": "9.0"
166
  }
167
 
168
  @app.get("/health")