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