File size: 5,077 Bytes
1de128a
 
 
 
 
 
 
 
 
 
2dd02fe
1e22a1f
2dd02fe
1e22a1f
 
2dd02fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0de7e06
2dd02fe
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
92a1fd3
2dd02fe
 
 
c742af8
2dd02fe
 
84394db
2dd02fe
 
1e22a1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67092ff
 
1e22a1f
 
 
 
 
 
 
 
 
67092ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e22a1f
67092ff
1e22a1f
2dd02fe
92a1fd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2dd02fe
 
0de7e06
2dd02fe
 
92a1fd3
 
 
 
 
 
2dd02fe
 
 
 
 
 
 
 
 
 
 
 
 
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
---
title: "Deploy ML API"
emoji: "🚀"
colorFrom: "blue"
colorTo: "green"
sdk: "docker"
sdk_version: "latest"
app_file: "app.py"
pinned: false
---

📖 Présentation du projet

Cette API permet de prédire l’attrition des employés à partir de données RH.
Elle a pour objectif d’aider les équipes RH à identifier les risques de départ et mettre en place des actions préventives.

- 📊 **Lister** les modèles ML disponibles (`/`)
- 🤖 **Prédire** avec un modèle donné (`/predict`)
- 🗄️ **Sauvegarder** automatiquement les inputs et outputs en base
- 📚 **Documentation Swagger/OpenAPI** générée automatiquement

---

## 📦 Prérequis

- Python 3.12+
- [Poetry](gestion des dépendances)
- Docker (PostgreSQL local via Compose)

---

## Installation

### 1. Cloner le dépôt
~~~bash
git clone https://github.com/marintosti12/deploy-machine-learning.git
cd deploy-machine-learning
~~~


### 2. Créer un environnement virtuel avec Poetry
~~~bash
poetry install
~~~

### 3. Configurer l’environnement

Crée un fichier **.env** à la racine :

~~~env
# PostgreSQL
DATABASE_URL=postgresql+psycopg2://futu:futu_pass@localhost:5432/futurisys
# Hugging Face
HF_TOKEN= Token Hugging Face
HF_REPO_ID= Repo Hugging Face
~~~


### 4. Base de données (PostgreSQL)

~~~bash
sudo docker compose up -d
~~~

🗄️ Base de données

~~~mermaid
classDiagram
  direction LR

  class MLModel {
    +String(36) id
    +String name
    +Text description
    +DateTime created_at
    +Boolean is_active
  }

  class MLInput {
    +String(36) id
    +DateTime created_at
    +Int id_employee
    +Int age
    +String genre
    +Int revenu_mensuel
    +String statut_marital
    +String departement
    +String poste
    +Int nombre_experiences_precedentes
    +Int nombre_heures_travailless
    +Int annee_experience_totale
    +Int annees_dans_l_entreprise
    +Int annees_dans_le_poste_actuel
    +Int nombre_participation_pee
    +Int nb_formations_suivies
    +Int nombre_employee_sous_responsabilite
    +Int code_sondage
    +Int distance_domicile_travail
    +Int niveau_education
    +String domaine_etude
    +String ayant_enfants
    +String frequence_deplacement
    +Int annees_depuis_la_derniere_promotion
    +Int annes_sous_responsable_actuel
    +Int satisfaction_employee_environnement
    +Int note_evaluation_precedente
    +Int niveau_hierarchique_poste
    +Int satisfaction_employee_nature_travail
    +Int satisfaction_employee_equipe
    +Int satisfaction_employee_equilibre_pro_perso
    +String eval_number
    +Int note_evaluation_actuelle
    +String heure_supplementaires
    +Int augementation_salaire_precedente
  }

  

  class MLOutput {
    +String(36) id
    +DateTime created_at
    +String(36) input_id  (FK -> MLInput.id)
    +String prediction
    +Float prob
    +String error
  }

  class EmployeeDataset {
    +BigInteger id  
    +DateTime created_at  
    +Integer id_employee  

    +Integer age
    +String genre
    +Integer revenu_mensuel
    +String statut_marital
    +String departement  
    +String poste

    +Integer nombre_experiences_precedentes
    +Integer nombre_heures_travailless
    +Integer annee_experience_totale
    +Integer annees_dans_l_entreprise
    +Integer annees_dans_le_poste_actuel

    +Integer a_quitte_l_entreprise

    +Integer nombre_participation_pee
    +Integer nb_formations_suivies
    +Integer nombre_employee_sous_responsabilite

    +Integer code_sondage
    +Integer distance_domicile_travail
    +Integer niveau_education
    +String domaine_etude

    +String ayant_enfants
    +String frequence_deplacement

    +Integer annees_depuis_la_derniere_promotion
    +Integer annes_sous_responsable_actuel
    +Integer satisfaction_employee_environnement
    +Integer note_evaluation_precedente
    +Integer niveau_hierarchique_poste
    +Integer satisfaction_employee_nature_travail
    +Integer satisfaction_employee_equipe
    +Integer satisfaction_employee_equilibre_pro_perso

    +String eval_number  
    +Integer note_evaluation_actuelle
    +String heure_supplementaires
    +Integer augementation_salaire_precedente

    +String source_file  
}

  %% Relations
  MLInput "1" --> "0..*" MLOutput
~~~

### 5. Lancer Migrations

~~~bash
export DATABASE_URL='postgresql+asyncpg://futu:futu_pass@localhost:5432/futurisys'
poetry run alembic upgrade head
~~~

### 6. Lancer Seeder

~~~bash
export DATABASE_URL='postgresql+psycopg2://futu:futu_pass@localhost:5432/futurisys'
poetry run python src/seeds/ml_models_seed.py 
~~~

### 7. Lancer l’API

~~~bash
poetry run uvicorn main:app --reload --app-dir src
~~~

### 8. Huggings Face

Pour générer les artefacts, exécuter les notebooks de machine learning.

Sur Hugging Face (Models), stocker les artefacts du modèle dans le dépôt du Space (models/) et nommer le fichier exactement comme le nom du modèle en base de données.


### 🧹 Qualité de code

**Lint :**
~~~bash
poetry run ruff check .
~~~

### 🧪 Tests & Couverture

**Lancer les tests :**
```bash
poetry run pytest --maxfail=1 --disable-warnings -q