Iralion's picture
Update app.py
144f9bd verified
raw
history blame
4.36 kB
import gradio as gr
import pandas as pd
import numpy as np
import joblib
# Télécharger 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')
encoder9 = joblib.load('y.joblib')
# Import du modèle du modele performent
model = joblib.load('rf.joblib')
# Import du normaliseur
scaler = joblib.load('scaler.joblib')
def prediction_simple(age, job, marital, education, housing, loan, contact, month, day_of_week,duration,campaign,pdays, previous, poutcome) :
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]
# Transformer les variables envecteurs
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
x_new = scaler.transform(x_new)
# Prediction
y_pred = model.predict(x_new)
return "Souscrire" if y_pred==1 else "Pas souscrire"
# Fonction de prédiction multiple
def Pred_func_csv(file):
# Lire le fichier csv
df = pd.read_csv(file)
predictions = []
# Boucle sur les lignes du dataframe
for row in df.iloc[:, :].values:
y_pred = Pred_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])
# ajouter la prediction sur List_predictions
predictions.append(y_pred)
df['y'] = predictions
df.to_csv('predictions.csv', index = False)
return 'predictions.csv'
# définir les blocks
demo = gr.Blocks(theme='earneleh/paris')
#gr.themes.Monochrome()
# gr.themes.Citrus()
# gr.themes.Glass()
# gr.themes.Ocean()
# Créer les inputs
inputs = [gr.Number(label='AGE'),
gr.Dropdown(choices=['admin.', 'blue-collar', 'entrepreneur', 'housemaid', 'management',
'retired', 'self-employed', 'services', 'student', 'technician',
'unemployed', 'unknown'],
label='JOB'),
gr.Dropdown(choices=['divorced', 'married', 'single', 'unknown'],label='MARITAL'),
gr.Dropdown(choices=['basic.4y', 'basic.6y', 'basic.9y', 'high.school', 'illiterate',
'professional.course', 'university.degree', 'unknown'], label='EDUCATION'),
gr.Dropdown(choices=['no', 'unknown', 'yes'], label='HOUSING'),
gr.Dropdown(choices=['no', 'unknown', 'yes'],label='LOAN'),
gr.Radio(choices=['cellular', 'telephone'],label='CONTACT'),
gr.Dropdown(choices=['apr', 'aug', 'dec', 'jul', 'jun', 'mar', 'may', 'nov', 'oct','sep'],label='MONTH'),
gr.Radio(choices=['fri', 'mon', 'thu', 'tue', 'wed'],label='DAY_OF_WEEK'),
gr.Number(label='DURATION'),
gr.Number(label='CAMPAIGN'),
gr.Number(label='PDAYS'),
gr.Number(label='PREVIOUS'),
gr.Dropdown(choices=['failure', 'nonexistent', 'success'], label='POUTCOME')]
# Créer les outputs
outputs = gr.Textbox(label='Y')
# Créer l'interface 1
interface1 = gr.Interface(fn = prediction_simple,
inputs = inputs,
outputs = outputs,
title="Prédire si un client souscrira à un dépot a terme",
)
# Créer l'interface 2
interface2 = gr.Interface(fn = Pred_func_csv,
inputs = gr.File(label='Upload a csv file'),
outputs = gr.File(label='Download a csv file'),
title="Prédire si un client souscrira à un dépot a terme avec la prédiction multiple",
)
# faire un tabbing des interfaces
with demo:
gr.TabbedInterface([interface1, interface2], ['Simple Prediction', 'Prédiction multiple'])
# lancer l'interface
demo.launch()