import pickle import pandas as pd import shap from shap.plots._force_matplotlib import draw_additive_plot import gradio as gr import numpy as np import matplotlib.pyplot as plt # load the model from disk loaded_model = pickle.load(open("coupon_xgb.pkl", 'rb')) # Setup SHAP explainer = shap.Explainer(loaded_model) # PLEASE DO NOT CHANGE THIS. # Create the main function for server def main_func(destination,passanger,weather,time,expiration,gender,age,maritalStatus,education,occupation,income,Bar,CoffeeHouse,CarryAway,RestaurantLessThan20,Restaurant20To50,coupon,has_children,toCoupon_GEQ5min,toCoupon_GEQ15min,toCoupon_GEQ25min,direction_same,direction_opp,temperature): new_row = pd.DataFrame.from_dict({'destination':destination,'passanger':passanger, 'weather':weather,'time':time,'expiration':expiration, 'gender':gender,'age':age,'maritalStatus':maritalStatus, 'education':education,'occupation':occupation,'income':income, 'Bar':Bar,'CoffeeHouse':CoffeeHouse,'CarryAway':CarryAway, 'RestaurantLessThan20':RestaurantLessThan20,'Restaurant20To50':Restaurant20To50,'coupon':coupon, 'has_children':has_children,'toCoupon_GEQ5min':toCoupon_GEQ5min,'toCoupon_GEQ15min':toCoupon_GEQ15min, 'toCoupon_GEQ25min':toCoupon_GEQ25min,'direction_same':direction_same,'direction_opp':direction_opp, 'temperature':temperature}, orient = 'index').transpose() prob = loaded_model.predict_proba(new_row) shap_values = explainer(new_row) # plot = shap.force_plot(shap_values[0], matplotlib=True, figsize=(30,30), show=False) # plot = shap.plots.waterfall(shap_values[0], max_display=6, show=False) plot = shap.plots.bar(shap_values[0], max_display=24, order=shap.Explanation.abs, show_data='auto', show=False) plt.tight_layout() local_plot = plt.gcf() plt.rcParams['figure.figsize'] = 6,4 plt.close() return {"Leave": float(prob[0][0]), "Stay": 1-float(prob[0][0])}, local_plot # Create the UI title = "**Employee Turnover Predictor & Interpreter** 🪐" description1 = """ This app takes six inputs about employees' satisfaction with different aspects of their work (such as work-life balance, ...) and predicts whether the employee intends to stay with the employer or leave. There are two outputs from the app: 1- the predicted probability of stay or leave, 2- Shapley's force-plot which visualizes the extent to which each factor impacts the stay/ leave prediction. """ description2 = """ To use the app, click on one of the examples, or adjust the values of the six employee satisfaction factors, and click on Analyze. ✨ """ with gr.Blocks(title=title) as demo: gr.Markdown(f"## {title}") # gr.Markdown("""![marketing](types-of-employee-turnover.jpg)""") gr.Markdown(description1) gr.Markdown("""---""") gr.Markdown(description2) gr.Markdown("""---""") with gr.Row(): with gr.Column(): destination = gr.Slider(label="destination Score", minimum=1, maximum=5, value=4, step=.1) passanger = gr.Slider(label="passanger Score", minimum=1, maximum=5, value=4, step=.1) weather = gr.Slider(label="weather Score", minimum=1, maximum=5, value=4, step=.1) time = gr.Slider(label="time Score", minimum=1, maximum=5, value=4, step=.1) expiration = gr.Slider(label="expiration Score", minimum=1, maximum=5, value=4, step=.1) gender = gr.Slider(label="gender Score", minimum=1, maximum=5, value=4, step=.1) age = gr.Slider(label="age Score", minimum=1, maximum=5, value=4, step=.1) maritalStatus = gr.Slider(label="maritalStatus Score", minimum=1, maximum=5, value=4, step=.1) education = gr.Slider(label="education Score", minimum=1, maximum=5, value=4, step=.1) occupation = gr.Slider(label="occupation Score", minimum=1, maximum=5, value=4, step=.1) income = gr.Slider(label="income Score", minimum=1, maximum=5, value=4, step=.1) Bar = gr.Slider(label="Bar Score", minimum=1, maximum=5, value=4, step=.1) CoffeeHouse = gr.Slider(label="CoffeeHouse Score", minimum=1, maximum=5, value=4, step=.1) CarryAway = gr.Slider(label="CarryAway Score", minimum=1, maximum=5, value=4, step=.1) RestaurantLessThan20 = gr.Slider(label="RestaurantLessThan20 Score", minimum=1, maximum=5, value=4, step=.1) Restaurant20To50 = gr.Slider(label="Restaurant20To50 Score", minimum=1, maximum=5, value=4, step=.1) coupon = gr.Slider(label="Coupon Score", minimum=1, maximum=5, value=4, step=.1) has_children = gr.Slider(label="Has_children Score", minimum=1, maximum=5, value=4, step=.1) toCoupon_GEQ5min = gr.Slider(label="toCoupon_GEQ5min Score", minimum=1, maximum=5, value=4, step=.1) toCoupon_GEQ15min = gr.Slider(label="toCoupon_GEQ15min Score", minimum=1, maximum=5, value=4, step=.1) toCoupon_GEQ25min = gr.Slider(label="toCoupon_GEQ25min Score", minimum=1, maximum=5, value=4, step=.1) direction_same = gr.Slider(label="direction_same Score", minimum=1, maximum=5, value=4, step=.1) direction_opp = gr.Slider(label="direction_opp Score", minimum=1, maximum=5, value=4, step=.1) temperature = gr.Slider(label="temperature Score", minimum=1, maximum=5, value=4, step=.1) submit_btn = gr.Button("Analyze") with gr.Column(visible=True,scale=1, min_width=600) as output_col: label = gr.Label(label = "Predicted Label") local_plot = gr.Plot(label = 'Shap:') submit_btn.click( main_func, [destination,passanger,weather,time,expiration,gender,age,maritalStatus,education,occupation,income,Bar,CoffeeHouse,CarryAway,RestaurantLessThan20,Restaurant20To50], [label,local_plot], api_name="Employee_Turnover" ) gr.Markdown("### Click on any of the examples below to see how it works:") gr.Examples([[4,4,4,4,5,5,4,4,4,4,5,5,4,4,4,4], [5,4,5,4,4,4,5,4,5,4,4,4,5,4,5,4]], [destination,passanger,weather,time,expiration,gender,age,maritalStatus,education,occupation,income,Bar,CoffeeHouse,CarryAway,RestaurantLessThan20,Restaurant20To50], [label,local_plot], main_func, cache_examples=True) demo.launch()