Iralion's picture
Update app.py
734c4b4 verified
# Fonction de prédiction
import gradio as gr
import joblib
import pandas as pd
import numpy as np
from keras.models import load_model
# Valeurs uniques
uniques = joblib.load('uniques.joblib')
# importer les encodeurs
encoders = joblib.load(f'encoders.joblib')
# 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'
def pred_csv(file):
df = pd.read_csv(file)
predictions = []
for row in df.iloc[:, :].values:
y_pred = prediction_func(
row[0], row[1], row[2], row[3], row[4],
row[5], row[6], row[7], row[8], row[9],
row[10], row[11], row[12], row[13], row[14]
)
predictions.append(y_pred)
df['y'] = predictions
df.to_csv("predictions.csv", index=False)
return "predictions.csv"
# Definir le theme
demo = gr.Blocks(theme = gr.themes.Glass())
# load les valeurs uniques
uniques = joblib.load('uniques.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
interface1 = gr.Interface(fn = prediction_func,
inputs = inputs,
outputs = outputs,
title="Bank marcketing"
)
# Créer l'interface 2
interface2 = gr.Interface(fn = pred_csv,
inputs = gr.File(label='Upload a csv file'),
outputs = gr.File(label='Download a csv file'),
title="Bank marcketing prédiction multiple",
)
# faire un tabbing des interfaces
with demo:
gr.TabbedInterface([interface1, interface2], ['Simple Prediction', 'Prédiction multiple'])
# lancer l'interface
demo.launch()