File size: 1,405 Bytes
0ad7b2b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()