Update app.py
Browse files
app.py
CHANGED
@@ -1,141 +1,141 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import pandas as pd
|
3 |
-
import joblib
|
4 |
-
|
5 |
-
# Load the trained models
|
6 |
-
xgb_model_depression = joblib.load('
|
7 |
-
xgb_model_anxiety = joblib.load('
|
8 |
-
|
9 |
-
# Depression and Anxiety Severity Class Mappings
|
10 |
-
depression_classes = {
|
11 |
-
0: "No Depression",
|
12 |
-
1: "Mild Depression",
|
13 |
-
2: "Moderate Depression",
|
14 |
-
3: "Severe Depression",
|
15 |
-
4: "Very Severe Depression",
|
16 |
-
5: "Extreme Depression"
|
17 |
-
}
|
18 |
-
|
19 |
-
anxiety_classes = {
|
20 |
-
0: "No Anxiety",
|
21 |
-
1: "Mild Anxiety",
|
22 |
-
2: "Moderate Anxiety",
|
23 |
-
3: "Severe Anxiety",
|
24 |
-
4: "Extreme Anxiety"
|
25 |
-
}
|
26 |
-
|
27 |
-
# Prediction functions
|
28 |
-
def predict_depression_severity(age, gender, bmi, who_bmi, phq_score, depressiveness, suicidal, depression_diagnosis, depression_treatment, gad_score, anxiousness, anxiety_diagnosis, anxiety_treatment, epworth_score, sleepiness):
|
29 |
-
# Convert categorical values to numeric
|
30 |
-
gender = 0 if gender == "Male" else 1
|
31 |
-
suicidal = int(suicidal) # Convert to int (0 or 1)
|
32 |
-
depression_diagnosis = int(depression_diagnosis) # Convert to int (0 or 1)
|
33 |
-
depression_treatment = int(depression_treatment) # Convert to int (0 or 1)
|
34 |
-
anxiety_diagnosis = int(anxiety_diagnosis) # Convert to int (0 or 1)
|
35 |
-
anxiety_treatment = int(anxiety_treatment) # Convert to int (0 or 1)
|
36 |
-
sleepiness = int(sleepiness) # Convert to int (0 or 1)
|
37 |
-
|
38 |
-
input_data = {
|
39 |
-
'age': age,
|
40 |
-
'gender': gender,
|
41 |
-
'bmi': bmi,
|
42 |
-
'who_bmi': who_bmi,
|
43 |
-
'phq_score': phq_score,
|
44 |
-
'depressiveness': depressiveness,
|
45 |
-
'suicidal': suicidal,
|
46 |
-
'depression_diagnosis': depression_diagnosis,
|
47 |
-
'depression_treatment': depression_treatment,
|
48 |
-
'gad_score': gad_score,
|
49 |
-
'anxiousness': anxiousness,
|
50 |
-
'anxiety_diagnosis': anxiety_diagnosis,
|
51 |
-
'anxiety_treatment': anxiety_treatment,
|
52 |
-
'epworth_score': epworth_score,
|
53 |
-
'sleepiness': sleepiness
|
54 |
-
}
|
55 |
-
|
56 |
-
# Ensure the input is a DataFrame
|
57 |
-
input_df = pd.DataFrame([input_data])
|
58 |
-
prediction = xgb_model_depression.predict(input_df)
|
59 |
-
|
60 |
-
# Map prediction to readable class
|
61 |
-
return depression_classes[prediction[0]]
|
62 |
-
|
63 |
-
def predict_anxiety_severity(age, gender, bmi, who_bmi, phq_score, depressiveness, suicidal, depression_diagnosis, depression_treatment, gad_score, anxiousness, anxiety_diagnosis, anxiety_treatment, epworth_score, sleepiness):
|
64 |
-
# Convert categorical values to numeric
|
65 |
-
gender = 0 if gender == "Male" else 1
|
66 |
-
suicidal = int(suicidal) # Convert to int (0 or 1)
|
67 |
-
depression_diagnosis = int(depression_diagnosis) # Convert to int (0 or 1)
|
68 |
-
depression_treatment = int(depression_treatment) # Convert to int (0 or 1)
|
69 |
-
anxiety_diagnosis = int(anxiety_diagnosis) # Convert to int (0 or 1)
|
70 |
-
anxiety_treatment = int(anxiety_treatment) # Convert to int (0 or 1)
|
71 |
-
sleepiness = int(sleepiness) # Convert to int (0 or 1)
|
72 |
-
|
73 |
-
input_data = {
|
74 |
-
'age': age,
|
75 |
-
'gender': gender,
|
76 |
-
'bmi': bmi,
|
77 |
-
'who_bmi': who_bmi,
|
78 |
-
'phq_score': phq_score,
|
79 |
-
'depressiveness': depressiveness,
|
80 |
-
'suicidal': suicidal,
|
81 |
-
'depression_diagnosis': depression_diagnosis,
|
82 |
-
'depression_treatment': depression_treatment,
|
83 |
-
'gad_score': gad_score,
|
84 |
-
'anxiousness': anxiousness,
|
85 |
-
'anxiety_diagnosis': anxiety_diagnosis,
|
86 |
-
'anxiety_treatment': anxiety_treatment,
|
87 |
-
'epworth_score': epworth_score,
|
88 |
-
'sleepiness': sleepiness
|
89 |
-
}
|
90 |
-
|
91 |
-
# Ensure the input is a DataFrame
|
92 |
-
input_df = pd.DataFrame([input_data])
|
93 |
-
prediction = xgb_model_anxiety.predict(input_df)
|
94 |
-
|
95 |
-
# Map prediction to readable class
|
96 |
-
return anxiety_classes[prediction[0]]
|
97 |
-
|
98 |
-
# Wrapper function to call both predictions
|
99 |
-
def predict_both(age, gender, bmi, who_bmi, phq_score, depressiveness, suicidal, depression_diagnosis, depression_treatment, gad_score, anxiousness, anxiety_diagnosis, anxiety_treatment, epworth_score, sleepiness):
|
100 |
-
depression_prediction = predict_depression_severity(age, gender, bmi, who_bmi, phq_score, depressiveness, suicidal, depression_diagnosis, depression_treatment, gad_score, anxiousness, anxiety_diagnosis, anxiety_treatment, epworth_score, sleepiness)
|
101 |
-
anxiety_prediction = predict_anxiety_severity(age, gender, bmi, who_bmi, phq_score, depressiveness, suicidal, depression_diagnosis, depression_treatment, gad_score, anxiousness, anxiety_diagnosis, anxiety_treatment, epworth_score, sleepiness)
|
102 |
-
|
103 |
-
return depression_prediction, anxiety_prediction
|
104 |
-
|
105 |
-
# Gradio interface setup
|
106 |
-
inputs = [
|
107 |
-
gr.Number(label="Age (in years)", info="Enter your age."),
|
108 |
-
gr.Radio(["Male", "Female"], label="Gender", info="Select your gender."),
|
109 |
-
gr.Number(label="BMI", info="Enter your BMI value."),
|
110 |
-
gr.Number(label="WHO BMI Classification", info="Enter your WHO BMI classification (0: Underweight, 1: Normal, 2: Overweight, 3: Obese)."),
|
111 |
-
gr.Number(label="PHQ Score", info="Enter your PHQ-9 score for depression (0-27)."),
|
112 |
-
gr.Number(label="Depressiveness", info="Enter your level of depressiveness (0-1)."),
|
113 |
-
gr.Radio([0, 1], label="Suicidal", info="Indicate if you've experienced suicidal thoughts (0: No, 1: Yes)."),
|
114 |
-
gr.Radio([0, 1], label="Depression Diagnosis", info="Indicate if you've been diagnosed with depression (0: No, 1: Yes)."),
|
115 |
-
gr.Radio([0, 1], label="Depression Treatment", info="Indicate if you're receiving treatment for depression (0: No, 1: Yes)."),
|
116 |
-
gr.Number(label="GAD Score", info="Enter your GAD-7 score for anxiety (0-21)."),
|
117 |
-
gr.Number(label="Anxiousness", info="Enter your level of anxiousness (0-1)."),
|
118 |
-
gr.Radio([0, 1], label="Anxiety Diagnosis", info="Indicate if you've been diagnosed with anxiety (0: No, 1: Yes)."),
|
119 |
-
gr.Radio([0, 1], label="Anxiety Treatment", info="Indicate if you're receiving treatment for anxiety (0: No, 1: Yes)."),
|
120 |
-
gr.Number(label="Epworth Score", info="Enter your Epworth Sleepiness Scale score (0-24)."),
|
121 |
-
gr.Number(label="Sleepiness", info="Enter your sleepiness score (0-1).")
|
122 |
-
]
|
123 |
-
|
124 |
-
outputs = [
|
125 |
-
gr.Textbox(label="Predicted Depression Severity", info="The predicted severity of your depression."),
|
126 |
-
gr.Textbox(label="Predicted Anxiety Severity", info="The predicted severity of your anxiety.")
|
127 |
-
]
|
128 |
-
|
129 |
-
# Creating the interface with a submit button
|
130 |
-
interface = gr.Interface(
|
131 |
-
fn=predict_both,
|
132 |
-
inputs=inputs,
|
133 |
-
outputs=outputs,
|
134 |
-
title="Mental Health Severity Prediction",
|
135 |
-
description="This app predicts the severity of depression and anxiety based on various inputs related to mental health. Please fill in the fields below and click submit to get predictions.",
|
136 |
-
live=False, # Set to False to ensure predictions are only made after submit
|
137 |
-
allow_flagging="never", # Prevent users from flagging the results
|
138 |
-
)
|
139 |
-
|
140 |
-
# Launch the app
|
141 |
-
interface.launch()
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import joblib
|
4 |
+
|
5 |
+
# Load the trained models
|
6 |
+
xgb_model_depression = joblib.load('xgb_model_depression.pkl')
|
7 |
+
xgb_model_anxiety = joblib.load('xgb_model_anxiety.pkl')
|
8 |
+
|
9 |
+
# Depression and Anxiety Severity Class Mappings
|
10 |
+
depression_classes = {
|
11 |
+
0: "No Depression",
|
12 |
+
1: "Mild Depression",
|
13 |
+
2: "Moderate Depression",
|
14 |
+
3: "Severe Depression",
|
15 |
+
4: "Very Severe Depression",
|
16 |
+
5: "Extreme Depression"
|
17 |
+
}
|
18 |
+
|
19 |
+
anxiety_classes = {
|
20 |
+
0: "No Anxiety",
|
21 |
+
1: "Mild Anxiety",
|
22 |
+
2: "Moderate Anxiety",
|
23 |
+
3: "Severe Anxiety",
|
24 |
+
4: "Extreme Anxiety"
|
25 |
+
}
|
26 |
+
|
27 |
+
# Prediction functions
|
28 |
+
def predict_depression_severity(age, gender, bmi, who_bmi, phq_score, depressiveness, suicidal, depression_diagnosis, depression_treatment, gad_score, anxiousness, anxiety_diagnosis, anxiety_treatment, epworth_score, sleepiness):
|
29 |
+
# Convert categorical values to numeric
|
30 |
+
gender = 0 if gender == "Male" else 1
|
31 |
+
suicidal = int(suicidal) # Convert to int (0 or 1)
|
32 |
+
depression_diagnosis = int(depression_diagnosis) # Convert to int (0 or 1)
|
33 |
+
depression_treatment = int(depression_treatment) # Convert to int (0 or 1)
|
34 |
+
anxiety_diagnosis = int(anxiety_diagnosis) # Convert to int (0 or 1)
|
35 |
+
anxiety_treatment = int(anxiety_treatment) # Convert to int (0 or 1)
|
36 |
+
sleepiness = int(sleepiness) # Convert to int (0 or 1)
|
37 |
+
|
38 |
+
input_data = {
|
39 |
+
'age': age,
|
40 |
+
'gender': gender,
|
41 |
+
'bmi': bmi,
|
42 |
+
'who_bmi': who_bmi,
|
43 |
+
'phq_score': phq_score,
|
44 |
+
'depressiveness': depressiveness,
|
45 |
+
'suicidal': suicidal,
|
46 |
+
'depression_diagnosis': depression_diagnosis,
|
47 |
+
'depression_treatment': depression_treatment,
|
48 |
+
'gad_score': gad_score,
|
49 |
+
'anxiousness': anxiousness,
|
50 |
+
'anxiety_diagnosis': anxiety_diagnosis,
|
51 |
+
'anxiety_treatment': anxiety_treatment,
|
52 |
+
'epworth_score': epworth_score,
|
53 |
+
'sleepiness': sleepiness
|
54 |
+
}
|
55 |
+
|
56 |
+
# Ensure the input is a DataFrame
|
57 |
+
input_df = pd.DataFrame([input_data])
|
58 |
+
prediction = xgb_model_depression.predict(input_df)
|
59 |
+
|
60 |
+
# Map prediction to readable class
|
61 |
+
return depression_classes[prediction[0]]
|
62 |
+
|
63 |
+
def predict_anxiety_severity(age, gender, bmi, who_bmi, phq_score, depressiveness, suicidal, depression_diagnosis, depression_treatment, gad_score, anxiousness, anxiety_diagnosis, anxiety_treatment, epworth_score, sleepiness):
|
64 |
+
# Convert categorical values to numeric
|
65 |
+
gender = 0 if gender == "Male" else 1
|
66 |
+
suicidal = int(suicidal) # Convert to int (0 or 1)
|
67 |
+
depression_diagnosis = int(depression_diagnosis) # Convert to int (0 or 1)
|
68 |
+
depression_treatment = int(depression_treatment) # Convert to int (0 or 1)
|
69 |
+
anxiety_diagnosis = int(anxiety_diagnosis) # Convert to int (0 or 1)
|
70 |
+
anxiety_treatment = int(anxiety_treatment) # Convert to int (0 or 1)
|
71 |
+
sleepiness = int(sleepiness) # Convert to int (0 or 1)
|
72 |
+
|
73 |
+
input_data = {
|
74 |
+
'age': age,
|
75 |
+
'gender': gender,
|
76 |
+
'bmi': bmi,
|
77 |
+
'who_bmi': who_bmi,
|
78 |
+
'phq_score': phq_score,
|
79 |
+
'depressiveness': depressiveness,
|
80 |
+
'suicidal': suicidal,
|
81 |
+
'depression_diagnosis': depression_diagnosis,
|
82 |
+
'depression_treatment': depression_treatment,
|
83 |
+
'gad_score': gad_score,
|
84 |
+
'anxiousness': anxiousness,
|
85 |
+
'anxiety_diagnosis': anxiety_diagnosis,
|
86 |
+
'anxiety_treatment': anxiety_treatment,
|
87 |
+
'epworth_score': epworth_score,
|
88 |
+
'sleepiness': sleepiness
|
89 |
+
}
|
90 |
+
|
91 |
+
# Ensure the input is a DataFrame
|
92 |
+
input_df = pd.DataFrame([input_data])
|
93 |
+
prediction = xgb_model_anxiety.predict(input_df)
|
94 |
+
|
95 |
+
# Map prediction to readable class
|
96 |
+
return anxiety_classes[prediction[0]]
|
97 |
+
|
98 |
+
# Wrapper function to call both predictions
|
99 |
+
def predict_both(age, gender, bmi, who_bmi, phq_score, depressiveness, suicidal, depression_diagnosis, depression_treatment, gad_score, anxiousness, anxiety_diagnosis, anxiety_treatment, epworth_score, sleepiness):
|
100 |
+
depression_prediction = predict_depression_severity(age, gender, bmi, who_bmi, phq_score, depressiveness, suicidal, depression_diagnosis, depression_treatment, gad_score, anxiousness, anxiety_diagnosis, anxiety_treatment, epworth_score, sleepiness)
|
101 |
+
anxiety_prediction = predict_anxiety_severity(age, gender, bmi, who_bmi, phq_score, depressiveness, suicidal, depression_diagnosis, depression_treatment, gad_score, anxiousness, anxiety_diagnosis, anxiety_treatment, epworth_score, sleepiness)
|
102 |
+
|
103 |
+
return depression_prediction, anxiety_prediction
|
104 |
+
|
105 |
+
# Gradio interface setup
|
106 |
+
inputs = [
|
107 |
+
gr.Number(label="Age (in years)", info="Enter your age."),
|
108 |
+
gr.Radio(["Male", "Female"], label="Gender", info="Select your gender."),
|
109 |
+
gr.Number(label="BMI", info="Enter your BMI value."),
|
110 |
+
gr.Number(label="WHO BMI Classification", info="Enter your WHO BMI classification (0: Underweight, 1: Normal, 2: Overweight, 3: Obese)."),
|
111 |
+
gr.Number(label="PHQ Score", info="Enter your PHQ-9 score for depression (0-27)."),
|
112 |
+
gr.Number(label="Depressiveness", info="Enter your level of depressiveness (0-1)."),
|
113 |
+
gr.Radio([0, 1], label="Suicidal", info="Indicate if you've experienced suicidal thoughts (0: No, 1: Yes)."),
|
114 |
+
gr.Radio([0, 1], label="Depression Diagnosis", info="Indicate if you've been diagnosed with depression (0: No, 1: Yes)."),
|
115 |
+
gr.Radio([0, 1], label="Depression Treatment", info="Indicate if you're receiving treatment for depression (0: No, 1: Yes)."),
|
116 |
+
gr.Number(label="GAD Score", info="Enter your GAD-7 score for anxiety (0-21)."),
|
117 |
+
gr.Number(label="Anxiousness", info="Enter your level of anxiousness (0-1)."),
|
118 |
+
gr.Radio([0, 1], label="Anxiety Diagnosis", info="Indicate if you've been diagnosed with anxiety (0: No, 1: Yes)."),
|
119 |
+
gr.Radio([0, 1], label="Anxiety Treatment", info="Indicate if you're receiving treatment for anxiety (0: No, 1: Yes)."),
|
120 |
+
gr.Number(label="Epworth Score", info="Enter your Epworth Sleepiness Scale score (0-24)."),
|
121 |
+
gr.Number(label="Sleepiness", info="Enter your sleepiness score (0-1).")
|
122 |
+
]
|
123 |
+
|
124 |
+
outputs = [
|
125 |
+
gr.Textbox(label="Predicted Depression Severity", info="The predicted severity of your depression."),
|
126 |
+
gr.Textbox(label="Predicted Anxiety Severity", info="The predicted severity of your anxiety.")
|
127 |
+
]
|
128 |
+
|
129 |
+
# Creating the interface with a submit button
|
130 |
+
interface = gr.Interface(
|
131 |
+
fn=predict_both,
|
132 |
+
inputs=inputs,
|
133 |
+
outputs=outputs,
|
134 |
+
title="Mental Health Severity Prediction",
|
135 |
+
description="This app predicts the severity of depression and anxiety based on various inputs related to mental health. Please fill in the fields below and click submit to get predictions.",
|
136 |
+
live=False, # Set to False to ensure predictions are only made after submit
|
137 |
+
allow_flagging="never", # Prevent users from flagging the results
|
138 |
+
)
|
139 |
+
|
140 |
+
# Launch the app
|
141 |
+
interface.launch()
|