avraux commited on
Commit ·
0a95fff
1
Parent(s): a0fb68d
create API and format JSON input
Browse files- README.md +37 -0
- app.py +24 -0
- inference.py +60 -8
- requirements.txt +6 -0
README.md
CHANGED
|
@@ -5,6 +5,43 @@ L'entrainement du modèle a été fait avec Google Collab : https://colab.resear
|
|
| 5 |
|
| 6 |
La documentation sur l'entraînement du modèle est la suivante : https://docs.google.com/spreadsheets/d/1oBshoNy2NJZQreOEbBfcWCCg2wo0PQDo/edit?gid=1220399676#gid=1220399676
|
| 7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
## Exemple d'utilisation
|
| 9 |
```python
|
| 10 |
import joblib
|
|
|
|
| 5 |
|
| 6 |
La documentation sur l'entraînement du modèle est la suivante : https://docs.google.com/spreadsheets/d/1oBshoNy2NJZQreOEbBfcWCCg2wo0PQDo/edit?gid=1220399676#gid=1220399676
|
| 7 |
|
| 8 |
+
# Input Data
|
| 9 |
+
Les données d'entrée du modèle sont les suivantes :
|
| 10 |
+
- puiss_admin_98
|
| 11 |
+
- conso_urb
|
| 12 |
+
- conso_exurb
|
| 13 |
+
- masse_ordma_max
|
| 14 |
+
- lib_mrq_BMW
|
| 15 |
+
- lib_mrq_MERCEDES
|
| 16 |
+
- lib_mrq_VOLKSWAGEN
|
| 17 |
+
- typ_boite_nb_rapp_A 5
|
| 18 |
+
- typ_boite_nb_rapp_A 6
|
| 19 |
+
- champ_v9_715/2007*692/2008EURO5
|
| 20 |
+
- Carrosserie_BREAK
|
| 21 |
+
- Carrosserie_COUPE
|
| 22 |
+
- Carrosserie_MINIBUS
|
| 23 |
+
- Carrosserie_TS TERRAINS/CHEMINS
|
| 24 |
+
- gamme_INFERIEURE
|
| 25 |
+
- gamme_LUXE
|
| 26 |
+
- gamme_MOY-INFERIEURE
|
| 27 |
+
- gamme_MOY-SUPER
|
| 28 |
+
- gamme_SUPERIEURE
|
| 29 |
+
|
| 30 |
+
Pour faciliter la réception d'information, on va demander des informations plus concises puis on va recréer l'ensemble de ses champs. On s'attends à un JSON de la forme :
|
| 31 |
+
|
| 32 |
+
{
|
| 33 |
+
"puiss_admin_98": 7,
|
| 34 |
+
"conso_urb": 5.6,
|
| 35 |
+
"conso_exurb": 4.3,
|
| 36 |
+
"masse_ordma_max": 1500,
|
| 37 |
+
"marque": "BMW",
|
| 38 |
+
"typ_boite": "A 5",
|
| 39 |
+
"champ_v9": false,
|
| 40 |
+
"carrosserie": "COUPE",
|
| 41 |
+
"gamme": "LUXE"
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
## Exemple d'utilisation
|
| 46 |
```python
|
| 47 |
import joblib
|
app.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
|
| 4 |
+
# Définir l'API
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
# Définir un modèle d'entrée
|
| 8 |
+
class InputData(BaseModel):
|
| 9 |
+
puiss_admin_98: int
|
| 10 |
+
conso_urb: float
|
| 11 |
+
conso_exurb: float
|
| 12 |
+
masse_ordma_max: float
|
| 13 |
+
marque: str
|
| 14 |
+
typ_boite: str
|
| 15 |
+
champ_v9 : bool
|
| 16 |
+
carrosserie: str
|
| 17 |
+
gamme: str
|
| 18 |
+
|
| 19 |
+
# Endpoint pour prédiction
|
| 20 |
+
@app.post("/predict")
|
| 21 |
+
def predict(data: InputData):
|
| 22 |
+
input_json = data.dict()
|
| 23 |
+
result = preprocess_and_predict(input_json)
|
| 24 |
+
return result
|
inference.py
CHANGED
|
@@ -1,14 +1,66 @@
|
|
|
|
|
| 1 |
import joblib
|
| 2 |
-
import numpy as np
|
| 3 |
|
| 4 |
-
# Charger le modèle
|
| 5 |
model = joblib.load("model.joblib")
|
| 6 |
|
| 7 |
-
|
| 8 |
-
def predict(input_data):
|
| 9 |
"""
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
-
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
import joblib
|
|
|
|
| 3 |
|
| 4 |
+
# Charger le modèle scikit-learn
|
| 5 |
model = joblib.load("model.joblib")
|
| 6 |
|
| 7 |
+
def preprocess_and_predict(input_json):
|
|
|
|
| 8 |
"""
|
| 9 |
+
Fonction pour traiter les données JSON entrantes, calculer les champs dérivés,
|
| 10 |
+
et prédire à l'aide d'un modèle scikit-learn.
|
| 11 |
+
|
| 12 |
+
Args:
|
| 13 |
+
input_json (dict): Données simplifiées fournies par l'utilisateur. Exemple :
|
| 14 |
+
{
|
| 15 |
+
"puiss_admin_98": 7,
|
| 16 |
+
"conso_urb": 5.6,
|
| 17 |
+
"conso_exurb": 4.3,
|
| 18 |
+
"masse_ordma_max": 1500,
|
| 19 |
+
"marque": "BMW",
|
| 20 |
+
"typ_boite": "A 5",
|
| 21 |
+
"champ_v9": false,
|
| 22 |
+
"carrosserie": "COUPE",
|
| 23 |
+
"gamme": "LUXE"
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
Returns:
|
| 27 |
+
dict: Prédiction du modèle.
|
| 28 |
"""
|
| 29 |
+
|
| 30 |
+
# Mapper les catégories aux colonnes du modèle
|
| 31 |
+
marque_mapping = ["BMW", "MERCEDES", "VOLKSWAGEN"]
|
| 32 |
+
typ_boite_mapping = ["A 5", "A 6"]
|
| 33 |
+
carrosserie_mapping = ["BREAK", "COUPE", "MINIBUS", "TS TERRAINS/CHEMINS"]
|
| 34 |
+
gamme_mapping = ["INFERIEURE", "LUXE", "MOY-INFERIEURE", "MOY-SUPER", "SUPERIEURE"]
|
| 35 |
+
|
| 36 |
+
# Initialiser un dictionnaire pour construire les colonnes nécessaires
|
| 37 |
+
processed_data = {
|
| 38 |
+
"puiss_admin_98": input_json.get("puiss_admin_98", 0),
|
| 39 |
+
"conso_urb": input_json.get("conso_urb", 0.0),
|
| 40 |
+
"conso_exurb": input_json.get("conso_exurb", 0.0),
|
| 41 |
+
"masse_ordma_max": input_json.get("masse_ordma_max", 0.0),
|
| 42 |
+
"champ_v9": input_json.get("champ_v9", False),
|
| 43 |
+
}
|
| 44 |
+
|
| 45 |
+
# Variables indicatrices pour la marque
|
| 46 |
+
for marque in marque_mapping:
|
| 47 |
+
processed_data[f"lib_mrq_{marque}"] = 1 if input_json.get("marque") == marque else 0
|
| 48 |
+
|
| 49 |
+
# Variables indicatrices pour le type de boîte
|
| 50 |
+
for typ_boite in typ_boite_mapping:
|
| 51 |
+
processed_data[f"typ_boite_nb_rapp_{typ_boite}"] = 1 if input_json.get("typ_boite") == typ_boite else 0
|
| 52 |
+
|
| 53 |
+
# Variables indicatrices pour la carrosserie
|
| 54 |
+
for carrosserie in carrosserie_mapping:
|
| 55 |
+
processed_data[f"Carrosserie_{carrosserie}"] = 1 if input_json.get("carrosserie") == carrosserie else 0
|
| 56 |
+
|
| 57 |
+
# Variables indicatrices pour la gamme
|
| 58 |
+
for gamme in gamme_mapping:
|
| 59 |
+
processed_data[f"gamme_{gamme}"] = 1 if input_json.get("gamme") == gamme else 0
|
| 60 |
+
|
| 61 |
+
# Convertir en DataFrame pour correspondre au format attendu par le modèle
|
| 62 |
+
input_dataframe = pd.DataFrame([processed_data])
|
| 63 |
+
|
| 64 |
+
# Faire une prédiction
|
| 65 |
+
prediction = model.predict(input_dataframe)
|
| 66 |
+
return {"prediction": prediction.tolist()}
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
pandas
|
| 4 |
+
numpy
|
| 5 |
+
scikit-learn
|
| 6 |
+
joblib
|