Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -6,24 +6,19 @@ Déploiement sur Hugging Face Spaces pour tests rapides.
|
|
| 6 |
Version de démonstration - Interface complète en développement.
|
| 7 |
"""
|
| 8 |
import gradio as gr
|
| 9 |
-
import mlflow
|
| 10 |
-
import mlflow.pyfunc
|
| 11 |
from huggingface_hub import hf_hub_download
|
| 12 |
|
| 13 |
# Configuration
|
| 14 |
HF_MODEL_REPO = "ASI-Engineer/employee-turnover-model"
|
| 15 |
-
FALLBACK_RUN_ID = "40e43c8e425345bab3d19f27eb8fe5d8"
|
| 16 |
|
| 17 |
|
| 18 |
def load_model():
|
| 19 |
"""
|
| 20 |
-
Charge le modèle depuis Hugging Face Hub
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
2. MLflow local (développement local)
|
| 25 |
"""
|
| 26 |
-
# Essayer HF Hub en premier (production) - charger directement le pickle
|
| 27 |
try:
|
| 28 |
import joblib
|
| 29 |
|
|
@@ -35,24 +30,8 @@ def load_model():
|
|
| 35 |
print(f"✅ Modèle chargé depuis HF Hub: {HF_MODEL_REPO}")
|
| 36 |
return model, "HF Hub"
|
| 37 |
except Exception as e:
|
| 38 |
-
print(f"
|
| 39 |
-
|
| 40 |
-
# Fallback: MLflow local (développement uniquement)
|
| 41 |
-
try:
|
| 42 |
-
mlflow.set_tracking_uri("sqlite:///mlflow.db")
|
| 43 |
-
# Essayer Model Registry d'abord
|
| 44 |
-
model = mlflow.pyfunc.load_model("models:/XGBoost_Employee_Turnover/latest") # type: ignore[attr-defined]
|
| 45 |
-
print("✅ Modèle chargé depuis MLflow Model Registry")
|
| 46 |
-
return model, "MLflow Registry"
|
| 47 |
-
except Exception:
|
| 48 |
-
try:
|
| 49 |
-
# Fallback sur run ID
|
| 50 |
-
model = mlflow.pyfunc.load_model(f"runs:/{FALLBACK_RUN_ID}/model") # type: ignore[attr-defined]
|
| 51 |
-
print(f"✅ Modèle chargé depuis MLflow run: {FALLBACK_RUN_ID}")
|
| 52 |
-
return model, "MLflow Local"
|
| 53 |
-
except Exception as e2:
|
| 54 |
-
print(f"❌ Erreur chargement MLflow: {e2}")
|
| 55 |
-
return None, "Error"
|
| 56 |
|
| 57 |
|
| 58 |
# Charger le modèle au démarrage
|
|
@@ -82,27 +61,9 @@ def get_model_info():
|
|
| 82 |
"model_type": type(model).__name__,
|
| 83 |
"features": "~50 features (après preprocessing)",
|
| 84 |
"algorithme": "XGBoost + SMOTE",
|
| 85 |
-
"hf_hub_repo": HF_MODEL_REPO
|
| 86 |
}
|
| 87 |
|
| 88 |
-
# Si MLflow local, ajouter les métriques
|
| 89 |
-
if model_source == "MLflow Local":
|
| 90 |
-
mlflow.set_tracking_uri("sqlite:///mlflow.db")
|
| 91 |
-
client = mlflow.MlflowClient()
|
| 92 |
-
runs = client.search_runs(
|
| 93 |
-
experiment_ids=["1"], order_by=["start_time DESC"], max_results=1
|
| 94 |
-
)
|
| 95 |
-
if runs:
|
| 96 |
-
run = runs[0]
|
| 97 |
-
metrics = run.data.metrics
|
| 98 |
-
info.update(
|
| 99 |
-
{
|
| 100 |
-
"run_id": run.info.run_id[:8],
|
| 101 |
-
"f1_score": f"{metrics.get('f1_score', 0):.4f}",
|
| 102 |
-
"accuracy": f"{metrics.get('accuracy', 0):.4f}",
|
| 103 |
-
}
|
| 104 |
-
)
|
| 105 |
-
|
| 106 |
info["info"] = "Interface de prédiction en développement - API FastAPI à venir"
|
| 107 |
return info
|
| 108 |
|
|
|
|
| 6 |
Version de démonstration - Interface complète en développement.
|
| 7 |
"""
|
| 8 |
import gradio as gr
|
|
|
|
|
|
|
| 9 |
from huggingface_hub import hf_hub_download
|
| 10 |
|
| 11 |
# Configuration
|
| 12 |
HF_MODEL_REPO = "ASI-Engineer/employee-turnover-model"
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
def load_model():
|
| 16 |
"""
|
| 17 |
+
Charge le modèle depuis Hugging Face Hub.
|
| 18 |
|
| 19 |
+
En production (HF Spaces), charge uniquement depuis HF Hub.
|
| 20 |
+
Le fallback MLflow local n'est disponible qu'en développement local.
|
|
|
|
| 21 |
"""
|
|
|
|
| 22 |
try:
|
| 23 |
import joblib
|
| 24 |
|
|
|
|
| 30 |
print(f"✅ Modèle chargé depuis HF Hub: {HF_MODEL_REPO}")
|
| 31 |
return model, "HF Hub"
|
| 32 |
except Exception as e:
|
| 33 |
+
print(f"❌ Erreur chargement depuis HF Hub: {e}")
|
| 34 |
+
return None, "Error"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
# Charger le modèle au démarrage
|
|
|
|
| 61 |
"model_type": type(model).__name__,
|
| 62 |
"features": "~50 features (après preprocessing)",
|
| 63 |
"algorithme": "XGBoost + SMOTE",
|
| 64 |
+
"hf_hub_repo": HF_MODEL_REPO,
|
| 65 |
}
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
info["info"] = "Interface de prédiction en développement - API FastAPI à venir"
|
| 68 |
return info
|
| 69 |
|