JoeHawkes commited on
Commit
4430cf7
·
verified ·
1 Parent(s): 1bd8cc1

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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("salar_xgb_team.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, 'education-num':education-num, 'sex':sex,
18
+ 'capital-gain':capital-gain, 'capital-loss':capital-loss, 'hours-per-week':hours-per-week,
19
+ 'salary-class':salary_class},
20
+ orient = 'index').transpose()
21
+
22
+ prob = loaded_model.predict_proba(new_row)
23
+
24
+ shap_values = explainer(new_row)
25
+ # plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
26
+ # plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
27
+ plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
28
+
29
+ plt.tight_layout()
30
+ local_plot = plt.gcf()
31
+ plt.close()
32
+
33
+ return {"Low Chance": float(prob[0][0]), "High Chance": 1-float(prob[0][0])}, local_plot
34
+
35
+ # Create the UI
36
+ title = "**Household Income Predictor** 🪐"
37
+ description1 = """This app takes info from subjects and predicts their household income."""
38
+
39
+ description2 = """
40
+ To use the app, click on one of the examples, or adjust the values of the factors, and click on Analyze. 🤞
41
+ """
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.Number(label="age Score", value=40)
51
+ sex = gr.Slider(label="sex Score", minimum=0, maximum=1, value=1, step=1)
52
+ cp = gr.Slider(label="cp Score", minimum=1, maximum=5, value=4, step=1)
53
+ trtbps = gr.Slider(label="trtbps Score", minimum=1, maximum=5, value=4, step=1)
54
+ chol = gr.Slider(label="chol Score", minimum=1, maximum=5, value=4, step=1)
55
+ fbs = gr.Slider(label="fbs Score", minimum=1, maximum=5, value=4, step=1)
56
+
57
+ restecg = gr.Slider(label="restecg Score", minimum=1, maximum=5, value=4, step=1)
58
+ thalachh = gr.Slider(label="thalachh Score", minimum=1, maximum=5, value=4, step=1)
59
+
60
+ exng = gr.Slider(label="exng Score", minimum=1, maximum=5, value=4, step=1)
61
+ oldpeak = gr.Slider(label="oldpeak Score", minimum=1, maximum=5, value=4, step=1)
62
+ slp = gr.Slider(label="slp Score", minimum=1, maximum=5, value=4, step=1)
63
+ caa = gr.Slider(label="caa Score", minimum=1, maximum=5, value=4, step=1)
64
+ thall = gr.Slider(label="thall Score", minimum=1, maximum=5, value=4, step=1)
65
+
66
+ submit_btn = gr.Button("Analyze")
67
+
68
+ with gr.Column(visible=True) as output_col:
69
+ label = gr.Label(label = "Predicted Label")
70
+ local_plot = gr.Plot(label = 'Shap:')
71
+
72
+ submit_btn.click(
73
+ main_func,
74
+ [age, sex, cp, trtbps, chol, fbs, restecg, thalachh,exng,oldpeak,slp,caa,thall],
75
+ [label,local_plot], api_name="Heart_Predictor"
76
+ )
77
+
78
+ gr.Markdown("### Click on any of the examples below to see how it works:")
79
+ 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)
80
+
81
+ demo.launch()