fmegahed commited on
Commit
6e991d4
·
1 Parent(s): d2168d5

Create app.py

Browse files

Version 1.0 of the app

Files changed (1) hide show
  1. app.py +127 -0
app.py ADDED
@@ -0,0 +1,127 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ import jinja2
3
+
4
+ from pycaret.classification import *
5
+ import imblearn as im
6
+ import sklearn
7
+
8
+ import gradio as gr
9
+ import numpy as np
10
+
11
+ def predict(age, female, race, elective, aweekend, zipinc_qrtl, hosp_region, hosp_division, hosp_locteach,
12
+ hosp_bedsize, h_contrl, pay, anemia, atrial_fibrillation,
13
+ cancer, cardiac_arrhythmias, carotid_artery_disease,
14
+ chronic_kidney_disease, chronic_pulmonary_disease, coagulopathy,
15
+ depression, diabetes_mellitus, drug_abuse, dyslipidemia, endocarditis,
16
+ family_history, fluid_and_electrolyte_disorder, heart_failure,
17
+ hypertension, known_cad, liver_disease, obesity, peripheral_vascular_disease,
18
+ prior_cabg, prior_icd, prior_mi, prior_pci, prior_ppm, prior_tia_stroke,
19
+ pulmonary_circulation_disorder, smoker, valvular_disease, weight_loss,
20
+ endovascular_tavr, transapical_tavr):
21
+
22
+ model = load_model('final_model')
23
+
24
+ df = pd.DataFrame.from_dict({
25
+ 'age': [age], 'female': [female], 'race': [race], 'elective': elective,
26
+ 'aweekend': [aweekend], 'zipinc_qrtl': [zipinc_qrtl],
27
+ 'hosp_region': [hosp_region], 'hosp_division': [hosp_division],
28
+ 'hosp_locteach': [hosp_locteach], 'hosp_bedsize': [hosp_bedsize],
29
+ 'h_contrl': [h_contrl], 'pay': [pay], 'anemia': [anemia],
30
+ 'atrial_fibrillation': [atrial_fibrillation], 'cancer': [cancer],
31
+ 'cardiac_arrhythmias': [cardiac_arrhythmias],
32
+ 'carotid_artery_disease': [carotid_artery_disease],
33
+ 'chronic_kidney_disease': [chronic_kidney_disease],
34
+ 'chronic_pulmonary_disease': [chronic_pulmonary_disease],
35
+ 'coagulopathy': [coagulopathy], 'depression': [depression],
36
+ 'diabetes_mellitus': [diabetes_mellitus], 'drug_abuse': [drug_abuse],
37
+ 'dyslipidemia': [dyslipidemia], 'endocarditis': [endocarditis],
38
+ 'family_history': [family_history], 'fluid_and_electrolyte_disorder': [fluid_and_electrolyte_disorder],
39
+ 'heart_failure': [heart_failure], 'hypertension': [hypertension],
40
+ 'known_cad': [known_cad], 'liver_disease': [liver_disease],
41
+ 'obesity': [obesity], 'peripheral_vascular_disease': [peripheral_vascular_disease],
42
+ 'prior_cabg': [prior_cabg], 'prior_icd': [prior_icd], 'prior_mi': [prior_mi],
43
+ 'prior_pci': [prior_pci], 'prior_ppm': [prior_ppm], 'prior_tia_stroke': [prior_tia_stroke],
44
+ 'pulmonary_circulation_disorder': [pulmonary_circulation_disorder],
45
+ 'smoker': [smoker], 'valvular_disease': [valvular_disease],
46
+ 'weight_loss': [weight_loss], 'endovascular_tavr': [endovascular_tavr],
47
+ 'transapical_tavr': [transapical_tavr]
48
+ })
49
+
50
+ df.loc[:, df.dtypes == 'object'] =\
51
+ df.select_dtypes(['object'])\
52
+ .apply(lambda x: x.astype('category'))
53
+
54
+ # converting ordinal column to ordinal
55
+ df.zipinc_qrtl = df.zipinc_qrtl.astype(ordinal_cat)
56
+
57
+ pred = predict_model(model, df, raw_score=True)
58
+
59
+ return {'Death %': round(100*pred['Score_Yes'][0], 2),
60
+ 'Survival %': round(100*pred['Score_No'][0], 2),
61
+ 'Predicting Death Outcome:': pred['Label'][0]}
62
+
63
+ # Defining the containers for each input
64
+ age = gr.inputs.Slider(minimum=0, maximum=100, default=60, label="Age")
65
+ female = gr.inputs.Dropdown(choices=["Female", "Male"],label = 'Sex')
66
+ race = gr.inputs.Dropdown(choices=['Asian or Pacific Islander', 'Black', 'Hispanic', 'Native American', 'White', 'Other'], label = 'Race')
67
+ elective = gr.inputs.Radio(choices=['Elective', 'NonElective'], label = 'Elective')
68
+ aweekend = gr.inputs.Radio(choices=["No", "Yes"], label = 'Weekend')
69
+ zipinc_qrtl = gr.inputs.Radio(choices=['FirstQ', 'SecondQ', 'ThirdQ', 'FourthQ'], label = 'Zip Income Quartile')
70
+ hosp_region = gr.inputs.Radio(choices=['Midwest', 'Northeast', 'South', 'West'], label = 'Hospital Region')
71
+ hosp_division = gr.inputs.Radio(choices=['New England', 'Middle Atlantic', 'East North Central', 'West North Central', 'South Atlantic', 'East South Central', 'West South Central', 'Mountain', 'Pacific'], label = 'Hospital Division')
72
+ hosp_locteach = gr.inputs.Radio(choices=['Urban teaching', 'Urban nonteaching', 'Rural'], label= 'Hospital Location/Teaching')
73
+ hosp_bedsize = gr.inputs.Radio(choices=['Small', 'Medium', 'Large'], label= 'Hospital Bedsize')
74
+ h_contrl = gr.inputs.Radio(choices= ['Government_nonfederal', 'Private_invest_own', 'Private_not_profit'], label = 'Hospital Control')
75
+ pay = gr.inputs.Dropdown(choices= ['Private insurance', 'Medicare', 'Medicaid', 'Self-pay', 'No charge', 'Other'], label = 'Payee')
76
+ anemia = gr.inputs.Radio(choices=["No", "Yes"], label = 'Anemia')
77
+ atrial_fibrillation = gr.inputs.Radio(choices=["No", "Yes"], label = 'Atrial Fibrillation')
78
+ cancer = gr.inputs.Radio(choices=["No", "Yes"], label = 'Cancer')
79
+ cardiac_arrhythmias = gr.inputs.Radio(choices=["No", "Yes"], label = 'Cardiac Arrhythmias')
80
+ carotid_artery_disease = gr.inputs.Radio(choices=["No", "Yes"], label = 'Carotid Artery Disease')
81
+ chronic_kidney_disease = gr.inputs.Radio(choices=["No", "Yes"], label = 'Chronic Kidney Disease')
82
+ chronic_pulmonary_disease = gr.inputs.Radio(choices=["No", "Yes"], label = 'Chronic Pulmonary Disease')
83
+ coagulopathy = gr.inputs.Radio(choices=["No", "Yes"], label = 'Coagulopathy')
84
+ depression = gr.inputs.Radio(choices=["No", "Yes"], label = 'Depression')
85
+ diabetes_mellitus = gr.inputs.Radio(choices=["No", "Yes"], label = 'Diabetes Mellitus')
86
+ drug_abuse = gr.inputs.Radio(choices=["No", "Yes"], label = 'Drug Abuse')
87
+ dyslipidemia = gr.inputs.Radio(choices=["No", "Yes"], label = 'Dyslipidemia')
88
+ endocarditis = gr.inputs.Radio(choices=["No", "Yes"], label = 'Endocarditis')
89
+ family_history = gr.inputs.Radio(choices=["No", "Yes"], label = 'Family History')
90
+ fluid_and_electrolyte_disorder = gr.inputs.Radio(choices=["No", "Yes"], label = 'Fluid and Electrolyte Disorder')
91
+ heart_failure = gr.inputs.Radio(choices=["No", "Yes"], label = 'Heart Failure')
92
+ hypertension = gr.inputs.Radio(choices=["No", "Yes"], label = 'Hypertension')
93
+ known_cad = gr.inputs.Radio(choices=["No", "Yes"], label = 'Known CAD')
94
+ liver_disease = gr.inputs.Radio(choices=["No", "Yes"], label = 'Liver Disease')
95
+ obesity = gr.inputs.Radio(choices=["No", "Yes"], label = 'Obesity')
96
+ peripheral_vascular_disease = gr.inputs.Radio(choices=["No", "Yes"], label = 'Peripheral Vascular Disease')
97
+ prior_cabg = gr.inputs.Radio(choices=["No", "Yes"], label = 'Prior CABG')
98
+ prior_icd = gr.inputs.Radio(choices=["No", "Yes"], label = 'Prior ICD')
99
+ prior_mi = gr.inputs.Radio(choices=["No", "Yes"], label = 'Prior MI')
100
+ prior_pci = gr.inputs.Radio(choices=["No", "Yes"], label = 'Prior PCI')
101
+ prior_ppm = gr.inputs.Radio(choices=["No", "Yes"], label = 'Prior PPM')
102
+ prior_tia_stroke = gr.inputs.Radio(choices=["No", "Yes"], label = 'Prior TIA Stroke')
103
+ pulmonary_circulation_disorder = gr.inputs.Radio(choices=["No", "Yes"], label = 'Pulmonary Circulation Disorder')
104
+ smoker = gr.inputs.Radio(choices=["No", "Yes"], label = 'Smoker')
105
+ valvular_disease = gr.inputs.Radio(choices=["No", "Yes"], label = 'Valvular Disease')
106
+ weight_loss = gr.inputs.Radio(choices=["No", "Yes"], label = 'Weight Loss')
107
+ endovascular_tavr = gr.inputs.Radio(choices=["No", "Yes"], label = 'Endovascular TAVR')
108
+ transapical_tavr = gr.inputs.Radio(choices=["No", "Yes"], label = 'Transapical TAVR', default= 'Yes')
109
+
110
+
111
+ # Defining and launching the interface
112
+ gr.Interface(predict, [age, female, race, elective, aweekend, zipinc_qrtl, hosp_region, hosp_division, hosp_locteach,
113
+ hosp_bedsize, h_contrl, pay, anemia, atrial_fibrillation,
114
+ cancer, cardiac_arrhythmias, carotid_artery_disease,
115
+ chronic_kidney_disease, chronic_pulmonary_disease, coagulopathy,
116
+ depression, diabetes_mellitus, drug_abuse, dyslipidemia, endocarditis,
117
+ family_history, fluid_and_electrolyte_disorder, heart_failure,
118
+ hypertension, known_cad, liver_disease, obesity, peripheral_vascular_disease,
119
+ prior_cabg, prior_icd, prior_mi, prior_pci, prior_ppm, prior_tia_stroke,
120
+ pulmonary_circulation_disorder, smoker, valvular_disease, weight_loss,
121
+ endovascular_tavr, transapical_tavr],
122
+ outputs = gr.Textbox(label="Predicted Outcomes for this Patient", lines=4),
123
+ live=True,
124
+ title = "Predicting In-Hospital Mortality After TAVR Using Preoperative Variables and Penalized Logistic Regression",
125
+ description = "The app below utilizes the finalized logistic regression model with an l2 penalty based on the manuscript by Alhwiti et al. The manuscript will be submitted to JACC: Cardiovascular Interventions. The data used for model building is all TAVR procedures between 2012 and 2019 as reported in the HCUP NIS database. <br><br> The purpose of the app is to provide evidence-based clinical support for interventional cardiology. <br> <br> For instruction on how to use the app and the encoding required for the variables, please see <b>XYZ: insert website link here</b>.",
126
+ examples = ex_data,
127
+ css = 'https://bootswatch.com/5/journal/bootstrap.css').launch(debug = False);