ananyakarn commited on
Commit
f04fed2
·
verified ·
1 Parent(s): 05ee77c

updated app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -28
app.py CHANGED
@@ -10,6 +10,7 @@ import gradio as gr
10
  from transformers import AutoTokenizer, AutoModel
11
  from sklearn.ensemble import RandomForestClassifier
12
  from sklearn.preprocessing import StandardScaler
 
13
 
14
  # =========================
15
  # 1. DOWNLOAD DATASET
@@ -30,7 +31,7 @@ if not os.path.exists(extract_path):
30
  zip_ref.extractall(extract_path)
31
 
32
  # =========================
33
- # 2. LOAD BERT
34
  # =========================
35
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
36
 
@@ -39,7 +40,7 @@ bert = AutoModel.from_pretrained("bert-base-multilingual-cased").to(device)
39
  bert.eval()
40
 
41
  # =========================
42
- # FEATURE FUNCTIONS
43
  # =========================
44
  def get_text_embedding(text):
45
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128).to(device)
@@ -68,19 +69,18 @@ def get_label(folder):
68
  return 1 if "_P" in folder else 0
69
 
70
  # =========================
71
- # 3. TRAIN MODEL (LIGHT)
72
  # =========================
73
  print("Training model...")
74
 
75
  X, y = [], []
76
- folders = os.listdir(extract_path)[:20] # 🔥 only 20 samples
 
 
77
 
78
  for folder in folders:
79
  path = os.path.join(extract_path, folder)
80
 
81
- if not os.path.isdir(path):
82
- continue
83
-
84
  text = load_text(path)
85
  text_feat = get_text_embedding(text)
86
  audio_feat = get_audio_features(path)
@@ -96,44 +96,56 @@ y = np.array(y)
96
  scaler = StandardScaler()
97
  X = scaler.fit_transform(X)
98
 
99
- model = RandomForestClassifier(n_estimators=50)
100
  model.fit(X, y)
101
 
102
  print("Model ready!")
103
 
104
  # =========================
105
- # 4. PREDICTION FUNCTION
106
  # =========================
107
- def predict(text, audio):
108
 
109
- text_feat = get_text_embedding(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
- if audio:
112
- y_audio, sr = librosa.load(audio, sr=16000)
113
- mfcc = librosa.feature.mfcc(y=y_audio, sr=sr, n_mfcc=20)
114
- audio_feat = np.mean(mfcc.T, axis=0)
115
- else:
116
- audio_feat = np.zeros(20)
117
 
118
- x = np.concatenate([text_feat, audio_feat])
119
- x = scaler.transform([x])
120
 
121
- pred = model.predict(x)[0]
 
 
122
 
123
- return "Depression Detected ⚠️" if pred == 1 else "No Depression ✅"
124
 
125
  # =========================
126
- # 5. UI
127
  # =========================
128
  app = gr.Interface(
129
- fn=predict,
130
- inputs=[
131
- gr.Textbox(label="Enter Text"),
132
- gr.Audio(type="filepath", label="Upload Audio (optional)")
133
- ],
134
  outputs="text",
135
  title="Multimodal Depression Detection",
136
- description="Training + inference demo using text and audio"
137
  )
138
 
139
  app.launch()
 
10
  from transformers import AutoTokenizer, AutoModel
11
  from sklearn.ensemble import RandomForestClassifier
12
  from sklearn.preprocessing import StandardScaler
13
+ from sklearn.metrics import accuracy_score, f1_score
14
 
15
  # =========================
16
  # 1. DOWNLOAD DATASET
 
31
  zip_ref.extractall(extract_path)
32
 
33
  # =========================
34
+ # 2. LOAD BERT MODEL
35
  # =========================
36
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
37
 
 
40
  bert.eval()
41
 
42
  # =========================
43
+ # 3. FEATURE FUNCTIONS
44
  # =========================
45
  def get_text_embedding(text):
46
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128).to(device)
 
69
  return 1 if "_P" in folder else 0
70
 
71
  # =========================
72
+ # 4. TRAIN MODEL (LIGHT)
73
  # =========================
74
  print("Training model...")
75
 
76
  X, y = [], []
77
+
78
+ folders = [f for f in os.listdir(extract_path) if os.path.isdir(os.path.join(extract_path, f))]
79
+ folders = folders[:20] # 🔥 small subset for speed
80
 
81
  for folder in folders:
82
  path = os.path.join(extract_path, folder)
83
 
 
 
 
84
  text = load_text(path)
85
  text_feat = get_text_embedding(text)
86
  audio_feat = get_audio_features(path)
 
96
  scaler = StandardScaler()
97
  X = scaler.fit_transform(X)
98
 
99
+ model = RandomForestClassifier(n_estimators=50, random_state=42)
100
  model.fit(X, y)
101
 
102
  print("Model ready!")
103
 
104
  # =========================
105
+ # 5. RUN PREDICTIONS
106
  # =========================
107
+ def run_on_dataset():
108
 
109
+ results = []
110
+ preds = []
111
+ labels = []
112
+
113
+ for i, folder in enumerate(folders):
114
+ path = os.path.join(extract_path, folder)
115
+
116
+ text = load_text(path)
117
+ text_feat = get_text_embedding(text)
118
+ audio_feat = get_audio_features(path)
119
+
120
+ x = np.concatenate([text_feat, audio_feat])
121
+ x = scaler.transform([x])
122
+
123
+ pred = model.predict(x)[0]
124
+ label = get_label(folder)
125
+
126
+ preds.append(pred)
127
+ labels.append(label)
128
 
129
+ results.append(f"{folder} → {'Depressed' if pred==1 else 'Control'}")
 
 
 
 
 
130
 
131
+ acc = accuracy_score(labels, preds)
132
+ f1 = f1_score(labels, preds)
133
 
134
+ output = "\n".join(results)
135
+ output += f"\n\nAccuracy: {acc:.3f}"
136
+ output += f"\nF1 Score: {f1:.3f}"
137
 
138
+ return output
139
 
140
  # =========================
141
+ # 6. GRADIO UI
142
  # =========================
143
  app = gr.Interface(
144
+ fn=run_on_dataset,
145
+ inputs=[],
 
 
 
146
  outputs="text",
147
  title="Multimodal Depression Detection",
148
+ description="Automatically trains on dataset and evaluates predictions (text + audio)"
149
  )
150
 
151
  app.launch()