Spaces:
Running
Running
Upload folder using huggingface_hub
Browse files- README_HF.md +33 -0
- app.py +149 -5
- requirements.txt +1 -0
README_HF.md
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Employee Turnover Prediction - DEV
|
| 3 |
+
emoji: 🎯
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.9.1
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# 🎯 Employee Turnover Prediction - Environment DEV
|
| 13 |
+
|
| 14 |
+
Interface de test pour prédire le risque de départ des employés.
|
| 15 |
+
|
| 16 |
+
## 🚀 Modèle
|
| 17 |
+
|
| 18 |
+
- **Algorithme**: XGBoost avec RandomizedSearchCV
|
| 19 |
+
- **Équilibrage**: SMOTE pour classes déséquilibrées (ratio 5:1)
|
| 20 |
+
- **Tracking**: MLflow pour versioning et reproductibilité
|
| 21 |
+
- **Métriques**: Optimisé pour F1-Score
|
| 22 |
+
|
| 23 |
+
## 📊 Utilisation
|
| 24 |
+
|
| 25 |
+
1. Ajustez les paramètres de l'employé (satisfaction, évaluation, projets, etc.)
|
| 26 |
+
2. Cliquez sur "Prédire le risque de départ"
|
| 27 |
+
3. Obtenez la probabilité de turnover et les recommandations
|
| 28 |
+
|
| 29 |
+
## 🔧 Développement
|
| 30 |
+
|
| 31 |
+
Ce Space est synchronisé automatiquement via CI/CD depuis la branche `dev` du repository GitHub.
|
| 32 |
+
|
| 33 |
+
**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bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Interface Gradio pour tester le modèle Employee Turnover en production.
|
| 4 |
|
| 5 |
+
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.sklearn
|
| 11 |
|
| 12 |
|
| 13 |
+
# Configuration MLflow
|
| 14 |
+
mlflow.set_tracking_uri("sqlite:///mlflow.db")
|
| 15 |
+
|
| 16 |
+
# Charger le modèle le plus récent
|
| 17 |
+
MODEL_URI = "models:/Employee_Turnover_Model/latest"
|
| 18 |
+
# Fallback: utiliser un run_id spécifique si le modèle n'est pas enregistré
|
| 19 |
+
FALLBACK_RUN_ID = "2dd66b2b125646e19cf123c6944c9185"
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def load_model():
|
| 23 |
+
"""Charge le modèle depuis MLflow."""
|
| 24 |
+
try:
|
| 25 |
+
model = mlflow.sklearn.load_model(MODEL_URI)
|
| 26 |
+
print(f"✅ Modèle chargé depuis Model Registry: {MODEL_URI}")
|
| 27 |
+
return model
|
| 28 |
+
except Exception as e:
|
| 29 |
+
print(f"⚠️ Model Registry non disponible, utilisation du run_id: {e}")
|
| 30 |
+
try:
|
| 31 |
+
model = mlflow.sklearn.load_model(f"runs:/{FALLBACK_RUN_ID}/model")
|
| 32 |
+
print(f"✅ Modèle chargé depuis run_id: {FALLBACK_RUN_ID}")
|
| 33 |
+
return model
|
| 34 |
+
except Exception as e2:
|
| 35 |
+
print(f"❌ Erreur lors du chargement du modèle: {e2}")
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# Charger le modèle au démarrage
|
| 40 |
+
try:
|
| 41 |
+
model = load_model()
|
| 42 |
+
MODEL_LOADED = model is not None
|
| 43 |
+
except Exception as e:
|
| 44 |
+
print(f"❌ Erreur lors du chargement du modèle: {e}")
|
| 45 |
+
MODEL_LOADED = False
|
| 46 |
+
model = None
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
def get_model_info():
|
| 50 |
+
"""Retourne les informations sur le modèle."""
|
| 51 |
+
if not MODEL_LOADED:
|
| 52 |
+
return {
|
| 53 |
+
"status": "❌ Modèle non disponible",
|
| 54 |
+
"error": "Le modèle n'a pas pu être chargé depuis MLflow",
|
| 55 |
+
"solution": "Vérifiez que main.py a bien été exécuté pour entraîner le modèle",
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
try:
|
| 59 |
+
# Obtenir des informations sur le modèle
|
| 60 |
+
client = mlflow.MlflowClient()
|
| 61 |
+
runs = client.search_runs(
|
| 62 |
+
experiment_ids=["1"], order_by=["start_time DESC"], max_results=1
|
| 63 |
+
)
|
| 64 |
+
|
| 65 |
+
if runs:
|
| 66 |
+
run = runs[0]
|
| 67 |
+
metrics = run.data.metrics
|
| 68 |
+
return {
|
| 69 |
+
"status": "✅ Modèle chargé avec succès",
|
| 70 |
+
"run_id": run.info.run_id[:8],
|
| 71 |
+
"f1_score": f"{metrics.get('f1_score', 0):.4f}",
|
| 72 |
+
"accuracy": f"{metrics.get('accuracy', 0):.4f}",
|
| 73 |
+
"features": "~50 features (après preprocessing)",
|
| 74 |
+
"algorithme": "XGBoost + SMOTE",
|
| 75 |
+
"info": "Interface de prédiction en développement - API FastAPI à venir",
|
| 76 |
+
}
|
| 77 |
+
else:
|
| 78 |
+
return {
|
| 79 |
+
"status": "✅ Modèle chargé",
|
| 80 |
+
"info": "Pas de métriques disponibles",
|
| 81 |
+
"run_id": FALLBACK_RUN_ID[:8],
|
| 82 |
+
}
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
return {"status": "✅ Modèle chargé (info limitées)", "error": str(e)}
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# Interface Gradio
|
| 89 |
+
with gr.Blocks(
|
| 90 |
+
title="Employee Turnover Prediction - DEV", theme=gr.themes.Soft()
|
| 91 |
+
) as demo:
|
| 92 |
+
gr.Markdown("# 🎯 Prédiction du Turnover - Employee Attrition")
|
| 93 |
+
gr.Markdown("## Environment DEV - Test de déploiement CI/CD")
|
| 94 |
+
|
| 95 |
+
gr.Markdown(
|
| 96 |
+
"""
|
| 97 |
+
### 📊 Statut du projet
|
| 98 |
+
|
| 99 |
+
Ce Space est synchronisé automatiquement depuis GitHub (branche `dev`).
|
| 100 |
+
|
| 101 |
+
**Actuellement disponible :**
|
| 102 |
+
- ✅ Pipeline d'entraînement MLflow complet (`main.py`)
|
| 103 |
+
- ✅ Déploiement automatique CI/CD (GitHub Actions → HF Spaces)
|
| 104 |
+
- ✅ Tests unitaires et linting automatisés
|
| 105 |
+
|
| 106 |
+
**En développement :**
|
| 107 |
+
- 🚧 Interface de prédiction interactive
|
| 108 |
+
- 🚧 API FastAPI avec endpoints de prédiction
|
| 109 |
+
- 🚧 Intégration PostgreSQL pour tracking des prédictions
|
| 110 |
+
"""
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
with gr.Row():
|
| 114 |
+
with gr.Column():
|
| 115 |
+
gr.Markdown("### 🔍 Informations sur le modèle")
|
| 116 |
+
check_btn = gr.Button("📊 Vérifier le statut du modèle", variant="primary")
|
| 117 |
+
|
| 118 |
+
with gr.Column():
|
| 119 |
+
model_output = gr.JSON(label="Statut")
|
| 120 |
+
|
| 121 |
+
check_btn.click(fn=get_model_info, inputs=[], outputs=model_output)
|
| 122 |
+
|
| 123 |
+
gr.Markdown("---")
|
| 124 |
+
|
| 125 |
+
gr.Markdown(
|
| 126 |
+
"""
|
| 127 |
+
### 🛠️ Prochaines étapes (selon etapes.txt)
|
| 128 |
+
|
| 129 |
+
1. **Étape 3** : Développement API FastAPI
|
| 130 |
+
- Endpoints de prédiction avec validation Pydantic
|
| 131 |
+
- Chargement dynamique des preprocessing artifacts (scaler, encoders)
|
| 132 |
+
- Documentation Swagger/OpenAPI automatique
|
| 133 |
+
|
| 134 |
+
2. **Étape 4** : Intégration PostgreSQL
|
| 135 |
+
- Stockage des inputs/outputs des prédictions
|
| 136 |
+
- Traçabilité complète des requêtes
|
| 137 |
+
|
| 138 |
+
3. **Étape 5** : Tests unitaires et fonctionnels
|
| 139 |
+
- Tests des endpoints API
|
| 140 |
+
- Tests de charge et performance
|
| 141 |
+
- Couverture de code avec pytest-cov
|
| 142 |
+
|
| 143 |
+
### 📚 Documentation
|
| 144 |
+
- **Repository GitHub** : [chaton59/OC_P5](https://github.com/chaton59/OC_P5)
|
| 145 |
+
- **MLflow Tracking** : Disponible en local (`./scripts/start_mlflow.sh`)
|
| 146 |
+
- **Métriques** : F1-Score optimisé, gestion classes déséquilibrées (SMOTE)
|
| 147 |
+
"""
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
if __name__ == "__main__":
|
| 152 |
+
demo.launch(server_name="0.0.0.0", server_port=7860, share=False)
|
requirements.txt
CHANGED
|
@@ -13,3 +13,4 @@ numpy==2.0.2
|
|
| 13 |
pandas==2.2.3
|
| 14 |
joblib==1.4.2
|
| 15 |
mlflow==3.8.0
|
|
|
|
|
|
| 13 |
pandas==2.2.3
|
| 14 |
joblib==1.4.2
|
| 15 |
mlflow==3.8.0
|
| 16 |
+
gradio==5.9.1
|