Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Fonction de prédiction
|
| 2 |
+
import gradio as gr
|
| 3 |
+
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
from keras.models import load_model
|
| 7 |
+
# importer les encodeurs
|
| 8 |
+
cat_data_columns= joblib.load('cat_data_columns.joblib')
|
| 9 |
+
# importer les encodeurs
|
| 10 |
+
encoders = []
|
| 11 |
+
for i in range(len(cat_data_columns)):
|
| 12 |
+
encoders.append(joblib.load(f'{cat_data_columns[i]}_encoder.joblib'))
|
| 13 |
+
# importer le modèle
|
| 14 |
+
model = load_model('DNN_model.h5')
|
| 15 |
+
# importer le scaler
|
| 16 |
+
scaler = joblib.load('scaler.joblib')
|
| 17 |
+
# Fonction de prédiction simple
|
| 18 |
+
def prediction_func(age, job, marital, education, default, housing, loan, contact, month, day_of_week, duration, campaign, pdays, previous, poutcome):
|
| 19 |
+
# encoder les valeurs
|
| 20 |
+
job = encoders[0].transform([job])[0]
|
| 21 |
+
marital = encoders[1].transform([marital])[0]
|
| 22 |
+
education = encoders[2].transform([education])[0]
|
| 23 |
+
default = encoders[3].transform([default])[0]
|
| 24 |
+
housing = encoders[4].transform([housing])[0]
|
| 25 |
+
loan = encoders[5].transform([loan])[0]
|
| 26 |
+
contact = encoders[6].transform([contact])[0]
|
| 27 |
+
month = encoders[7].transform([month])[0]
|
| 28 |
+
day_of_week = encoders[8].transform([day_of_week])[0]
|
| 29 |
+
poutcome = encoders[9].transform([poutcome])[0]
|
| 30 |
+
# vecteur des valeurs
|
| 31 |
+
x_new = np.array([age, job, marital, education, default, housing, loan, contact, month, day_of_week, duration, campaign, pdays, previous, poutcome]).reshape(1, -1)
|
| 32 |
+
# normaliser les valeurs
|
| 33 |
+
x_new = scaler.transform(x_new)
|
| 34 |
+
# prédire la valeur
|
| 35 |
+
y_pred = np.round(model.predict(x_new))
|
| 36 |
+
# retourner
|
| 37 |
+
if y_pred == 1:
|
| 38 |
+
return 'Souscrire'
|
| 39 |
+
else:
|
| 40 |
+
return 'Pas souscrire'
|
| 41 |
+
# load les valeurs uniques
|
| 42 |
+
uniques = []
|
| 43 |
+
for i in range(len(cat_data_columns)):
|
| 44 |
+
uniques.append(joblib.load(f'{cat_data_columns[i]}_unique.joblib'))
|
| 45 |
+
# créer les inputs
|
| 46 |
+
inputs = [gr.Number(label="age"),
|
| 47 |
+
gr.Dropdown(uniques[0], label="job"),
|
| 48 |
+
gr.Dropdown(uniques[1], label="marital"),
|
| 49 |
+
gr.Dropdown(uniques[2], label="education"),
|
| 50 |
+
gr.Dropdown(uniques[3], label="default"),
|
| 51 |
+
gr.Dropdown(uniques[4], label="housing"),
|
| 52 |
+
gr.Dropdown(uniques[5], label="loan"),
|
| 53 |
+
gr.Dropdown(uniques[6], label="contact"),
|
| 54 |
+
gr.Dropdown(uniques[7], label="month"),
|
| 55 |
+
gr.Dropdown(uniques[8], label="day_of_week"),
|
| 56 |
+
gr.Number(label="duration"),
|
| 57 |
+
gr.Number(label="campaign"),
|
| 58 |
+
gr.Number(label="pdays"),
|
| 59 |
+
gr.Number(label="previous"),
|
| 60 |
+
gr.Dropdown(uniques[9], label="poutcome")]
|
| 61 |
+
# créer les outputs
|
| 62 |
+
outputs = gr.Textbox(label = 'Souscription')
|
| 63 |
+
# Interface
|
| 64 |
+
Interface =gr.Interface(fn = prediction_func,
|
| 65 |
+
inputs = inputs,
|
| 66 |
+
outputs = outputs,
|
| 67 |
+
title = 'Bank Marketing Prediction',
|
| 68 |
+
theme = 'earneleh/paris')
|
| 69 |
+
|
| 70 |
+
Interface.launch()
|