Spaces:
Sleeping
Sleeping
File size: 1,284 Bytes
3255f93 | 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 | # Générer un example de donnée pour un client
import pandas as pd
import json
import joblib
import numpy as np
import re
from typing import Optional
df = joblib.load("./data/app_test_clean_v2.joblib")
def sanitize(name: str) -> str:
"""
Transforme un nom de colonne en identifiant Python valide.
"""
# Remplacer tout caractère non alphanumérique par un underscore
name = re.sub(r'[^0-9a-zA-Z_]', '_', name)
# Si le nom commence par un chiffre → préfixer
if re.match(r'^[0-9]', name):
name = f"col_{name}"
return name
df.columns = [sanitize(c) for c in df.columns]
# Prendre un individu au hasard
example = df.sample(1).iloc[0].to_dict()
for k, v in example.items():
if isinstance(v, float) and (np.isnan(v)):
example[k] = None
fields = {}
# Générer automatiquement les types
for col, dtype in df.dtypes.items():
if dtype == bool or df[col].dropna().isin([0, 1, True, False]).all():
fields[col] = (Optional[bool], None)
elif "int" in str(dtype):
fields[col] = (Optional[int], None)
elif "float" in str(dtype):
fields[col] = (Optional[float], None)
else:
fields[col] = (Optional[str], None)
# Afficher sous format JSON
print(json.dumps(example, indent=4))
|