File size: 4,279 Bytes
5ec0c43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af576c2
6f8ce16
af576c2
 
 
 
 
eb736ea
8d47fa9
eb736ea
 
d6c93c3
eb736ea
6ba95f1
 
eb736ea
2127779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb736ea
6ba95f1
 
d6c93c3
eb736ea
 
 
6ba95f1
eb736ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af576c2
257e726
 
 
 
5ec0c43
af576c2
 
5ec0c43
 
 
 
 
 
 
 
af576c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36d28ec
 
 
 
5ec0c43
 
af576c2
 
5ec0c43
 
 
af576c2
 
 
 
 
 
 
5ec0c43
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# from fastapi import FastAPI
# from pydantic import BaseModel
# import pandas as pd
# import joblib
# import os

# app = FastAPI()

# MODEL_PATH = os.path.join("model", "modele_xgb_getaround.pkl")
# model = joblib.load(MODEL_PATH)

# # donnée d'entrée
# class InputData(BaseModel):
#     model_key: str
#     mileage: int
#     engine_power: int
#     fuel: str
#     paint_color: str
#     car_type: str
#     private_parking_available: bool
#     has_gps: bool
#     has_air_conditioning: bool
#     automatic_car: bool
#     has_getaround_connect: bool
#     has_speed_regulator: bool
#     winter_tires: bool

# @app.post("/predict")
# def predict(data: InputData):
#     # Convertir l'entrée en DataFrame
#     df = pd.DataFrame([data.dict()])
    
#     # Faire la prédiction
#     prediction = model.predict(df)
    
#     # Retourner la prédiction sous forme JSON
#     return {"prediction": prediction.tolist()}

from fastapi import FastAPI
from fastapi.responses import JSONResponse, RedirectResponse
from pydantic import BaseModel
import pandas as pd
import joblib
import os

app = FastAPI(
    title="API Getaround",
    description="""

L'API GetAround estime le prix journalier de location d'un véhicule. Le modèle prédictif a été entraîné sur les données réelles de GetAround.

---
### Instructions pour remplir les champs :

| Champ | Type | Description | Valeurs possibles |
|-------|------|-------------|------------------|
| `model_key` | str | Marque du véhicule | Citroën, Peugeot, PGO, Renault, Audi, BMW, Ford, Mercedes, Opel, Porsche, Volkswagen, KIA Motors, Alfa Romeo, Ferrari, Fiat, Lamborghini, Maserati, Lexus, Honda, Mazda, Mini, Mitsubishi, Nissan, SEAT, Subaru, Suzuki, Toyota, Yamaha |
| `mileage` | int | Kilométrage du véhicule | Nombre entier |
| `engine_power` | int | Puissance du moteur (en chevaux) | Nombre entier |
| `fuel` | str | Type de carburant | diesel, petrol, hybrid_petrol, electro |
| `paint_color` | str | Couleur de la voiture | black, grey, white, red, silver, blue, orange, beige, brown, green |
| `car_type` | str | Type de véhicule | convertible, coupe, estate, hatchback, sedan, subcompact, suv, van |
| `private_parking_available` | bool | Parking privé disponible | true / false |
| `has_gps` | bool | GPS intégré | true / false |
| `has_air_conditioning` | bool | Climatisation | true / false |
| `automatic_car` | bool | Transmission automatique | true / false |
| `has_getaround_connect` | bool | Connectivité GetAround | true / false |
| `has_speed_regulator` | bool | Régulateur de vitesse | true / false |
| `winter_tires` | bool | Pneus hiver | true / false |

---

### Exemple JSON d'entrée :

```json
{
  "model_key": "Renault",
  "mileage": 50000,
  "engine_power": 120,
  "fuel": "diesel",
  "paint_color": "white",
  "car_type": "estate",
  "private_parking_available": false,
  "has_gps": true,
  "has_air_conditioning": false,
  "automatic_car": false,
  "has_getaround_connect": false,
  "has_speed_regulator": false,
  "winter_tires": true
}

""",
    version="1.0"
)

@app.get("/")
def root():
    return RedirectResponse(url="/docs")

# Chemin vers le modèle
MODEL_PATH = os.path.join("model", "modele_xgb_getaround.pkl")

# Charger le modèle
if os.path.exists(MODEL_PATH):
    model = joblib.load(MODEL_PATH)
else:
    model = None
    print(f"Attention : modèle non trouvé à {MODEL_PATH}")

# Classe de données d'entrée
class InputData(BaseModel):
    model_key: str
    mileage: int
    engine_power: int
    fuel: str
    paint_color: str
    car_type: str
    private_parking_available: bool
    has_gps: bool
    has_air_conditioning: bool
    automatic_car: bool
    has_getaround_connect: bool
    has_speed_regulator: bool
    winter_tires: bool

# # Route racine pour test
# @app.get("/")
# def root():
#     return {"message": "API FastAPI Getaround en ligne !"}

# Route de prédiction
@app.post("/predict")
def predict(data: InputData):
    if model is None:
        return {"error": "Modèle non chargé"}
    
    # Convertir l'entrée en DataFrame
    df = pd.DataFrame([data.dict()])
    
    # Faire la prédiction
    prediction = model.predict(df)
    
    # Retourner la prédiction sous forme JSON
    return {"prediction": prediction.tolist()}