Spaces:
Running
Running
Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,274 +1,73 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
- 🔐 Authentification API Key
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
##
|
| 19 |
-
|
| 20 |
-
```
|
| 21 |
-
OC_P5/
|
| 22 |
-
├── app.py # Point d'entrée FastAPI
|
| 23 |
-
├── src/
|
| 24 |
-
│ ├── auth.py # Authentification API Key
|
| 25 |
-
│ ├── config.py # Configuration centralisée
|
| 26 |
-
│ ├── logger.py # Logging structuré (NOUVEAU)
|
| 27 |
-
│ ├── models.py # Chargement modèle HF Hub
|
| 28 |
-
│ ├── preprocessing.py # Pipeline preprocessing
|
| 29 |
-
│ ├── rate_limit.py # Rate limiting (NOUVEAU)
|
| 30 |
-
│ └── schemas.py # Validation Pydantic
|
| 31 |
-
├── tests/ # Suite pytest (33 tests, 88% couverture)
|
| 32 |
-
├── logs/ # Logs JSON (NOUVEAU)
|
| 33 |
-
│ ├── api.log # Tous les logs
|
| 34 |
-
│ └── error.log # Erreurs uniquement
|
| 35 |
-
├── docs/ # Documentation
|
| 36 |
-
├── ml_model/ # Scripts training
|
| 37 |
-
└── data/ # Données sources
|
| 38 |
-
```
|
| 39 |
-
|
| 40 |
-
## 🚀 Installation
|
| 41 |
-
|
| 42 |
-
### Prérequis
|
| 43 |
-
- Python 3.12+
|
| 44 |
-
- Poetry 1.7+
|
| 45 |
-
- Git
|
| 46 |
-
|
| 47 |
-
### Setup rapide
|
| 48 |
-
|
| 49 |
-
```bash
|
| 50 |
-
# 1. Cloner le repo
|
| 51 |
-
git clone https://github.com/chaton59/OC_P5.git
|
| 52 |
-
cd OC_P5
|
| 53 |
-
|
| 54 |
-
# 2. Installer les dépendances
|
| 55 |
-
poetry install
|
| 56 |
-
|
| 57 |
-
# 3. Configurer l'environnement
|
| 58 |
-
cp .env.example .env
|
| 59 |
-
# Éditer .env avec vos valeurs
|
| 60 |
-
|
| 61 |
-
# 4. Lancer l'API
|
| 62 |
-
poetry run uvicorn app:app --reload
|
| 63 |
-
|
| 64 |
-
# 5. Accéder à la documentation
|
| 65 |
-
# http://localhost:8000/docs
|
| 66 |
-
```
|
| 67 |
-
|
| 68 |
-
## 📝 Configuration (.env)
|
| 69 |
-
|
| 70 |
-
```bash
|
| 71 |
-
# Mode développement (désactive auth + active logs détaillés)
|
| 72 |
-
DEBUG=true
|
| 73 |
-
|
| 74 |
-
# API Key (requis en production)
|
| 75 |
-
API_KEY=your-secret-key-here
|
| 76 |
-
|
| 77 |
-
# Logging (DEBUG, INFO, WARNING, ERROR, CRITICAL)
|
| 78 |
-
LOG_LEVEL=INFO
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
|
|
|
|
|
|
|
|
|
| 84 |
|
| 85 |
-
##
|
| 86 |
-
|
| 87 |
-
### Mode DEBUG (développement)
|
| 88 |
-
```bash
|
| 89 |
-
# L'API Key n'est PAS requise
|
| 90 |
-
curl http://localhost:8000/predict -H "Content-Type: application/json" -d '{...}'
|
| 91 |
-
```
|
| 92 |
|
| 93 |
-
###
|
| 94 |
```bash
|
| 95 |
-
|
| 96 |
-
curl http://localhost:8000/predict \
|
| 97 |
-
-H "X-API-Key: your-secret-key" \
|
| 98 |
-H "Content-Type: application/json" \
|
| 99 |
-
-d '{
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
```bash
|
| 106 |
-
GET /health
|
| 107 |
-
|
| 108 |
-
# Réponse
|
| 109 |
-
{
|
| 110 |
-
"status": "healthy",
|
| 111 |
-
"model_loaded": true,
|
| 112 |
-
"model_type": "Pipeline",
|
| 113 |
-
"version": "2.2.0"
|
| 114 |
-
}
|
| 115 |
```
|
| 116 |
|
| 117 |
-
###
|
| 118 |
```bash
|
| 119 |
-
POST /predict
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
# Payload (tous les champs d'un employé)
|
| 124 |
-
{
|
| 125 |
-
"nombre_participation_pee": 0,
|
| 126 |
-
"nb_formations_suivies": 2,
|
| 127 |
-
"satisfaction_employee_environnement": 3,
|
| 128 |
-
...
|
| 129 |
-
}
|
| 130 |
-
|
| 131 |
-
# Réponse
|
| 132 |
-
{
|
| 133 |
-
"prediction": 0, # 0 = reste, 1 = part
|
| 134 |
-
"probability_0": 0.85, # Probabilité de rester
|
| 135 |
-
"probability_1": 0.15, # Probabilité de partir
|
| 136 |
-
"risk_level": "Low" # Low, Medium, High
|
| 137 |
-
}
|
| 138 |
```
|
| 139 |
|
| 140 |
-
|
| 141 |
-
```
|
| 142 |
-
POST /predict/batch
|
| 143 |
-
X-API-Key: your-key (en production)
|
| 144 |
-
|
| 145 |
-
# Envoi des 3 fichiers CSV bruts
|
| 146 |
-
curl -X POST "http://localhost:8000/predict/batch" \
|
| 147 |
-
-H "X-API-Key: your-key" \
|
| 148 |
-
-F "sondage_file=@data/extrait_sondage.csv" \
|
| 149 |
-
-F "eval_file=@data/extrait_eval.csv" \
|
| 150 |
-
-F "sirh_file=@data/extrait_sirh.csv"
|
| 151 |
-
|
| 152 |
-
# Réponse
|
| 153 |
{
|
| 154 |
"total_employees": 1470,
|
| 155 |
-
"predictions": [
|
| 156 |
-
{"employee_id": 1, "prediction": 1, "probability_leave": 0.84, "risk_level": "High"},
|
| 157 |
-
{"employee_id": 2, "prediction": 0, "probability_leave": 0.11, "risk_level": "Low"}
|
| 158 |
-
],
|
| 159 |
"summary": {
|
| 160 |
"total_stay": 1169,
|
| 161 |
"total_leave": 301,
|
| 162 |
-
"high_risk_count": 222
|
| 163 |
-
"medium_risk_count": 233,
|
| 164 |
-
"low_risk_count": 1015
|
| 165 |
}
|
| 166 |
}
|
| 167 |
```
|
| 168 |
|
| 169 |
-
##
|
| 170 |
-
|
| 171 |
-
### Logs structurés JSON
|
| 172 |
-
|
| 173 |
-
**Fichiers** :
|
| 174 |
-
- `logs/api.log` : Tous les logs
|
| 175 |
-
- `logs/error.log` : Erreurs uniquement
|
| 176 |
-
|
| 177 |
-
**Format** :
|
| 178 |
-
```json
|
| 179 |
-
{
|
| 180 |
-
"timestamp": "2025-12-26T10:30:45",
|
| 181 |
-
"level": "INFO",
|
| 182 |
-
"logger": "employee_turnover_api",
|
| 183 |
-
"message": "Request POST /predict",
|
| 184 |
-
"method": "POST",
|
| 185 |
-
"path": "/predict",
|
| 186 |
-
"status_code": 200,
|
| 187 |
-
"duration_ms": 23.45,
|
| 188 |
-
"client_host": "127.0.0.1"
|
| 189 |
-
}
|
| 190 |
-
```
|
| 191 |
-
|
| 192 |
-
## 🛡️ Rate Limiting
|
| 193 |
-
|
| 194 |
-
**Configuration** :
|
| 195 |
-
- **Développement** : Désactivé (DEBUG=true)
|
| 196 |
-
- **Production** : 20 requêtes/minute par IP ou API Key
|
| 197 |
-
|
| 198 |
-
**En cas de dépassement** :
|
| 199 |
-
```json
|
| 200 |
-
{
|
| 201 |
-
"error": "Rate limit exceeded",
|
| 202 |
-
"message": "20 per 1 minute"
|
| 203 |
-
}
|
| 204 |
-
```
|
| 205 |
-
|
| 206 |
-
## ✅ Tests
|
| 207 |
-
|
| 208 |
-
```bash
|
| 209 |
-
# Tous les tests
|
| 210 |
-
poetry run pytest tests/ -v
|
| 211 |
-
|
| 212 |
-
# Avec couverture
|
| 213 |
-
poetry run pytest tests/ --cov --cov-report=html
|
| 214 |
-
|
| 215 |
-
# Voir rapport HTML
|
| 216 |
-
open htmlcov/index.html
|
| 217 |
-
```
|
| 218 |
-
|
| 219 |
-
**Résultats** :
|
| 220 |
-
- ✅ 33 tests passés
|
| 221 |
-
- 📊 88% de couverture globale
|
| 222 |
-
|
| 223 |
-
## 🚀 Déploiement
|
| 224 |
-
|
| 225 |
-
### Variables d'environnement requises
|
| 226 |
-
```bash
|
| 227 |
-
DEBUG=false
|
| 228 |
-
API_KEY=<votre-clé-sécurisée>
|
| 229 |
-
LOG_LEVEL=INFO
|
| 230 |
-
```
|
| 231 |
-
|
| 232 |
-
### HuggingFace Spaces
|
| 233 |
-
Prêt pour déploiement avec `app.py` et `requirements.txt`
|
| 234 |
-
|
| 235 |
-
## 📚 Documentation
|
| 236 |
-
|
| 237 |
-
- **API Interactive** : http://localhost:8000/docs
|
| 238 |
-
- **ReDoc** : http://localhost:8000/redoc
|
| 239 |
-
- **Guide complet** : [docs/API_GUIDE.md](docs/API_GUIDE.md)
|
| 240 |
-
- **Standards** : [docs/standards.md](docs/standards.md)
|
| 241 |
-
- **Couverture tests** : [docs/TEST_COVERAGE.md](docs/TEST_COVERAGE.md)
|
| 242 |
-
|
| 243 |
-
## 📦 Dépendances principales
|
| 244 |
-
|
| 245 |
-
- **FastAPI** 0.115.14 : Framework web
|
| 246 |
-
- **Pydantic** 2.12.5 : Validation données
|
| 247 |
-
- **XGBoost** 2.1.3 : Modèle ML
|
| 248 |
-
- **SlowAPI** 0.1.9 : Rate limiting
|
| 249 |
-
- **python-json-logger** 4.0.0 : Logs structurés
|
| 250 |
-
- **pytest** 9.0.2 : Tests
|
| 251 |
-
|
| 252 |
-
## 🔄 Changelog
|
| 253 |
-
|
| 254 |
-
### v2.2.0 (27 décembre 2025)
|
| 255 |
-
- 📦 Nouvel endpoint `/predict/batch` pour traitement CSV direct
|
| 256 |
-
- 🔧 Fix preprocessing : ajout du scaling des features
|
| 257 |
-
- 🔧 Fix preprocessing : correction de l'ordre des colonnes
|
| 258 |
-
- 📊 Amélioration précision des prédictions (~90%)
|
| 259 |
-
|
| 260 |
-
### v2.1.0 (26 décembre 2025)
|
| 261 |
-
- ✨ Système de logging structuré JSON
|
| 262 |
-
- 🛡️ Rate limiting avec SlowAPI
|
| 263 |
-
- ⚡ Amélioration gestion d'erreurs
|
| 264 |
-
- 📊 Monitoring des performances
|
| 265 |
-
|
| 266 |
-
### v2.0.0 (26 décembre 2025)
|
| 267 |
-
- ✅ Suite de tests complète (36 tests)
|
| 268 |
-
- 🔐 Authentification API Key
|
| 269 |
-
- 📊 88% de couverture de code
|
| 270 |
-
|
| 271 |
-
## 👥 Auteurs
|
| 272 |
|
| 273 |
-
|
| 274 |
-
- **Repo** : [github.com/chaton59/OC_P5](https://github.com/chaton59/OC_P5)
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Employee Turnover Prediction API
|
| 3 |
+
emoji: 👔
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: docker
|
| 7 |
+
pinned: true
|
| 8 |
+
license: mit
|
| 9 |
+
app_port: 7860
|
| 10 |
+
---
|
| 11 |
+
|
| 12 |
+
# Employee Turnover Prediction API 🚀
|
| 13 |
+
|
| 14 |
+
API de prédiction du turnover des employés avec XGBoost + SMOTE.
|
| 15 |
+
|
| 16 |
+
## 🎯 Fonctionnalités
|
| 17 |
+
|
| 18 |
+
- ✅ Prédiction de turnover (0 = reste, 1 = part)
|
| 19 |
+
- 📦 **Nouveau** : Endpoint batch pour traiter vos fichiers CSV directement
|
| 20 |
+
- 📊 Probabilités et niveau de risque (Low/Medium/High)
|
| 21 |
- 🔐 Authentification API Key
|
| 22 |
+
- 📝 Logs structurés JSON
|
| 23 |
+
- 🛡️ Rate limiting (20 req/min)
|
| 24 |
+
- 📚 Documentation OpenAPI/Swagger
|
| 25 |
|
| 26 |
+
## 🔗 Endpoints
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
| Endpoint | Description |
|
| 29 |
+
|----------|-------------|
|
| 30 |
+
| `/docs` | Documentation interactive Swagger |
|
| 31 |
+
| `/health` | Status de l'API |
|
| 32 |
+
| `/ui` | Interface Gradio interactive |
|
| 33 |
+
| `/predict` | Prédiction unitaire (JSON) |
|
| 34 |
+
| `/predict/batch` | Prédiction batch (3 fichiers CSV) |
|
| 35 |
|
| 36 |
+
## 🚀 Utilisation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
+
### Prédiction unitaire
|
| 39 |
```bash
|
| 40 |
+
curl -X POST https://asi-engineer-oc-p5-dev.hf.space/predict \
|
|
|
|
|
|
|
| 41 |
-H "Content-Type: application/json" \
|
| 42 |
+
-d '{
|
| 43 |
+
"nombre_participation_pee": 0,
|
| 44 |
+
"nb_formations_suivies": 2,
|
| 45 |
+
"satisfaction_employee_environnement": 3,
|
| 46 |
+
...
|
| 47 |
+
}'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
```
|
| 49 |
|
| 50 |
+
### Prédiction batch (fichiers CSV)
|
| 51 |
```bash
|
| 52 |
+
curl -X POST https://asi-engineer-oc-p5-dev.hf.space/predict/batch \
|
| 53 |
+
-F "sondage_file=@extrait_sondage.csv" \
|
| 54 |
+
-F "eval_file=@extrait_eval.csv" \
|
| 55 |
+
-F "sirh_file=@extrait_sirh.csv"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
```
|
| 57 |
|
| 58 |
+
**Réponse :**
|
| 59 |
+
```json
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
{
|
| 61 |
"total_employees": 1470,
|
| 62 |
+
"predictions": [...],
|
|
|
|
|
|
|
|
|
|
| 63 |
"summary": {
|
| 64 |
"total_stay": 1169,
|
| 65 |
"total_leave": 301,
|
| 66 |
+
"high_risk_count": 222
|
|
|
|
|
|
|
| 67 |
}
|
| 68 |
}
|
| 69 |
```
|
| 70 |
|
| 71 |
+
## 📚 Documentation complète
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
+
Voir [GitHub Repository](https://github.com/chaton59/OC_P5) pour la documentation complète.
|
|
|