Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ from sklearn.metrics import classification_report, confusion_matrix, ConfusionMa
|
|
| 11 |
import warnings
|
| 12 |
warnings.filterwarnings("ignore")
|
| 13 |
|
| 14 |
-
df = pd.read_csv("pre_deployment_mental_health_dataset_balanced.csv")
|
| 15 |
df.head()
|
| 16 |
|
| 17 |
print(f"Shape of dataset: {df.shape}")
|
|
@@ -134,6 +134,7 @@ from sklearn.model_selection import train_test_split
|
|
| 134 |
from sklearn.preprocessing import LabelEncoder
|
| 135 |
|
| 136 |
# Load the dataset
|
|
|
|
| 137 |
df_clean = df.drop(columns=["Soldier_ID"])
|
| 138 |
|
| 139 |
# Encode categorical variables
|
|
@@ -152,55 +153,156 @@ X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_sta
|
|
| 152 |
model = LogisticRegression(max_iter=1000)
|
| 153 |
model.fit(X_train, y_train)
|
| 154 |
|
| 155 |
-
#
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
|
|
|
| 175 |
pred = model.predict(input_df)[0]
|
| 176 |
risk_label = label_encoders["Risk_Level"].inverse_transform([pred])[0]
|
| 177 |
return f"π Predicted Mental Health Risk: **{risk_label}**"
|
| 178 |
|
| 179 |
-
# Gradio Interface
|
| 180 |
-
demo = gr.Interface(
|
| 181 |
-
fn=predict_risk,
|
| 182 |
-
inputs=[
|
| 183 |
-
gr.Slider(20, 60, step=1, label="Age"),
|
| 184 |
-
gr.Radio(["Male", "Female"], label="Gender"),
|
| 185 |
-
gr.Dropdown(["E1", "E2", "E3", "E4", "E5", "O1", "O2", "O3"], label="Rank"),
|
| 186 |
-
gr.Slider(1, 25, step=1, label="Service Years"),
|
| 187 |
-
gr.Slider(0, 10, step=1, label="Deployment Count"),
|
| 188 |
-
gr.Radio(["Yes", "No"], label="Previous Mental Health Issues"),
|
| 189 |
-
gr.Slider(0.0, 10.0, step=0.1, label="Sleep Hours"),
|
| 190 |
-
gr.Slider(0, 10, step=1, label="Stress Level"),
|
| 191 |
-
gr.Slider(0, 21, step=1, label="Anxiety Score"),
|
| 192 |
-
gr.Slider(0, 27, step=1, label="Depression Score"),
|
| 193 |
-
gr.Slider(1.0, 10.0, step=0.1, label="Physical Fitness Score"),
|
| 194 |
-
gr.Slider(1.0, 10.0, step=0.1, label="Social Support Score"),
|
| 195 |
-
gr.Radio(["Yes", "No"], label="Substance Use"),
|
| 196 |
-
gr.Radio(["Low", "Moderate", "High"], label="Combat Training Intensity"),
|
| 197 |
-
gr.Radio(["Single", "Married", "Divorced", "Engaged"], label="Family Status")
|
| 198 |
-
],
|
| 199 |
-
outputs="text",
|
| 200 |
-
title="π§ Mental Health Risk Predictor (Military)",
|
| 201 |
-
description="Enter a soldier's pre-deployment details to estimate mental health risk level using logistic regression."
|
| 202 |
-
)
|
| 203 |
-
|
| 204 |
-
# Run the app
|
| 205 |
-
demo.launch()
|
| 206 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
import warnings
|
| 12 |
warnings.filterwarnings("ignore")
|
| 13 |
|
| 14 |
+
df = pd.read_csv("/Users/hassan/Desktop/pre_deployment_mental_health_dataset_balanced.csv")
|
| 15 |
df.head()
|
| 16 |
|
| 17 |
print(f"Shape of dataset: {df.shape}")
|
|
|
|
| 134 |
from sklearn.preprocessing import LabelEncoder
|
| 135 |
|
| 136 |
# Load the dataset
|
| 137 |
+
df = pd.read_csv("pre_deployment_mental_health_dataset_balanced.csv")
|
| 138 |
df_clean = df.drop(columns=["Soldier_ID"])
|
| 139 |
|
| 140 |
# Encode categorical variables
|
|
|
|
| 153 |
model = LogisticRegression(max_iter=1000)
|
| 154 |
model.fit(X_train, y_train)
|
| 155 |
|
| 156 |
+
# Create test profile dropdown (first 5 samples)
|
| 157 |
+
sample_data = df.head(5).set_index("Soldier_ID").drop(columns=["Risk_Level"])
|
| 158 |
+
|
| 159 |
+
# Define prediction function
|
| 160 |
+
def predict_risk(*args):
|
| 161 |
+
input_dict = dict(zip(X.columns, args))
|
| 162 |
+
|
| 163 |
+
# Encode string values before passing to model
|
| 164 |
+
for col in input_dict:
|
| 165 |
+
if col in label_encoders:
|
| 166 |
+
val = input_dict[col]
|
| 167 |
+
# If value is not already a known label, skip encoding
|
| 168 |
+
if val not in label_encoders[col].classes_:
|
| 169 |
+
# Decode the most frequent value instead
|
| 170 |
+
fallback = label_encoders[col].classes_[0]
|
| 171 |
+
input_dict[col] = label_encoders[col].transform([fallback])[0]
|
| 172 |
+
else:
|
| 173 |
+
input_dict[col] = label_encoders[col].transform([val])[0]
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
input_df = pd.DataFrame([input_dict])
|
| 177 |
pred = model.predict(input_df)[0]
|
| 178 |
risk_label = label_encoders["Risk_Level"].inverse_transform([pred])[0]
|
| 179 |
return f"π Predicted Mental Health Risk: **{risk_label}**"
|
| 180 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
+
def autofill(soldier_id):
|
| 183 |
+
row = sample_data.loc[soldier_id].copy()
|
| 184 |
+
return row.tolist()
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
# Create Gradio interface
|
| 190 |
+
with gr.Blocks(title="Military Mental Health Risk Predictor") as demo:
|
| 191 |
+
gr.Markdown("# π§ Mental Health Risk Predictor (Military)")
|
| 192 |
+
gr.Markdown("Predict a soldier's mental health risk using full or quick input modes.")
|
| 193 |
+
|
| 194 |
+
with gr.Tabs():
|
| 195 |
+
# ----------------- Tab 1: Full Input Mode -----------------
|
| 196 |
+
with gr.Tab("Full Input Mode"):
|
| 197 |
+
gr.Markdown("### π Full Feature Input")
|
| 198 |
+
|
| 199 |
+
dropdown = gr.Dropdown(choices=list(sample_data.index), label="Auto-Fill From Soldier ID")
|
| 200 |
+
autofill_btn = gr.Button("Auto-Fill")
|
| 201 |
+
|
| 202 |
+
# Original full input fields
|
| 203 |
+
age = gr.Slider(20, 60, step=1, label="Age")
|
| 204 |
+
gender = gr.Radio(["Male", "Female"], label="Gender")
|
| 205 |
+
rank = gr.Dropdown(["E1", "E2", "E3", "E4", "E5", "O1", "O2", "O3"], label="Rank")
|
| 206 |
+
service_years = gr.Slider(1, 25, step=1, label="Service Years")
|
| 207 |
+
deployment_count = gr.Slider(0, 10, step=1, label="Deployment Count")
|
| 208 |
+
mental_issues = gr.Radio(["Yes", "No"], label="Previous Mental Health Issues")
|
| 209 |
+
sleep = gr.Slider(0.0, 10.0, step=0.1, label="Sleep Hours")
|
| 210 |
+
stress = gr.Slider(0, 10, step=1, label="Stress Level")
|
| 211 |
+
anxiety = gr.Slider(0, 21, step=1, label="Anxiety Score")
|
| 212 |
+
depression = gr.Slider(0, 27, step=1, label="Depression Score")
|
| 213 |
+
fitness = gr.Slider(1.0, 10.0, step=0.1, label="Physical Fitness Score")
|
| 214 |
+
support = gr.Slider(1.0, 10.0, step=0.1, label="Social Support Score")
|
| 215 |
+
substance_use = gr.Radio(["Yes", "No"], label="Substance Use")
|
| 216 |
+
combat_intensity = gr.Radio(["Low", "Moderate", "High"], label="Combat Training Intensity")
|
| 217 |
+
family_status = gr.Radio(["Single", "Married", "Divorced", "Engaged"], label="Family Status")
|
| 218 |
+
|
| 219 |
+
output_full = gr.Textbox(label="Prediction", interactive=False)
|
| 220 |
+
|
| 221 |
+
all_inputs = [
|
| 222 |
+
age, gender, rank, service_years, deployment_count,
|
| 223 |
+
mental_issues, sleep, stress, anxiety, depression,
|
| 224 |
+
fitness, support, substance_use, combat_intensity, family_status
|
| 225 |
+
]
|
| 226 |
+
|
| 227 |
+
gr.Button("Submit").click(
|
| 228 |
+
fn=predict_risk,
|
| 229 |
+
inputs=all_inputs,
|
| 230 |
+
outputs=output_full
|
| 231 |
+
)
|
| 232 |
+
|
| 233 |
+
autofill_btn.click(
|
| 234 |
+
fn=autofill,
|
| 235 |
+
inputs=dropdown,
|
| 236 |
+
outputs=all_inputs
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
gr.Markdown("β οΈ Tip: Use the dropdown to quickly fill test data.")
|
| 240 |
+
|
| 241 |
+
gr.Markdown("""
|
| 242 |
+
### π’π π΄ Risk Level Legend
|
| 243 |
+
|
| 244 |
+
- π’ **Low Risk**: Soldier shows strong mental and physical indicators. Fit for deployment without concern.
|
| 245 |
+
- π **Moderate Risk**: Some factors suggest reduced resilience or emerging stress. Monitor or support as needed.
|
| 246 |
+
- π΄ **High Risk**: Significant signs of mental strain. Recommend further evaluation or support intervention.
|
| 247 |
+
""")
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
# ----------------- Tab 2: Quick Input Mode -----------------
|
| 251 |
+
with gr.Tab("Quick Input Mode"):
|
| 252 |
+
gr.Markdown("### β‘ Quick Mode (Important Features Only)")
|
| 253 |
+
|
| 254 |
+
stress_q = gr.Slider(0, 10, step=1, label="Stress Level")
|
| 255 |
+
anxiety_q = gr.Slider(0, 21, step=1, label="Anxiety Score")
|
| 256 |
+
depression_q = gr.Slider(0, 27, step=1, label="Depression Score")
|
| 257 |
+
fitness_q = gr.Slider(1.0, 10.0, step=0.1, label="Physical Fitness Score")
|
| 258 |
+
support_q = gr.Slider(1.0, 10.0, step=0.1, label="Social Support Score")
|
| 259 |
+
sleep_q = gr.Slider(0.0, 10.0, step=0.1, label="Sleep Hours")
|
| 260 |
+
service_q = gr.Slider(1, 25, step=1, label="Service Years")
|
| 261 |
+
|
| 262 |
+
quick_inputs = [stress_q, anxiety_q, depression_q, fitness_q, support_q, sleep_q, service_q]
|
| 263 |
+
|
| 264 |
+
output_quick = gr.Textbox(label="Prediction", interactive=False)
|
| 265 |
+
|
| 266 |
+
def predict_quick(stress, anxiety, depression, fitness, support, sleep, service):
|
| 267 |
+
input_dict = {
|
| 268 |
+
"Stress_Level": stress,
|
| 269 |
+
"Anxiety_Score": anxiety,
|
| 270 |
+
"Depression_Score": depression,
|
| 271 |
+
"Physical_Fitness_Score": fitness,
|
| 272 |
+
"Social_Support_Score": support,
|
| 273 |
+
"Sleep_Hours": sleep,
|
| 274 |
+
"Service_Years": service
|
| 275 |
+
}
|
| 276 |
+
|
| 277 |
+
# Fill missing fields
|
| 278 |
+
for col in X.columns:
|
| 279 |
+
if col not in input_dict:
|
| 280 |
+
if col in label_encoders:
|
| 281 |
+
mode_encoded = X[col].mode()[0]
|
| 282 |
+
input_dict[col] = label_encoders[col].inverse_transform([mode_encoded])[0]
|
| 283 |
+
else:
|
| 284 |
+
input_dict[col] = X[col].median()
|
| 285 |
+
|
| 286 |
+
# ENSURE THE ORDER MATCHES X.columns
|
| 287 |
+
ordered_values = [input_dict[col] for col in X.columns]
|
| 288 |
+
return predict_risk(*ordered_values)
|
| 289 |
+
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
gr.Button("Submit").click(
|
| 294 |
+
fn=predict_quick,
|
| 295 |
+
inputs=quick_inputs,
|
| 296 |
+
outputs=output_quick
|
| 297 |
+
)
|
| 298 |
+
|
| 299 |
+
gr.Markdown("""
|
| 300 |
+
### π’π π΄ Risk Level Legend
|
| 301 |
+
|
| 302 |
+
- π’ **Low Risk**: Soldier shows strong mental and physical indicators. Fit for deployment without concern.
|
| 303 |
+
- π **Moderate Risk**: Some factors suggest reduced resilience or emerging stress. Monitor or support as needed.
|
| 304 |
+
- π΄ **High Risk**: Significant signs of mental strain. Recommend further evaluation or support intervention.
|
| 305 |
+
""")
|
| 306 |
+
|
| 307 |
+
|
| 308 |
+
demo.launch()
|