File size: 7,985 Bytes
86508a7 9a01838 9141ffb 9922c0f 9141ffb a1ef568 9922c0f a1ef568 9141ffb 9922c0f a1ef568 9922c0f a1ef568 9922c0f a1ef568 9922c0f a1ef568 9922c0f a1ef568 9922c0f a1ef568 9922c0f a1ef568 9141ffb a1ef568 9922c0f a1ef568 9922c0f 9141ffb b825e19 ecd4ae1 b825e19 ecd4ae1 a1b617f ecd4ae1 9a01838 b825e19 ecd4ae1 a1b617f c4219e5 a1b617f ecd4ae1 a1ef568 86508a7 ecd4ae1 af38814 ecd4ae1 af38814 ecd4ae1 af38814 b825e19 ecd4ae1 b825e19 ecd4ae1 0d8b40f df5eaed 86508a7 68b03ab 39bf2c4 0a0390e 86508a7 0a0390e 86508a7 04010a4 2e2da6d 49a82a3 6389f00 73c527b 6389f00 3df2819 73c527b 6389f00 73c527b 6389f00 73c527b 6389f00 73c527b 6389f00 73c527b aba9f25 b1b1600 73c527b f856ca1 aba9f25 73c527b 6389f00 a1b617f 73c527b 49eb61f 04010a4 77356a3 04010a4 0a0390e 04010a4 2893502 9714d72 1f58cfb 9714d72 9bdc3a8 9714d72 1f58cfb b7548e0 6389f00 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
import pickle
import pandas as pd
import shap
from shap.plots._force_matplotlib import draw_additive_plot
import gradio as gr
import numpy as np
import matplotlib.pyplot as plt
# load the model from disk
loaded_model = pickle.load(open("db_xgb.pkl", 'rb'))
# Setup SHAP
explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.
# Define mapping functions
def map_HighBP(value):
mapping = {'No': 0, 'Yes': 1}
return mapping[value]
def map_HighChol(value):
mapping = {'No': 0, 'Yes': 1}
return mapping[value]
def map_CholCheck(value):
mapping = {'No': 0, 'Yes': 1}
return mapping[value]
def map_Smoker(value):
mapping = {'No': 0, 'Yes': 1}
return mapping[value]
def map_Stroke(value):
mapping = {'No': 0, 'Yes': 1}
return mapping[value]
def map_HeartDiseaseorAttack(value):
mapping = {'No': 0, 'Yes': 1}
return mapping[value]
def map_PhysActivity(value):
mapping = {'No': 0, 'Yes': 1}
return mapping[value]
def map_Fruits(value):
mapping = {'No': 0, 'Yes': 1}
return mapping[value]
def map_Veggies(value):
mapping = {'No': 0, 'Yes': 1}
return mapping[value]
def map_HvyAlcoholConsump(value):
mapping = {'No': 0, 'Yes': 1}
return mapping[value]
def map_AnyHealthcare(value):
mapping = {'No': 0, 'Yes': 1}
return mapping[value]
def map_NoDocbcCost(value):
mapping = {'No': 0, 'Yes': 1}
return mapping[value]
def map_DiffWalk(value):
mapping = {'No': 0, 'Yes': 1}
return mapping[value]
def map_Sex(value):
mapping = {'Female': 0, 'Male': 1}
return mapping[value]
def map_Education(value):
mapping = {
"Never attended school": 1,
"Grades 1-8": 2,
"Grades 9-11": 3,
"Grade 12 or GED": 4,
"College 1-3 years": 5,
"College 4+ years": 6
}
return mapping[value]
def map_Income(value):
mapping = {
"> $10,000": 1,
"> $20,000": 2,
"> $25,000": 3,
"> $30,000": 4,
"> $35,000": 5,
"> $50,000": 6,
"> $60,000": 7,
"< $75,000": 8
}
return mapping[value]
# Create the main function for server
def main_func(HighBP, HighChol, CholCheck, BMI, Smoker, Stroke, HeartDiseaseorAttack, PhysActivity, Fruits, Veggies, HvyAlcoholConsump, AnyHealthcare, NoDocbcCost, GenHlth, MentHlth, PhysHlth, DiffWalk, Sex, Age, Education, Income):
new_row = pd.DataFrame.from_dict({
'HighBP': map_HighBP(HighBP),
'HighChol': map_HighChol(HighChol),
'CholCheck': map_CholCheck(CholCheck),
'BMI': BMI,
'Smoker': map_Smoker(Smoker),
'Stroke': map_Stroke(Stroke),
'HeartDiseaseorAttack': map_HeartDiseaseorAttack(HeartDiseaseorAttack),
'PhysActivity': map_PhysActivity(PhysActivity),
'Fruits': map_Fruits(Fruits),
'Veggies': map_Veggies(Veggies),
'HvyAlcoholConsump': map_HvyAlcoholConsump(HvyAlcoholConsump),
'AnyHealthcare': map_AnyHealthcare(AnyHealthcare),
'NoDocbcCost': map_NoDocbcCost(NoDocbcCost),
'GenHlth': GenHlth,
'MentHlth': MentHlth,
'PhysHlth': PhysHlth,
'DiffWalk': map_DiffWalk(DiffWalk),
'Sex': map_Sex(Sex),
'Age': Age,
'Education': map_Education(Education),
'Income': map_Income(Income)
}, orient='index').transpose()
prob = loaded_model.predict_proba(new_row)
shap_values = explainer(new_row)
plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
plt.tight_layout()
local_plot = plt.gcf()
plt.close()
return {"Low Chance of Diabetes": float(prob[0][0]), "High Chance of Diabetes": 1-float(prob[0][0])}, local_plot
# Create the UI
title = "**Diabetes Predictor Application** πͺ"
description1 = """This app takes information from subjects and predicts their diabetes likelihood. Do not use for medical diagnosis."""
description2 = """
To use the app, click on one of the examples, or adjust the values of the factors, and click on Analyze. π€
"""
with gr.Blocks(title=title) as demo:
gr.Markdown(f"## {title}")
gr.Markdown(description1)
gr.Markdown("""---""")
gr.Markdown(description2)
gr.Markdown("""---""")
with gr.Row():
CholCheck = gr.Radio(label="Did you check your cholestorol in the past 5 years?", choices=["No", "Yes"])
HighChol = gr.Radio(label="Do you have high cholesterol?", choices=["No", "Yes"])
with gr.Row():
DiffWalk = gr.Radio(label="Do you have serious difficulty walking or climbing stairs?", choices=["No", "Yes"])
BMI = gr.Number(label="BMI")
with gr.Row():
Smoker = gr.Radio(label="Are you a smoker?", choices=["No", "Yes"])
HvyAlcoholConsump = gr.Radio(label="Do you drink often?", choices=["No", "Yes"])
with gr.Row():
Stroke = gr.Radio(label="Have you had a stroke?", choices=["No", "Yes"])
HighBP = gr.Radio(label="Do you have high blood pressure?", choices=["No", "Yes"])
HeartDiseaseorAttack = gr.Radio(label="Do you have coronary heart disease or myocardial infarction?", choices=["No", "Yes"])
with gr.Row():
PhysActivity = gr.Radio(label="Did you partake in physical activity in the past 30 days?", choices=["No", "Yes"])
Fruits = gr.Radio(label="Do you consume fruit 1 or more times per day?", choices=["No", "Yes"])
Veggies = gr.Radio(label="Do you consume vegetables 1 or more times per day?", choices=["No", "Yes"])
with gr.Row():
AnyHealthcare = gr.Radio(label="Do you have any kind of health care coverage?", choices=["No", "Yes"])
NoDocbcCost = gr.Radio(label="Was there a time in the past 12 months when you needed to see a doctor but could not because of cost?", choices=["No", "Yes"])
with gr.Row():
MentHlth = gr.Number(label="How many days in the past 30 days did you have poor mental health?")
PhysHlth = gr.Number(label="How many days in the past 30 days did you have poor physical health?")
GenHlth = gr.Slider(label="In general, rank your overall health on a scale: 1(excellent)-5(poor)", minimum=1, maximum=5)
with gr.Row():
Sex = gr.Dropdown(label="Sex", choices=["Female", "Male"])
Age = gr.Number(label="Age")
with gr.Row():
Education = gr.Dropdown(label="Education Level", choices=["Never attended school", "Grades 1-8", "Grades 9-11", "Grade 12 or GED", "College 1-3 years", "College 4+ years"])
Income = gr.Dropdown(label="Income Level", choices=["> $10,000", "> $20,000", "> $25,000", "> $30,000", "> $35,000","> $50,000","> $60,000", "< $75,000"])
with gr.Column(visible=True) as output_col:
label = gr.Label(label = "Predicted Label")
local_plot = gr.Plot(label = 'Shap:')
submit_btn = gr.Button("Analyze")
submit_btn.click(
main_func,
[HighBP, HighChol, CholCheck, BMI, Smoker, Stroke, HeartDiseaseorAttack, PhysActivity, Fruits, Veggies,
HvyAlcoholConsump, AnyHealthcare, NoDocbcCost, GenHlth, MentHlth, PhysHlth, DiffWalk, Sex, Age, Education, Income],
[label,local_plot],api_name="Diabetes Predictor"
)
gr.Markdown("### Click on any of the examples below to see how it works:")
gr.Examples(
[["No", "No", "Yes", 22, "No", "No", "No", "Yes", "Yes", "Yes", "No", "No", "Yes", 3, 25, 23, "No", "Female", 22, "Grade 12 or GED", "> $35,000"],
["Yes", "Yes", "Yes", 30, "Yes", "Yes", "Yes", "No", "No", "No", "Yes", "Yes", "No", 2, 20, 23, "No", "Male", 21, "College 4+ years", "< $75,000"]],
[HighBP, CholCheck, HighChol, BMI, Smoker, Stroke, HeartDiseaseorAttack, PhysActivity, Fruits, Veggies, HvyAlcoholConsump,
AnyHealthcare, NoDocbcCost, GenHlth, MentHlth, PhysHlth, DiffWalk, Sex, Age, Education, Income],
[label, local_plot],
main_func,
cache_examples=True
)
demo.launch() |