Spaces:
Paused
Paused
edit de l'API
Browse files- api.py +11 -6
- model.joblib +2 -2
- preprocessor.joblib +2 -2
- request.py +65 -0
- requirements.txt +2 -0
api.py
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import pandas as pd
|
| 2 |
-
import joblib
|
| 3 |
import uvicorn
|
| 4 |
from pydantic import BaseModel
|
| 5 |
from typing import Literal, List, Union
|
|
@@ -46,9 +48,13 @@ class Car(BaseModel):
|
|
| 46 |
winter_tires: bool
|
| 47 |
|
| 48 |
|
| 49 |
-
# Chargement du modèle
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
|
| 54 |
@app.get("/", include_in_schema=False)
|
|
@@ -66,8 +72,7 @@ async def predict(cars: List[Car]):
|
|
| 66 |
**Sortie** : `{"prediction": [prix_en_euros, ...]}`
|
| 67 |
"""
|
| 68 |
car_features = pd.DataFrame(jsonable_encoder(cars))
|
| 69 |
-
|
| 70 |
-
prediction = model.predict(car_features_transformed)
|
| 71 |
return {"prediction": prediction.tolist()}
|
| 72 |
|
| 73 |
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import mlflow
|
| 3 |
+
import mlflow.pyfunc
|
| 4 |
import pandas as pd
|
|
|
|
| 5 |
import uvicorn
|
| 6 |
from pydantic import BaseModel
|
| 7 |
from typing import Literal, List, Union
|
|
|
|
| 48 |
winter_tires: bool
|
| 49 |
|
| 50 |
|
| 51 |
+
# Chargement du modèle depuis MLflow au démarrage (une seule fois)
|
| 52 |
+
MLFLOW_TRACKING_URI = os.getenv("MLFLOW_TRACKING_URI", "https://atomik31-mlflow.hf.space")
|
| 53 |
+
mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
|
| 54 |
+
|
| 55 |
+
print(f"Chargement du modèle depuis MLflow : {MLFLOW_TRACKING_URI}")
|
| 56 |
+
model = mlflow.pyfunc.load_model("models:/GetAround_price_predictor@production")
|
| 57 |
+
print("Modèle chargé en mémoire.")
|
| 58 |
|
| 59 |
|
| 60 |
@app.get("/", include_in_schema=False)
|
|
|
|
| 72 |
**Sortie** : `{"prediction": [prix_en_euros, ...]}`
|
| 73 |
"""
|
| 74 |
car_features = pd.DataFrame(jsonable_encoder(cars))
|
| 75 |
+
prediction = model.predict(car_features)
|
|
|
|
| 76 |
return {"prediction": prediction.tolist()}
|
| 77 |
|
| 78 |
|
model.joblib
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:074b922a05eee7516a7aba920a1c2608c3eb5b76c2305b0c44f278eed8cfe399
|
| 3 |
+
size 42983521
|
preprocessor.joblib
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d0646694ed246c9fb4c3b6619b1b36615dec18badc23784e9d733d6d72653597
|
| 3 |
+
size 4912
|
request.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Script de test de l'API GetAround.
|
| 3 |
+
Lancer d'abord l'API avec : uvicorn api:app --port 8000
|
| 4 |
+
"""
|
| 5 |
+
import requests
|
| 6 |
+
|
| 7 |
+
# Test avec une seule voiture
|
| 8 |
+
response = requests.post("http://localhost:8000/predict", json=[
|
| 9 |
+
{
|
| 10 |
+
"model_key": "Citroën",
|
| 11 |
+
"mileage": 13929,
|
| 12 |
+
"engine_power": 317,
|
| 13 |
+
"fuel": "petrol",
|
| 14 |
+
"paint_color": "grey",
|
| 15 |
+
"car_type": "convertible",
|
| 16 |
+
"private_parking_available": True,
|
| 17 |
+
"has_gps": True,
|
| 18 |
+
"has_air_conditioning": False,
|
| 19 |
+
"automatic_car": False,
|
| 20 |
+
"has_getaround_connect": False,
|
| 21 |
+
"has_speed_regulator": True,
|
| 22 |
+
"winter_tires": True
|
| 23 |
+
}
|
| 24 |
+
])
|
| 25 |
+
|
| 26 |
+
print("Status :", response.status_code)
|
| 27 |
+
print("Réponse :", response.json())
|
| 28 |
+
|
| 29 |
+
# Test avec plusieurs voitures
|
| 30 |
+
response2 = requests.post("http://localhost:8000/predict", json=[
|
| 31 |
+
{
|
| 32 |
+
"model_key": "BMW",
|
| 33 |
+
"mileage": 5000,
|
| 34 |
+
"engine_power": 200,
|
| 35 |
+
"fuel": "petrol",
|
| 36 |
+
"paint_color": "black",
|
| 37 |
+
"car_type": "sedan",
|
| 38 |
+
"private_parking_available": True,
|
| 39 |
+
"has_gps": True,
|
| 40 |
+
"has_air_conditioning": True,
|
| 41 |
+
"automatic_car": True,
|
| 42 |
+
"has_getaround_connect": True,
|
| 43 |
+
"has_speed_regulator": True,
|
| 44 |
+
"winter_tires": False
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
"model_key": "Renault",
|
| 48 |
+
"mileage": 80000,
|
| 49 |
+
"engine_power": 90,
|
| 50 |
+
"fuel": "diesel",
|
| 51 |
+
"paint_color": "white",
|
| 52 |
+
"car_type": "hatchback",
|
| 53 |
+
"private_parking_available": False,
|
| 54 |
+
"has_gps": False,
|
| 55 |
+
"has_air_conditioning": True,
|
| 56 |
+
"automatic_car": False,
|
| 57 |
+
"has_getaround_connect": False,
|
| 58 |
+
"has_speed_regulator": False,
|
| 59 |
+
"winter_tires": True
|
| 60 |
+
}
|
| 61 |
+
])
|
| 62 |
+
|
| 63 |
+
print("\nTest multi-voitures :")
|
| 64 |
+
print("Status :", response2.status_code)
|
| 65 |
+
print("Réponse :", response2.json())
|
requirements.txt
CHANGED
|
@@ -5,3 +5,5 @@ pandas
|
|
| 5 |
scikit-learn==1.6.1
|
| 6 |
numpy
|
| 7 |
joblib
|
|
|
|
|
|
|
|
|
| 5 |
scikit-learn==1.6.1
|
| 6 |
numpy
|
| 7 |
joblib
|
| 8 |
+
mlflow
|
| 9 |
+
boto3
|