Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,50 +1,131 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
-
import joblib
|
| 4 |
-
import numpy as np
|
| 5 |
from typing import List
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
app = FastAPI(
|
| 8 |
title="GetAround Pricing API",
|
| 9 |
version="1.0.0",
|
| 10 |
-
description="
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
class PredictionRequest(BaseModel):
|
| 21 |
-
|
| 22 |
|
| 23 |
class PredictionResponse(BaseModel):
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
@app.get("/")
|
| 27 |
def root():
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
|
|
|
| 31 |
|
| 32 |
@app.get("/health")
|
| 33 |
def health():
|
| 34 |
-
|
| 35 |
|
| 36 |
@app.post("/predict", response_model=PredictionResponse)
|
| 37 |
-
def predict(
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
X_processed = preprocessor.transform(df)
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, HTTPException
|
| 2 |
+
from pydantic import BaseModel, Field
|
|
|
|
|
|
|
| 3 |
from typing import List
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
import joblib
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# ======================================================
|
| 11 |
+
# 🔵 API METADATA
|
| 12 |
+
# ======================================================
|
| 13 |
|
| 14 |
app = FastAPI(
|
| 15 |
title="GetAround Pricing API",
|
| 16 |
version="1.0.0",
|
| 17 |
+
description="""
|
| 18 |
+
API permettant de prédire le prix de location journalier d’un véhicule GetAround.
|
| 19 |
+
Basée sur un pipeline sklearn (préprocessing + RandomForest).
|
| 20 |
+
|
| 21 |
+
### 🔍 Exemple d’input :
|
| 22 |
+
|
| 23 |
+
```json
|
| 24 |
+
{
|
| 25 |
+
"input": [
|
| 26 |
+
{
|
| 27 |
+
"model_key": "citroen",
|
| 28 |
+
"mileage": 150000,
|
| 29 |
+
"engine_power": 90,
|
| 30 |
+
"fuel": "diesel",
|
| 31 |
+
"paint_color": "black",
|
| 32 |
+
"car_type": "sedan",
|
| 33 |
+
"private_parking_available": 0,
|
| 34 |
+
"has_gps": 1,
|
| 35 |
+
"has_air_conditioning": 1,
|
| 36 |
+
"automatic_car": 0,
|
| 37 |
+
"has_getaround_connect": 1,
|
| 38 |
+
"has_speed_regulator": 1,
|
| 39 |
+
"winter_tires": 0
|
| 40 |
+
}
|
| 41 |
+
]
|
| 42 |
+
}
|
| 43 |
+
````
|
| 44 |
+
|
| 45 |
+
```
|
| 46 |
+
"""
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
)
|
| 50 |
|
| 51 |
+
# ======================================================
|
| 52 |
+
|
| 53 |
+
# 🔵 Load model
|
| 54 |
+
|
| 55 |
+
# ======================================================
|
| 56 |
+
|
| 57 |
+
MODEL_PATH = Path(**file**).parent / "model.joblib"
|
| 58 |
+
|
| 59 |
+
try:
|
| 60 |
+
bundle = joblib.load(MODEL_PATH)
|
| 61 |
+
pipeline = bundle["model"] # <-- pipeline complet
|
| 62 |
+
feature_columns = bundle["feature_columns"]
|
| 63 |
+
except Exception as e:
|
| 64 |
+
raise RuntimeError(f"Erreur chargement modèle : {e}")
|
| 65 |
+
|
| 66 |
+
# ======================================================
|
| 67 |
+
|
| 68 |
+
# 🔵 INPUT SCHEMA
|
| 69 |
+
|
| 70 |
+
# ======================================================
|
| 71 |
+
|
| 72 |
+
class CarFeatures(BaseModel):
|
| 73 |
+
model_key: str = Field(..., example="citroen")
|
| 74 |
+
mileage: float = Field(..., example=150000)
|
| 75 |
+
engine_power: float = Field(..., example=90)
|
| 76 |
+
fuel: str = Field(..., example="diesel")
|
| 77 |
+
paint_color: str = Field(..., example="black")
|
| 78 |
+
car_type: str = Field(..., example="sedan")
|
| 79 |
+
private_parking_available: int = Field(..., example=0)
|
| 80 |
+
has_gps: int = Field(..., example=1)
|
| 81 |
+
has_air_conditioning: int = Field(..., example=1)
|
| 82 |
+
automatic_car: int = Field(..., example=0)
|
| 83 |
+
has_getaround_connect: int = Field(..., example=1)
|
| 84 |
+
has_speed_regulator: int = Field(..., example=1)
|
| 85 |
+
winter_tires: int = Field(..., example=0)
|
| 86 |
|
| 87 |
class PredictionRequest(BaseModel):
|
| 88 |
+
input: List[CarFeatures]
|
| 89 |
|
| 90 |
class PredictionResponse(BaseModel):
|
| 91 |
+
prediction: List[float]
|
| 92 |
+
|
| 93 |
+
# ======================================================
|
| 94 |
+
|
| 95 |
+
# 🔵 ROUTES
|
| 96 |
+
|
| 97 |
+
# ======================================================
|
| 98 |
|
| 99 |
@app.get("/")
|
| 100 |
def root():
|
| 101 |
+
return {
|
| 102 |
+
"message": "Bienvenue sur la GetAround Pricing API (HuggingFace).",
|
| 103 |
+
"use": "POST /predict pour obtenir un prix."
|
| 104 |
+
}
|
| 105 |
|
| 106 |
@app.get("/health")
|
| 107 |
def health():
|
| 108 |
+
return {"status": "ok"}
|
| 109 |
|
| 110 |
@app.post("/predict", response_model=PredictionResponse)
|
| 111 |
+
def predict(request: PredictionRequest):
|
| 112 |
+
|
| 113 |
+
```
|
| 114 |
+
# Convertir la liste d'objets CarFeatures en DataFrame
|
| 115 |
+
df = pd.DataFrame([item.dict() for item in request.input])
|
| 116 |
|
| 117 |
+
# Ajouter colonnes manquantes si besoin
|
| 118 |
+
for col in feature_columns:
|
| 119 |
+
if col not in df.columns:
|
| 120 |
+
df[col] = np.nan
|
| 121 |
|
| 122 |
+
df = df[feature_columns]
|
|
|
|
| 123 |
|
| 124 |
+
# Pipeline sklearn direct
|
| 125 |
+
try:
|
| 126 |
+
preds = pipeline.predict(df)
|
| 127 |
+
except Exception as e:
|
| 128 |
+
raise HTTPException(status_code=500, detail=f"Erreur : {e}")
|
| 129 |
|
| 130 |
+
return PredictionResponse(prediction=[float(x) for x in preds])
|
| 131 |
+
```
|