File size: 2,801 Bytes
dc51856
 
7ce110a
 
dc51856
3cc2196
7ce110a
 
b8befe8
 
7ce110a
b8befe8
dc51856
b8befe8
 
 
7ce110a
 
dc51856
 
 
 
 
 
 
 
 
 
 
7ce110a
5a3a9a3
7ce110a
5a3a9a3
dc51856
7ce110a
 
5a3a9a3
 
 
 
 
 
 
 
 
 
 
dc51856
 
 
 
 
 
 
 
5a3a9a3
dc51856
 
5a3a9a3
dc51856
5a3a9a3
dc51856
 
 
35aad6d
 
 
 
 
dc51856
 
 
54daf5b
 
 
 
 
 
 
1418093
 
54daf5b
 
 
 
 
 
 
dc51856
 
1418093
dc51856
3cc2196
 
 
 
21b208a
7ce110a
dc51856
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
95
96
97
98
99
100
import uvicorn
import pandas as pd 
from pydantic import BaseModel
from fastapi import FastAPI
from joblib import load
import os

description = """
API GetAround – conçue pour vous aider à prédire le prix de location de votre voiture !
Voici les points d’accès disponibles :

/ : Cet endpoint permet d’explorer la fonctionnalité de prédiction.

/predict : Cet endpoint accepte une requête POST avec des données JSON en entrée. Vous pouvez l’utiliser pour effectuer des prédictions en fournissant les informations nécessaires sur votre voiture.

N’hésitez pas à utiliser l’endpoint /predict en envoyant une requête POST avec les données JSON requises pour obtenir une estimation précise du prix de location de votre véhicule.
"""

tags_metadata = [
    {
        "name": "Simple Endpoint",
        "description": "Simple endpoint to try out!",
    },
    {
        "name": "Prediction",
        "description": "Prediction of the rental price based"
    }
]

app = FastAPI(
    title="🚙 GetAround price prediction ",
    description=description,
    version="1.0",
    openapi_tags=tags_metadata,
)

# ✅ Vérification du chargement du modèle
model = None
model_status = "❌ Modèle non chargé"

try:
    model = load("model.joblib")
    model_status = "✅ Modèle chargé avec succès"
except Exception as e:
    model_status = f"❌ Erreur de chargement du modèle : {e}"


# Data types for prediction
class PredictionFeatures(BaseModel):
    model_key: str = "Peugeot"
    mileage: int = 13131
    engine_power: int = 110
    fuel: str = "diesel"
    paint_color: str = "grey"
    car_type: str = "convertible"
    private_parking_available: bool = True
    has_gps: bool = True
    has_air_conditioning: bool = True
    automatic_car: bool = True
    has_getaround_connect: bool = True
    has_speed_regulator: bool = True
    winter_tires: bool = True

@app.get("/", tags=["Simple Endpoint"])
def index():
    return {
        "message": "Bienvenue sur l'API GET Around! auteur —> Jerome Moulinier",
        "model_status": model_status
    }

@app.post("/predict", tags=["Prediction"])
async def predict(features: PredictionFeatures):
    try:
        # Convertir en DataFrame
        information = pd.DataFrame([features.dict()])
        
        # Prédiction
        prediction = model.predict(information)

        # Résultat arrondi à 2 décimales
        return {"prediction": round(prediction.tolist()[0], 2)}

    except Exception as e:
        return {
            "error": str(e),
            "columns_received": list(information.columns),
            "example_data": information.to_dict(orient="records")[0]
        }



if __name__ == "__main__":
    
    port = int(os.getenv("PORT", 7860))
    uvicorn.run(app, host="0.0.0.0", port=port)