oc_p5-dev / exemples /demo_unitaire.py
ASI-Engineer's picture
Upload folder using huggingface_hub
e6211d5 verified
#!/usr/bin/env python3
"""
๐Ÿ”ฎ Prรฉdiction UNITAIRE - API locale (Gradio)
Usage: python demo_unitaire.py
Prรฉrequis: Lancer l'API locale avec `python app.py`
"""
import re
import sys
try:
from gradio_client import Client
except ImportError:
print("โŒ gradio_client non installรฉ. Installez-le avec:")
print(" pip install gradio_client")
sys.exit(1)
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# CONFIGURATION
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
API_URL = "http://127.0.0.1:7860"
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# OPTIONS (menus numรฉrotรฉs)
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
DOMAINES = {
1: "Infra & Cloud",
2: "Transformation Digitale",
3: "Marketing",
4: "Entrepreunariat",
5: "Ressources Humaines",
6: "Autre",
}
FREQUENCES = {1: "Aucun", 2: "Occasionnel", 3: "Frequent"}
STATUTS = {1: "Cรฉlibataire", 2: "Mariรฉ(e)", 3: "Divorcรฉ(e)"}
DEPARTEMENTS = {1: "Commercial", 2: "Consulting", 3: "Ressources Humaines"}
POSTES = {
1: "Cadre Commercial",
2: "Assistant de Direction",
3: "Consultant",
4: "Tech Lead",
5: "Manager",
6: "Senior Manager",
7: "Reprรฉsentant Commercial",
8: "Directeur Technique",
9: "Ressources Humaines",
}
print("โ•”โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•—")
print("โ•‘ ๐Ÿ”ฎ PRร‰DICTION UNITAIRE - API Locale โ•‘")
print("โ•šโ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•")
print(f"\n๐ŸŒ API: {API_URL}\n")
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# COLLECTE DES DONNร‰ES
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
print("โ•" * 60)
print("๐Ÿ“‹ DONNร‰ES SONDAGE")
print("โ•" * 60)
nombre_participation_pee = int(input("Participations PEE [0-3]: "))
nb_formations_suivies = int(input("Formations suivies [0-6]: "))
distance_domicile_travail = int(input("Distance domicile-travail km [1-30]: "))
niveau_education = int(
input("Niveau รฉducation [1=Bac, 2=Bac+2, 3=Licence, 4=Master, 5=Doctorat]: ")
)
print(f"\nDomaine d'รฉtude: {DOMAINES}")
domaine_choix = int(input("Choix [1-6]: "))
domaine_etude = DOMAINES.get(domaine_choix, "Autre")
ayant_enfants_choix = int(input("A des enfants? [0=Non, 1=Oui]: "))
ayant_enfants = "Y" if ayant_enfants_choix == 1 else "N"
print(f"\nFrรฉquence dรฉplacement: {FREQUENCES}")
freq_choix = int(input("Choix [1-3]: "))
frequence_deplacement = FREQUENCES.get(freq_choix, "Aucun")
annees_depuis_promo = int(input("Annรฉes depuis derniรจre promotion [0-15]: "))
annees_sous_responsable = int(input("Annรฉes sous responsable actuel [0-17]: "))
print("\n" + "โ•" * 60)
print("๐Ÿ“Š DONNร‰ES ร‰VALUATION")
print("โ•" * 60)
satisfaction_environnement = int(input("Satisfaction environnement [1-4]: "))
note_eval_precedente = int(input("Note รฉvaluation prรฉcรฉdente [1-4]: "))
niveau_hierarchique = int(input("Niveau hiรฉrarchique [1-5]: "))
satisfaction_travail = int(input("Satisfaction nature travail [1-4]: "))
satisfaction_equipe = int(input("Satisfaction รฉquipe [1-4]: "))
satisfaction_equilibre = int(input("Satisfaction รฉquilibre pro/perso [1-4]: "))
note_eval_actuelle = int(input("Note รฉvaluation actuelle [3-4]: "))
heures_sup_choix = int(input("Heures supplรฉmentaires? [0=Non, 1=Oui]: "))
heure_supplementaires = "Oui" if heures_sup_choix == 1 else "Non"
augmentation_salaire = float(input("Augmentation salaire prรฉcรฉdente % [0-25]: "))
print("\n" + "โ•" * 60)
print("๐Ÿ’ผ DONNร‰ES RH (SIRH)")
print("โ•" * 60)
age = int(input("ร‚ge [18-60]: "))
genre_choix = int(input("Genre [1=Homme, 2=Femme]: "))
genre = "M" if genre_choix == 1 else "F"
revenu_mensuel = float(input("Revenu mensuel โ‚ฌ [1000-20000]: "))
print(f"\nStatut marital: {STATUTS}")
statut_choix = int(input("Choix [1-3]: "))
statut_marital = STATUTS.get(statut_choix, "Cรฉlibataire")
print(f"\nDรฉpartement: {DEPARTEMENTS}")
dept_choix = int(input("Choix [1-3]: "))
departement = DEPARTEMENTS.get(dept_choix, "Commercial")
print(f"\nPoste: {POSTES}")
poste_choix = int(input("Choix [1-9]: "))
poste = POSTES.get(poste_choix, "Consultant")
nombre_exp_precedentes = int(input("Expรฉriences prรฉcรฉdentes [0-9]: "))
annees_exp_totale = int(input("Annรฉes expรฉrience totale [0-40]: "))
annees_entreprise = int(input("Annรฉes dans l'entreprise [0-40]: "))
annees_poste = int(input("Annรฉes dans le poste actuel [0-18]: "))
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# PRร‰DICTION
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
print("\nโณ Connexion ร  l'API...")
try:
client = Client(API_URL)
print("โœ… Connectรฉ")
print("โณ Envoi de la prรฉdiction...")
result = client.predict(
nombre_participation_pee=nombre_participation_pee,
nb_formations_suivies=nb_formations_suivies,
nombre_employee_sous_responsabilite=1,
distance_domicile_travail=distance_domicile_travail,
niveau_education=niveau_education,
domaine_etude=domaine_etude,
ayant_enfants=ayant_enfants,
frequence_deplacement=frequence_deplacement,
annees_depuis_la_derniere_promotion=annees_depuis_promo,
annes_sous_responsable_actuel=annees_sous_responsable,
satisfaction_employee_environnement=satisfaction_environnement,
note_evaluation_precedente=note_eval_precedente,
niveau_hierarchique_poste=niveau_hierarchique,
satisfaction_employee_nature_travail=satisfaction_travail,
satisfaction_employee_equipe=satisfaction_equipe,
satisfaction_employee_equilibre_pro_perso=satisfaction_equilibre,
note_evaluation_actuelle=note_eval_actuelle,
heure_supplementaires=heure_supplementaires,
augementation_salaire_precedente=augmentation_salaire,
age=age,
genre=genre,
revenu_mensuel=revenu_mensuel,
statut_marital=statut_marital,
departement=departement,
poste=poste,
nombre_experiences_precedentes=nombre_exp_precedentes,
nombre_heures_travailless=80,
annee_experience_totale=annees_exp_totale,
annees_dans_l_entreprise=annees_entreprise,
annees_dans_le_poste_actuel=annees_poste,
api_name="/predict",
)
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
# AFFICHAGE DU Rร‰SULTAT
# โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
print("\n" + "โ•" * 60)
print("๐Ÿ“Š Rร‰SULTAT DE LA PRร‰DICTION")
print("โ•" * 60)
if isinstance(result, str):
# Extraire les probabilitรฉs du Markdown
prob_depart = re.search(r"Probabilitรฉ de dรฉpart[^:]*:\s*([\d.]+)%", result)
prob_maintien = re.search(r"Probabilitรฉ de maintien[^:]*:\s*([\d.]+)%", result)
confiance = re.search(r"Confiance[^:]*:\s*([\d.]+)%", result)
# Niveau de risque
if "RISQUE ร‰LEVร‰" in result:
print("\n๐Ÿ”ด Niveau de risque: ร‰LEVร‰")
elif "RISQUE MOYEN" in result:
print("\n๐ŸŸ  Niveau de risque: MOYEN")
else:
print("\n๐ŸŸข Niveau de risque: FAIBLE")
# Probabilitรฉs
if prob_maintien:
print(f"\n๐Ÿ“ˆ Probabilitรฉ de rester: {prob_maintien.group(1)}%")
if prob_depart:
print(f"๐Ÿ“‰ Probabilitรฉ de partir: {prob_depart.group(1)}%")
if confiance:
print(f"๐ŸŽฏ Confiance du modรจle: {confiance.group(1)}%")
# Prรฉdiction finale
print("\n" + "โ”€" * 60)
if "Dรฉpart probable" in result:
print("๐Ÿšจ PRร‰DICTION FINALE: VA PARTIR")
else:
print("โœ… PRร‰DICTION FINALE: VA RESTER")
print("โ”€" * 60)
else:
print(f"\n๐Ÿ“‹ Rรฉsultat: {result}")
print("\nโœ… Prรฉdiction unitaire terminรฉe avec succรจs!")
except ConnectionError:
print("\nโŒ Impossible de se connecter ร  l'API locale.")
print(" Lancez d'abord: python app.py")
sys.exit(1)
except Exception as e:
print(f"\nโŒ Erreur: {e}")
sys.exit(1)