ananyakarn commited on
Commit
05a11f8
·
verified ·
1 Parent(s): 95a6b1b

fixed error

Browse files
Files changed (1) hide show
  1. app.py +43 -17
app.py CHANGED
@@ -14,8 +14,7 @@ import os, requests, zipfile, time
14
  def safe_download(url, path):
15
  if os.path.exists(path):
16
  return
17
-
18
- for i in range(3):
19
  try:
20
  r = requests.get(url, stream=True, timeout=60)
21
  with open(path, "wb") as f:
@@ -25,7 +24,6 @@ def safe_download(url, path):
25
  return
26
  except:
27
  time.sleep(3)
28
-
29
  raise Exception("Download failed")
30
 
31
  # =========================
@@ -37,14 +35,12 @@ def load_split(csv_file):
37
  df = pd.read_csv(csv_file)
38
  df.columns = df.columns.str.strip()
39
  split = {}
40
-
41
  for _, row in df.iterrows():
42
  try:
43
  pid = str(int(row["Participant_ID"]))
44
  split[pid] = int(row["PHQ8_Binary"])
45
  except:
46
  continue
47
-
48
  return split
49
 
50
  train_labels = load_split("train_split_Depression_AVEC2017.csv")
@@ -87,8 +83,16 @@ def get_paths():
87
 
88
  ALL_PATHS = get_paths()
89
 
90
- # 🔥 LIMIT DATA (IMPORTANT for HF)
91
- ALL_PATHS = ALL_PATHS[:12]
 
 
 
 
 
 
 
 
92
 
93
  # =========================
94
  # 5. LIBRARIES
@@ -117,7 +121,7 @@ def load_text(folder):
117
  try:
118
  file = [f for f in os.listdir(folder) if "TRANSCRIPT" in f][0]
119
  df = pd.read_csv(os.path.join(folder, file))
120
- return " ".join(df.iloc[:50, -1].astype(str)) # 🔥 reduced
121
  except:
122
  return ""
123
 
@@ -152,11 +156,10 @@ def get_visual(folder):
152
  feats.append(np.concatenate([df.mean().values, df.std().values]))
153
  except:
154
  feats.append(np.zeros(20))
155
-
156
  return np.concatenate(feats)
157
 
158
  # =========================
159
- # 8. BUILD
160
  # =========================
161
  def build(labels):
162
  Xt, Xa, Xv, y = [], [], [], []
@@ -176,8 +179,15 @@ def build(labels):
176
  Xt, Xa, Xv, y_train = build(train_labels)
177
  Xt_test, Xa_test, Xv_test, y_test = build(dev_labels)
178
 
 
 
 
 
 
 
 
179
  # =========================
180
- # 9. NORMALIZE
181
  # =========================
182
  sc_t, sc_a, sc_v = StandardScaler(), StandardScaler(), StandardScaler()
183
 
@@ -190,12 +200,14 @@ Xa_test = sc_a.transform(Xa_test)
190
  Xv_test = sc_v.transform(Xv_test)
191
 
192
  # =========================
193
- # 🔥 RANDOM FOREST (FIXED)
194
  # =========================
195
  X_train_rf = np.concatenate([Xt, Xa, Xv], axis=1)
196
  X_test_rf = np.concatenate([Xt_test, Xa_test, Xv_test], axis=1)
197
 
198
  rf = RandomForestClassifier(n_estimators=100, max_depth=10, random_state=42)
 
 
199
  rf.fit(X_train_rf, y_train)
200
 
201
  rf_preds = rf.predict(X_test_rf)
@@ -204,7 +216,7 @@ rf_acc = accuracy_score(y_test, rf_preds)
204
  rf_f1 = f1_score(y_test, rf_preds)
205
 
206
  # =========================
207
- # 10. TORCH MODELS
208
  # =========================
209
  Xt = torch.tensor(Xt, dtype=torch.float32).to(device)
210
  Xa = torch.tensor(Xa, dtype=torch.float32).to(device)
@@ -218,25 +230,39 @@ Xv_test = torch.tensor(Xv_test, dtype=torch.float32).to(device)
218
  class Simple(nn.Module):
219
  def __init__(self, v):
220
  super().__init__()
221
- self.net = nn.Sequential(nn.Linear(768+40+v,128), nn.ReLU(), nn.Linear(128,1))
 
 
 
 
 
222
  def forward(self,t,a,v):
223
  return self.net(torch.cat([t,a,v],1))
224
 
225
  model = Simple(Xv.shape[1]).to(device)
 
226
  opt = torch.optim.Adam(model.parameters(), lr=1e-4)
227
  loss_fn = nn.BCEWithLogitsLoss()
228
 
229
- for _ in range(5):
 
 
230
  opt.zero_grad()
231
  loss = loss_fn(model(Xt,Xa,Xv).squeeze(), yt)
232
  loss.backward()
233
  opt.step()
 
234
 
 
 
 
235
  with torch.no_grad():
236
- preds = (torch.sigmoid(model(Xt_test,Xa_test,Xv_test).squeeze())>0.5).int().cpu().numpy()
 
 
237
 
238
  # =========================
239
- # RESULTS
240
  # =========================
