File size: 3,886 Bytes
9331de5
 
 
f63ae60
 
 
 
 
 
9331de5
 
 
 
 
 
f63ae60
9331de5
f63ae60
 
9331de5
 
2bf86cd
9331de5
f63ae60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9331de5
 
 
 
f63ae60
 
 
 
 
 
 
9331de5
 
 
 
 
 
 
 
 
 
f63ae60
 
 
 
db4f6ec
 
 
 
 
 
 
f63ae60
db4f6ec
 
 
f63ae60
 
 
 
 
 
 
 
 
c1e60e8
f63ae60
c1e60e8
f63ae60
c1e60e8
9331de5
 
 
 
 
 
 
 
 
 
 
f63ae60
 
 
 
 
9331de5
 
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
"""Model loading and prediction wrapper.

Loaded once at API startup (see api.main:lifespan) — never per-request.
Wraps an ONNX Runtime session (converted from the original LightGBM model via
scripts/export_to_onnx.py). Threshold is read from model_info.json with a
fallback to the value in api.settings.

The constructor is duck-typed (predict_fn callable) so tests can inject a
fake without round-tripping through a real .onnx file.
"""

from __future__ import annotations

import json
from pathlib import Path
from typing import Callable

import numpy as np
import onnxruntime as ort
import pandas as pd

from api.schemas import Decision

# Takes a (n, n_features) float32 array and returns a (n, 2) probability matrix
# in column order [prob_class_0, prob_class_1].
PredictFn = Callable[[np.ndarray], np.ndarray]


def resolve_threshold_and_version(
    model_info_path: Path, default_threshold: float
) -> tuple[float, str]:
    """Parse threshold + version from model_info.json.

    Extracted so test fixtures can reuse the exact same parsing rules as the
    production loader, instead of re-implementing the dict navigation.
    """
    info = json.loads(model_info_path.read_text())
    threshold = float(
        info.get("metrics", {}).get("best_threshold_mean", default_threshold)
    )
    version = str(info.get("version", "unknown"))
    return threshold, version


class CreditScoringPredictor:
    """Singleton-style wrapper. Build once via load(), reuse for every request."""

    def __init__(
        self,
        predict_fn: PredictFn,
        threshold: float,
        model_version: str,
    ) -> None:
        self._predict_fn = predict_fn
        self._threshold = threshold
        self._model_version = model_version

    @classmethod
    def load(
        cls,
        model_path: Path,
        model_info_path: Path,
        default_threshold: float,
    ) -> "CreditScoringPredictor":
        threshold, version = resolve_threshold_and_version(
            model_info_path, default_threshold
        )

        # Single-threaded: this endpoint serves one request × one row at a time.
        # The default thread pool (intra_op = num_cpus) buys nothing on 1-row
        # inference (already ~30 µs single-threaded) and contends with pandas
        # during the feature-assembly step on small shared VMs like HF Spaces.
        sess_options = ort.SessionOptions()
        sess_options.intra_op_num_threads = 1
        sess_options.inter_op_num_threads = 1
        session = ort.InferenceSession(
            str(model_path),
            sess_options=sess_options,
            providers=["CPUExecutionProvider"],
        )
        input_name = session.get_inputs()[0].name
        # The LightGBM→ONNX graph emits two outputs: labels (idx 0) and the
        # (n, 2) probability matrix (idx 1) — we want the latter.
        proba_output_name = session.get_outputs()[1].name

        def predict_fn(arr: np.ndarray) -> np.ndarray:
            return session.run([proba_output_name], {input_name: arr})[0]

        return cls(
            predict_fn=predict_fn,
            threshold=threshold,
            model_version=version,
        )

    @property
    def threshold(self) -> float:
        return self._threshold

    @property
    def model_version(self) -> str:
        return self._model_version

    def predict(self, features: pd.DataFrame) -> tuple[float, Decision]:
        """Return (probability_of_default, decision)."""
        # ONNX Runtime requires float32 contiguous arrays. The column order is
        # already aligned upstream by InferenceArtefacts.feature_names via
        # reindex(columns=...) in assemble().
        arr = features.to_numpy(dtype=np.float32)
        proba = float(self._predict_fn(arr)[0, 1])
        decision: Decision = "REFUSED" if proba >= self._threshold else "GRANTED"
        return proba, decision