Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,3 @@
|
|
1 |
-
import pickle
|
2 |
-
import pandas as pd
|
3 |
-
import shap
|
4 |
-
from shap.plots._force_matplotlib import draw_additive_plot
|
5 |
-
import gradio as gr
|
6 |
-
import numpy as np
|
7 |
-
import matplotlib.pyplot as plt
|
8 |
-
|
9 |
-
# load the model from disk
|
10 |
-
loaded_model = pickle.load(open("db_xgb.pkl", 'rb'))
|
11 |
-
|
12 |
-
# Setup SHAP
|
13 |
-
explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.
|
14 |
-
|
15 |
-
# Create the main function for server
|
16 |
-
def main_func(HighBP, HighChol, CholCheck, BMI, Smoker, Stroke, HeartDiseaseorAttack, PhysActivity, Fruits, Veggies, HvyAlcoholConsump, AnyHealthcare, NoDocbcCost, GenHlth, MentHlth, PhysHlth, DiffWalk, Sex, Age, Education, Income):
|
17 |
-
new_row = pd.DataFrame.from_dict({'HighBP': HighBP, 'HighChol': HighChol, 'CholCheck': CholCheck, 'BMI': BMI, 'Smoker': Smoker, 'Stroke': Stroke, 'HeartDiseaseorAttack': HeartDiseaseorAttack, 'PhysActivity':PhysActivity, 'Fruits':Fruits, 'Veggies':Veggies, 'HvyAlcoholConsump': HvyAlcoholConsump, 'AnyHealthcare': AnyHealthcare, 'NoDocbcCost': NoDocbcCost, 'GenHlth': GenHlth, 'MentHlth': MentHlth, 'PhysHlth': PhysHlth, 'DiffWalk': DiffWalk, 'Sex': Sex, 'Age': Age, 'Education': Education, 'Income': Income},
|
18 |
-
orient = 'index').transpose()
|
19 |
-
|
20 |
-
prob = loaded_model.predict_proba(new_row)
|
21 |
-
|
22 |
-
shap_values = explainer(new_row)
|
23 |
-
# plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
|
24 |
-
# plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
|
25 |
-
plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
|
26 |
-
|
27 |
-
plt.tight_layout()
|
28 |
-
local_plot = plt.gcf()
|
29 |
-
plt.close()
|
30 |
-
|
31 |
-
return {"Low Chance": float(prob[0][0]), "High Chance": 1-float(prob[0][0])}, local_plot
|
32 |
-
|
33 |
-
# Create the UI
|
34 |
-
title = "**Diabetes Predictor & Interpreter** 🪐"
|
35 |
-
description1 = """This app takes info from subjects and predicts their diabetes likelihood. Do not use for medical diagnosis."""
|
36 |
-
|
37 |
-
description2 = """
|
38 |
-
To use the app, click on one of the examples, or adjust the values of the factors, and click on Analyze. 🤞
|
39 |
-
"""
|
40 |
-
|
41 |
with gr.Blocks(title=title) as demo:
|
42 |
gr.Markdown(f"## {title}")
|
43 |
gr.Markdown(description1)
|
@@ -45,10 +5,10 @@ with gr.Blocks(title=title) as demo:
|
|
45 |
gr.Markdown(description2)
|
46 |
gr.Markdown("""---""")
|
47 |
|
48 |
-
input_block_left
|
49 |
HighBP = gr.Radio(label="Do you have high Blood Pressure?", choices=["No", "Yes"], default="Yes", description="0 = no high BP, 1 = high BP"),
|
50 |
HighChol = gr.Radio(label="Do you have high Cholesterol?", choices=["No", "Yes"], default="Yes", description="0 = no high cholesterol, 1 = high cholesterol"),
|
51 |
-
CholCheck = gr.Radio(label="Did you have a
|
52 |
BMI = gr.Number(label="BMI", minimum=0, maximum=98, default=1),
|
53 |
Smoker = gr.Radio(label="Are you a smoker?", choices=["No", "Yes"], default="Yes", description="No = never smoked, Yes = smoked at least 100 cigarettes"),
|
54 |
Stroke = gr.Radio(label="Have you had a stroke?", choices=["No", "Yes"], default="Yes", description="No = never had a stroke, Yes = had a stroke"),
|
@@ -60,8 +20,8 @@ with gr.Blocks(title=title) as demo:
|
|
60 |
AnyHealthcare = gr.Radio(label="Do you have any kind of health care coverage? (e.g., health insurance, prepaid plans such as HMO)", choices=["No", "Yes"], default="Yes", description="No = no coverage, Yes = coverage"),
|
61 |
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"], default="Yes", description="No = no barrier, Yes = cost barrier")
|
62 |
)
|
63 |
-
|
64 |
-
input_block_right
|
65 |
GenHlth = gr.Slider(label="In general, rank your health on a scale: 1(excellent)-5(poor)", minimum=1, maximum=5, default=1, step=1, description="1 = excellent, 5 = poor"),
|
66 |
MentHlth = gr.Number(label="Poor Mental Health in the Past 30 Days", minimum=0, maximum=30, default=1, description="Days not good out of last 30"),
|
67 |
PhysHlth = gr.Number(label="Poor Physical Health in the Past 30 Days", minimum=0, maximum=30, default=1, description="Days not good out of last 30"),
|
@@ -72,9 +32,9 @@ with gr.Blocks(title=title) as demo:
|
|
72 |
Income = gr.Dropdown(label="Income Level", choices=["< $10,000", "$10,000 - $24,999", "$25,000 - $49,999", "$50,000 - $74,999", "$75,000 or more"], default="< $10,000", description="Income level")
|
73 |
)
|
74 |
|
75 |
-
input_block
|
76 |
|
77 |
-
output_block
|
78 |
gr.Label(label="Predicted Label"),
|
79 |
gr.Plot(label="SHAP Plot")
|
80 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
with gr.Blocks(title=title) as demo:
|
2 |
gr.Markdown(f"## {title}")
|
3 |
gr.Markdown(description1)
|
|
|
5 |
gr.Markdown(description2)
|
6 |
gr.Markdown("""---""")
|
7 |
|
8 |
+
input_block_left = gr.Column(
|
9 |
HighBP = gr.Radio(label="Do you have high Blood Pressure?", choices=["No", "Yes"], default="Yes", description="0 = no high BP, 1 = high BP"),
|
10 |
HighChol = gr.Radio(label="Do you have high Cholesterol?", choices=["No", "Yes"], default="Yes", description="0 = no high cholesterol, 1 = high cholesterol"),
|
11 |
+
CholCheck = gr.Radio(label="Did you have a Cholesterol check in past 5 years?", choices=["No", "Yes"], default="Yes", description="No = not checked in 5 years, Yes = checked in 5 years"),
|
12 |
BMI = gr.Number(label="BMI", minimum=0, maximum=98, default=1),
|
13 |
Smoker = gr.Radio(label="Are you a smoker?", choices=["No", "Yes"], default="Yes", description="No = never smoked, Yes = smoked at least 100 cigarettes"),
|
14 |
Stroke = gr.Radio(label="Have you had a stroke?", choices=["No", "Yes"], default="Yes", description="No = never had a stroke, Yes = had a stroke"),
|
|
|
20 |
AnyHealthcare = gr.Radio(label="Do you have any kind of health care coverage? (e.g., health insurance, prepaid plans such as HMO)", choices=["No", "Yes"], default="Yes", description="No = no coverage, Yes = coverage"),
|
21 |
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"], default="Yes", description="No = no barrier, Yes = cost barrier")
|
22 |
)
|
23 |
+
|
24 |
+
input_block_right = gr.Column(
|
25 |
GenHlth = gr.Slider(label="In general, rank your health on a scale: 1(excellent)-5(poor)", minimum=1, maximum=5, default=1, step=1, description="1 = excellent, 5 = poor"),
|
26 |
MentHlth = gr.Number(label="Poor Mental Health in the Past 30 Days", minimum=0, maximum=30, default=1, description="Days not good out of last 30"),
|
27 |
PhysHlth = gr.Number(label="Poor Physical Health in the Past 30 Days", minimum=0, maximum=30, default=1, description="Days not good out of last 30"),
|
|
|
32 |
Income = gr.Dropdown(label="Income Level", choices=["< $10,000", "$10,000 - $24,999", "$25,000 - $49,999", "$50,000 - $74,999", "$75,000 or more"], default="< $10,000", description="Income level")
|
33 |
)
|
34 |
|
35 |
+
input_block = gr.Row([input_block_left, input_block_right])
|
36 |
|
37 |
+
output_block = gr.Column(
|
38 |
gr.Label(label="Predicted Label"),
|
39 |
gr.Plot(label="SHAP Plot")
|
40 |
)
|