greykingreys's picture
Update app.py
9ccbfdc verified
#Deployement avec Gradio
#fonctions de predictions
import gradio as gr
import joblib
import pandas as pd
import numpy as np
#importer les encoders
encoder0 = joblib.load('job.joblib')
encoder1 = joblib.load('marital.joblib')
encoder2 = joblib.load('education.joblib')
encoder3 = joblib.load('housing.joblib')
encoder4 = joblib.load('loan.joblib')
encoder5 = joblib.load('contact.joblib')
encoder6 = joblib.load('month.joblib')
encoder7 = joblib.load('day_of_week.joblib')
encoder8 = joblib.load('poutcome.joblib')
#Importer les listes
job = joblib.load('job_list.joblib')
marital = joblib.load('marital_list.joblib')
education = joblib.load('education_list.joblib')
housing = joblib.load('housing_list.joblib')
loan = joblib.load('loan_list.joblib')
contact = joblib.load('contact_list.joblib')
month = joblib.load('month_list.joblib')
day_of_week = joblib.load('day_of_week_list.joblib')
poutcome = joblib.load('poutcome_list.joblib')
y = joblib.load('y_list.joblib')
#importer le model
gb_model = joblib.load('gb_model.joblib')
#importer le normaliser
scaler = joblib.load('scaler.joblib')
def pred_fun(age,job,marital,education,housing,loan,contact,month,day_of_week,duration,campaign,pdays,previous,poutcome):
#Encoder les variables marque, transmission, quartier
job = encoder0.transform([job])[0]
marital = encoder1.transform([marital])[0]
education = encoder2.transform([education])[0]
housing = encoder3.transform([housing])[0]
loan = encoder4.transform([loan])[0]
contact = encoder5.transform([contact])[0]
month = encoder6.transform([month])[0]
day_of_week = encoder7.transform([day_of_week])[0]
poutcome = encoder8.transform([poutcome])[0]
#vecteurs des valeurs numerique
x_new = np.array( (age,job,marital,education,housing,loan,contact,month,day_of_week,duration,campaign,pdays,previous,poutcome))
x_new = x_new.reshape(1, -1)
#normaliser les données
x_new = scaler.transform(x_new)
#predire
y_pred = gb_model.predict(x_new)
#arrondir
y_pred = round(y_pred[0], 2)
return f"{'Yes'if y_pred == 1 else 'no'}"
def pred_fun_csv(file):
#lire le fichier csv
df = pd.read_csv(file)
predictions = []
for row in df.iloc[:, :].values:
predictions.append(pred_fun(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]))
df['y'] = predictions
df.to_csv('predictions.csv', index = False)
return 'predictions.csv'
demo = gr.Blocks(theme = gr.themes.Monochrome())
#creer les inputes
inputs = [
gr.Number(label = 'age'),
gr.Dropdown(choices = job, label = 'job'),
gr.Dropdown(choices = marital, label = 'martial'),
gr.Dropdown(choices = education, label = 'education'),
gr.Dropdown(choices = housing, label = 'housing'),
gr.Dropdown(choices = loan, label = 'loan'),
gr.Dropdown(choices = contact, label = 'contact'),
gr.Dropdown(choices = month, label = 'month'),
gr.Dropdown(choices = day_of_week, label = 'day_of_week'),
gr.Number(label = 'duration'),
gr.Number(label = 'campaign'),
gr.Number(label = 'pdays'),
gr.Number(label = 'previous'),
gr.Dropdown(choices = poutcome, label = 'poutcome')
]
Outputs = gr.Textbox(label = 'Etat')
interface1 = gr.Interface(
fn = pred_fun,
inputs = inputs,
outputs = Outputs,
title = "Saisir les donnees",
description = """Cette modele predi si un client va s'ouscrire ou non a partir de quelques informations"""
)
interface2 = gr.Interface(
fn = pred_fun_csv,
inputs = gr.File(label = 'Televerser le fichier csv'),
outputs = gr.File(label = 'Telecharger le ficher csv'),
title = "Televerser un fichier csv",
description = """Cette modele predi si un client va s'ouscrire ou non a partir de quelques informations"""
)
with demo:
gr.TabbedInterface([interface1, interface2], ['simple prediction', 'multiple predictions'])
demo.launch()