Spaces:
Sleeping
Sleeping
Upload processing.py
Browse files- processing.py +46 -34
processing.py
CHANGED
|
@@ -5,9 +5,8 @@ import json
|
|
| 5 |
import os
|
| 6 |
import boto3
|
| 7 |
|
| 8 |
-
# --- 1. CHARGEMENT DES CONFIGURATIONS
|
| 9 |
-
|
| 10 |
-
# On charge la liste des colonnes depuis le Secret Hugging Face
|
| 11 |
FEATURES = json.loads(os.getenv("MODEL_FEATURES", "[]"))
|
| 12 |
|
| 13 |
def load_from_s3(file_name):
|
|
@@ -19,7 +18,6 @@ def load_from_s3(file_name):
|
|
| 19 |
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
|
| 20 |
region_name=os.getenv("AWS_REGION")
|
| 21 |
)
|
| 22 |
-
# Remplace 'NOM_DE_TON_DOSSIER' par le nom du dossier dans ton bucket
|
| 23 |
key = f"projet-economie/{file_name}"
|
| 24 |
response = s3.get_object(Bucket=os.getenv("AWS_BUCKET_NAME"), Key=key)
|
| 25 |
return json.loads(response['Body'].read().decode('utf-8'))
|
|
@@ -27,27 +25,28 @@ def load_from_s3(file_name):
|
|
| 27 |
print(f"⚠️ Erreur S3 sur {file_name}: {e}")
|
| 28 |
return {}
|
| 29 |
|
| 30 |
-
# On remplace tes dictionnaires en dur par les versions complètes de S3
|
| 31 |
DEP_RISK_MAP = load_from_s3("mapping_dep_risk.json")
|
| 32 |
APE_SECTION_MAP = load_from_s3("mapping_ape_section.json")
|
| 33 |
|
| 34 |
-
# --- 2.
|
| 35 |
-
|
| 36 |
def get_sigma(model):
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
def calculate_survival_risk(mu, horizon, s):
|
| 53 |
z = (np.log(horizon) - mu) / s
|
|
@@ -60,31 +59,44 @@ def map_statut_expert(p2):
|
|
| 60 |
if p2 > 5: return '🟡 OBSERVATION'
|
| 61 |
return '🟢 SAIN'
|
| 62 |
|
| 63 |
-
# --- 3. PRÉPARATION DES DONNÉES
|
| 64 |
-
|
| 65 |
def prepare_input(data):
|
| 66 |
-
#
|
| 67 |
df = pd.DataFrame(0.0, index=[0], columns=FEATURES)
|
| 68 |
-
|
| 69 |
-
# Remplissage par nom de colonne (Pandas gère l'index automatiquement)
|
| 70 |
-
df['age_au_diagnostic'] = float(data.get('age_estime', 0))
|
| 71 |
-
df['Tranche_effectif_num'] = float(data.get('Tranche_effectif_num', 0))
|
| 72 |
-
df['is_ess'] = int(data.get('is_ess', 0))
|
| 73 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
code_dep = str(data.get('code_departement', '')).strip().upper()
|
| 75 |
-
|
|
|
|
| 76 |
|
|
|
|
| 77 |
code_ape = str(data.get('code_ape', '')).zfill(2)
|
| 78 |
section_name = APE_SECTION_MAP.get(code_ape)
|
| 79 |
if section_name:
|
| 80 |
col_ape = f"APE_{section_name}"
|
| 81 |
if col_ape in df.columns:
|
| 82 |
-
df[col_ape] = 1.0
|
|
|
|
|
|
|
| 83 |
|
|
|
|
| 84 |
cj_prefix = str(data.get('categorie_juridique', ''))[:4]
|
| 85 |
col_cj = f"CJ_{cj_prefix}"
|
| 86 |
if col_cj in df.columns:
|
| 87 |
-
df[col_cj] = 1.0
|
| 88 |
|
| 89 |
-
#
|
| 90 |
-
|
|
|
|
|
|
|
|
|
| 5 |
import os
|
| 6 |
import boto3
|
| 7 |
|
| 8 |
+
# --- 1. CHARGEMENT DES CONFIGURATIONS ---
|
| 9 |
+
# On récupère la liste des colonnes depuis le Secret
|
|
|
|
| 10 |
FEATURES = json.loads(os.getenv("MODEL_FEATURES", "[]"))
|
| 11 |
|
| 12 |
def load_from_s3(file_name):
|
|
|
|
| 18 |
aws_secret_access_key=os.getenv("AWS_SECRET_ACCESS_KEY"),
|
| 19 |
region_name=os.getenv("AWS_REGION")
|
| 20 |
)
|
|
|
|
| 21 |
key = f"projet-economie/{file_name}"
|
| 22 |
response = s3.get_object(Bucket=os.getenv("AWS_BUCKET_NAME"), Key=key)
|
| 23 |
return json.loads(response['Body'].read().decode('utf-8'))
|
|
|
|
| 25 |
print(f"⚠️ Erreur S3 sur {file_name}: {e}")
|
| 26 |
return {}
|
| 27 |
|
|
|
|
| 28 |
DEP_RISK_MAP = load_from_s3("mapping_dep_risk.json")
|
| 29 |
APE_SECTION_MAP = load_from_s3("mapping_ape_section.json")
|
| 30 |
|
| 31 |
+
# --- 2. CALCULS ---
|
|
|
|
| 32 |
def get_sigma(model):
|
| 33 |
+
try:
|
| 34 |
+
config = json.loads(model.save_config())
|
| 35 |
+
def find_key(obj, key):
|
| 36 |
+
if isinstance(obj, dict):
|
| 37 |
+
for k, v in obj.items():
|
| 38 |
+
if k == key: return v
|
| 39 |
+
res = find_key(v, key)
|
| 40 |
+
if res is not None: return res
|
| 41 |
+
elif isinstance(obj, list):
|
| 42 |
+
for item in obj:
|
| 43 |
+
res = find_key(item, key)
|
| 44 |
+
if res is not None: return res
|
| 45 |
+
return None
|
| 46 |
+
scale = find_key(config, 'aft_loss_distribution_scale')
|
| 47 |
+
return float(scale) if scale else 0.8
|
| 48 |
+
except:
|
| 49 |
+
return 0.8
|
| 50 |
|
| 51 |
def calculate_survival_risk(mu, horizon, s):
|
| 52 |
z = (np.log(horizon) - mu) / s
|
|
|
|
| 59 |
if p2 > 5: return '🟡 OBSERVATION'
|
| 60 |
return '🟢 SAIN'
|
| 61 |
|
| 62 |
+
# --- 3. PRÉPARATION DES DONNÉES ---
|
|
|
|
| 63 |
def prepare_input(data):
|
| 64 |
+
# Création du DF avec les colonnes du Secret
|
| 65 |
df = pd.DataFrame(0.0, index=[0], columns=FEATURES)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# Remplissage des variables
|
| 68 |
+
# On utilise .loc[0, col] pour être sûr de ne pas créer de nouvelles colonnes
|
| 69 |
+
if 'age_au_diagnostic' in df.columns:
|
| 70 |
+
df.loc[0, 'age_au_diagnostic'] = float(data.get('age_estime', 0))
|
| 71 |
+
|
| 72 |
+
if 'Tranche_effectif_num' in df.columns:
|
| 73 |
+
df.loc[0, 'Tranche_effectif_num'] = float(data.get('Tranche_effectif_num', 0))
|
| 74 |
+
|
| 75 |
+
if 'is_ess' in df.columns:
|
| 76 |
+
df.loc[0, 'is_ess'] = int(data.get('is_ess', 0))
|
| 77 |
+
|
| 78 |
+
# Risque départemental
|
| 79 |
code_dep = str(data.get('code_departement', '')).strip().upper()
|
| 80 |
+
if 'risque_departemental' in df.columns:
|
| 81 |
+
df.loc[0, 'risque_departemental'] = float(DEP_RISK_MAP.get(code_dep, 0.05))
|
| 82 |
|
| 83 |
+
# Mapping APE
|
| 84 |
code_ape = str(data.get('code_ape', '')).zfill(2)
|
| 85 |
section_name = APE_SECTION_MAP.get(code_ape)
|
| 86 |
if section_name:
|
| 87 |
col_ape = f"APE_{section_name}"
|
| 88 |
if col_ape in df.columns:
|
| 89 |
+
df.loc[0, col_ape] = 1.0
|
| 90 |
+
elif 'APE_Autres_Secteurs' in df.columns:
|
| 91 |
+
df.loc[0, 'APE_Autres_Secteurs'] = 1.0
|
| 92 |
|
| 93 |
+
# Mapping CJ
|
| 94 |
cj_prefix = str(data.get('categorie_juridique', ''))[:4]
|
| 95 |
col_cj = f"CJ_{cj_prefix}"
|
| 96 |
if col_cj in df.columns:
|
| 97 |
+
df.loc[0, col_cj] = 1.0
|
| 98 |
|
| 99 |
+
# LOG DE DEBUG (Visible dans les logs HF)
|
| 100 |
+
print(f"DEBUG: Age envoyé={data.get('age_estime')} | Valeur dans DF={df['age_au_diagnostic'].iloc[0]}")
|
| 101 |
+
|
| 102 |
+
return xgb.DMatrix(df)
|