Spaces:
Running
Running
feat: finish logic
Browse files- Pipfile +15 -0
- Pipfile.lock +0 -0
- app.py +293 -220
- random_forest_model.joblib +3 -0
- requirements.txt +72 -0
Pipfile
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[[source]]
|
| 2 |
+
url = "https://pypi.org/simple"
|
| 3 |
+
verify_ssl = true
|
| 4 |
+
name = "pypi"
|
| 5 |
+
|
| 6 |
+
[packages]
|
| 7 |
+
gradio = "*"
|
| 8 |
+
pandas = "*"
|
| 9 |
+
joblib = "*"
|
| 10 |
+
scikit-learn = "*"
|
| 11 |
+
|
| 12 |
+
[dev-packages]
|
| 13 |
+
|
| 14 |
+
[requires]
|
| 15 |
+
python_version = "3.9"
|
Pipfile.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
CHANGED
|
@@ -1,239 +1,312 @@
|
|
| 1 |
import pandas as pd
|
| 2 |
import gradio as gr
|
|
|
|
| 3 |
|
| 4 |
|
| 5 |
# Inputs for UI
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
primary_hue=gr.themes.colors.zinc,
|
| 10 |
-
neutral_hue=gr.themes.colors.slate,
|
| 11 |
-
)
|
| 12 |
-
) as demo:
|
| 13 |
-
age = (
|
| 14 |
-
gr.Number(
|
| 15 |
-
label="Age",
|
| 16 |
-
info="Age of client at admission",
|
| 17 |
-
minimum=0,
|
| 18 |
-
maximum=100,
|
| 19 |
-
),
|
| 20 |
-
)
|
| 21 |
-
race = (
|
| 22 |
-
gr.Dropdown(
|
| 23 |
-
[
|
| 24 |
-
"Alaska Native (Aleut, Eskimo, Indian)",
|
| 25 |
-
"American Indian (other than Alaska Native)",
|
| 26 |
-
"Asian or Pacific Islander",
|
| 27 |
-
"Black or African American",
|
| 28 |
-
"White",
|
| 29 |
-
"Asian",
|
| 30 |
-
"Other single race",
|
| 31 |
-
"Two or more races",
|
| 32 |
-
"Native Hawaiian or Other Pacific Islander",
|
| 33 |
-
],
|
| 34 |
-
label="Race",
|
| 35 |
-
),
|
| 36 |
-
)
|
| 37 |
-
education = (
|
| 38 |
-
gr.Dropdown(
|
| 39 |
-
[
|
| 40 |
-
"Less than one school grade, no schooling, nursery school, or kindergarten to Grade 8",
|
| 41 |
-
"Grades 9 to 11",
|
| 42 |
-
"Grade 12 (or GED)",
|
| 43 |
-
"1-3 years of college, university, or vocational school",
|
| 44 |
-
"4 years of college, university, BA/BS, some postgraduate study, or more",
|
| 45 |
-
],
|
| 46 |
-
label="Education",
|
| 47 |
-
info="The highest school grade completed for adults or children not attending school, or current school grade for school-age children (3-17 years old) attending school",
|
| 48 |
-
),
|
| 49 |
-
)
|
| 50 |
-
marital = (
|
| 51 |
-
gr.Dropdown(
|
| 52 |
-
["Never married", "Now married", "Separated", "Divorced, widowed"],
|
| 53 |
-
label="Marital Status",
|
| 54 |
-
info="Client's marital status, compatible with U.S. Census categories",
|
| 55 |
-
),
|
| 56 |
-
)
|
| 57 |
-
primary_income = (
|
| 58 |
-
gr.Dropdown(
|
| 59 |
-
[
|
| 60 |
-
"Wages/salary",
|
| 61 |
-
"Public assistance",
|
| 62 |
-
"Retirement/pension, disability",
|
| 63 |
-
"Other",
|
| 64 |
-
"None",
|
| 65 |
-
],
|
| 66 |
-
label="Primary Source of Income/Support",
|
| 67 |
-
info="Client's principal source of financial support (for children younger than 18 years old, the primary parental source of income/support)",
|
| 68 |
-
),
|
| 69 |
-
)
|
| 70 |
-
health_insurance = (
|
| 71 |
-
gr.Dropdown(
|
| 72 |
-
[
|
| 73 |
-
"Private insurance, Blue Cross/Blue Shield, HMO",
|
| 74 |
-
"Medicaid",
|
| 75 |
-
"Medicare, other (e.g. TRICARE, CHAMPUS)",
|
| 76 |
-
"None",
|
| 77 |
-
],
|
| 78 |
-
label="Health Insurance",
|
| 79 |
-
info="Client's health insurance at admission",
|
| 80 |
-
),
|
| 81 |
-
)
|
| 82 |
-
primary_substance = (
|
| 83 |
-
gr.Dropdown(
|
| 84 |
-
[
|
| 85 |
-
"None",
|
| 86 |
-
"Alcohol",
|
| 87 |
-
"Cocaine/crack",
|
| 88 |
-
"Marijuana/hashish: Includes THC and any other cannabis sativa preparations",
|
| 89 |
-
"Heroin",
|
| 90 |
-
"Non-prescription methadone",
|
| 91 |
-
"Other opiates and synthetics: Includes buprenorphine, butorphanol, codeine, hydrocodone, hydromorphone, meperidine,morphine, opium, oxycodone, pentazocine, propoxyphene, tramadol, and other narcotic analgesics, opiates, or synthetics",
|
| 92 |
-
"PCP: Phencyclidine",
|
| 93 |
-
"Hallucinogens: Includes LSD, DMT, mescaline, peyote, psilocybin, STP, and other hallucinogens",
|
| 94 |
-
"Methamphetamine/speed",
|
| 95 |
-
"Other amphetamines: Includes amphetamines, MDMA, ‘bath salts’, phenmetrazine, and other amines and related drugs",
|
| 96 |
-
"Other stimulants: Includes methylphenidate and any other stimulants",
|
| 97 |
-
"Benzodiazepines: Includes alprazolam, chlordiazepoxide, clonazepam, clorazepate, diazepam, flunitrazepam,flurazepam, halazepam, lorazepam, oxazepam, prazepam, temazepam, triazolam, and other unspecified benzodiazepines",
|
| 98 |
-
"Other tranquilizers: Includes meprobamate, and other non-benzodiazepine tranquilizers",
|
| 99 |
-
"Barbiturates: Includes amobarbital, pentobarbital, phenobarbital, secobarbital, etc.",
|
| 100 |
-
"Other sedatives or hypnotics: Includes chloral hydrate, ethchlorvynol, glutethimide, methaqualone, and othernon-barbiturate sedatives and hypnotics",
|
| 101 |
-
"Inhalants: Includes aerosols; chloroform, ether, nitrous oxide and other anesthetics; gasoline; glue; nitrites; paint thinnerand other solvents; and other inappropriately inhaled products",
|
| 102 |
-
"Over-the-counter medications: Includes aspirin, dextromethorphan and other cough syrups, diphenhydramine and otheranti-histamines, ephedrine, sleep aids, and any other legally obtained, non-prescription medication",
|
| 103 |
-
"Other drugs: Includes diphenylhydantoin/phenytoin, GHB/GBL, ketamine, synthetic cannabinoid 'Spice', carisoprodol(Soma), and other drugs",
|
| 104 |
-
],
|
| 105 |
-
label="Primary Substance Use",
|
| 106 |
-
info="Client's primary substance use at admission",
|
| 107 |
-
),
|
| 108 |
-
)
|
| 109 |
-
first_use = (
|
| 110 |
-
gr.Number(
|
| 111 |
-
label="Age at First Use of Primary Substance",
|
| 112 |
-
info="For alcohol use, this is the age of first intoxication",
|
| 113 |
-
minimum=0,
|
| 114 |
-
maximum=100,
|
| 115 |
-
),
|
| 116 |
-
)
|
| 117 |
-
frequency = (
|
| 118 |
-
gr.Radio(
|
| 119 |
-
["No use in the past month", "Some use", "Daily use"],
|
| 120 |
-
label="Frequency of Primary Substance Use",
|
| 121 |
-
),
|
| 122 |
-
)
|
| 123 |
-
days_waiting = (
|
| 124 |
-
gr.Number(
|
| 125 |
-
label="Days Waiting to Enter Substance Use Treatment",
|
| 126 |
-
info="Number of days from the first contact or request for substance use treatment service until the client was admitted and the first clinical substance use treatment service was provided",
|
| 127 |
-
minimum=0,
|
| 128 |
-
),
|
| 129 |
-
)
|
| 130 |
-
arrests = (
|
| 131 |
-
gr.Radio(
|
| 132 |
-
["None", "Once", "Two or more times"],
|
| 133 |
-
label="Arrests",
|
| 134 |
-
info="Number of arrests in the 30 days prior to admission",
|
| 135 |
-
),
|
| 136 |
-
)
|
| 137 |
-
attendance = (
|
| 138 |
-
gr.Dropdown(
|
| 139 |
-
[
|
| 140 |
-
"No attendance",
|
| 141 |
-
"1-3 times in the past month",
|
| 142 |
-
"4-7 times in the past month",
|
| 143 |
-
"8-30 times in the past month",
|
| 144 |
-
"Some attendance, frequency is unknown",
|
| 145 |
-
],
|
| 146 |
-
label="Attendance at Substance Use Self-help Groups in Past 30 Days",
|
| 147 |
-
info="Frequency of attendance at a substance use self-help group in the 30 days prior to the reference date (the date of admission). Includes Alcoholics Anonymous (AA), Narcotics Anonymous (NA), and other self-help/mutual support groups focused on recovery from substance use and dependence",
|
| 148 |
-
),
|
| 149 |
-
)
|
| 150 |
-
services = (
|
| 151 |
-
gr.Dropdown(
|
| 152 |
-
[
|
| 153 |
-
"Detox, 24-hour, hospital inpatient",
|
| 154 |
-
"Detox, 24-hour, free-standing residential",
|
| 155 |
-
"Rehab/residential, hospital (non-detox)",
|
| 156 |
-
"Rehab/residential, short term (30 days or fewer)",
|
| 157 |
-
"Rehab/residential, long term (more than 30 days)",
|
| 158 |
-
"Ambulatory, intensive outpatient",
|
| 159 |
-
"Ambulatory, non-intensive outpatient",
|
| 160 |
-
"Ambulatory, detoxification",
|
| 161 |
-
],
|
| 162 |
-
label="Type of Treatment Service/Setting",
|
| 163 |
-
info="Type of treatment service or treatment setting in which the client is placed at the time of admission or transfer",
|
| 164 |
-
),
|
| 165 |
-
)
|
| 166 |
-
co_occuring = (
|
| 167 |
-
gr.Radio(
|
| 168 |
-
["Yes", "No"], label="Co-occurring Mental and Substance Use Disorders"
|
| 169 |
-
),
|
| 170 |
-
)
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
def conversion():
|
| 174 |
# Convert inputs to model inputs
|
| 175 |
-
if
|
| 176 |
-
|
| 177 |
-
elif
|
| 178 |
-
|
| 179 |
-
elif
|
| 180 |
-
|
| 181 |
-
elif
|
| 182 |
-
|
| 183 |
-
elif
|
| 184 |
-
|
| 185 |
-
elif
|
| 186 |
-
|
| 187 |
-
elif
|
| 188 |
-
|
| 189 |
-
elif
|
| 190 |
-
|
| 191 |
-
elif
|
| 192 |
-
|
| 193 |
-
elif
|
| 194 |
-
|
| 195 |
-
elif
|
| 196 |
-
|
| 197 |
else:
|
| 198 |
-
|
|
|
|
| 199 |
|
| 200 |
-
race = race.index() + 1
|
| 201 |
-
education = education.index() + 1
|
| 202 |
-
marital = marital.index() + 1
|
| 203 |
-
primary_income = primary_income.index() + 1
|
| 204 |
-
health_insurance = health_insurance.index() + 1
|
| 205 |
-
primary_substance = primary_substance.index() + 1
|
| 206 |
-
frequency = frequency(type="index") + 1
|
| 207 |
|
|
|
|
| 208 |
if days_waiting == 0:
|
| 209 |
-
|
| 210 |
-
elif
|
| 211 |
-
|
| 212 |
-
elif
|
| 213 |
-
|
| 214 |
-
elif
|
| 215 |
-
|
| 216 |
else:
|
| 217 |
-
|
|
|
|
|
|
|
|
|
|
| 218 |
|
| 219 |
-
|
| 220 |
-
attendance = attendance.index() + 1
|
| 221 |
-
services = services.index() + 1
|
| 222 |
|
| 223 |
if first_use <= 11:
|
| 224 |
-
|
| 225 |
-
elif
|
| 226 |
-
|
| 227 |
-
elif
|
| 228 |
-
|
| 229 |
-
elif
|
| 230 |
-
|
| 231 |
-
elif
|
| 232 |
-
|
| 233 |
-
elif
|
| 234 |
-
|
| 235 |
else:
|
| 236 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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17:
|
| 14 |
+
age_range = 2
|
| 15 |
+
elif 18 <= age <= 20:
|
| 16 |
+
age_range = 3
|
| 17 |
+
elif 21 <= age <= 24:
|
| 18 |
+
age_range = 4
|
| 19 |
+
elif 25 <= age <= 29:
|
| 20 |
+
age_range = 5
|
| 21 |
+
elif 30 <= age <= 34:
|
| 22 |
+
age_range = 6
|
| 23 |
+
elif 35 <= age <= 39:
|
| 24 |
+
age_range = 7
|
| 25 |
+
elif 40 <= age <= 44:
|
| 26 |
+
age_range = 8
|
| 27 |
+
elif 45 <= age <= 49:
|
| 28 |
+
age_range = 9
|
| 29 |
+
elif 50 <= age <= 54:
|
| 30 |
+
age_range = 10
|
| 31 |
+
elif 55 <= age <= 64:
|
| 32 |
+
age_range = 11
|
| 33 |
else:
|
| 34 |
+
age_range = 12
|
| 35 |
+
return age_range
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
def days_waiting_conversion(days_waiting):
|
| 39 |
if days_waiting == 0:
|
| 40 |
+
days_waiting_range = 0
|
| 41 |
+
elif 1 <= days_waiting <= 7:
|
| 42 |
+
days_waiting_range = 1
|
| 43 |
+
elif 8 <= days_waiting <= 14:
|
| 44 |
+
days_waiting_range = 2
|
| 45 |
+
elif 15 <= days_waiting <= 30:
|
| 46 |
+
days_waiting_range = 3
|
| 47 |
else:
|
| 48 |
+
days_waiting_range = 4
|
| 49 |
+
|
| 50 |
+
return days_waiting_range
|
| 51 |
+
|
| 52 |
|
| 53 |
+
def first_use_conversion(first_use):
|
|
|
|
|
|
|
| 54 |
|
| 55 |
if first_use <= 11:
|
| 56 |
+
first_use_range = 1
|
| 57 |
+
elif 12 <= first_use <= 14:
|
| 58 |
+
first_use_range = 2
|
| 59 |
+
elif 15 <= first_use <= 17:
|
| 60 |
+
first_use_range = 3
|
| 61 |
+
elif 18 <= first_use <= 20:
|
| 62 |
+
first_use_range = 4
|
| 63 |
+
elif 21 <= first_use <= 24:
|
| 64 |
+
first_use_range = 5
|
| 65 |
+
elif 25 <= first_use <= 29:
|
| 66 |
+
first_use_range = 6
|
| 67 |
else:
|
| 68 |
+
first_use_range = 7
|
| 69 |
+
return first_use_range
|
| 70 |
+
|
| 71 |
+
|
| 72 |
+
def load_model():
|
| 73 |
+
# Load the model
|
| 74 |
+
rf = joblib.load("random_forest_model.joblib")
|
| 75 |
+
return rf
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
MODEL = load_model()
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
def call_the_model(
|
| 82 |
+
age,
|
| 83 |
+
arrests,
|
| 84 |
+
substance,
|
| 85 |
+
marital,
|
| 86 |
+
education,
|
| 87 |
+
primary_income,
|
| 88 |
+
days_waiting,
|
| 89 |
+
frequency,
|
| 90 |
+
self_help,
|
| 91 |
+
health_insurance,
|
| 92 |
+
first_use,
|
| 93 |
+
race,
|
| 94 |
+
services,
|
| 95 |
+
co_occuring,
|
| 96 |
+
):
|
| 97 |
+
# create a dataframe
|
| 98 |
+
data = {
|
| 99 |
+
"AGE": [age_conversion(age)],
|
| 100 |
+
"ARRESTS": [arrests],
|
| 101 |
+
"SUB1": [substance + 1],
|
| 102 |
+
"MARSTAT": [marital + 1],
|
| 103 |
+
"EDUC": [education + 1],
|
| 104 |
+
"PRIMINC": [primary_income + 1],
|
| 105 |
+
"DAYWAIT": [days_waiting_conversion(days_waiting)],
|
| 106 |
+
"FREQ1": [frequency + 1],
|
| 107 |
+
"FREQ_ATND_SELF_HELP": [self_help + 1],
|
| 108 |
+
"HLTHINS": [health_insurance + 1],
|
| 109 |
+
"FRSTUSE1": [first_use_conversion(first_use)],
|
| 110 |
+
"RACE": [race + 1],
|
| 111 |
+
"SERVICES": [services + 1],
|
| 112 |
+
"PSYPROB": [co_occuring],
|
| 113 |
+
}
|
| 114 |
+
input_df = pd.DataFrame(data)
|
| 115 |
+
|
| 116 |
+
# print(df)
|
| 117 |
+
# res = MODEL.predict(input_df)
|
| 118 |
+
prob = MODEL.predict_proba(input_df)
|
| 119 |
+
no, yes = prob[0]
|
| 120 |
+
if no > yes:
|
| 121 |
+
return f"The client is {round(no * 100)}% likely to relapse."
|
| 122 |
+
else:
|
| 123 |
+
return f"The client is {round(yes * 100)}% likely to complete the treatment program."
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def inference(age: gr.Number, race: gr.Dropdown):
|
| 127 |
+
pass
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
with gr.Blocks(
|
| 131 |
+
theme=gr.themes.Soft(
|
| 132 |
+
primary_hue=gr.themes.colors.zinc,
|
| 133 |
+
neutral_hue=gr.themes.colors.slate,
|
| 134 |
+
)
|
| 135 |
+
) as demo:
|
| 136 |
+
age = gr.Number(
|
| 137 |
+
label="Age", info="Age of client at admission", minimum=0, maximum=100, value=25
|
| 138 |
+
)
|
| 139 |
+
race = gr.Dropdown(
|
| 140 |
+
[
|
| 141 |
+
"Alaska Native (Aleut, Eskimo, Indian)",
|
| 142 |
+
"American Indian (other than Alaska Native)",
|
| 143 |
+
"Asian or Pacific Islander",
|
| 144 |
+
"Black or African American",
|
| 145 |
+
"White",
|
| 146 |
+
"Asian",
|
| 147 |
+
"Other single race",
|
| 148 |
+
"Two or more races",
|
| 149 |
+
"Native Hawaiian or Other Pacific Islander",
|
| 150 |
+
],
|
| 151 |
+
label="Race",
|
| 152 |
+
type="index",
|
| 153 |
+
value=0,
|
| 154 |
+
)
|
| 155 |
+
education = gr.Dropdown(
|
| 156 |
+
[
|
| 157 |
+
"Less than one school grade, no schooling, nursery school, or kindergarten to Grade 8",
|
| 158 |
+
"Grades 9 to 11",
|
| 159 |
+
"Grade 12 (or GED)",
|
| 160 |
+
"1-3 years of college, university, or vocational school",
|
| 161 |
+
"4 years of college, university, BA/BS, some postgraduate study, or more",
|
| 162 |
+
],
|
| 163 |
+
label="Education",
|
| 164 |
+
info="The highest school grade completed for adults or children not attending school, or current school grade for school-age children (3-17 years old) attending school",
|
| 165 |
+
type="index",
|
| 166 |
+
value=0,
|
| 167 |
+
)
|
| 168 |
+
marital = gr.Dropdown(
|
| 169 |
+
["Never married", "Now married", "Separated", "Divorced, widowed"],
|
| 170 |
+
label="Marital Status",
|
| 171 |
+
info="Client's marital status, compatible with U.S. Census categories",
|
| 172 |
+
type="index",
|
| 173 |
+
value=0,
|
| 174 |
+
)
|
| 175 |
+
primary_income = gr.Dropdown(
|
| 176 |
+
[
|
| 177 |
+
"Wages/salary",
|
| 178 |
+
"Public assistance",
|
| 179 |
+
"Retirement/pension, disability",
|
| 180 |
+
"Other",
|
| 181 |
+
"None",
|
| 182 |
+
],
|
| 183 |
+
label="Primary Source of Income/Support",
|
| 184 |
+
info="Client's principal source of financial support (for children younger than 18 years old, the primary parental source of income/support)",
|
| 185 |
+
type="index",
|
| 186 |
+
value=0,
|
| 187 |
+
)
|
| 188 |
+
health_insurance = gr.Dropdown(
|
| 189 |
+
[
|
| 190 |
+
"Private insurance, Blue Cross/Blue Shield, HMO",
|
| 191 |
+
"Medicaid",
|
| 192 |
+
"Medicare, other (e.g. TRICARE, CHAMPUS)",
|
| 193 |
+
"None",
|
| 194 |
+
],
|
| 195 |
+
label="Health Insurance",
|
| 196 |
+
info="Client's health insurance at admission",
|
| 197 |
+
type="index",
|
| 198 |
+
value=0,
|
| 199 |
+
)
|
| 200 |
+
primary_substance = gr.Dropdown(
|
| 201 |
+
[
|
| 202 |
+
"None",
|
| 203 |
+
"Alcohol",
|
| 204 |
+
"Cocaine/crack",
|
| 205 |
+
"Marijuana/hashish: Includes THC and any other cannabis sativa preparations",
|
| 206 |
+
"Heroin",
|
| 207 |
+
"Non-prescription methadone",
|
| 208 |
+
"Other opiates and synthetics: Includes buprenorphine, butorphanol, codeine, hydrocodone, hydromorphone, meperidine,morphine, opium, oxycodone, pentazocine, propoxyphene, tramadol, and other narcotic analgesics, opiates, or synthetics",
|
| 209 |
+
"PCP: Phencyclidine",
|
| 210 |
+
"Hallucinogens: Includes LSD, DMT, mescaline, peyote, psilocybin, STP, and other hallucinogens",
|
| 211 |
+
"Methamphetamine/speed",
|
| 212 |
+
"Other amphetamines: Includes amphetamines, MDMA, ‘bath salts’, phenmetrazine, and other amines and related drugs",
|
| 213 |
+
"Other stimulants: Includes methylphenidate and any other stimulants",
|
| 214 |
+
"Benzodiazepines: Includes alprazolam, chlordiazepoxide, clonazepam, clorazepate, diazepam, flunitrazepam,flurazepam, halazepam, lorazepam, oxazepam, prazepam, temazepam, triazolam, and other unspecified benzodiazepines",
|
| 215 |
+
"Other tranquilizers: Includes meprobamate, and other non-benzodiazepine tranquilizers",
|
| 216 |
+
"Barbiturates: Includes amobarbital, pentobarbital, phenobarbital, secobarbital, etc.",
|
| 217 |
+
"Other sedatives or hypnotics: Includes chloral hydrate, ethchlorvynol, glutethimide, methaqualone, and othernon-barbiturate sedatives and hypnotics",
|
| 218 |
+
"Inhalants: Includes aerosols; chloroform, ether, nitrous oxide and other anesthetics; gasoline; glue; nitrites; paint thinnerand other solvents; and other inappropriately inhaled products",
|
| 219 |
+
"Over-the-counter medications: Includes aspirin, dextromethorphan and other cough syrups, diphenhydramine and otheranti-histamines, ephedrine, sleep aids, and any other legally obtained, non-prescription medication",
|
| 220 |
+
"Other drugs: Includes diphenylhydantoin/phenytoin, GHB/GBL, ketamine, synthetic cannabinoid 'Spice', carisoprodol(Soma), and other drugs",
|
| 221 |
+
],
|
| 222 |
+
label="Primary Substance Use",
|
| 223 |
+
info="Client's primary substance use at admission",
|
| 224 |
+
type="index",
|
| 225 |
+
value=0,
|
| 226 |
+
)
|
| 227 |
+
first_use = gr.Number(
|
| 228 |
+
label="Age at First Use of Primary Substance",
|
| 229 |
+
info="For alcohol use, this is the age of first intoxication",
|
| 230 |
+
minimum=0,
|
| 231 |
+
maximum=100,
|
| 232 |
+
value=25,
|
| 233 |
+
)
|
| 234 |
+
frequency = gr.Radio(
|
| 235 |
+
["No use in the past month", "Some use", "Daily use"],
|
| 236 |
+
label="Frequency of Primary Substance Use",
|
| 237 |
+
type="index",
|
| 238 |
+
value="No use in the past month",
|
| 239 |
+
)
|
| 240 |
+
days_waiting = gr.Number(
|
| 241 |
+
label="Days Waiting to Enter Substance Use Treatment",
|
| 242 |
+
info="Number of days from the first contact or request for substance use treatment service until the client was admitted and the first clinical substance use treatment service was provided",
|
| 243 |
+
minimum=0,
|
| 244 |
+
)
|
| 245 |
+
arrests = gr.Radio(
|
| 246 |
+
["None", "Once", "Two or more times"],
|
| 247 |
+
label="Arrests",
|
| 248 |
+
info="Number of arrests in the 30 days prior to admission",
|
| 249 |
+
type="index",
|
| 250 |
+
value="None",
|
| 251 |
+
)
|
| 252 |
+
attendance = gr.Dropdown(
|
| 253 |
+
[
|
| 254 |
+
"No attendance",
|
| 255 |
+
"1-3 times in the past month",
|
| 256 |
+
"4-7 times in the past month",
|
| 257 |
+
"8-30 times in the past month",
|
| 258 |
+
"Some attendance, frequency is unknown",
|
| 259 |
+
],
|
| 260 |
+
label="Attendance at Substance Use Self-help Groups in Past 30 Days",
|
| 261 |
+
info="Frequency of attendance at a substance use self-help group in the 30 days prior to the reference date (the date of admission). Includes Alcoholics Anonymous (AA), Narcotics Anonymous (NA), and other self-help/mutual support groups focused on recovery from substance use and dependence",
|
| 262 |
+
type="index",
|
| 263 |
+
value=0,
|
| 264 |
+
)
|
| 265 |
+
services = gr.Dropdown(
|
| 266 |
+
[
|
| 267 |
+
"Detox, 24-hour, hospital inpatient",
|
| 268 |
+
"Detox, 24-hour, free-standing residential",
|
| 269 |
+
"Rehab/residential, hospital (non-detox)",
|
| 270 |
+
"Rehab/residential, short term (30 days or fewer)",
|
| 271 |
+
"Rehab/residential, long term (more than 30 days)",
|
| 272 |
+
"Ambulatory, intensive outpatient",
|
| 273 |
+
"Ambulatory, non-intensive outpatient",
|
| 274 |
+
"Ambulatory, detoxification",
|
| 275 |
+
],
|
| 276 |
+
label="Type of Treatment Service/Setting",
|
| 277 |
+
info="Type of treatment service or treatment setting in which the client is placed at the time of admission or transfer",
|
| 278 |
+
type="index",
|
| 279 |
+
value=0,
|
| 280 |
+
)
|
| 281 |
+
|
| 282 |
+
co_occuring = gr.Checkbox(
|
| 283 |
+
value=False, label="Co-occurring Mental and Substance Use Disorders"
|
| 284 |
+
)
|
| 285 |
+
|
| 286 |
+
submit_btn = gr.Button("SUBMIT", variant="primary", size="large")
|
| 287 |
+
|
| 288 |
+
result = gr.Label(label="Result")
|
| 289 |
+
|
| 290 |
+
submit_btn.click(
|
| 291 |
+
call_the_model,
|
| 292 |
+
[
|
| 293 |
+
age,
|
| 294 |
+
arrests,
|
| 295 |
+
primary_substance,
|
| 296 |
+
marital,
|
| 297 |
+
education,
|
| 298 |
+
primary_income,
|
| 299 |
+
days_waiting,
|
| 300 |
+
frequency,
|
| 301 |
+
attendance,
|
| 302 |
+
health_insurance,
|
| 303 |
+
first_use,
|
| 304 |
+
race,
|
| 305 |
+
services,
|
| 306 |
+
co_occuring,
|
| 307 |
+
],
|
| 308 |
+
[result],
|
| 309 |
+
)
|
| 310 |
|
| 311 |
|
| 312 |
if __name__ == "__main__":
|
random_forest_model.joblib
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:8eaf166a1d1be8c79a0ebbb4b090bff7562e101e2ba39d03889f73769feb45da
|
| 3 |
+
size 513118921
|
requirements.txt
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-i https://pypi.org/simple
|
| 2 |
+
aiofiles==23.2.1; python_version >= '3.7'
|
| 3 |
+
altair==5.2.0; python_version >= '3.8'
|
| 4 |
+
annotated-types==0.6.0; python_version >= '3.8'
|
| 5 |
+
anyio==4.2.0; python_version >= '3.8'
|
| 6 |
+
attrs==23.2.0; python_version >= '3.7'
|
| 7 |
+
certifi==2024.2.2; python_version >= '3.6'
|
| 8 |
+
charset-normalizer==3.3.2; python_full_version >= '3.7.0'
|
| 9 |
+
click==8.1.7; python_version >= '3.7'
|
| 10 |
+
colorama==0.4.6
|
| 11 |
+
contourpy==1.2.0; python_version >= '3.9'
|
| 12 |
+
cycler==0.12.1; python_version >= '3.8'
|
| 13 |
+
exceptiongroup==1.2.0; python_version < '3.11'
|
| 14 |
+
fastapi==0.109.2; python_version >= '3.8'
|
| 15 |
+
ffmpy==0.3.1
|
| 16 |
+
filelock==3.13.1; python_version >= '3.8'
|
| 17 |
+
fonttools==4.48.1; python_version >= '3.8'
|
| 18 |
+
fsspec==2024.2.0; python_version >= '3.8'
|
| 19 |
+
gradio==4.18.0; python_version >= '3.8'
|
| 20 |
+
gradio-client==0.10.0; python_version >= '3.8'
|
| 21 |
+
h11==0.14.0; python_version >= '3.7'
|
| 22 |
+
httpcore==1.0.2; python_version >= '3.8'
|
| 23 |
+
httpx==0.26.0; python_version >= '3.8'
|
| 24 |
+
huggingface-hub==0.20.3; python_full_version >= '3.8.0'
|
| 25 |
+
idna==3.6; python_version >= '3.5'
|
| 26 |
+
importlib-resources==6.1.1; python_version >= '3.8'
|
| 27 |
+
jinja2==3.1.3; python_version >= '3.7'
|
| 28 |
+
joblib==1.3.2; python_version >= '3.7'
|
| 29 |
+
jsonschema==4.21.1; python_version >= '3.8'
|
| 30 |
+
jsonschema-specifications==2023.12.1; python_version >= '3.8'
|
| 31 |
+
kiwisolver==1.4.5; python_version >= '3.7'
|
| 32 |
+
markdown-it-py==3.0.0; python_version >= '3.8'
|
| 33 |
+
markupsafe==2.1.5; python_version >= '3.7'
|
| 34 |
+
matplotlib==3.8.2; python_version >= '3.9'
|
| 35 |
+
mdurl==0.1.2; python_version >= '3.7'
|
| 36 |
+
numpy==1.26.4; python_version < '3.11'
|
| 37 |
+
orjson==3.9.13; python_version >= '3.8'
|
| 38 |
+
packaging==23.2; python_version >= '3.7'
|
| 39 |
+
pandas==2.2.0; python_version >= '3.9'
|
| 40 |
+
pillow==10.2.0; python_version >= '3.8'
|
| 41 |
+
pydantic==2.6.1; python_version >= '3.8'
|
| 42 |
+
pydantic-core==2.16.2; python_version >= '3.8'
|
| 43 |
+
pydub==0.25.1
|
| 44 |
+
pygments==2.17.2; python_version >= '3.7'
|
| 45 |
+
pyparsing==3.1.1; python_full_version >= '3.6.8'
|
| 46 |
+
python-dateutil==2.8.2; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
|
| 47 |
+
python-multipart==0.0.9; python_version >= '3.8'
|
| 48 |
+
pytz==2024.1
|
| 49 |
+
pyyaml==6.0.1; python_version >= '3.6'
|
| 50 |
+
referencing==0.33.0; python_version >= '3.8'
|
| 51 |
+
requests==2.31.0; python_version >= '3.7'
|
| 52 |
+
rich==13.7.0
|
| 53 |
+
rpds-py==0.17.1; python_version >= '3.8'
|
| 54 |
+
ruff==0.2.1; python_version >= '3.7'
|
| 55 |
+
scikit-learn==1.4.0; python_version >= '3.9'
|
| 56 |
+
scipy==1.12.0; python_version >= '3.9'
|
| 57 |
+
semantic-version==2.10.0; python_version >= '2.7'
|
| 58 |
+
shellingham==1.5.4
|
| 59 |
+
six==1.16.0; python_version >= '2.7' and python_version not in '3.0, 3.1, 3.2, 3.3'
|
| 60 |
+
sniffio==1.3.0; python_version >= '3.7'
|
| 61 |
+
starlette==0.36.3; python_version >= '3.8'
|
| 62 |
+
threadpoolctl==3.2.0; python_version >= '3.8'
|
| 63 |
+
tomlkit==0.12.0; python_version >= '3.7'
|
| 64 |
+
toolz==0.12.1; python_version >= '3.7'
|
| 65 |
+
tqdm==4.66.2; python_version >= '3.7'
|
| 66 |
+
typer[all]==0.9.0; python_version >= '3.6'
|
| 67 |
+
typing-extensions==4.9.0; python_version >= '3.8'
|
| 68 |
+
tzdata==2023.4; python_version >= '2'
|
| 69 |
+
urllib3==2.2.0; python_version >= '3.8'
|
| 70 |
+
uvicorn==0.27.1; python_version >= '3.8'
|
| 71 |
+
websockets==11.0.3; python_version >= '3.7'
|
| 72 |
+
zipp==3.17.0; python_version < '3.10'
|