Spaces:
Runtime error
Runtime error
Update app.py
#3
by
ec4eu
- opened
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import pickle
|
| 2 |
import pandas as pd
|
| 3 |
-
pip! install shap
|
| 4 |
import shap
|
| 5 |
from shap.plots._force_matplotlib import draw_additive_plot
|
| 6 |
import gradio as gr
|
|
@@ -14,11 +13,12 @@ loaded_model = pickle.load(open("heart_xgb.pkl", 'rb'))
|
|
| 14 |
explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.
|
| 15 |
|
| 16 |
# Create the main function for server
|
| 17 |
-
def main_func(age, sex, cp, trtbps, chol, fbs, restecg, thalachh,
|
| 18 |
new_row = pd.DataFrame.from_dict({'age':age,'sex':sex,
|
| 19 |
'cp':cp,'trtbps':trtbps,'chol':chol,
|
| 20 |
-
'fbs':fbs, 'restecg':restecg,
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
prob = loaded_model.predict_proba(new_row)
|
| 24 |
|
|
@@ -35,33 +35,35 @@ def main_func(age, sex, cp, trtbps, chol, fbs, restecg, thalachh, exng, oldpeak,
|
|
| 35 |
|
| 36 |
# Create the UI
|
| 37 |
title = "**Heart Attack Predictor & Interpreter** 🪐"
|
| 38 |
-
description1 = """
|
| 39 |
-
"""
|
| 40 |
|
| 41 |
description2 = """
|
| 42 |
-
To use the app, click on one of the examples, or adjust the values of the
|
| 43 |
"""
|
| 44 |
|
| 45 |
with gr.Blocks(title=title) as demo:
|
| 46 |
gr.Markdown(f"## {title}")
|
| 47 |
-
# gr.Markdown("""""")
|
| 48 |
gr.Markdown(description1)
|
| 49 |
gr.Markdown("""---""")
|
| 50 |
gr.Markdown(description2)
|
| 51 |
gr.Markdown("""---""")
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
cp = gr.Slider(label="cp Score", minimum=1, maximum=5, value=4, step=1)
|
| 55 |
-
trtbps = gr.Slider(label="
|
| 56 |
-
chol = gr.Slider(label="
|
| 57 |
-
fbs = gr.Slider(label="
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
submit_btn = gr.Button("Analyze")
|
| 66 |
|
| 67 |
with gr.Column(visible=True) as output_col:
|
|
@@ -70,11 +72,11 @@ with gr.Blocks(title=title) as demo:
|
|
| 70 |
|
| 71 |
submit_btn.click(
|
| 72 |
main_func,
|
| 73 |
-
[age, sex, cp, trtbps, chol, fbs, restecg, thalachh,
|
| 74 |
-
[label,local_plot], api_name="
|
| 75 |
)
|
| 76 |
|
| 77 |
gr.Markdown("### Click on any of the examples below to see how it works:")
|
| 78 |
-
gr.Examples([[24,0,4,4,5,5,4,4,5,5,1,2,3], [
|
| 79 |
|
| 80 |
demo.launch()
|
|
|
|
| 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
|
|
|
|
| 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 |
|
|
|
|
| 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(description1)
|
| 47 |
gr.Markdown("""---""")
|
| 48 |
gr.Markdown(description2)
|
| 49 |
gr.Markdown("""---""")
|
| 50 |
+
|
| 51 |
+
age = gr.Number(label="age Score", value=40)
|
| 52 |
+
sex = gr.Slider(label="sex Score", minimum=0, maximum=1, value=1, step=1)
|
| 53 |
cp = gr.Slider(label="cp Score", minimum=1, maximum=5, value=4, step=1)
|
| 54 |
+
trtbps = gr.Slider(label="trtbps Score", minimum=1, maximum=5, value=4, step=1)
|
| 55 |
+
chol = gr.Slider(label="chol Score", minimum=1, maximum=5, value=4, step=1)
|
| 56 |
+
fbs = gr.Slider(label="fbs Score", minimum=1, maximum=5, value=4, step=1)
|
| 57 |
+
|
| 58 |
+
restecg = gr.Slider(label="restecg Score", minimum=1, maximum=5, value=4, step=1)
|
| 59 |
+
thalachh = gr.Slider(label="thalachh Score", minimum=1, maximum=5, value=4, step=1)
|
| 60 |
+
|
| 61 |
+
exng = gr.Slider(label="exng Score", minimum=1, maximum=5, value=4, step=1)
|
| 62 |
+
oldpeak = gr.Slider(label="oldpeak Score", minimum=1, maximum=5, value=4, step=1)
|
| 63 |
+
slp = gr.Slider(label="slp Score", minimum=1, maximum=5, value=4, step=1)
|
| 64 |
+
caa = gr.Slider(label="caa Score", minimum=1, maximum=5, value=4, step=1)
|
| 65 |
+
thall = gr.Slider(label="thall Score", minimum=1, maximum=5, value=4, step=1)
|
| 66 |
+
|
| 67 |
submit_btn = gr.Button("Analyze")
|
| 68 |
|
| 69 |
with gr.Column(visible=True) as output_col:
|
|
|
|
| 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([[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]], [age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,slp,caa,thall], [label,local_plot], main_func, cache_examples=True)
|
| 81 |
|
| 82 |
demo.launch()
|