ASI-Engineer commited on
Commit
6ca43a7
·
verified ·
1 Parent(s): c77c313

Upload folder using huggingface_hub

Browse files
exemples/demo_unitaire.py CHANGED
@@ -3,14 +3,23 @@
3
  🔮 Prédiction UNITAIRE - Interface simple avec entrées numériques uniquement
4
 
5
  Usage: python demo_unitaire.py
 
6
  """
7
 
8
- import requests
 
 
 
 
 
 
 
 
9
 
10
  # ═══════════════════════════════════════════════════════════════
11
  # CONFIGURATION
12
  # ═══════════════════════════════════════════════════════════════
13
- API_URL = "http://127.0.0.1:7860" # API locale
14
 
15
  # ═══════════════════════════════════════════════════════════════
16
  # OPTIONS À AFFICHER (pour référence utilisateur)
@@ -111,78 +120,88 @@ annees_entreprise = int(input("Années dans l'entreprise [0-40]: "))
111
  annees_poste = int(input("Années dans le poste actuel [0-18]: "))
112
 
113
  # ═══════════════════════════════════════════════════════════════
114
- # CONSTRUCTION DE LA REQUÊTE
115
  # ═══════════════════════════════════════════════════════════════
116
- employee_data = {
117
- "nombre_participation_pee": nombre_participation_pee,
118
- "nb_formations_suivies": nb_formations_suivies,
119
- "nombre_employee_sous_responsabilite": 1,
120
- "distance_domicile_travail": distance_domicile_travail,
121
- "niveau_education": niveau_education,
122
- "domaine_etude": domaine_etude,
123
- "ayant_enfants": ayant_enfants,
124
- "frequence_deplacement": frequence_deplacement,
125
- "annees_depuis_la_derniere_promotion": annees_depuis_promo,
126
- "annes_sous_responsable_actuel": annees_sous_responsable,
127
- "satisfaction_employee_environnement": satisfaction_environnement,
128
- "note_evaluation_precedente": note_eval_precedente,
129
- "niveau_hierarchique_poste": niveau_hierarchique,
130
- "satisfaction_employee_nature_travail": satisfaction_travail,
131
- "satisfaction_employee_equipe": satisfaction_equipe,
132
- "satisfaction_employee_equilibre_pro_perso": satisfaction_equilibre,
133
- "note_evaluation_actuelle": note_eval_actuelle,
134
- "heure_supplementaires": heure_supplementaires,
135
- "augementation_salaire_precedente": augmentation_salaire,
136
- "age": age,
137
- "genre": genre,
138
- "revenu_mensuel": revenu_mensuel,
139
- "statut_marital": statut_marital,
140
- "departement": departement,
141
- "poste": poste,
142
- "nombre_experiences_precedentes": nombre_exp_precedentes,
143
- "nombre_heures_travailless": 80,
144
- "annee_experience_totale": annees_exp_totale,
145
- "annees_dans_l_entreprise": annees_entreprise,
146
- "annees_dans_le_poste_actuel": annees_poste,
147
- }
148
-
149
- # ═══════════════════════════════════════════════════════════════
150
- # PRÉDICTION
151
- # ═══════════════════════════════════════════════════════════════
152
- print("\n⏳ Envoi de la requête à l'API locale...")
153
 
154
  try:
155
- response = requests.post(
156
- f"{API_URL}/predict",
157
- json=employee_data,
158
- headers={"Content-Type": "application/json"},
159
- timeout=30,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  )
161
- response.raise_for_status()
162
- result = response.json()
163
 
164
  print("\n" + "═" * 60)
165
  print("📊 RÉSULTAT DE LA PRÉDICTION")
166
  print("═" * 60)
167
 
168
- prediction = result.get("prediction", "N/A")
169
- prob_stay = result.get("probability_stay", 0) * 100
170
- prob_leave = result.get("probability_leave", 0) * 100
171
- risk = result.get("risk_level", "N/A")
172
-
173
- if prediction == 1:
174
- print("\n🚨 PRÉDICTION: VA PARTIR")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
175
  else:
176
- print("\n PRÉDICTION: VA RESTER")
177
-
178
- print(f"\n📈 Probabilité de rester: {prob_stay:.1f}%")
179
- print(f"📉 Probabilité de partir: {prob_leave:.1f}%")
180
-
181
- risk_emoji = {"Low": "🟢", "Medium": "🟠", "High": "🔴"}.get(risk, "⚪")
182
- print(f"\n{risk_emoji} Niveau de risque: {risk}")
183
 
184
- except requests.exceptions.ConnectionError:
185
- print("\n❌ Impossible de se connecter à l'API locale.")
186
- print(" Lancez d'abord: ./lancer_api.sh")
187
  except Exception as e:
188
  print(f"\n❌ Erreur: {e}")
 
 
3
  🔮 Prédiction UNITAIRE - Interface simple avec entrées numériques uniquement
4
 
5
  Usage: python demo_unitaire.py
6
+ Note: Utilise l'API Gradio locale qui retourne du Markdown
7
  """
