Spaces:
Sleeping
Sleeping
Download api.py from OthmaneW/Detection-Fraude-PFA: direct link, hf CLI and curl.
- Browser
- Download file 15.8 kB
-
https://huggingface.co/spaces/OthmaneW/Detection-Fraude-PFA/resolve/main/api.py
- Command line
-
hf download hf://spaces/OthmaneW/Detection-Fraude-PFA/api.py
-
curl -L -o api.py https://huggingface.co/spaces/OthmaneW/Detection-Fraude-PFA/resolve/main/api.py
15.8 kB
| """ | |
| api.py β API de DΓ©tection de Fraude v2.0 | |
| ========================================== | |
| PFA : Système de Détection d'Anomalies dans les Transactions Financières | |
| Auteurs : Othmane & Ramzi | |
| Framework : FastAPI + Uvicorn | |
| Session 1 : POST /predict β Random Forest | |
| Session 2 : POST /explain β RF + SHAP | |
| POST /analyze β RF + Autoencoder + SHAP (pipeline complet) | |
| Usage : uvicorn api:app --host 0.0.0.0 --port 8000 --reload | |
| Docs : http://localhost:8000/docs | |
| """ | |
| from __future__ import annotations | |
| import os | |
| import time | |
| from contextlib import asynccontextmanager | |
| from typing import Any, Optional | |
| os.environ["TF_CPP_MIN_LOG_LEVEL"] = "2" | |
| import joblib | |
| import numpy as np | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel, Field, field_validator | |
| import shap | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Chemins | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| BASE_DIR = os.path.dirname(os.path.abspath(__file__)) | |
| MODEL_PATH = os.path.join(BASE_DIR, "models", "baseline_model.pkl") | |
| SCALER_PATH = os.path.join(BASE_DIR, "data", "scaler.pkl") | |
| AE_PATH = os.path.join(BASE_DIR, "models", "autoencoder.keras") | |
| THRESHOLD_PATH = os.path.join(BASE_DIR, "models", "threshold.pkl") | |
| N_FEATURES = 30 | |
| FEATURE_NAMES = [f"V{i}" for i in range(1, 29)] + ["Time", "Amount"] | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Ressources globales (chargΓ©es une seule fois) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| resources: dict[str, Any] = { | |
| "rf_model": None, | |
| "scaler": None, | |
| "ae_model": None, | |
| "threshold": None, | |
| "explainer": None, | |
| "ae_ready": False, | |
| } | |
| async def lifespan(app: FastAPI): | |
| """Charge tous les modèles au démarrage.""" | |
| print("Demarrage de l'API v2.0 β Chargement des modeles...") | |
| # ββ Random Forest (obligatoire) βββββββββββββββββββββββββββ | |
| if not os.path.exists(MODEL_PATH): | |
| raise RuntimeError(f"Modele introuvable : {MODEL_PATH} β python train_baseline.py") | |
| resources["rf_model"] = joblib.load(MODEL_PATH) | |
| print(f" [OK] Random Forest charge") | |
| # ββ Scaler (obligatoire) ββββββββββββββββββββββββββββββββββ | |
| if not os.path.exists(SCALER_PATH): | |
| raise RuntimeError(f"Scaler introuvable : {SCALER_PATH} β python data_prep.py") | |
| resources["scaler"] = joblib.load(SCALER_PATH) | |
| print(f" [OK] Scaler charge") | |
| # ββ SHAP TreeExplainer ββββββββββββββββββββββββββββββββββββ | |
| resources["explainer"] = shap.TreeExplainer(resources["rf_model"]) | |
| print(f" [OK] SHAP TreeExplainer pret") | |
| # ββ Autoencoder (optionnel β Session 2) βββββββββββββββββββ | |
| if os.path.exists(AE_PATH) and os.path.exists(THRESHOLD_PATH): | |
| try: | |
| import tensorflow as tf | |
| resources["ae_model"] = tf.keras.models.load_model(AE_PATH) | |
| resources["threshold"] = joblib.load(THRESHOLD_PATH) | |
| resources["ae_ready"] = True | |
| print(f" [OK] Autoencoder charge (seuil = {resources['threshold']:.6f})") | |
| except Exception as e: | |
| print(f" [!] Autoencoder non disponible : {e}") | |
| else: | |
| print(f" [!] Autoencoder non trouve β /analyze utilisera RF seul") | |
| print(" API prete sur http://localhost:8000/docs\n") | |
| yield | |
| for k in resources: | |
| resources[k] = None | |
| print("Arret de l'API.") | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Application | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| app = FastAPI( | |
| title="API Detection de Fraude β PFA", | |
| description=( | |
| "Systeme IA hybride de detection d'anomalies dans les transactions financieres.\n\n" | |
| "**Auteurs** : Othmane & Ramzi β 4eme annee IA/Data\n\n" | |
| "**Session 1** : Random Forest Baseline\n" | |
| "**Session 2** : Autoencoder + SHAP XAI\n" | |
| "**Dataset** : Kaggle Credit Card Fraud Detection" | |
| ), | |
| version="2.0.0", | |
| lifespan=lifespan, | |
| ) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # SchΓ©mas Pydantic | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class TransactionRequest(BaseModel): | |
| """ | |
| Payload d'une transaction. Features dans l'ordre : | |
| [V1, V2, ..., V28, Time (secondes), Amount (euros)] | |
| """ | |
| features: list[float] = Field( | |
| ..., | |
| min_length=N_FEATURES, | |
| max_length=N_FEATURES, | |
| description="30 features : V1-V28 + Time (s) + Amount (EUR)", | |
| example=[ | |
| -1.36, -0.07, 2.54, 1.38, -0.34, 0.46, 0.24, 0.10, | |
| 0.36, 0.09, -0.55, -0.62, -0.99, -0.31, 1.47, -0.47, | |
| 0.21, 0.03, 0.40, 0.25, -0.02, 0.28, -0.11, 0.07, | |
| 0.13, -0.19, 0.13, -0.02, 0.0, 149.62, | |
| ], | |
| ) | |
| def check_length(cls, v): | |
| if len(v) != N_FEATURES: | |
| raise ValueError(f"Attendu {N_FEATURES} features, recu {len(v)}") | |
| return v | |
| class FeatureContribution(BaseModel): | |
| rank: int | |
| feature: str | |
| shap_value: float | |
| feature_value: float | |
| direction: str | |
| class PredictionResponse(BaseModel): | |
| prediction: int | |
| label: str | |
| probability_fraud: float | |
| confidence: str | |
| latency_ms: float | |
| class ExplainResponse(BaseModel): | |
| prediction: int | |
| label: str | |
| probability_fraud: float | |
| confidence: str | |
| top_features: list[FeatureContribution] | |
| base_value: float | |
| latency_ms: float | |
| class AnalyzeResponse(BaseModel): | |
| # PrΓ©dictions | |
| rf_prediction: int | |
| rf_probability: float | |
| ae_prediction: Optional[int] | |
| ae_error: Optional[float] | |
| ae_threshold: Optional[float] | |
| final_prediction: int | |
| label: str | |
| confidence: str | |
| # Explication | |
| top_features: list[FeatureContribution] | |
| # MΓ©ta | |
| ae_available: bool | |
| latency_ms: float | |
| class HealthResponse(BaseModel): | |
| model_config = {"protected_namespaces": ()} | |
| status: str | |
| model_loaded: bool | |
| ae_available: bool | |
| version: str | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Utilitaires internes | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def _normalize(features: np.ndarray) -> np.ndarray: | |
| """Normalise Time et Amount avec le scaler, laisse V1-V28 intacts.""" | |
| feat = features.copy() | |
| ta_scaled = resources["scaler"].transform(feat[:, 28:30]) | |
| feat[:, 28] = ta_scaled[:, 0] | |
| feat[:, 29] = ta_scaled[:, 1] | |
| return feat | |
| def _get_confidence(prob: float) -> str: | |
| if prob >= 0.8: return "Tres elevee" | |
| if prob >= 0.5: return "Elevee" | |
| if prob >= 0.2: return "Moderee" | |
| return "Faible" | |
| def _shap_top_features(X: np.ndarray, top_n: int = 5) -> tuple[list, float]: | |
| """Retourne top N features SHAP + base_value.""" | |
| shap_vals_all = resources["explainer"].shap_values(X) | |
| if isinstance(shap_vals_all, list): | |
| sv = shap_vals_all[1][0] | |
| else: | |
| sv = shap_vals_all[0, :, 1] if len(shap_vals_all.shape) == 3 else shap_vals_all[0] | |
| base = (resources["explainer"].expected_value[1] | |
| if isinstance(resources["explainer"].expected_value, (list, np.ndarray)) | |
| else resources["explainer"].expected_value) | |
| sorted_idx = np.argsort(np.abs(sv))[::-1] | |
| top = [] | |
| for rank, idx in enumerate(sorted_idx[:top_n]): | |
| i = int(idx) | |
| top.append(FeatureContribution( | |
| rank=rank + 1, | |
| feature=FEATURE_NAMES[i], | |
| shap_value=round(float(sv[i]), 6), | |
| feature_value=round(float(X[0, i]), 6), | |
| direction="Augmente risque fraude" if sv[i] > 0 else "Reduit risque fraude", | |
| )) | |
| return top, float(base) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Routes | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| async def health_check(): | |
| """Statut de l'API et des modèles chargés.""" | |
| return HealthResponse( | |
| status="healthy" if resources["rf_model"] else "degraded", | |
| model_loaded=resources["rf_model"] is not None, | |
| ae_available=resources["ae_ready"], | |
| version="2.0.0", | |
| ) | |
| async def predict(request: TransactionRequest) -> PredictionResponse: | |
| """ | |
| **Session 1** β PrΓ©dit si une transaction est frauduleuse via Random Forest. | |
| Retourne la classe (0/1), la probabilitΓ© de fraude et le niveau de confiance. | |
| """ | |
| if resources["rf_model"] is None: | |
| raise HTTPException(503, "Modele RF non disponible") | |
| t0 = time.perf_counter() | |
| X = np.array(request.features, dtype=np.float64).reshape(1, -1) | |
| X = _normalize(X) | |
| pred = int(resources["rf_model"].predict(X)[0]) | |
| prob = float(resources["rf_model"].predict_proba(X)[0, 1]) | |
| latency = round((time.perf_counter() - t0) * 1000, 3) | |
| return PredictionResponse( | |
| prediction=pred, | |
| label="Fraude" if pred == 1 else "Legitime", | |
| probability_fraud=round(prob, 6), | |
| confidence=_get_confidence(prob), | |
| latency_ms=latency, | |
| ) | |
| async def explain(request: TransactionRequest) -> ExplainResponse: | |
| """ | |
| **Session 2** β PrΓ©dit + explique via SHAP TreeExplainer. | |
| Retourne les **5 features** qui ont le plus influencΓ© la dΓ©cision, | |
| avec leur valeur SHAP (positive = pousse vers fraude, nΓ©gative = lΓ©gitime). | |
| """ | |
| if resources["rf_model"] is None: | |
| raise HTTPException(503, "Modele RF non disponible") | |
| t0 = time.perf_counter() | |
| X = np.array(request.features, dtype=np.float64).reshape(1, -1) | |
| X = _normalize(X) | |
| pred = int(resources["rf_model"].predict(X)[0]) | |
| prob = float(resources["rf_model"].predict_proba(X)[0, 1]) | |
| top_features, base_val = _shap_top_features(X) | |
| latency = round((time.perf_counter() - t0) * 1000, 3) | |
| return ExplainResponse( | |
| prediction=pred, | |
| label="Fraude" if pred == 1 else "Legitime", | |
| probability_fraud=round(prob, 6), | |
| confidence=_get_confidence(prob), | |
| top_features=top_features, | |
| base_value=round(base_val, 6), | |
| latency_ms=latency, | |
| ) | |
| async def analyze(request: TransactionRequest) -> AnalyzeResponse: | |
| """ | |
| **Session 2** β Pipeline complet de dΓ©tection et d'explication. | |
| 1. **Random Forest** : dΓ©tection supervisΓ©e (post-SMOTE) | |
| 2. **Autoencoder** : dΓ©tection par anomalie de reconstruction (si disponible) | |
| 3. **Règle de fusion** : fraude si RF **OU** Autoencoder dit fraude | |
| 4. **SHAP** : explication des 5 features les plus influentes (RF) | |
| C'est la route principale du système hybride. | |
| """ | |
| if resources["rf_model"] is None: | |
| raise HTTPException(503, "Modele RF non disponible") | |
| t0 = time.perf_counter() | |
| X = np.array(request.features, dtype=np.float64).reshape(1, -1) | |
| X = _normalize(X) | |
| # ββ Random Forest ββββββββββββββββββββββββββββββββββββββββββ | |
| rf_pred = int(resources["rf_model"].predict(X)[0]) | |
| rf_prob = float(resources["rf_model"].predict_proba(X)[0, 1]) | |
| # ββ Autoencoder βββββββββββββββββββββββββββββββββββββββββββ | |
| ae_pred = ae_err = ae_thr = None | |
| if resources["ae_ready"] and resources["ae_model"] is not None: | |
| reconstruction = resources["ae_model"].predict(X, verbose=0) | |
| ae_err = float(np.mean(np.power(X - reconstruction, 2))) | |
| ae_thr = float(resources["threshold"]) | |
| ae_pred = int(ae_err > ae_thr) | |
| # ββ Fusion OR βββββββββββββββββββββββββββββββββββββββββββββ | |
| if ae_pred is not None: | |
| final_pred = int(max(rf_pred, ae_pred)) | |
| else: | |
| final_pred = rf_pred | |
| # ProbabilitΓ© finale : max des deux signaux | |
| final_prob = rf_prob | |
| if ae_pred is not None and ae_thr and ae_thr > 0: | |
| ae_prob = min(ae_err / (ae_thr * 3), 1.0) | |
| final_prob = max(rf_prob, ae_prob) | |
| # ββ SHAP ββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| top_features, _ = _shap_top_features(X) | |
| latency = round((time.perf_counter() - t0) * 1000, 3) | |
| return AnalyzeResponse( | |
| rf_prediction=rf_pred, | |
| rf_probability=round(rf_prob, 6), | |
| ae_prediction=ae_pred, | |
| ae_error=round(ae_err, 6) if ae_err is not None else None, | |
| ae_threshold=round(ae_thr, 6) if ae_thr is not None else None, | |
| final_prediction=final_pred, | |
| label="Fraude" if final_pred == 1 else "Legitime", | |
| confidence=_get_confidence(final_prob), | |
| top_features=top_features, | |
| ae_available=resources["ae_ready"], | |
| latency_ms=latency, | |
| ) | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| # Entry point | |
| # βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| if __name__ == "__main__": | |
| import uvicorn | |
| uvicorn.run("api:app", host="0.0.0.0", port=8000, reload=True, log_level="info") | |