241
  print("\n===== FINAL RESULTS =====")
242
 
 
14
  def safe_download(url, path):
15
  if os.path.exists(path):
16
  return
17
+ for _ in range(3):
 
18
  try:
19
  r = requests.get(url, stream=True, timeout=60)
20
  with open(path, "wb") as f:
 
24
  return
25
  except:
26
  time.sleep(3)
 
27
  raise Exception("Download failed")
28
 
29
  # =========================
 
35
  df = pd.read_csv(csv_file)
36
  df.columns = df.columns.str.strip()
37
  split = {}
 
38
  for _, row in df.iterrows():
39
  try:
40
  pid = str(int(row["Participant_ID"]))
41
  split[pid] = int(row["PHQ8_Binary"])
42
  except:
43
  continue
 
44
  return split
45
 
46
  train_labels = load_split("train_split_Depression_AVEC2017.csv")
 
83
 
84
  ALL_PATHS = get_paths()
85
 
86
+ # FILTER VALID PARTICIPANTS (IMPORTANT FIX)
87
+ ALL_PATHS = [
88
+ p for p in ALL_PATHS
89
+ if os.path.basename(p).split("_")[0] in REQUIRED_IDS
90
+ ]
91
+
92
+ print("Usable participants:", len(ALL_PATHS))
93
+
94
+ if len(ALL_PATHS) < 10:
95
+ raise Exception("Too few participants extracted!")
96
 
97
  # =========================
98
  # 5. LIBRARIES
 
121
  try:
122
  file = [f for f in os.listdir(folder) if "TRANSCRIPT" in f][0]
123
  df = pd.read_csv(os.path.join(folder, file))
124
+ return " ".join(df.iloc[:50, -1].astype(str))
125
  except:
126
  return ""
127
 
 
156
  feats.append(np.concatenate([df.mean().values, df.std().values]))
157
  except:
158
  feats.append(np.zeros(20))
 
159
  return np.concatenate(feats)
160
 
161
  # =========================
162
+ # 8. BUILD DATA
163
  # =========================
164
  def build(labels):
165
  Xt, Xa, Xv, y = [], [], [], []
 
179
  Xt, Xa, Xv, y_train = build(train_labels)
180
  Xt_test, Xa_test, Xv_test, y_test = build(dev_labels)
181
 
182
+ print("Train size:", len(y_train))
183
+ print("Test size:", len(y_test))
184
+
185
+ # ✅ SAFETY CHECK
186
+ if len(set(y_train)) < 2 or len(set(y_test)) < 2:
187
+ raise Exception("Dataset has only one class!")
188
+
189
  # =========================
190
+ # 9. NORMALIZATION
191
  # =========================
192
  sc_t, sc_a, sc_v = StandardScaler(), StandardScaler(), StandardScaler()
193
 
 
200
  Xv_test = sc_v.transform(Xv_test)
201
 
202
  # =========================
203
+ # 10. RANDOM FOREST
204
  # =========================
205
  X_train_rf = np.concatenate([Xt, Xa, Xv], axis=1)
206
  X_test_rf = np.concatenate([Xt_test, Xa_test, Xv_test], axis=1)
207
 
208
  rf = RandomForestClassifier(n_estimators=100, max_depth=10, random_state=42)
209
+
210
+ print("\nTraining Random Forest...")
211
  rf.fit(X_train_rf, y_train)
212
 
213
  rf_preds = rf.predict(X_test_rf)
 
216
  rf_f1 = f1_score(y_test, rf_preds)
217
 
218
  # =========================
219
+ # 11. TORCH MODEL
220
  # =========================
221
  Xt = torch.tensor(Xt, dtype=torch.float32).to(device)
222
  Xa = torch.tensor(Xa, dtype=torch.float32).to(device)
 
230
  class Simple(nn.Module):
231
  def __init__(self, v):
232
  super().__init__()
233
+ self.net = nn.Sequential(
234
+ nn.Linear(768+40+v,128),
235
+ nn.ReLU(),
236
+ nn.Linear(128,1)
237
+ )
238
+
239
  def forward(self,t,a,v):
240
  return self.net(torch.cat([t,a,v],1))
241
 
242
  model = Simple(Xv.shape[1]).to(device)
243
+
244
  opt = torch.optim.Adam(model.parameters(), lr=1e-4)
245
  loss_fn = nn.BCEWithLogitsLoss()
246
 
247
+ print("\nTraining Neural Model...")
248
+
249
+ for epoch in range(5):
250
  opt.zero_grad()
251
  loss = loss_fn(model(Xt,Xa,Xv).squeeze(), yt)
252
  loss.backward()
253
  opt.step()
254
+ print(f"Epoch {epoch+1}, Loss: {loss.item():.4f}")
255
 
256
+ # =========================
257
+ # 12. EVALUATION
258
+ # =========================
259
  with torch.no_grad():
260
+ preds = (torch.sigmoid(
261
+ model(Xt_test,Xa_test,Xv_test).squeeze()
262
+ ) > 0.5).int().cpu().numpy()
263
 
264
  # =========================
265
+ # 13. RESULTS
266
  # =========================
267
  print("\n===== FINAL RESULTS =====")
268