8
 
9
+ import re
10
+ import sys
11
+
12
+ try:
13
+ from gradio_client import Client
14
+ except ImportError:
15
+ print("❌ gradio_client non installé. Installez-le avec:")
16
+ print(" pip install gradio_client")
17
+ sys.exit(1)
18
 
19
  # ═══════════════════════════════════════════════════════════════
20
  # CONFIGURATION
21
  # ═══════════════════════════════════════════════════════════════
22
+ API_URL = "http://127.0.0.1:7860" # API Gradio locale
23
 
24
  # ═══════════════════════════════════════════════════════════════
25
  # OPTIONS À AFFICHER (pour référence utilisateur)
 
120
  annees_poste = int(input("Années dans le poste actuel [0-18]: "))
121
 
122
  # ═══════════════════════════════════════════════════════════════
123
+ # PRÉDICTION VIA GRADIO CLIENT
124
  # ═══════════════════════════════════════════════════════════════
125
+ print("\n⏳ Connexion à l'API Gradio locale...")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  try:
128
+ client = Client(API_URL)
129
+ print("✅ Connecté\n")
130
+ print("⏳ Envoi de la prédiction...")
131
+
132
+ result = client.predict(
133
+ nombre_participation_pee=nombre_participation_pee,
134
+ nb_formations_suivies=nb_formations_suivies,
135
+ nombre_employee_sous_responsabilite=1,
136
+ distance_domicile_travail=distance_domicile_travail,
137
+ niveau_education=niveau_education,
138
+ domaine_etude=domaine_etude,
139
+ ayant_enfants=ayant_enfants,
140
+ frequence_deplacement=frequence_deplacement,
141
+ annees_depuis_la_derniere_promotion=annees_depuis_promo,
142
+ annes_sous_responsable_actuel=annees_sous_responsable,
143
+ satisfaction_employee_environnement=satisfaction_environnement,
144
+ note_evaluation_precedente=note_eval_precedente,
145
+ niveau_hierarchique_poste=niveau_hierarchique,
146
+ satisfaction_employee_nature_travail=satisfaction_travail,
147
+ satisfaction_employee_equipe=satisfaction_equipe,
148
+ satisfaction_employee_equilibre_pro_perso=satisfaction_equilibre,
149
+ note_evaluation_actuelle=note_eval_actuelle,
150
+ heure_supplementaires=heure_supplementaires,
151
+ augementation_salaire_precedente=augmentation_salaire,
152
+ age=age,
153
+ genre=genre,
154
+ revenu_mensuel=revenu_mensuel,
155
+ statut_marital=statut_marital,
156
+ departement=departement,
157
+ poste=poste,
158
+ nombre_experiences_precedentes=nombre_exp_precedentes,
159
+ nombre_heures_travailless=80,
160
+ annee_experience_totale=annees_exp_totale,
161
+ annees_dans_l_entreprise=annees_entreprise,
162
+ annees_dans_le_poste_actuel=annees_poste,
163
+ api_name="/predict",
164
  )
 
 
165
 
166
  print("\n" + "═" * 60)
167
  print("📊 RÉSULTAT DE LA PRÉDICTION")
168
  print("═" * 60)
169
 
