|
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 |
|
|
|
|
|
loaded_model = pickle.load(open("db_xgb.pkl", 'rb')) |
|
|
|
|
|
explainer = shap.Explainer(loaded_model) |
|
|
|
|
|
def main_func(Diabetes, 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({'Diabetes': Diabetes, '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}, |
|
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": float(prob[0][0]), "High Chance": 1-float(prob[0][0])}, local_plot |
|
|
|
|
|
title = "**Diabetes Predictor & Interpreter** πͺ" |
|
description1 = """This app takes info 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("""---""") |
|
|
|
|
|
Diabetes = gr.Slider(label="Diabetes Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
HighBP = gr.Slider(label="BP Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
HighChol = gr.Slider(label="Cholesterol Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
CholCheck = gr.Slider(label="CholCheck Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
BMI = gr.Number(label="BMI Score", minimum = 0, maximum = 98, value = 1) |
|
Smoker = gr.Slider(label="Smoker Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
Stroke = gr.Slider(label="Stroke Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
HeartDiseaseorAttack = gr.Slider(label="HeartDiseaseorAttack Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
PhysActivity = gr.Slider(label="Physical Activity Score", minimum = 0, maximum = 1, value = 1, step = 1)) |
|
Fruits = gr.Slider(label="Fruits Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
Veggies = gr.Slider(label="Veggies Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
HvyAlcoholConsump = gr.Slider(label="Alcohol Consumption Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
AnyHealthcare = gr.Slider(label="AnyHealthcare Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
NoDocbcCost = gr.Slider(label="NoDocbcCost Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
GenHlth = gr.Slider(label="GenHlth Score", minimum = 1, maximum = 5, value = 1, step = 1) |
|
PhysHealth = gr.Number(label="PhysHealth Score", minimum = 0, maximum = 30, value=1) |
|
MentHlth = gr.Number(label="MentHlth Score", minimum = 0, maximum = 30, value = 1, step = 1) |
|
DiffWalk = gr.Number(label="DiffWalk Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
Sex = gr.Slider(label="sex Score", minimum = 0, maximum = 1, value = 1, step = 1) |
|
Age = gr.Number(label="age Score", minimum = 1, maximum = 100, value = 1) |
|
Education = gr.Slider(label="Education Score", minimum = 1, maximum = 6, value = 1, step = 1) |
|
Income = gr.Slider(label="Income Score", minimum = 1, maximum = 8, value = 1, step = 1) |
|
|
|
submit_btn = gr.Button("Analyze") |
|
|
|
with gr.Column(visible=True) as output_col: |
|
label = gr.Label(label = "Predicted Label") |
|
local_plot = gr.Plot(label = 'Shap:') |
|
|
|
submit_btn.click( |
|
main_func, |
|
[Diabetes, 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([[0,0,1,0,22,0,0,0,1,1,1,0,0,1,3,25,23,1,1,21,5,3], [1,1,1,1,30,1,1,1,0,0,0,1,1,0,2,20,23,0,0,21,3,2]], [Diabetes, HighBP, HighChol, CholCheck, 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() |