Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,106 +1,84 @@
|
|
| 1 |
-
#
|
| 2 |
import gradio as gr
|
| 3 |
import joblib
|
| 4 |
-
import pandas as pd
|
| 5 |
import numpy as np
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
return "
|
| 29 |
-
else :
|
| 30 |
-
return "D'occasion"
|
| 31 |
-
|
| 32 |
-
# Fonction de prédiction multiple
|
| 33 |
-
def Pred_func_csv(file):
|
| 34 |
-
# Lire le fichier csv
|
| 35 |
-
df = pd.read_csv(file)
|
| 36 |
-
predictions = []
|
| 37 |
-
# Boucle sur les lignes du dataframe
|
| 38 |
-
for row in df.iloc[:, :].values:
|
| 39 |
-
y_pred = Pred_funct_simple(row[0], row[1],row[2],row[3],row[4])
|
| 40 |
-
# ajouter la prediction sur List_predictions
|
| 41 |
-
predictions.append(y_pred)
|
| 42 |
-
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
#gr.themes.Monochrome()
|
| 50 |
-
# gr.themes.Citrus()
|
| 51 |
-
# gr.themes.Glass()
|
| 52 |
-
# gr.themes.Ocean()
|
| 53 |
|
| 54 |
-
#
|
| 55 |
-
|
|
|
|
| 56 |
'Chrysler', 'Citroen', 'Dacia', 'Daihatsu', 'Dodge', 'Fiat',
|
| 57 |
'Ford', 'GMC', 'Great Wall', 'Honda', 'Hyundai', 'Infinity',
|
| 58 |
'Isuzu', 'Jaguar', 'Jeep', 'Kia', 'Land Rover', 'Lexus', 'Lincoln',
|
| 59 |
'Maserati', 'Mazda', 'Mercedes-Benz', 'Mini', 'Mitsubishi',
|
| 60 |
'Nissan', 'Opel', 'Peugeot', 'Porsche', 'Range Rover', 'Renault',
|
| 61 |
'Saturn', 'Seat', 'Skoda', 'Ssangyong', 'Subaru', 'Suzuki', 'Tata',
|
| 62 |
-
'Toyota', 'Volkswagen'],
|
| 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 |
-
|
| 95 |
-
|
| 96 |
-
inputs = gr.File(label='Upload a csv file'),
|
| 97 |
-
outputs = gr.File(label='Download a csv file'),
|
| 98 |
-
title="Prédire si une voiture est du type Venant ou D'occasion avec la prédiction multiple",
|
| 99 |
-
)
|
| 100 |
-
|
| 101 |
-
# faire un tabbing des interfaces
|
| 102 |
-
with demo:
|
| 103 |
-
gr.TabbedInterface([interface1, interface2], ['Simple Prediction', 'Prédiction multiple'])
|
| 104 |
-
|
| 105 |
-
# lancer l'interface
|
| 106 |
-
demo.launch()
|
|
|
|
| 1 |
+
# Importer gradio
|
| 2 |
import gradio as gr
|
| 3 |
import joblib
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
# Télécharger le model
|
| 7 |
+
model = joblib.load("knn_model.joblib")
|
| 8 |
+
# Téléchager le scaler
|
| 9 |
+
scaler = joblib.load("scaler.joblib")
|
| 10 |
+
# Télécharger les encoders
|
| 11 |
+
encoder0 = joblib.load("marque.joblib")
|
| 12 |
+
encoder1 = joblib.load("transmission.joblib")
|
| 13 |
+
encoder2 = joblib.load('region.joblib')
|
| 14 |
+
# Foncton de prediction simple
|
| 15 |
+
def predict_simple(marque, annee, transmission, prix, region):
|
| 16 |
+
marque = encoder0.transform([marque])[0]
|
| 17 |
+
transmission = encoder1.transform([transmission])[0]
|
| 18 |
+
region = encoder2.transform([region])[0]
|
| 19 |
+
# vecteur des valeurs numériques
|
| 20 |
+
x_new = np.array([marque, annee, transmission, prix, region])
|
| 21 |
+
x_new = x_new.reshape(1,-1)
|
| 22 |
+
# Normaliser les données
|
| 23 |
+
x_new = scaler.transform(x_new)
|
| 24 |
+
# Prediction
|
| 25 |
+
y_pred = model.predict(x_new)
|
| 26 |
+
# Afficher
|
| 27 |
+
return "Venante" if y_pred == 1 else "Occasion"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
|
| 29 |
+
# FOnction de prediction multiple
|
| 30 |
+
def predict_multiple(file):
|
| 31 |
+
# Lire le fichier
|
| 32 |
+
df = pd.read_csv(file)
|
| 33 |
+
predictions = []
|
| 34 |
+
for row in df.iloc[:, :].values:
|
| 35 |
+
y_pred = predict_simple(row[0], row[1],row[2],row[3],row[4])
|
| 36 |
+
# ajouter la prediction sur List_predictions
|
| 37 |
+
predictions.append(y_pred)
|
| 38 |
|
| 39 |
+
df.to_csv('predictions.csv', index = False)
|
| 40 |
+
return 'predictions.csv'
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Interface gradio
|
| 43 |
+
demo = gr.Blocks(theme = gr.themes.Glass())
|
| 44 |
+
inputs = [gr.Dropdown(choices= ['Acura', 'Audi', 'Autres', 'BMW', 'Buick', 'Cadillac', 'Chevrolet',
|
| 45 |
'Chrysler', 'Citroen', 'Dacia', 'Daihatsu', 'Dodge', 'Fiat',
|
| 46 |
'Ford', 'GMC', 'Great Wall', 'Honda', 'Hyundai', 'Infinity',
|
| 47 |
'Isuzu', 'Jaguar', 'Jeep', 'Kia', 'Land Rover', 'Lexus', 'Lincoln',
|
| 48 |
'Maserati', 'Mazda', 'Mercedes-Benz', 'Mini', 'Mitsubishi',
|
| 49 |
'Nissan', 'Opel', 'Peugeot', 'Porsche', 'Range Rover', 'Renault',
|
| 50 |
'Saturn', 'Seat', 'Skoda', 'Ssangyong', 'Subaru', 'Suzuki', 'Tata',
|
| 51 |
+
'Toyota', 'Volkswagen'],label= "Marque"),
|
| 52 |
+
gr.Slider(2000, 2025, step=1, label="Année"),
|
| 53 |
+
gr.Dropdown(choices=['Automatique', 'Manuelle', 'Tiptronic'],label="Transmission"),
|
| 54 |
+
gr.Number(label="Prix"),
|
| 55 |
+
gr.Dropdown(choices=['Almadies', 'Almadies 2', 'Amitié', 'Avenue Bourguiba', 'Bambilor',
|
| 56 |
+
'Bargny', 'Bel air', 'Biscuiterie', 'Cambérène', 'Castor',
|
| 57 |
+
'Cite Mixta', 'Cité Damel', 'Cité asecna', 'Cité assemblée',
|
| 58 |
+
'Cité avion', 'Cité biagui', 'Cité keur gorgui', 'Colobane',
|
| 59 |
+
'Comico', 'Dalifort', 'Derkle', 'Diamaguene', 'Diamniadio',
|
| 60 |
+
'Dieuppeul', 'Djily mbaye', 'Fann', 'Fann Hock', 'Fenêtre mermoz',
|
| 61 |
+
'Gibraltar', 'Golf', 'Gorée', 'Grand-Dakar', 'Grand-Yoff',
|
| 62 |
+
'Guediawaye', 'Gueule-Tapée', 'Hann Bel-Air', 'Hann marinas',
|
| 63 |
+
'Hann maristes', 'Hlm', 'Hlm grand-yoff', 'Keur Massar',
|
| 64 |
+
'Keur ndiaye lô', 'Lac Rose', 'Lac rose', 'Liberte 1', 'Liberte 2',
|
| 65 |
+
'Liberte 3', 'Liberte 4', 'Liberte 5', 'Liberte 6',
|
| 66 |
+
'Liberte 6 extension', 'Malika', 'Mamelles', 'Mbao', 'Mermoz',
|
| 67 |
+
'Médina', 'Ndiakhirate', 'Ngor', 'Niague', 'Nord foire', 'Ouakam',
|
| 68 |
+
'Ouest foire', 'Parcelles Assainies', 'Patte d‘oie', 'Pikine',
|
| 69 |
+
'Plateau', 'Point-e', 'Rufisque', 'Sacré-cœur', 'Sangalkam',
|
| 70 |
+
'Sebikotane', 'Sicap Liberté', 'Sicap Sacré-cœur', 'Sicap baobab',
|
| 71 |
+
'Sicap foire', 'Sicap mbao', 'Sud foire', 'Thiaroye',
|
| 72 |
+
'Tivaouane peulh', 'VDN', 'Virage', 'Yene', 'Yeumbeul', 'Yoff',
|
| 73 |
+
'Zac Mbao', 'Zone de captage'], label="Region")]
|
| 74 |
+
outputs = gr.Textbox(label="Etat")
|
| 75 |
+
interface1 = gr.Interface(fn = predict_simple,inputs=inputs,outputs=outputs,
|
| 76 |
+
title=" Predire si une voiture est venante ou occasion",
|
| 77 |
+
description=" Faire la prediction simple pour savoir si une voiture est venante ou occasion")
|
| 78 |
+
interface2 = gr.Interface(fn = predict_multiple, inputs=gr.File(label="Téléversez un fichier csv"),
|
| 79 |
+
outputs=gr.File(label="Télécharger votre fichier"),
|
| 80 |
+
title=" Predire si une voiture est venante ou occasion avec une prediction multiple",
|
| 81 |
+
description=" Faire la prediction simple pour savoir si une voiture est venante ou occasion avec une prediction multiple")
|
| 82 |
+
with demo :
|
| 83 |
+
gr.TabbedInterface([interface1, interface2],['Simple Prediction', 'Prédiction multiple'])
|
| 84 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|