Spaces:
Running
Running
| #!/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) | |