anantpinak33
commited on
Commit
β’
6fe7b87
1
Parent(s):
5cb0486
Upload 6 files
Browse files- README.md +5 -5
- app.py +138 -0
- gitattributes +34 -0
- heart_xgb.pkl +3 -0
- heart_xgbV2.pkl +3 -0
- requirements.txt +10 -0
README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version: 3.
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
|
|
1 |
---
|
2 |
+
title: Group Eight
|
3 |
+
emoji: π
|
4 |
+
colorFrom: indigo
|
5 |
+
colorTo: green
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.27.0
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
license: mit
|
app.py
ADDED
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
theme = gr.themes.Default(primary_hue="blue").set(
|
11 |
+
background_fill_primary="#D3D3D3",
|
12 |
+
block_background_fill="#D3D3D3",
|
13 |
+
)
|
14 |
+
|
15 |
+
|
16 |
+
# load the model from disk
|
17 |
+
loaded_model = pickle.load(open("heart_xgbV2.pkl", 'rb'))
|
18 |
+
|
19 |
+
# Setup SHAP
|
20 |
+
explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.
|
21 |
+
|
22 |
+
gender_dict = {"Male":0,"Female":1}
|
23 |
+
cp_dict = {"Typical Angina":0, "Atypical Angina":1, "Non-Anginal":2, "Asymptomatic":3}
|
24 |
+
fbs_dict = {"Yes":1,"No":0}
|
25 |
+
exng_dict = {"Yes":1,"No":0}
|
26 |
+
restecg_dict = {"Normal":0, "Having ST-T abnormality":1, "Showing probable or definite left ventricular hypertrophy by Estes' Criteria":2}
|
27 |
+
thall_dict = {"Fixed Defect":1, "Normal Blood Flow":2, "Reversible Defect":3}
|
28 |
+
slp_dict = {"Upsloping":1, "Flat":2, "Downsloping":3}
|
29 |
+
|
30 |
+
# Create the main function for server
|
31 |
+
def main_func(age, sex, cp, trtbps, chol, fbs, restecg,thalachh,exng,oldpeak,slp,caa,thall):
|
32 |
+
new_row = pd.DataFrame.from_dict({'age':age,'sex':gender_dict[sex],
|
33 |
+
'cp':cp_dict[cp],'trtbps':trtbps,'chol':chol,
|
34 |
+
'fbs':fbs_dict[fbs], 'restecg':restecg_dict[restecg], 'thalachh':thalachh, 'exng':exng_dict[exng],
|
35 |
+
'oldpeak':oldpeak,'slp':slp_dict[slp],'caa':caa,'thall':thall_dict[thall]},
|
36 |
+
orient = 'index').transpose()
|
37 |
+
|
38 |
+
prob = loaded_model.predict_proba(new_row)
|
39 |
+
|
40 |
+
shap_values = explainer(new_row)
|
41 |
+
# plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
|
42 |
+
# plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
|
43 |
+
plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
|
44 |
+
|
45 |
+
plt.tight_layout()
|
46 |
+
local_plot = plt.gcf()
|
47 |
+
plt.close()
|
48 |
+
|
49 |
+
return {"Lower Chance of a Heart Attack": float(prob[0][0]), "Higher Chance of a Heart Attack": 1-float(prob[0][0])}, local_plot
|
50 |
+
|
51 |
+
# Create the UI
|
52 |
+
title = "**Heart Attack Predictor & Interpreter** πͺ"
|
53 |
+
description1 = "This app takes info from subjects and predicts their heart attack likelihood."
|
54 |
+
|
55 |
+
description_notmedical="**Do not use for medical diagnosis.**"
|
56 |
+
|
57 |
+
|
58 |
+
description2 = "**Fill all the options** or no result will be generated!!!**"
|
59 |
+
|
60 |
+
|
61 |
+
description3 = "To use the app, please fill all the options, and click on Analyze. π€"
|
62 |
+
|
63 |
+
|
64 |
+
descriptionExamples = "If you would like to see how the model works, please scroll down and try one of the examples!"
|
65 |
+
|
66 |
+
|
67 |
+
##Pinak
|
68 |
+
with gr.Blocks(title=title, theme=theme) as demo:
|
69 |
+
|
70 |
+
gr.Markdown("<span style='color: #FF0000;font-size: 20px'> **Heart Attack Predictor & Interpreter** πͺ</span>")
|
71 |
+
gr.Markdown("""---""")
|
72 |
+
gr.Markdown("<span style='font-size: 20px;'> **Do not use for medical diagnosis.**")
|
73 |
+
gr.Markdown("""---""")
|
74 |
+
gr.Markdown("<span style='font-size: 16px;'> If you would like to see how the model works, please scroll down and try one of the examples!")
|
75 |
+
gr.Markdown("""---""")
|
76 |
+
gr.Markdown("<span style='font-size: 16px;'> This app takes info from subjects and predicts their heart attack likelihood.")
|
77 |
+
gr.Markdown("""---""")
|
78 |
+
gr.Markdown("<span style='font-size: 16px;'> To use the app, please fill in all the options, and click on Analyze. π€")
|
79 |
+
gr.Markdown("<span style='font-size: 16px;'> **Fill all the options or no result will be generated!!!**")
|
80 |
+
gr.Markdown("""---""")
|
81 |
+
|
82 |
+
with gr.Row():
|
83 |
+
with gr.Column():
|
84 |
+
age = gr.Number(label="What is your age?", value=40)
|
85 |
+
with gr.Column():
|
86 |
+
slp = gr.Dropdown(["Upsloping", "Flat", "Downsloping"], label="What was the slope of the peak exercise ST segment?")
|
87 |
+
|
88 |
+
with gr.Row():
|
89 |
+
with gr.Column():
|
90 |
+
sex = gr.Radio(["Female", "Male"], label = "What is your sex?")
|
91 |
+
cp = gr.Radio(["Typical Angina", "Atypical Angina", "Non-Anginal", "Asymptomatic"], label = "What kind of chest pain is it?")
|
92 |
+
with gr.Column():
|
93 |
+
restecg = gr.Radio(["Normal", "Having ST-T abnormality", "Showing probable or definite left ventricular hypertrophy by Estes' Criteria"],
|
94 |
+
label = "What is your resting ECG result?")
|
95 |
+
|
96 |
+
with gr.Row():
|
97 |
+
with gr.Column():
|
98 |
+
fbs = gr.Radio(["Yes", "No"], label = "Is your fasting Blood Sugar >120 mg/dl?")
|
99 |
+
with gr.Column():
|
100 |
+
exng = gr.Radio(["Yes", "No"], label = "Do you have Exercise Induced Angina?")
|
101 |
+
with gr.Row():
|
102 |
+
with gr.Column():
|
103 |
+
caa = gr.Radio([1, 2, 3], label="How many vessels were colored by the fluoroscopy?")
|
104 |
+
|
105 |
+
with gr.Column():
|
106 |
+
thall = gr.Radio(["Fixed Defect", "Normal Blood Flow", "Reversible Defect"], label="What is your Thalassemia condition?")
|
107 |
+
|
108 |
+
with gr.Row():
|
109 |
+
with gr.Column():
|
110 |
+
trtbps = gr.Slider(label = "What is your resting blood Pressure (in mm Hg)?", minimum = 10, maximum = 250, value = 100, step = 1)
|
111 |
+
|
112 |
+
with gr.Column():
|
113 |
+
chol = gr.Slider(label = "What is your cholesterol in mg/dl (via BMI sensor)?", minimum = 30, maximum = 300, value = 180, step = 1)
|
114 |
+
with gr.Row():
|
115 |
+
with gr.Column():
|
116 |
+
oldpeak = gr.Slider(label = "What was the ST depression induced by exercise relative to rest?", minimum = 0, maximum = 6.2, step = 0.1)
|
117 |
+
with gr.Column():
|
118 |
+
thalachh = gr.Slider(label="What is your maximum heart rate?", minimum = 60, maximum = 250, value=100, step = 1)
|
119 |
+
|
120 |
+
|
121 |
+
with gr.Row():
|
122 |
+
submit_btn = gr.Button("Analyze")
|
123 |
+
|
124 |
+
##Do not need to touch
|
125 |
+
with gr.Column(visible=True) as output_col:
|
126 |
+
label = gr.Label(label = "Predicted Label")
|
127 |
+
local_plot = gr.Plot(label = 'Shap:')
|
128 |
+
|
129 |
+
submit_btn.click(
|
130 |
+
main_func,
|
131 |
+
[age, sex, cp, trtbps, chol, fbs, restecg,thalachh,exng,oldpeak,slp,caa,thall],
|
132 |
+
[label,local_plot], api_name="Heart_Predictor"
|
133 |
+
)
|
134 |
+
|
135 |
+
gr.Examples([[24, "Male", "Typical Angina", 130, 150, "Yes", "Having ST-T abnormality",170, "Yes", 5.1, "Flat", 2, "Normal Blood Flow"],
|
136 |
+
[59, "Female", "Non-Anginal", 150, 170, "No", "Showing probable or definite left ventricular hypertrophy by Estes' Criteria",190, "No", 6, "Upsloping", 3, "Reversible Defect"]], [age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,slp,caa,thall], [label,local_plot], main_func, cache_examples=True)
|
137 |
+
|
138 |
+
demo.launch()
|
gitattributes
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
3 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
4 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
5 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
6 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
7 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
8 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
9 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
10 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
11 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
13 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
14 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
15 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
16 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
17 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
18 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
19 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
20 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
21 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
22 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
23 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
24 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
25 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
26 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
27 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
29 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
30 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
31 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
32 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
heart_xgb.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:15c0afec50d47e4a5842340d8c47b8a14be0e60f6d2bd4898974a210fd795903
|
3 |
+
size 138725
|
heart_xgbV2.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c89450258148337aaf93e65ca5526dcabe996126869ad0c6f4f2a7d37ffb28f8
|
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
|