File size: 6,389 Bytes
ad86b94
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from fastapi import APIRouter, HTTPException, status, Depends
from pydantic import BaseModel
from typing import List
from datetime import datetime
import logging
from sqlalchemy.orm import Session
import os
import joblib
from huggingface_hub import hf_hub_download
import pandas as pd
import numpy as np
from imblearn.pipeline import Pipeline as ImbPipeline
from xgboost import XGBClassifier

from src.api.validation import ApplicationTest, Bureau
from src.data.models import PredictLogs
from src.data.database import get_db

logger = logging.getLogger(__name__)

router = APIRouter(
    prefix="/predict",
    tags=["prediction"],
)

class PredictionRequest(BaseModel):
    application_data: ApplicationTest
    bureau_data: List[Bureau]

class PredictionResponse(BaseModel):
    SK_ID_CURR: int
    prediction: int
    probability: float
    inference_time_ms: float


# Variables globales
model = None
pipeline = None
_model_loaded = False   # petit flag pour éviter de recharger à chaque requête


def load_model():
    """

    Charge le modèle et le pipeline depuis Hugging Face.

    Peut être appelée au startup OU en lazy loading.

    """
    global model, pipeline, _model_loaded

    if _model_loaded:
        logger.info("/////////////////////////////// Modele déjà chargé !! //////////")
        print("/////////////////////////////// Modele déjà chargé !! //////////")
        return  # déjà chargé

    try:
        print("#######################################################")
        print("Chargement du modèle et du pipeline depuis Hugging Face (lazy)...")

        hf_repository = os.getenv("HF_REPOSITORY")
        hf_model = os.getenv("HF_MODEL")
        hf_pipeline = os.getenv("HF_PIPELINE")

        if not all([hf_repository, hf_model, hf_pipeline]):
            raise RuntimeError(
                "Variables d'environnement HF manquantes (HF_REPOSITORY, HF_MODEL, HF_PIPELINE)"
            )

        logger.info(f"Téléchargement du modèle {hf_model} depuis {hf_repository}...")
        model_path = hf_hub_download(
            repo_id=hf_repository,
            filename=hf_model
        )
        logger.info(f"Modèle téléchargé à: {model_path}")
        model = joblib.load(model_path)
        logger.info("Modèle chargé avec succès.")

        logger.info(f"Téléchargement de la pipeline {hf_pipeline} depuis {hf_repository}...")
        pipeline_path = hf_hub_download(
            repo_id=hf_repository,
            filename=hf_pipeline
        )
        logger.info(f"Pipeline téléchargée à: {pipeline_path}")
        pipeline = joblib.load(pipeline_path)
        logger.info("Pipeline chargée avec succès.")

        _model_loaded = True

    except Exception as e:
        logger.exception(f"Erreur lors du chargement du modèle/pipeline: {e}")
        # On remonte une 500 pour que le client comprenne que c'est côté serveur
        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Erreur lors du chargement du modèle/pipeline: {str(e)}",
        )


@router.post("/", response_model=PredictionResponse, status_code=status.HTTP_200_OK)
async def predict(request: PredictionRequest, db: Session = Depends(get_db)):
    """

    Endpoint de prédiction.

    Lazy loading : si le modèle n'est pas chargé, on le charge à la première requête.

    """
    start = datetime.now()

    try:
        # Lazy loading ici
        if model is None or pipeline is None:
            load_model()
        else:
            print("############################ modèle déjà chargé !!!! ##########")

        input_payload = request.model_dump()

        # ======= Préparation des données =======
        application_dict = request.application_data.model_dump()
        bureau_list = [b.model_dump() for b in request.bureau_data]

        df_application = pd.DataFrame([application_dict])
        df_bureau = pd.DataFrame(bureau_list) if bureau_list else pd.DataFrame()

        # pipeline est un ApplicationBureauPipeline
        X_processed = pipeline.transform(df_application, df_bureau)

        pred = int(model.predict(X_processed)[0])

        if hasattr(model, "predict_proba"):
            proba = float(model.predict_proba(X_processed)[0][1])
        elif hasattr(model, "decision_function"):
            decision = model.decision_function(X_processed)[0]
            proba = float(1 / (1 + np.exp(-decision)))
        else:
            proba = float(pred)

        elapsed_ms = (datetime.now() - start).total_seconds() * 1000

        response_obj = PredictionResponse(
            SK_ID_CURR=request.application_data.SK_ID_CURR,
            prediction=pred,
            probability=round(proba, 4),
            inference_time_ms=round(elapsed_ms, 2),
        )

        # Log succès
        log_entry = PredictLogs(
            input_payload=input_payload,
            prediction_result=response_obj.model_dump(),
            processing_time_ms=round(elapsed_ms, 2),
            status="success",
            error_message=None,
        )
        db.add(log_entry)
        db.commit()

        return response_obj

    except HTTPException:
        # Log erreur HTTP explicite
        elapsed_ms = (datetime.now() - start).total_seconds() * 1000
        log_entry = PredictLogs(
            input_payload=request.model_dump(),
            prediction_result=None,
            processing_time_ms=round(elapsed_ms, 2),
            status="error",
            error_message="HTTPException",
        )
        db.add(log_entry)
        db.commit()
        raise

    except Exception as e:
        logger.exception("Erreur lors de la prédiction")

        elapsed_ms = (datetime.now() - start).total_seconds() * 1000
        log_entry = PredictLogs(
            input_payload=request.model_dump(),
            prediction_result=None,
            processing_time_ms=round(elapsed_ms, 2),
            status="error",
            error_message=str(e),
        )
        db.add(log_entry)
        db.commit()

        raise HTTPException(
            status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
            detail=f"Erreur lors de la prédiction: {str(e)}",
        )