eileenlynch commited on
Commit
417ac9d
·
1 Parent(s): d41d5ec

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -4
app.py CHANGED
@@ -1,7 +1,70 @@
 
 
 
 
1
  import gradio as gr
 
 
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.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
6
+ import numpy as np
7
+ import matplotlib.pyplot as plt
8
 
9
+ # load the model from disk
10
+ loaded_model = pickle.load(open("h22_xgb.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(ValueDiversity,AdequateResources,Voice,GrowthAdvancement,Workload,WorkLifeBalance):
17
+ new_row = pd.DataFrame.from_dict({'ValueDiversity':ValueDiversity,'AdequateResources':AdequateResources,
18
+ 'Voice':Voice,'GrowthAdvancement':GrowthAdvancement,'Workload':Workload,
19
+ 'WorkLifeBalance':WorkLifeBalance}, orient = 'index').transpose()
20
+
21
+ prob = loaded_model.predict_proba(new_row)
22
+
23
+ shap_values = explainer(new_row)
24
+ # plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False)
25
+ # plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False)
26
+ plot = shap.plots.bar(shap_values[0], max_display=6, order=shap.Explanation.abs, show_data='auto', show=False)
27
+
28
+ plt.tight_layout()
29
+ local_plot = plt.gcf()
30
+ plt.close()
31
+
32
+ return {"Leave": float(prob[0][0]), "Stay": 1-float(prob[0][0])}, local_plot
33
+
34
+ # Create the UI
35
+ title = "Heart Attack Predictor Application ❤️"
36
+ description1 = "This app takes information gathered from patients and healthcare providers to predict the likelihood of a cardiac event. NOTE- this application should not be used for medical or diagnostic purposes👍"
37
+
38
+ description2 = """
39
+ To use the app, click on one of the examples, or adjust the values of the six employee satisfaction factors, and click on Analyze. 🤞
40
+ """
41
+
42
+ with gr.Blocks(title=title) as demo:
43
+ gr.Markdown(f"## {title}")
44
+ # gr.Markdown("""![marketing](file/marketing.jpg)""")
45
+ gr.Markdown(description1)
46
+ gr.Markdown("""---""")
47
+ gr.Markdown(description2)
48
+ gr.Markdown("""---""")
49
+ ValueDiversity = gr.Slider(label="ValueDiversity Score", minimum=1, maximum=5, value=4, step=1)
50
+ AdequateResources = gr.Slider(label="AdequateResources Score", minimum=1, maximum=5, value=4, step=1)
51
+ Voice = gr.Slider(label="Voice Score", minimum=1, maximum=5, value=4, step=1)
52
+ GrowthAdvancement = gr.Slider(label="GrowthAdvancement Score", minimum=1, maximum=5, value=4, step=1)
53
+ Workload = gr.Slider(label="Workload Score", minimum=1, maximum=5, value=4, step=1)
54
+ WorkLifeBalance = gr.Slider(label="WorkLifeBalance Score", minimum=1, maximum=5, value=4, step=1)
55
+ submit_btn = gr.Button("Analyze")
56
+
57
+ with gr.Column(visible=True) as output_col:
58
+ label = gr.Label(label = "Predicted Label")
59
+ local_plot = gr.Plot(label = 'Shap:')
60
+
61
+ submit_btn.click(
62
+ main_func,
63
+ [ValueDiversity,AdequateResources,Voice,GrowthAdvancement,Workload,WorkLifeBalance],
64
+ [label,local_plot], api_name="Employee_Turnover"
65
+ )
66
+
67
+ gr.Markdown("### Click on any of the examples below to see how it works:")
68
+ gr.Examples([[4,4,4,4,5,5], [5,4,5,4,4,4]], [ValueDiversity,AdequateResources,Voice,GrowthAdvancement,Workload,WorkLifeBalance], [label,local_plot], main_func, cache_examples=True)
69
+
70
+ demo.launch()