credit-scoring-api-v2 / monitoring /generate_ref_data.py
perachon's picture
fix & améliorations
0ad7b2b
import json
from pathlib import Path
import pandas as pd
LOG_FILE = Path("logs/api_requests.jsonl")
OUT_PATH = Path("monitoring/ref_data.csv")
def main() -> None:
if not LOG_FILE.exists():
raise FileNotFoundError(
f"Fichier introuvable: {LOG_FILE}. "
"Impossible de générer une référence à partir des logs."
)
values: list[float] = []
with open(LOG_FILE, "r", encoding="utf-8") as f:
for line in f:
try:
record = json.loads(line)
except json.JSONDecodeError:
continue
if record.get("event") == "prediction":
out = record.get("output") or {}
if "probability_default" in out:
values.append(float(out["probability_default"]))
if len(values) < 2:
raise ValueError(
"Pas assez de prédictions dans logs/api_requests.jsonl pour construire ref_data.csv."
)
# Par convention: la référence est le début de l'historique (période stable)
split_index = max(1, len(values) // 2)
ref_values = values[:split_index]
df = pd.DataFrame({"probability_default": ref_values})
OUT_PATH.parent.mkdir(parents=True, exist_ok=True)
df.to_csv(OUT_PATH, index=False)
print(f"✅ Référence exportée: {OUT_PATH} ({len(df)} lignes)")
if __name__ == "__main__":
main()