Update app.py
Browse files
app.py
CHANGED
|
@@ -12,71 +12,72 @@ loaded_model = pickle.load(open("salar_xgb_team.pkl", 'rb'))
|
|
| 12 |
# Setup SHAP
|
| 13 |
explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.
|
| 14 |
|
| 15 |
-
#
|
| 16 |
def main_func(age, education_num, sex, capital_gain, capital_loss, hours_per_week):
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
prob = loaded_model.predict_proba(new_row)
|
| 24 |
-
|
| 25 |
shap_values = explainer(new_row)
|
| 26 |
-
|
| 27 |
-
#
|
| 28 |
-
plot = shap.plots.bar(shap_values[0], max_display=6,
|
| 29 |
-
|
| 30 |
plt.tight_layout()
|
| 31 |
local_plot = plt.gcf()
|
| 32 |
plt.close()
|
| 33 |
-
|
| 34 |
-
return {"
|
| 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.
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 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:
|
| 70 |
-
label = gr.Label(label
|
| 71 |
-
local_plot = gr.Plot(label
|
| 72 |
-
|
| 73 |
submit_btn.click(
|
| 74 |
main_func,
|
| 75 |
-
[age,
|
| 76 |
-
[label,local_plot],
|
|
|
|
| 77 |
)
|
| 78 |
-
|
| 79 |
-
gr.Markdown("###
|
| 80 |
-
gr.Examples(
|
| 81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
demo.launch()
|
|
|
|
| 12 |
# Setup SHAP
|
| 13 |
explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS.
|
| 14 |
|
| 15 |
+
# Main prediction function
|
| 16 |
def main_func(age, education_num, sex, capital_gain, capital_loss, hours_per_week):
|
| 17 |
+
sex_binary = 0 if sex == "Male" else 1
|
| 18 |
+
new_row = pd.DataFrame({
|
| 19 |
+
'age': [age],
|
| 20 |
+
'education-num': [education_num],
|
| 21 |
+
'sex': [sex_binary],
|
| 22 |
+
'capital-gain': [capital_gain],
|
| 23 |
+
'capital-loss': [capital_loss],
|
| 24 |
+
'hours-per-week': [hours_per_week]
|
| 25 |
+
})
|
| 26 |
+
|
| 27 |
prob = loaded_model.predict_proba(new_row)
|
|
|
|
| 28 |
shap_values = explainer(new_row)
|
| 29 |
+
|
| 30 |
+
# SHAP bar plot
|
| 31 |
+
plot = shap.plots.bar(shap_values[0], max_display=6, show=False)
|
|
|
|
| 32 |
plt.tight_layout()
|
| 33 |
local_plot = plt.gcf()
|
| 34 |
plt.close()
|
| 35 |
+
|
| 36 |
+
return {"≤50K": float(prob[0][0]), ">50K": float(prob[0][1])}, local_plot
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
# Gradio UI
|
| 39 |
+
title = "**Salary Predictor & SHAP Explainer** 💼"
|
| 40 |
+
description1 = "This app uses demographic and financial info to predict if someone earns over 50K annually."
|
| 41 |
+
description2 = "Adjust the inputs below and click Analyze to see the prediction and SHAP feature importance."
|
| 42 |
+
|
| 43 |
with gr.Blocks(title=title) as demo:
|
| 44 |
gr.Markdown(f"## {title}")
|
| 45 |
gr.Markdown(description1)
|
| 46 |
+
gr.Markdown("---")
|
| 47 |
gr.Markdown(description2)
|
| 48 |
+
gr.Markdown("---")
|
| 49 |
+
|
| 50 |
+
age = gr.Slider(label="Age", minimum=18, maximum=70, value=35)
|
| 51 |
+
education_num = gr.Slider(label="Education Number", minimum=1, maximum=16, value=10)
|
| 52 |
+
sex = gr.Radio(["Male", "Female"], label="Sex")
|
| 53 |
+
capital_gain = gr.Number(label="Capital Gain", value=0)
|
| 54 |
+
capital_loss = gr.Number(label="Capital Loss", value=0)
|
| 55 |
+
hours_per_week = gr.Slider(label="Hours per Week", minimum=1, maximum=100, value=40)
|
| 56 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
submit_btn = gr.Button("Analyze")
|
| 58 |
+
|
| 59 |
with gr.Column(visible=True) as output_col:
|
| 60 |
+
label = gr.Label(label="Predicted Probability")
|
| 61 |
+
local_plot = gr.Plot(label="SHAP Feature Importance")
|
| 62 |
+
|
| 63 |
submit_btn.click(
|
| 64 |
main_func,
|
| 65 |
+
[age, education_num, sex, capital_gain, capital_loss, hours_per_week],
|
| 66 |
+
[label, local_plot],
|
| 67 |
+
api_name="Salary_Predictor"
|
| 68 |
)
|
| 69 |
+
|
| 70 |
+
gr.Markdown("### Try one of the following examples:")
|
| 71 |
+
gr.Examples(
|
| 72 |
+
examples=[
|
| 73 |
+
[28, 12, "Male", 0, 0, 45],
|
| 74 |
+
[52, 14, "Female", 7688, 0, 60],
|
| 75 |
+
[35, 9, "Male", 0, 1902, 40]
|
| 76 |
+
],
|
| 77 |
+
inputs=[age, education_num, sex, capital_gain, capital_loss, hours_per_week],
|
| 78 |
+
outputs=[label, local_plot],
|
| 79 |
+
fn=main_func,
|
| 80 |
+
cache_examples=True
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
demo.launch()
|