Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pickle
|
| 4 |
+
from sklearn.preprocessing import LabelEncoder, StandardScaler
|
| 5 |
+
from sklearn.linear_model import Ridge
|
| 6 |
+
|
| 7 |
+
# Charger le modèle et le scaler
|
| 8 |
+
with open("ridge_model.pkl", "rb") as model_file:
|
| 9 |
+
ridge_model = pickle.load(model_file)
|
| 10 |
+
|
| 11 |
+
with open("scaler.pkl", "rb") as scaler_file:
|
| 12 |
+
scaler = pickle.load(scaler_file)
|
| 13 |
+
|
| 14 |
+
# Initialisation des encodeurs
|
| 15 |
+
label_encoder_fuel = LabelEncoder()
|
| 16 |
+
label_encoder_seller = LabelEncoder()
|
| 17 |
+
label_encoder_trans = LabelEncoder()
|
| 18 |
+
|
| 19 |
+
# Fonction pour effectuer une prédiction
|
| 20 |
+
def predict(Kms_Driven, Present_Price, Fuel_Type, Seller_Type, Transmission, Age):
|
| 21 |
+
try:
|
| 22 |
+
# Encoder les variables catégorielles
|
| 23 |
+
Fuel_Type_encoded = label_encoder_fuel.fit_transform([Fuel_Type])[0]
|
| 24 |
+
Seller_Type_encoded = label_encoder_seller.fit_transform([Seller_Type])[0]
|
| 25 |
+
Transmission_encoded = label_encoder_trans.fit_transform([Transmission])[0]
|
| 26 |
+
|
| 27 |
+
# Préparer les caractéristiques d'entrée
|
| 28 |
+
input_features = np.array([
|
| 29 |
+
Kms_Driven,
|
| 30 |
+
Present_Price,
|
| 31 |
+
Fuel_Type_encoded,
|
| 32 |
+
Seller_Type_encoded,
|
| 33 |
+
Transmission_encoded,
|
| 34 |
+
Age
|
| 35 |
+
]).reshape(1, -1)
|
| 36 |
+
|
| 37 |
+
# Normaliser les données
|
| 38 |
+
input_features_scaled = scaler.transform(input_features)
|
| 39 |
+
|
| 40 |
+
# Faire une prédiction
|
| 41 |
+
prediction = ridge_model.predict(input_features_scaled)
|
| 42 |
+
|
| 43 |
+
return prediction[0]
|
| 44 |
+
except Exception as e:
|
| 45 |
+
return f"Erreur : {str(e)}"
|
| 46 |
+
|
| 47 |
+
# Définir les entrées
|
| 48 |
+
inputs = [
|
| 49 |
+
gr.Number(label="Kms Driven"),
|
| 50 |
+
gr.Number(label="Present Price"),
|
| 51 |
+
gr.Dropdown(["Petrol", "Diesel"], label="Fuel Type"),
|
| 52 |
+
gr.Dropdown(["Individual", "Dealer"], label="Seller Type"),
|
| 53 |
+
gr.Dropdown(["Manual", "Automatic"], label="Transmission"),
|
| 54 |
+
gr.Number(label="Age")
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
# Définir la sortie
|
| 58 |
+
outputs = gr.Textbox(label="Prédiction")
|
| 59 |
+
|
| 60 |
+
# Créer l'application Gradio
|
| 61 |
+
app = gr.Interface(
|
| 62 |
+
fn=predict,
|
| 63 |
+
inputs=inputs,
|
| 64 |
+
outputs=outputs,
|
| 65 |
+
title="Prédictions avec le modèle Ridge",
|
| 66 |
+
description="Entrez les valeurs des caractéristiques pour obtenir une prédiction."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# Lancer l'application
|
| 70 |
+
app.launch(debug=True)
|