File size: 3,655 Bytes
4fb37b1 c87599f 4fb37b1 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
import numpy as np
import pandas as pd
# Fonction de prédiction
import gradio as gr
import joblib
# importer les encodeurs
encoder0 = joblib.load('Fuel_Type.joblib')
encoder1 = joblib.load('Seller_Type.joblib')
encoder2 = joblib.load('Transmission.joblib')
# importer le modèle
xgb = joblib.load('xgb.joblib')
# importer le normaliseur
scaler = joblib.load('scaler.joblib')
def Pred_func(Kms_Driven,Present_Price, Fuel_Type, Seller_Type, Transmission, Age):
# Encoder les valeurs des Fuel_Type, Seller_Type et Transmission
Fuel_Type = encoder0.transform([Fuel_Type])[0]
Seller_Type = encoder1.transform([Seller_Type])[0]
Transmission = encoder2.transform([Transmission])[0]
# vecteur des valeurs numériques
x_new = np.array([Kms_Driven,Present_Price, Fuel_Type, Seller_Type, Transmission, Age])
x_new = x_new.reshape(1,-1) # convert en un 2D array
# Normaliser les données
x_new = scaler.transform(x_new)
# Prédire
y_pred = xgb.predict(x_new)
# Arrondir
y_pred = round(y_pred[0],2)
return str(y_pred) + 'k$'
# 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:
# nouvelle ligne avec les valeurs des Fuel_Type, Seller_Type et Transmission encodées
new_row = np.array([row[0], row[1], encoder0.transform([row[2]])[0], encoder1.transform([row[3]])[0], encoder2.transform([row[4]])[0], row[5]])
new_row = new_row.reshape(1,-1) # convertir en un 2D array
# Normaliser les données
new_row = scaler.transform(new_row)
# Prédire
y_pred = xgb.predict(new_row)
# Arrondir
y_pred = round(y_pred[0],2)
# ajouter la prediction sur List_predictions
predictions.append(y_pred)
df['Selling_Price'] = predictions
df.to_csv('predictions.csv', index = False)
return 'predictions.csv'
# définir les blocks
demo = gr.Blocks(theme='NoCrypt/miku')
# Créer les inputs
inputs = [gr.Number(label='Kms Driven'),
gr.Number(label='Present Price'),
gr.Radio(choices=['Petrol', 'Diesel', 'CNG'], label='Fuel Type'),
gr.Radio(choices=['Dealer', 'Individual'], label='Seller Type'),
gr.Radio(choices=['Manual', 'Automatic'], label='Transmission'),
gr.Number(label='Age')]
# Créer les outputs
outputs = gr.Textbox(label='Selling Price')
# Créer l'interface 1
interface1 = gr.Interface(fn = Pred_func,
inputs = inputs,
outputs = outputs,
title="Predict the selling price of a car with a single input",
description = """This machine learning model allows us to predict the selling price of a car
from the kms driven, present price, fuel type, seller type, transmission and age of the car.
""")
# 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="Predict the selling price of a car with a multiple inputs",
description = """This machine learning model allows us to predict the selling price of a car
from the kms driven, present price, fuel type, seller type, transmission and age of the car.
""")
# faire un tabbing des interfaces
with demo:
gr.TabbedInterface([interface1, interface2], ['Simple Prediction', 'Prédiction multiple'])
# lancer l'interface
demo.launch()
|