Spaces:
Running
Running
File size: 6,786 Bytes
25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 25f8116 e6211d5 |
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 |
#!/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)
|