tby4rr commited on
Commit
4bc5e29
·
verified ·
1 Parent(s): b77aa4c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
10
+
11
+ # load the model from disk
12
+ loaded_model = pickle.load(open("heart_xgb.pkl", 'rb'))
13
+
14
+ # Setup SHAP
15
+ explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.
16
+
17
+ # Create the main function for server
18
+ def main_func(ID, Diabetes_binary, HighBP, HighCol, CholCheck, BMI, Smoker, Stroke, HeartDiseaseorAttack, PhysActivity, Fruits, Veggies, HvyAlcoholConsump, AnyHealthcare, NoDocbcCost, GenHlth, MentHlth, PhysHealth, DiffWalk, Sex, Age, Education, Income):
19
+ new_row = pd.DataFrame.from_dict({'ID':ID,'Diabetes_binary':Diabetes_binary,
20
+ 'HighBP':HighBP,'HighCol':HighCol,'CholCheck':CholCheck,
21
+ 'BMI':BMI, 'Smoker':Smoker,'Stroke':Stroke,'HeartDiseaseorAttack':HeartDiseaseorAttack,
22
+ 'PhysActivity':PhysActivity,'Fruits':Fruits,'Veggies':Veggies,'HvyAlcoholConsump':HvyAlcoholConsump, 'AnyHealthcare':AnyHealthcare, 'NoDocbcCost':NoDocbcCost, 'GenHlth':GenHlth, 'MenHlth': MenHlth, 'PhysHealth':PhysHealth, 'DiffWalk':DiffWalk, 'Sex':Sex, 'Age':Age, 'Education':Education, 'Income':Income},
23
+ orient = 'index').transpose()
24
+
25
+ prob = loaded_model.predict_proba(new_row)
26
+
27
+ shap_values = explainer(new_row)
28
+ # plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
29
+ # plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
30
+ plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
31
+
32
+ plt.tight_layout()
33
+ local_plot = plt.gcf()
34
+ plt.close()
35
+
36
+ return {"Low Chance": float(prob[0][0]), "High Chance": 1-float(prob[0][0])}, local_plot
37
+
38
+ # Create the UI
39
+ title = "**Heart Attack Predictor & Interpreter** 🪐"
40
+ description1 = """This app takes info from subjects and predicts their heart attack likelihood. Do not use for medical diagnosis."""
41
+
42
+ description2 = """
43
+ To use the app, click on one of the examples, or adjust the values of the factors, and click on Analyze. 🤞
44
+ """
45
+
46
+ with gr.Blocks(title=title) as demo:
47
+ gr.Markdown(f"## {title}")
48
+ gr.Markdown(description1)
49
+ gr.Markdown("""---""")
50
+ gr.Markdown(description2)
51
+ gr.Markdown("""---""")
52
+
53
+ age = gr.Number(label="age Score", value=40)
54
+ sex = gr.Slider(label="sex Score", minimum=0, maximum=1, value=1, step=1)
55
+ cp = gr.Slider(label="cp Score", minimum=1, maximum=5, value=4, step=1)
56
+ trtbps = gr.Slider(label="trtbps Score", minimum=1, maximum=5, value=4, step=1)
57
+ chol = gr.Slider(label="chol Score", minimum=1, maximum=5, value=4, step=1)
58
+ fbs = gr.Slider(label="fbs Score", minimum=1, maximum=5, value=4, step=1)
59
+
60
+ restecg = gr.Slider(label="restecg Score", minimum=1, maximum=5, value=4, step=1)
61
+ thalachh = gr.Slider(label="thalachh Score", minimum=1, maximum=5, value=4, step=1)
62
+
63
+ exng = gr.Slider(label="exng Score", minimum=1, maximum=5, value=4, step=1)
64
+ oldpeak = gr.Slider(label="oldpeak Score", minimum=1, maximum=5, value=4, step=1)
65
+ slp = gr.Slider(label="slp Score", minimum=1, maximum=5, value=4, step=1)
66
+ caa = gr.Slider(label="caa Score", minimum=1, maximum=5, value=4, step=1)
67
+ thall = gr.Slider(label="thall Score", minimum=1, maximum=5, value=4, step=1)
68
+
69
+ submit_btn = gr.Button("Analyze")
70
+
71
+ with gr.Column(visible=True) as output_col:
72
+ label = gr.Label(label = "Predicted Label")
73
+ local_plot = gr.Plot(label = 'Shap:')
74
+
75
+ submit_btn.click(
76
+ main_func,
77
+ [ID, Diabetes_binary, HighBP, HighCol, CholCheck, BMI, Smoker, Stroke, HeartDiseaseorAttack, PhysActivity, Fruits, Veggies, HvyAlcoholConsump, AnyHealthcare, NoDocbcCost, GenHlth, MentHlth, PhysHealth, DiffWalk, Sex, Age, Education, Income],
78
+ [label,local_plot], api_name="Diabetes_Predictor"
79
+ )
80
+
81
+ gr.Markdown("### Click on any of the examples below to see how it works:")
82
+ gr.Examples([[24,0,4,4,5,5,4,4,5,5,1,2,3], [24,0,4,4,5,3,3,2,1,1,1,2,3]], [ID, Diabetes_binary, HighBP, HighCol, CholCheck, BMI, Smoker, Stroke, HeartDiseaseorAttack, PhysActivity, Fruits, Veggies, HvyAlcoholConsump, AnyHealthcare, NoDocbcCost, GenHlth, MentHlth, PhysHealth, DiffWalk, Sex, Age, Education, Income], [label,local_plot], main_func, cache_examples=True)
83
+
84
+ demo.launch()