Spaces:
Sleeping
Sleeping
| # Fonction de prédiction | |
| import gradio as gr | |
| import joblib | |
| from keras.models import load_model | |
| import pandas as pd | |
| import numpy as np | |
| #importer la liste des noms des variables cattégo | |
| cat_data_columns= joblib.load('cat_data_columns.joblib') | |
| # importer les encodeurs | |
| encoders = [] | |
| for i in range(len(cat_data_columns)): | |
| encoders.append(joblib.load(f'{cat_data_columns[i]}_encoder.joblib')) | |
| # importer le modèle | |
| model = load_model('DNN_model.h5') | |
| # importer le scaler | |
| scaler = joblib.load('scaler.joblib') | |
| # Fonction de prédiction simple | |
| def prediction_func(age, job, marital, education, default, housing, loan, contact, month, day_of_week, duration, campaign, pdays, previous, poutcome): | |
| # encoder les valeurs | |
| job = encoders[0].transform([job])[0] | |
| marital = encoders[1].transform([marital])[0] | |
| education = encoders[2].transform([education])[0] | |
| default = encoders[3].transform([default])[0] | |
| housing = encoders[4].transform([housing])[0] | |
| loan = encoders[5].transform([loan])[0] | |
| contact = encoders[6].transform([contact])[0] | |
| month = encoders[7].transform([month])[0] | |
| day_of_week = encoders[8].transform([day_of_week])[0] | |
| poutcome = encoders[9].transform([poutcome])[0] | |
| # vecteur des valeurs | |
| x_new = np.array([age, job, marital, education, default, housing, loan, contact, month, day_of_week, duration, campaign, pdays, previous, poutcome]).reshape(1, -1) | |
| # normaliser les valeurs | |
| x_new = scaler.transform(x_new) | |
| # prédire la valeur | |
| y_pred = np.round(model.predict(x_new)) | |
| # retourner | |
| if y_pred == 1: | |
| return 'Souscrire' | |
| else: | |
| return 'Pas souscrire' | |
| # load les valeurs uniques | |
| uniques = [] | |
| for i in range(len(cat_data_columns)): | |
| uniques.append(joblib.load(f'{cat_data_columns[i]}_unique.joblib')) | |
| # créer les inputs | |
| inputs = [gr.Number(label="age"), | |
| gr.Dropdown(uniques[0], label="job"), | |
| gr.Dropdown(uniques[1], label="marital"), | |
| gr.Dropdown(uniques[2], label="education"), | |
| gr.Dropdown(uniques[3], label="default"), | |
| gr.Dropdown(uniques[4], label="housing"), | |
| gr.Dropdown(uniques[5], label="loan"), | |
| gr.Dropdown(uniques[6], label="contact"), | |
| gr.Dropdown(uniques[7], label="month"), | |
| gr.Dropdown(uniques[8], label="day_of_week"), | |
| gr.Number(label="duration"), | |
| gr.Number(label="campaign"), | |
| gr.Number(label="pdays"), | |
| gr.Number(label="previous"), | |
| gr.Dropdown(uniques[9], label="poutcome")] | |
| # créer les outputs | |
| outputs = gr.Textbox(label = 'Souscription') | |
| # Interface | |
| Interface =gr.Interface(fn = prediction_func, | |
| inputs = inputs, | |
| outputs = outputs, | |
| title = 'Bank Marketing Prediction') | |
| Interface.launch() |