Spaces:
Sleeping
Sleeping
| # Fonction de prédiction | |
| import gradio as gr | |
| import joblib | |
| import pandas as pd | |
| import numpy as np | |
| # 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 = gr.themes.Ocean()) | |
| #gr.themes.Monochrome() | |
| # gr.themes.Citrus() | |
| # gr.themes.Glass() | |
| # gr.themes.Ocean() | |
| # Créer les inputs | |
| inputs = [gr.Number(label='Kms Driven'), | |
| gr.Number(label='Present Price'), | |
| gr.Dropdown(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() | |