Spaces:
Runtime error
Runtime error
File size: 4,895 Bytes
f5e2d5e 966824d 123e7f4 966824d 123e7f4 966824d 123e7f4 966824d 123e7f4 966824d f5e2d5e 966824d f5e2d5e 966824d f5e2d5e 966824d f5e2d5e 362fed2 f5e2d5e 966824d 123e7f4 f5e2d5e 123e7f4 966824d 123e7f4 f5e2d5e edf622b 966824d f5e2d5e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
import gradio as gr
from joblib import load
import pandas as pd
dv , model = load("train_model.joblib")
# creating a predict function to be passed into gradio UI
def predict(age, job, marital, education, default, housing,
loan, contact, month,day_of_week,campaign,pdays,
previous,poutcome,emp_var_rate,cons_price_idx,cons_conf_idx):
customer = {
'age': age,
'job': job,
'marital': marital,
'education': education,
'default': default,
'housing': housing,
'loan': loan,
'contact': contact,
'month': month,
'day_of_week': day_of_week,
'campaign': campaign,
'pdays': pdays,
'previous': previous,
'poutcome': poutcome,
'emp_var_rate': emp_var_rate,
'cons_price_idx': cons_price_idx,
'cons_conf_idx': cons_conf_idx,
}
print(customer)
df_transformed = dv.transform([customer])
prediction = model.predict_proba(df_transformed)[0]
# return prediction[1]
return {'Failure(!Deposit)': prediction[0], 'Success(Deposit)': prediction[1]}
# Desposited = prediction >= 0.50
# result = {
# "deposit_probability": float(prediction),
# "Deposited": bool(Deposited)
# }
# print(f' The probabilty of depositing in the bank is : {str(prediction)}')
# In[4]:
def feature_importance(age, job, marital, education, default, housing,
loan, contact, month,day_of_week,campaign,pdays,
previous,poutcome,emp_var_rate,cons_price_idx,cons_conf_idx):
customer = {
'age': age,
'job': job,
'marital': marital,
'education': education,
'default': default,
'housing': housing,
'loan': loan,
'contact': contact,
'month': month,
'day_of_week': day_of_week,
'campaign': campaign,
'pdays': pdays,
'previous': previous,
'poutcome': poutcome,
'emp_var_rate': emp_var_rate,
'cons_price_idx': cons_price_idx,
'cons_conf_idx': cons_conf_idx,
}
df_transformed = pd.DataFrame(dv.transform(customer))
df_transformed.columns = dv.get_feature_names_out()
important_features = pd.DataFrame({'cols':df_transformed.columns, 'imp':model.feature_importances_}
).sort_values('imp', ascending=False)
#return important_features.plot('cols', 'imp', 'barh', figsize=(12,7), legend=False)
return print(important_features.to_json())
age = gr.inputs.Slider(minimum=1,default = 35, maximum=100, step=1,label = 'Age') #default=data['age'].mean()
job = gr.inputs.Dropdown(choices=["Housemaid", "Services","Admin.","Blue-Collar","Technician",
"Retired","Management","Unemployed","Self-Employed","Unknown","Entrepreneur","Student"],
label = 'Job')
marital = gr.inputs.Dropdown(choices=["Married", "Single","Divorced","Unknown"],label = 'Marital')
education = gr.inputs.Dropdown(choices=["Basic.4y", "High.School","Basic.6y","Basic.9y","Professional.Course",
"Unknown","University.Degree","Illiterate"],label = 'Education')
default = gr.inputs.Radio(["Yes", "No","Unknown"],label = 'Default',type="index")
housing = gr.inputs.Radio(choices=["Yes", "No","Unknown"],label = 'Housing',type="index")
loan = gr.inputs.Radio(["Yes", "No","Unknown"],type="index",label = 'Loan')
contact = gr.inputs.Radio(["Telephone", "Cellular"],type = "index",label = 'Contact')
month = gr.inputs.Dropdown(choices=['Mar', 'Apr','May', 'Jun', 'Jul', 'Aug','Sep','Oct', 'Nov', 'Dec'],label = 'Month')
day_of_week = gr.inputs.Dropdown(choices=['Mon', 'Tue', 'Wed', 'Thu', 'Fri'],label = 'Day of Week')
campaign = gr.inputs.Slider(minimum=1,default = 2, maximum=56, step = 1,label = 'Campaign')
pdays = gr.inputs.Slider(minimum=0,default = 0, maximum=27, step = 1,label = 'Last Contact(in days)')
previous = gr.inputs.Slider(minimum=0,default = 0, maximum=7, step = 1,label = 'Previous Contacts')
poutcome = gr.inputs.Radio(["Nonexistent", "Failure","Success"],label = 'Previous Outcome',type="index")
emp_var_rate = gr.inputs.Slider(minimum=-3,default = 1, maximum=1,step= 1, label = 'Employment Variation Rate')
cons_price_idx = gr.inputs.Slider(minimum=92,default = 94, maximum=95,step = 1, label = 'Consumer Price Index ')
cons_conf_idx = gr.inputs.Slider(minimum=-51,default = -42, maximum=-27, step = 1, label = 'Consumer Confidence Index')
iface = gr.Interface(predict,[age, job, marital, education, default, housing,
loan, contact, month,day_of_week,campaign,pdays,
previous,poutcome,emp_var_rate,cons_price_idx,cons_conf_idx],
outputs = "label",
interpretation="default"
)
iface.launch(share=True , debug=True)
|