170
+ # Le résultat est du Markdown - on l'affiche directement
171
+ # mais on extrait aussi les valeurs clés
172
+ if isinstance(result, str):
173
+ # Extraire les probabilités du Markdown
174
+ prob_depart = re.search(r"Probabilité de départ[^:]*:\s*([\d.]+)%", result)
175
+ prob_maintien = re.search(r"Probabilité de maintien[^:]*:\s*([\d.]+)%", result)
176
+ confiance = re.search(r"Confiance[^:]*:\s*([\d.]+)%", result)
177
+
178
+ # Détecter le risque
179
+ if "RISQUE ÉLEVÉ" in result:
180
+ print("\n🔴 RISQUE ÉLEVÉ DE DÉPART")
181
+ elif "RISQUE MOYEN" in result:
182
+ print("\n🟠 RISQUE MOYEN DE DÉPART")
183
+ else:
184
+ print("\n🟢 RISQUE FAIBLE DE DÉPART")
185
+
186
+ # Afficher les probabilités
187
+ if prob_maintien:
188
+ print(f"\n📈 Probabilité de rester: {prob_maintien.group(1)}%")
189
+ if prob_depart:
190
+ print(f"📉 Probabilité de partir: {prob_depart.group(1)}%")
191
+ if confiance:
192
+ print(f"🎯 Confiance du modèle: {confiance.group(1)}%")
193
+
194
+ # Afficher la prédiction
195
+ if "Départ probable" in result:
196
+ print("\n🚨 PRÉDICTION: VA PARTIR")
197
+ else:
198
+ print("\n✅ PRÉDICTION: VA RESTER")
199
  else:
200
+ print(f"\n📋 Résultat: {result}")
 
 
 
 
 
 
201
 
202
+ except ConnectionError:
203
+ print("\n❌ Impossible de se connecter à l'API Gradio locale.")
204
+ print(" Lancez d'abord: python app.py")
205
  except Exception as e:
206
  print(f"\n❌ Erreur: {e}")
207
+ sys.exit(1)
exemples/demo_unitaire_hf.py CHANGED
@@ -7,6 +7,7 @@ Prérequis: pip install gradio_client
7
  """
8
 
9
  import os
 
10
  import sys
11
 
12
  try:
@@ -165,23 +166,34 @@ try:
165
  print("📊 RÉSULTAT DE LA PRÉDICTION (HF)")
166
  print("═" * 60)
167
 
168
- # Le résultat Gradio peut être un dict ou une string
169
- if isinstance(result, dict):
170
- prediction = result.get("prediction", "N/A")
171
- prob_stay = result.get("probability_stay", 0) * 100
172
- prob_leave = result.get("probability_leave", 0) * 100
173
- risk = result.get("risk_level", "N/A")
174
-
175
- if prediction == 1:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  print("\n🚨 PRÉDICTION: VA PARTIR")
177
  else:
178
  print("\n✅ PRÉDICTION: VA RESTER")
179
-
180
- print(f"\n📈 Probabilité de rester: {prob_stay:.1f}%")
181
- print(f"📉 Probabilité de partir: {prob_leave:.1f}%")
182
-
183
- risk_emoji = {"Low": "🟢", "Medium": "🟠", "High": "🔴"}.get(risk, "⚪")
184
- print(f"\n{risk_emoji} Niveau de risque: {risk}")
185
  else:
186
  print(f"\n📋 Résultat: {result}")
187
 
 
7
  """
8
 
9
  import os
10
+ import re
11
  import sys
12
 
13
  try:
 
166
  print("📊 RÉSULTAT DE LA PRÉDICTION (HF)")
167
  print("═" * 60)
168
 
169
+ # Le résultat est du Markdown - on extrait les valeurs clés
170
+ if isinstance(result, str):
171
+ # Extraire les probabilités du Markdown
172
+ prob_depart = re.search(r"Probabilité de départ[^:]*:\s*([\d.]+)%", result)
173
+ prob_maintien = re.search(r"Probabilité de maintien[^:]*:\s*([\d.]+)%", result)
174
+ confiance = re.search(r"Confiance[^:]*:\s*([\d.]+)%", result)
175
+
176
+ # Détecter le risque
177
+ if "RISQUE ÉLEVÉ" in result:
178
+ print("\n🔴 RISQUE ÉLEVÉ DE DÉPART")
179
+ elif "RISQUE MOYEN" in result:
180
+ print("\n🟠 RISQUE MOYEN DE DÉPART")
181
+ else:
182
+ print("\n🟢 RISQUE FAIBLE DE DÉPART")
183
+
184
+ # Afficher les probabilités
185
+ if prob_maintien:
186
+ print(f"\n📈 Probabilité de rester: {prob_maintien.group(1)}%")
187
+ if prob_depart:
188
+ print(f"📉 Probabilité de partir: {prob_depart.group(1)}%")
189
+ if confiance:
190
+ print(f"🎯 Confiance du modèle: {confiance.group(1)}%")
191
+
192
+ # Afficher la prédiction
193
+ if "Départ probable" in result:
194
  print("\n🚨 PRÉDICTION: VA PARTIR")
195
  else:
196
  print("\n✅ PRÉDICTION: VA RESTER")
 
 
 
 
 
 
197
  else:
198
  print(f"\n📋 Résultat: {result}")
199