#!/usr/bin/env python3 """ πŸ“¦ PrΓ©diction BATCH - API Locale Usage: python demo_batch.py PrΓ©requis: API locale dΓ©marrΓ©e sur http://127.0.0.1:7860 """ import os import sys from datetime import datetime try: import pandas as pd from gradio_client import Client, handle_file except ImportError: print("❌ DΓ©pendances manquantes. Installez avec:") print(" pip install gradio_client pandas") sys.exit(1) # ═══════════════════════════════════════════════════════════════ # CONFIGURATION # ═══════════════════════════════════════════════════════════════ API_URL = os.getenv("LOCAL_API_URL", "http://127.0.0.1:7860") SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) # Fichiers par dΓ©faut DEFAULT_FILES = { "eval": os.path.join(SCRIPT_DIR, "02_predict_batch_eval.csv"), "sirh": os.path.join(SCRIPT_DIR, "02_predict_batch_sirh.csv"), "sondage": os.path.join(SCRIPT_DIR, "02_predict_batch_sondage.csv"), } print("╔══════════════════════════════════════════════════════════╗") print("β•‘ πŸ“¦ PRΓ‰DICTION BATCH - API Locale β•‘") print("β•šβ•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•β•") print(f"\n🌐 API: {API_URL}\n") # ═══════════════════════════════════════════════════════════════ # SΓ‰LECTION DES FICHIERS # ═══════════════════════════════════════════════════════════════ print("═" * 60) print("πŸ“ SΓ‰LECTION DES FICHIERS CSV") print("═" * 60) use_default = ( input("\nUtiliser les fichiers exemples par dΓ©faut? [O/n]: ").strip().lower() ) if use_default in ("", "o", "oui", "y", "yes"): fichier_eval = DEFAULT_FILES["eval"] fichier_sirh = DEFAULT_FILES["sirh"] fichier_sondage = DEFAULT_FILES["sondage"] print(f"\nπŸ“„ Γ‰valuation: {os.path.basename(fichier_eval)}") print(f"πŸ“„ SIRH: {os.path.basename(fichier_sirh)}") print(f"πŸ“„ Sondage: {os.path.basename(fichier_sondage)}") else: print("\nEntrez les chemins des fichiers CSV:") fichier_eval = input("πŸ“„ Fichier Γ©valuation: ").strip() fichier_sirh = input("πŸ“„ Fichier SIRH: ").strip() fichier_sondage = input("πŸ“„ Fichier sondage: ").strip() # VΓ©rification des fichiers for name, path in [ ("Γ‰valuation", fichier_eval), ("SIRH", fichier_sirh), ("Sondage", fichier_sondage), ]: if not os.path.exists(path): print(f"\n❌ Fichier {name} introuvable: {path}") sys.exit(1) # ═══════════════════════════════════════════════════════════════ # PRΓ‰DICTION BATCH # ═══════════════════════════════════════════════════════════════ print("\n" + "═" * 60) print("⏳ TRAITEMENT EN COURS...") print("═" * 60) try: print("\n⏳ Connexion Γ  l'API...") client = Client(API_URL) print("βœ… ConnectΓ©") print("⏳ Envoi des fichiers...") result = client.predict( fichier_eval=handle_file(fichier_eval), fichier_sirh=handle_file(fichier_sirh), fichier_sondage=handle_file(fichier_sondage), api_name="/predict_batch", ) # ═══════════════════════════════════════════════════════════════ # AFFICHAGE DU RΓ‰SULTAT # ═══════════════════════════════════════════════════════════════ print("\n" + "═" * 60) print("πŸ“Š RΓ‰SULTAT DE LA PRΓ‰DICTION BATCH") print("═" * 60) if isinstance(result, dict): # Lecture du fichier rΓ©sultat result_path = result.get("value") or result.get("path") if result_path and os.path.exists(result_path): df = pd.read_csv(result_path) total = len(df) # Statistiques if "prediction" in df.columns: restent = (df["prediction"] == "Reste").sum() partent = (df["prediction"] == "Part").sum() else: restent = partent = 0 if "risk_level" in df.columns: risque_eleve = (df["risk_level"] == "Γ‰levΓ©").sum() risque_moyen = (df["risk_level"] == "Moyen").sum() risque_faible = (df["risk_level"] == "Faible").sum() else: risque_eleve = risque_moyen = risque_faible = 0 # Affichage des stats print(f"\nπŸ‘₯ Total employΓ©s analysΓ©s: {total}") print(f"\nπŸ“ˆ Vont RESTER: {restent} ({100 * restent / total:.1f}%)") print(f"πŸ“‰ Vont PARTIR: {partent} ({100 * partent / total:.1f}%)") print(f"\n🟒 Risque faible: {risque_faible}") print(f"🟠 Risque moyen: {risque_moyen}") print(f"πŸ”΄ Risque Γ©levΓ©: {risque_eleve}") # Sauvegarde timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") output_file = os.path.join(SCRIPT_DIR, f"predictions_batch_{timestamp}.csv") df.to_csv(output_file, index=False) print("\n" + "─" * 60) print(f"πŸ’Ύ Fichier sauvegardΓ©: {os.path.basename(output_file)}") print("─" * 60) # AperΓ§u print("\nπŸ“‹ AperΓ§u des rΓ©sultats:") cols = ["employee_id", "prediction", "prob_depart", "risk_level"] cols_exist = [c for c in cols if c in df.columns] if cols_exist: print(df[cols_exist].head(10).to_string(index=False)) else: print(f"\n⚠️ Fichier rΓ©sultat non trouvΓ©: {result_path}") else: print(f"\nπŸ“‹ RΓ©sultat: {result}") print("\nβœ… PrΓ©diction batch terminΓ©e avec succΓ¨s!") except Exception as e: print(f"\n❌ Erreur: {e}") sys.exit(1)