File size: 4,337 Bytes
9060cc5 ac500ef 9060cc5 |
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 |
import gradio as gr
import pandas as pd
import joblib
import os
# Load the models
preprocessor_path = os.path.join('.', 'preprocessor.joblib')
model_path = os.path.join('.', 'Best_model.joblib')
preprocessor = joblib.load(preprocessor_path)
best_model = joblib.load(model_path)
# Define prediction function
def predict(age, job, marital, education, default, housing, loan, contact, month, day_of_week, duration, campaign, pdays, previous, poutcome, emp_var_rate, cons_price_idx, cons_conf_idx, euribor3m, nr_employed):
# Create a DataFrame for the input data
data = pd.DataFrame({
'age': [age],
'job': [job],
'marital': [marital],
'education': [education],
'default': [default],
'housing': [housing],
'loan': [loan],
'contact': [contact],
'month': [month],
'day_of_week': [day_of_week],
'duration': [duration],
'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],
'euribor3m': [euribor3m],
'nr.employed': [nr_employed]
})
# Preprocess the data
preprocessed_data = preprocessor.transform(data)
# Make predictions
prediction = best_model.predict(preprocessed_data)
probability = best_model.predict_proba(preprocessed_data)
return {
"Prediction": prediction[0],
"Probability (Yes)": probability[0][1],
"Probability (No)": probability[0][0]
}
# Define the interface
def gradio_interface():
with gr.Blocks() as app:
gr.Markdown("# Term Deposit Subscription Prediction")
with gr.Row():
age = gr.Number(label="Age", value=30)
job = gr.Dropdown(["housemaid", "services", "admin.", "blue-collar", "technician", "retired", "management", "unemployed", "self-employed", "unknown", "entrepreneur", "student"], label="Job")
marital = gr.Dropdown(["married", "single", "divorced", "unknown"], label="Marital Status")
education = gr.Dropdown(["basic.4y", "high.school", "basic.6y", "basic.9y", "professional.course", "unknown", "university.degree", "illiterate", "tertiary", "secondary", "primary"], label="Education")
with gr.Row():
default = gr.Dropdown(["no", "unknown", "yes"], label="Default")
housing = gr.Dropdown(["no", "yes", "unknown"], label="Housing Loan")
loan = gr.Dropdown(["no", "yes", "unknown"], label="Personal Loan")
contact = gr.Dropdown(["telephone", "cellular", "unknown"], label="Contact Type")
with gr.Row():
month = gr.Dropdown(["may", "jun", "jul", "aug", "oct", "nov", "dec", "mar", "apr", "sep", "jan", "feb"], label="Month")
day_of_week = gr.Dropdown(["mon", "tue", "wed", "thu", "fri"], label="Day of Week")
with gr.Row():
duration = gr.Number(label="Call Duration (seconds)", value=100)
campaign = gr.Number(label="Number of Contacts during Campaign", value=1)
pdays = gr.Number(label="Days since Last Contact", value=999)
previous = gr.Number(label="Number of Contacts before Campaign", value=0)
poutcome = gr.Dropdown(["nonexistent", "failure", "success", "unknown", "other"], label="Previous Outcome")
with gr.Row():
emp_var_rate = gr.Number(label="Employment Variation Rate", value=1.1)
cons_price_idx = gr.Number(label="Consumer Price Index", value=93.994)
cons_conf_idx = gr.Number(label="Consumer Confidence Index", value=-36.4)
euribor3m = gr.Number(label="Euribor 3-Month Rate", value=4.857)
nr_employed = gr.Number(label="Number of Employees", value=5191.0)
predict_btn = gr.Button("Predict")
output = gr.JSON()
predict_btn.click(
predict,
inputs=[age, job, marital, education, default, housing, loan, contact, month, day_of_week, duration, campaign, pdays, previous, poutcome, emp_var_rate, cons_price_idx, cons_conf_idx, euribor3m, nr_employed],
outputs=output
)
return app
# Launch the app
if __name__ == "__main__":
app = gradio_interface()
app.launch()
|