Download inference.py from avraux/co2pred: direct link, hf CLI and curl.
- Browser
- Download file 2.88 kB
-
https://huggingface.co/spaces/avraux/co2pred/resolve/main/inference.py
- Command line
-
hf download hf://spaces/avraux/co2pred/inference.py
-
curl -L -o inference.py https://huggingface.co/spaces/avraux/co2pred/resolve/main/inference.py
2.88 kB
| import pandas as pd | |
| import joblib | |
| # Charger le modèle scikit-learn | |
| model = joblib.load("model.joblib") | |
| def preprocess_and_predict(input_json): | |
| """ | |
| Fonction pour traiter les données JSON entrantes, calculer les champs dérivés, | |
| et prédire à l'aide d'un modèle scikit-learn. | |
| Args: | |
| input_json (dict): Données simplifiées fournies par l'utilisateur. Exemple : | |
| { | |
| "puiss_admin_98": 7, | |
| "conso_urb": 5.6, | |
| "conso_exurb": 4.3, | |
| "masse_ordma_max": 1500, | |
| "marque": "BMW", | |
| "typ_boite": "A 5", | |
| "champ_v9": 715/2007*195/2013EURO5, | |
| "carrosserie": "COUPE", | |
| "gamme": "LUXE" | |
| } | |
| Returns: | |
| dict: Prédiction du modèle. | |
| """ | |
| # Mapper les catégories aux colonnes du modèle | |
| marque_mapping = ["BMW", "MERCEDES", "VOLKSWAGEN"] | |
| typ_boite_mapping = ["A 5", "A 6", "A 7", "A 8", "M 5", "M 6"] | |
| carrosserie_mapping = ["BREAK", "COUPE", "MINIBUS", "TS TERRAINS/CHEMINS"] | |
| gamme_mapping = ["INFERIEURE", "LUXE", "MOY-INFERIEURE", "MOY-SUPER", "SUPERIEURE"] | |
| champ_v9_mapping = ["\"715/2007*195/2013EURO5", "\"715/2007*195/2013EURO6", "\"715/2007*566/2011EURO5", "\"715/2007*630/2012EURO5", "715/2007*195/2013EURO5", "715/2007*630/2012EURO5", "715/2007*692/2008EURO5"] | |
| # Initialiser un dictionnaire pour construire les colonnes nécessaires | |
| processed_data = { | |
| "puiss_admin_98": input_json.get("puiss_admin_98", 0), | |
| "conso_urb": input_json.get("conso_urb", 0.0), | |
| "conso_exurb": input_json.get("conso_exurb", 0.0), | |
| "masse_ordma_max": input_json.get("masse_ordma_max", 0.0) | |
| } | |
| # Variables indicatrices pour la marque | |
| for marque in marque_mapping: | |
| processed_data[f"lib_mrq_{marque}"] = 1 if input_json.get("marque") == marque else 0 | |
| # Variables indicatrices pour le type de boîte | |
| for typ_boite in typ_boite_mapping: | |
| processed_data[f"typ_boite_nb_rapp_{typ_boite}"] = 1 if input_json.get("typ_boite") == typ_boite else 0 | |
| # Variables indicatrices pour la norme champ | |
| for champ_v9 in champ_v9_mapping: | |
| processed_data[f"champ_v9_{champ_v9}"] = 1 if input_json.get("champ_v9") == champ_v9 else 0 | |
| # Variables indicatrices pour la carrosserie | |
| for carrosserie in carrosserie_mapping: | |
| processed_data[f"Carrosserie_{carrosserie}"] = 1 if input_json.get("carrosserie") == carrosserie else 0 | |
| # Variables indicatrices pour la gamme | |
| for gamme in gamme_mapping: | |
| processed_data[f"gamme_{gamme}"] = 1 if input_json.get("gamme") == gamme else 0 | |
| # Convertir en DataFrame pour correspondre au format attendu par le modèle | |
| input_dataframe = pd.DataFrame([processed_data]) | |
| # Faire une prédiction | |
| prediction = model.predict(input_dataframe) | |
| return {"prediction": prediction.tolist()} | |