Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import pickle
|
2 |
+
|
3 |
+
import gradio as gr
|
4 |
+
import numpy as np
|
5 |
+
import pandas as pd
|
6 |
+
|
7 |
+
|
8 |
+
class ModelPredictor:
|
9 |
+
def __init__(self, model_path, model_filenames):
|
10 |
+
self.model_path = model_path
|
11 |
+
self.model_filenames = model_filenames
|
12 |
+
self.models = self.load_models()
|
13 |
+
self.prediction_map = {
|
14 |
+
'YOWRDISC': ["Did not feel discouraged about life every day", "Felt discouraged about life every day"],
|
15 |
+
'YO_MDEA3': ["Does not have changes in appetite or weight", "Has changes in appetite or weight"],
|
16 |
+
'YOPB2WK': ["Did not feel this way for 2 weeks or more with other problems during the past 12 months", "Felt this way for 2 weeks or more with other problems during the past 12 months"],
|
17 |
+
'YOWRDCSN': ["Able to make decisions when problems were worst", "Unable to make decisions when problems were worst"],
|
18 |
+
'YODPDISC': ["Did not feel discouraged about life when sad/empty/depressed", "Felt discouraged about life when sad/empty/depressed"],
|
19 |
+
'YOWRELES': ["Did not eat less than usual every day when problems were worst", "Ate less than usual every day when problems were worst"],
|
20 |
+
'YORX12MO': ["Not currently receiving treatment/counseling for mood", "Currently receiving treatment/counseling for mood"],
|
21 |
+
'YODPLSIN': ["Did not lose interest in things when sad/empty/depressed", "Lost interest in things when sad/empty/depressed"],
|
22 |
+
'YODSCEV': ["Never had a period of time where felt discouraged or hopeless", "Ever had a period of time where felt discouraged or hopeless"],
|
23 |
+
'YO_MDEA8': ["Ability to concentrate or make decisions", "Inability to concentrate or make decisions"],
|
24 |
+
'YOWRHRS': ["Time that most severe/frequent mood lasted less than 1 hour", "Time that most severe/frequent mood lasted at least 1 hour but no more than 3 hours", "Time that most severe/frequent mood lasted at least 3 hours but no more than 5 hours"],
|
25 |
+
'YOWRIMP': ["Mood so severe couldn't do daily activities not very often", "Mood so severe couldn't do daily activities sometimes", "Mood so severe couldn't do daily activities often"],
|
26 |
+
'YO_MDEA2': ["Did not lose interest or pleasure in most things", "Lost interest or pleasure in most things"],
|
27 |
+
'YODPPROB': ["Never had other problems during 2 weeks or longer", "Ever had other problems during 2 weeks or longer"],
|
28 |
+
'YOWRPROB': ["No particular time that is the worst one ever", "One particular time that is the worst one ever"],
|
29 |
+
'YOLOSEV': ["Never had a period of time lost interest in enjoyable things", "Ever had a period of time lost interest in enjoyable things"],
|
30 |
+
'YO_MDEA5': ["Others did not notice that was restless or lethargic", "Others noticed that was restless or lethargic"],
|
31 |
+
'YODSMMDE': ["Has less than 5 symptoms", "Has 5 or more symptoms"],
|
32 |
+
'YODPR2WK': ["No time when feelings lasted every day for 2 weeks or longer", "Time when feelings lasted every day for 2 weeks or longer"]
|
33 |
+
}
|
34 |
+
|
35 |
+
def load_models(self):
|
36 |
+
models = []
|
37 |
+
for filename in self.model_filenames:
|
38 |
+
filepath = self.model_path + filename
|
39 |
+
with open(filepath, 'rb') as file:
|
40 |
+
model = pickle.load(file)
|
41 |
+
models.append(model)
|
42 |
+
return models
|
43 |
+
|
44 |
+
def make_predictions(self, user_input):
|
45 |
+
predictions = []
|
46 |
+
for model in self.models:
|
47 |
+
pred = model.predict(user_input)
|
48 |
+
pred = np.array(pred).flatten()
|
49 |
+
predictions.append(pred)
|
50 |
+
return predictions
|
51 |
+
|
52 |
+
def get_majority_vote(self, predictions):
|
53 |
+
combined_predictions = np.concatenate(predictions)
|
54 |
+
majority_vote = np.bincount(combined_predictions).argmax()
|
55 |
+
return majority_vote
|
56 |
+
|
57 |
+
def evaluate_severity(self, majority_vote_count):
|
58 |
+
if majority_vote_count > 10:
|
59 |
+
return "Mental health severity: Severe"
|
60 |
+
elif majority_vote_count > 7:
|
61 |
+
return "Mental health severity: Moderate"
|
62 |
+
elif majority_vote_count > 4:
|
63 |
+
return "Mental health severity: Low"
|
64 |
+
else:
|
65 |
+
return "Mental health severity: Very Low"
|
66 |
+
|
67 |
+
model_filenames = [
|
68 |
+
"YOWRDISC.pkl", "YO_MDEA3.pkl", "YOPB2WK.pkl", "YOWRDCSN.pkl", "YODPDISC.pkl",
|
69 |
+
"YOWRELES.pkl", "YORX12MO.pkl", "YODPLSIN.pkl", "YOWRIMP.pkl", "YODPPROB.pkl",
|
70 |
+
"YOWRPROB.pkl", "YOLOSEV.pkl", "YO_MDEA5.pkl", "YODSMMDE.pkl", "YODPR2WK.pkl"
|
71 |
+
]
|
72 |
+
model_path = "models/"
|
73 |
+
predictor = ModelPredictor(model_path, model_filenames)
|
74 |
+
|
75 |
+
def predict(user_inputs):
|
76 |
+
user_input_data = {key: [int(value)] for key, value in user_inputs.items()}
|
77 |
+
user_input = pd.DataFrame(user_input_data)
|
78 |
+
predictions = predictor.make_predictions(user_input)
|
79 |
+
majority_vote = predictor.get_majority_vote(predictions)
|
80 |
+
majority_vote_count = sum([1 for pred in np.concatenate(predictions) if pred == 1])
|
81 |
+
severity = predictor.evaluate_severity(majority_vote_count)
|
82 |
+
|
83 |
+
results = []
|
84 |
+
for i, pred in enumerate(predictions):
|
85 |
+
model_name = model_filenames[i].split('.')[0]
|
86 |
+
pred_value = pred[0]
|
87 |
+
if model_name in predictor.prediction_map:
|
88 |
+
if pred_value < len(predictor.prediction_map[model_name]):
|
89 |
+
results.append(f"Prediction from model {model_name}: {predictor.prediction_map[model_name][pred_value]}")
|
90 |
+
else:
|
91 |
+
results.append(f"Prediction from model {model_name}: Unknown prediction value {pred_value}")
|
92 |
+
else:
|
93 |
+
results.append(f"Prediction from model {model_name}: Unknown model")
|
94 |
+
|
95 |
+
return results, severity
|
96 |
+
|
97 |
+
inputs = [
|
98 |
+
gr.inputs.Dropdown(["1", "0"], label="YNURSMDE (1 = Yes, 0 = No)"),
|
99 |
+
gr.inputs.Dropdown(["1", "2"], label="YMDEYR (1 = Yes, 2 = No)"),
|
100 |
+
gr.inputs.Dropdown(["1", "0"], label="YSOCMDE (1 = Yes, 0 = No)"),
|
101 |
+
gr.inputs.Dropdown(["1", "2"], label="YUCOSUITHK (1 = Yes, 2 = No)"),
|
102 |
+
gr.inputs.Dropdown(["1", "2", "3", "4"], label="YMDESUD5ANYO (1 = SUD only, no MDE, 2 = MDE only, no SUD, 3 = SUD and MDE, 4 = Neither SUD or MDE)"),
|
103 |
+
gr.inputs.Dropdown(["1", "0"], label="YMSUD5YANY (1 = Yes, 0 = No)"),
|
104 |
+
gr.inputs.Dropdown(["1", "2", "3", "4"], label="YUSUITHK (1 = Yes, 2 = No, 3 = I'm not sure, 4 = I don't want to answer)"),
|
105 |
+
gr.inputs.Dropdown(["1", "0"], label="YMDETXRX (1 = Yes, 0 = No)"),
|
106 |
+
gr.inputs.Dropdown(["1", "2", "3", "4"], label="YUSUITHKYR (1 = Yes, 2 = No, 3 = I'm not sure, 4 = I don't want to answer)"),
|
107 |
+
gr.inputs.Dropdown(["1", "0"], label="YMDERSUD5ANY (1 = Yes, 0 = No)"),
|
108 |
+
gr.inputs.Dropdown(["1", "2", "3", "4"], label="YUSUIPLNYR (1 = Yes, 2 = No, 3 = I'm not sure, 4 = I don't want to answer)"),
|
109 |
+
gr.inputs.Dropdown(["1", "0"], label="YCOUNMDE (1 = Yes, 0 = No)"),
|
110 |
+
gr.inputs.Dropdown(["1", "0"], label="YPSY1MDE (1 = Yes, 0 = No)"),
|
111 |
+
gr.inputs.Dropdown(["1", "0"], label="YHLTMDE (1 = Yes, 0 = No)"),
|
112 |
+
gr.inputs.Dropdown(["1", "0"], label="YDOCMDE (1 = Yes, 0 = No)"),
|
113 |
+
gr.inputs.Dropdown(["1", "0"], label="YPSY2MDE (1 = Yes, 0 = No)"),
|
114 |
+
gr.inputs.Dropdown(["1", "0"], label="YMDEHARX (1 = Yes, 0 = No)"),
|
115 |
+
gr.inputs.Dropdown(["1", "2", "3"], label="LVLDIFMEM2 (1 = No Difficulty, 2 = Some difficulty, 3 = A lot of difficulty or cannot do at all)"),
|
116 |
+
gr.inputs.Dropdown(["1", "2"], label="MDEIMPY (1 = Yes, 2 = No)"),
|
117 |
+
gr.inputs.Dropdown(["1", "0"], label="YMDEHPO (1 = Yes, 0 = No)"),
|
118 |
+
gr.inputs.Dropdown(["1", "2", "3", "4", "5"], label="CONEGMH (1 = Not at all, 2 = A little, 3 = Some, 4 = Quite a bit, 5 = A lot)"),
|
119 |
+
gr.inputs.Dropdown(["1", "2", "3"], label="COCLNEGMH (1 = Not at all, 2 = A little or some, 3 = Quite a bit or a lot)"),
|
120 |
+
gr.inputs.Dropdown(["1", "0"], label="YMIMS5YANY (1 = Yes, 0 = No)"),
|
121 |
+
gr.inputs.Dropdown(["1", "0"], label="YMDEIMAD5YR (1 = Yes, 0 = No)"),
|
122 |
+
gr.inputs.Dropdown(["1", "0"], label="YMIUD5YANY (1 = Yes, 0 = No)"),
|
123 |
+
gr.inputs.Dropdown(["1", "2"], label="YUCOSUIPLN (1 = Yes, 2 = No)"),
|
124 |
+
gr.inputs.Dropdown(["1", "0"], label="YMDEHPRX (1 = Yes, 0 = No)"),
|
125 |
+
gr.inputs.Dropdown(["1", "0"], label="YMIMI5YANY (1 = Yes, 0 = No)"),
|
126 |
+
gr.inputs.Dropdown(["1", "2", "3", "4"], label="YUSUIPLN (1 = Yes, 2 = No, 3 = I'm not sure, 4 = I don't want to answer)"),
|
127 |
+
gr.inputs.Dropdown(["1", "0"], label="YTXMDEYR (1 = Yes, 0 = No)"),
|
128 |
+
gr.inputs.Dropdown(["1", "0"], label="YMDEAUD5YR (1 = Yes, 0 = No)"),
|
129 |
+
gr.inputs.Dropdown(["1", "0"], label="YRXMDEYR (1 = Yes, 0 = No)"),
|
130 |
+
gr.inputs.Dropdown(["1", "2"], label="YMDELT (1 = Yes, 2 = No)")
|
131 |
+
]
|
132 |
+
|
133 |
+
outputs = [
|
134 |
+
gr.outputs.Textbox(label="Prediction Results"),
|
135 |
+
gr.outputs.Textbox(label="Mental Health Severity")
|
136 |
+
]
|
137 |
+
|
138 |
+
gr.Interface(fn=predict, inputs=inputs, outputs=outputs, title="Adolescent Mental Health Prediction").launch()
|