Spaces:
Build error
Build error
Commit
·
3f1b7f4
1
Parent(s):
45807b4
Update app.py
Browse files
app.py
CHANGED
|
@@ -25,7 +25,7 @@ def main_func(age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,sl
|
|
| 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=
|
| 29 |
|
| 30 |
plt.tight_layout()
|
| 31 |
local_plot = plt.gcf()
|
|
@@ -35,49 +35,96 @@ def main_func(age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,sl
|
|
| 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,
|
| 42 |
"""
|
| 43 |
|
| 44 |
with gr.Blocks(title=title) as demo:
|
| 45 |
-
gr.Markdown(f"## {title}")
|
| 46 |
-
gr.Markdown(description1)
|
| 47 |
-
gr.Markdown("""---""")
|
| 48 |
-
gr.Markdown(description2)
|
| 49 |
-
gr.Markdown("""---""")
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
exng = gr.Radio(["No", "Yes"], label="Exercise Risk", info="Do you have heart risk during exercise?", type="index")
|
| 62 |
-
oldpeak = gr.Slider(label="Rate your ST depression during exercise:", minimum=1, maximum=10, value=5, step=0.1)
|
| 63 |
-
slp = gr.Radio(["unslopping", "flat", "downsloping"], label="Slope" ,info="What is the slope of your peak ST segment", type="index")
|
| 64 |
-
caa = gr.Slider(label="Major Blood Vessels", info="How many major blood vessels do you have?", minimum=0, maximum=4, value=2, step=1)
|
| 65 |
-
thall = gr.Slider(label="thall Score", info="What is your thall score?", minimum=0, maximum=3, value=2, step=1)
|
| 66 |
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
submit_btn.click(
|
| 74 |
main_func,
|
| 75 |
[age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,slp,caa,thall],
|
| 76 |
[label,local_plot], api_name="Heart_Predictor"
|
| 77 |
)
|
| 78 |
-
|
| 79 |
-
gr.Markdown("### Click on any of the examples below to see how it works:")
|
| 80 |
-
gr.Examples([[77,"Male",3,200,564, 1,2,202,1,6.2,2,4,1], [24,"Male",4,4,5,3,3,2,1,1,1,2,3]], [age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,slp,caa,thall], [label,local_plot], main_func, cache_examples=True)
|
| 81 |
-
|
| 82 |
|
|
|
|
| 83 |
demo.launch()
|
|
|
|
| 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=8, order=shap.Explanation.abs, show_data='auto', show=False)
|
| 29 |
|
| 30 |
plt.tight_layout()
|
| 31 |
local_plot = plt.gcf()
|
|
|
|
| 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 these results for an actual medical diagnosis."""
|
| 39 |
|
| 40 |
description2 = """
|
| 41 |
+
To use the app, simply adjust the inputs and click the "Analyze" button. You can also click one of the examples below to see how it's done!
|
| 42 |
"""
|
| 43 |
|
| 44 |
with gr.Blocks(title=title) as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
with gr.Row():
|
| 47 |
+
with gr.Column():
|
| 48 |
+
gr.Markdown(f"# {title}")
|
| 49 |
+
gr.Markdown(f"## How does it work?")
|
| 50 |
+
gr.Markdown(description1)
|
| 51 |
+
gr.Markdown("""---""")
|
| 52 |
+
gr.Markdown(description2)
|
| 53 |
+
with gr.Column():
|
| 54 |
+
gr.Image("dr.jpg")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
gr.Markdown("""---""")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
with gr.Column():
|
| 60 |
+
gr.Markdown(f"## Edit the Inputs Below:")
|
| 61 |
+
gr.Markdown("""---""")
|
| 62 |
+
|
| 63 |
+
with gr.Row():
|
| 64 |
+
age = gr.Number(label="Age", info="How old are you?", value=40)
|
| 65 |
+
# sex = gr.Radio(["Male", "Female"], label = "What Gender are you?", type = "index")
|
| 66 |
+
sex = gr.Radio(["Male", "Female"], label="Sex", info="What gender are you?", type="index")
|
| 67 |
+
# sex = gr.Radio(choices=["Male", "Female"])
|
| 68 |
+
|
| 69 |
+
cp = gr.Radio(["Typical Angina", "Atypical Angina", "Non-anginal Pain", "Asymptomatic"], label="Chest Pain", info="What kind of chest pain do you have?", type="index")
|
| 70 |
+
# cp = gr.Slider(label="Chest Pain Type", minimum=1, maximum=5, value=4, step=1)
|
| 71 |
+
# trtbps = gr.Slider(label="Resting blood pressure (in mm Hg)", minimum=1, maximum=200, value=4, step=1)
|
| 72 |
+
trtbps = gr.Number(label="trtbps", value=100)
|
| 73 |
+
chol = gr.Number(label="chol", value=70)
|
| 74 |
+
fbs = gr.Radio(["False", "True"], label="fbs", info="Is your fasting blood sugar > 120 mg/dl?" , type="index")
|
| 75 |
+
|
| 76 |
+
# restecg = gr.Slider(label="Resting ECG Score", minimum=1, maximum=5, value=4, step=1)
|
| 77 |
+
restecg = gr.Dropdown(["Normal", "Having ST-T wave abnormality", "Showing probable or definite left ventricular hypertrophy by Estes' criteria"], label="rest_ecg", type="index")
|
| 78 |
+
thalachh = gr.Slider(label="thalach Score", minimum=1, maximum=205, value=4, step=1)
|
| 79 |
+
exng = gr.Radio(["No", "Yes"], label="Exercise Induced Angina", type="index")
|
| 80 |
+
oldpeak = gr.Slider(label="Oldpeak Score", minimum=1, maximum=10, value=4, step=1)
|
| 81 |
+
slp = gr.Slider(label="Slp Score", minimum=1, maximum=5, value=4, step=1)
|
| 82 |
+
caa = gr.Slider(label="Number of Major Vessels", minimum=1, maximum=3, value=3, step=1)
|
| 83 |
+
thall = gr.Slider(label="Thall Score", minimum=1, maximum=5, value=4, step=1)
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
with gr.Column():
|
| 90 |
+
gr.Markdown(f"## Output:")
|
| 91 |
+
gr.Markdown("""---""")
|
| 92 |
+
with gr.Column(visible=True) as output_col:
|
| 93 |
+
label = gr.Label(label = "Predicted Label")
|
| 94 |
+
local_plot = gr.Plot(label = 'Shap:')
|
| 95 |
+
|
| 96 |
+
gr.Markdown(f"## Examples:")
|
| 97 |
+
gr.Markdown("""---""")
|
| 98 |
+
gr.Markdown("### Click on any of the examples below to see how it works:")
|
| 99 |
+
gr.Examples([[24,"Male","Typical Angina",4,5,"True","Normal",4,"No",5,1,2,3], [24,"Female","Asymptomatic",4,5,"False","Normal",2,"Yes",1,1,2,3]], [age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,slp,caa,thall], [label,local_plot], main_func, cache_examples=True)
|
| 100 |
|
| 101 |
+
|
| 102 |
+
submit_btn = gr.Button("Analyze", variant="primary")
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
gr.Markdown("""---""")
|
| 106 |
+
gr.Markdown(f"## Data Dictionary:")
|
| 107 |
+
gr.Markdown("""
|
| 108 |
+
|
| 109 |
+
Age : Age of the patient
|
| 110 |
+
Sex : Sex of the patient
|
| 111 |
+
trtbps : resting blood pressure (in mm Hg)
|
| 112 |
+
chol : cholestoral in mg/dl fetched via BMI sensor
|
| 113 |
+
fbs : (fasting blood sugar > 120 mg/dl) (1 = true; 0 = false)
|
| 114 |
+
rest_ecg : resting electrocardiographic results
|
| 115 |
+
Value 0: normal
|
| 116 |
+
Value 1: having ST-T wave abnormality (T wave inversions and/or ST elevation or depression of > 0.05 mV)
|
| 117 |
+
Value 2: showing probable or definite left ventricular hypertrophy by Estes' criteria
|
| 118 |
+
|
| 119 |
+
thalach : maximum heart rate achieved
|
| 120 |
+
target : 0 = less chance of heart attack 1= more chance of heart attack""")
|
| 121 |
+
|
| 122 |
|
| 123 |
submit_btn.click(
|
| 124 |
main_func,
|
| 125 |
[age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,slp,caa,thall],
|
| 126 |
[label,local_plot], api_name="Heart_Predictor"
|
| 127 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
|
| 130 |
demo.launch()
|