Spaces:
Runtime error
Runtime error
updated app.py
Browse files
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 |
-
#
|
| 72 |
# =========================
|
| 73 |
print("Training model...")
|
| 74 |
|
| 75 |
X, y = [], []
|
| 76 |
-
|
|
|
|
|
|
|
| 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 |
-
#
|
| 106 |
# =========================
|
| 107 |
-
def
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
-
|
| 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 |
-
|
| 119 |
-
|
| 120 |
|
| 121 |
-
|
|
|
|
|
|
|
| 122 |
|
| 123 |
-
return
|
| 124 |
|
| 125 |
# =========================
|
| 126 |
-
#
|
| 127 |
# =========================
|
| 128 |
app = gr.Interface(
|
| 129 |
-
fn=
|
| 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="
|
| 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()
|