anantpinak33 commited on
Commit
07e1c25
·
1 Parent(s): 01bf5dd

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +103 -0
  2. heart_xgb.pkl +3 -0
  3. requirements.txt +10 -0
app.py ADDED
@@ -0,0 +1,103 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("heart_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(age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,slp,caa,thall):
17
+ new_row = pd.DataFrame.from_dict({'age':age,'sex':sex,
18
+ 'cp':cp,'trtbps':trtbps,'chol':chol,
19
+ 'fbs':fbs, 'restecg':restecg,'thalachh':thalachh,'exng':exng,
20
+ 'oldpeak':oldpeak,'slp':slp,'caa':caa,'thall':thall},
21
+ orient = 'index').transpose()
22
+
23
+ prob = loaded_model.predict_proba(new_row)
24
+
25
+ shap_values = explainer(new_row)
26
+ # plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
27
+ # plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
28
+ plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
29
+
30
+ plt.tight_layout()
31
+ local_plot = plt.gcf()
32
+ plt.close()
33
+
34
+ return {"Low Chance": float(prob[0][0]), "High Chance": 1-float(prob[0][0])}, local_plot\
35
+
36
+ # Create the UI
37
+ title = "**Heart Attack Predictor & Interpreter** 🪐"
38
+ description1 = """This app takes info from subjects and predicts their heart attack likelihood. Do not use for medical diagnosis."""
39
+
40
+ description2 = """
41
+ To use the app, click on one of the examples, or adjust the values of the factors, and click on Analyze. 🤞
42
+ """
43
+
44
+ with gr.Blocks(title=title) as demo:
45
+ gr.Markdown(f"## {title}")
46
+ gr.Markdown(description2)
47
+ gr.Markdown("""---""")
48
+ gr.Markdown(description2)
49
+ gr.Markdown("""---""")
50
+
51
+ with gr.Row():
52
+ age = gr.Number(label="age Score", value=40)
53
+
54
+ with gr.Row():
55
+ with gr.Column():
56
+ sex = gr.Dropdown(label = "Sex", choices = ["Female", "Male"], type = "index" )
57
+ cp = gr.Dropdown(label="CP (Chest Pain) Score", choices = ["Typical Angina", "Atypical Angina", "Non-Anginal", "Asymptomatic"], type = "index", )
58
+
59
+ with gr.Column():
60
+ cp = gr.Dropdown(label="cp Score", choices = ["Typical", "Atypical", "Non-Anginal", "Asymptomatic"], type = "index",
61
+ )
62
+ trtbps = gr.Number(label="trtbps Score", info = "resting blood pressure (in mm Hg)")
63
+
64
+ with gr.Row():
65
+ with gr.Column():
66
+ fbs = gr.Slider(label="fbs Score", minimum=0, maximum=1, value=1, step=1)
67
+ with gr.Column():
68
+ restecg = gr.Slider(label="restecg Score", minimum=0, maximum=1, value=1, step=1)
69
+
70
+ with gr.Row():
71
+ with gr.Column():
72
+ thalachh = gr.Number(label="thalachh Score", info = "Maximum heart rate")
73
+ with gr.Column():
74
+ chol = gr.Number(label="chol Score", info = " cholestoral in mg/dl fetched via BMI sensor" )
75
+ exng = gr.Slider(label="exng Score", minimum=1, maximum=5, value=4, step=1)
76
+
77
+
78
+ with gr.Row():
79
+ with gr.Column():
80
+ oldpeak = gr.Slider(label="oldpeak Score", minimum=1, maximum=5, value=4, step=1)
81
+ with gr.Column():
82
+ slp = gr.Slider(label="slp Score", minimum=1, maximum=5, value=4, step=1)
83
+ with gr.Column():
84
+ caa = gr.Slider(label="caa Score", minimum=1, maximum=5, value=4, step=1)
85
+ with gr.Column():
86
+ thall = gr.Slider(label="thall Score", minimum=1, maximum=5, value=4, step=1)
87
+
88
+ submit_btn = gr.Button("Analyze")
89
+
90
+ with gr.Column(visible=True) as output_col:
91
+ label = gr.Label(label = "Predicted Label")
92
+ local_plot = gr.Plot(label = 'Shap:')
93
+
94
+ submit_btn.click(
95
+ main_func,
96
+ [age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,slp,caa,thall],
97
+ [label,local_plot], api_name="Heart_Predictor"
98
+ )
99
+
100
+ gr.Markdown("### Click on any of the examples below to see how it works:")
101
+
102
+
103
+ demo.launch()
heart_xgb.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:f3d7f96dab791241a7536e789aadc37358ad1ccf276dccd89885ca8c2fdbdd4f
3
+ size 132944
requirements.txt ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
 
1
+ gradio==3.1.3
2
+ Pillow
3
+ yake
4
+ pandas
5
+ sklearn
6
+ shap
7
+ xgboost
8
+ matplotlib
9
+ numpy
10
+ streamlit