Upload folder using huggingface_hub
Browse files- api/logger.py +6 -0
- api/main.py +16 -0
- database/migrations/2026-05-13_add_timing_breakdown.sql +22 -0
- database/models.py +6 -0
- tests/integration/test_predict_logs_to_db.py +10 -1
- tests/unit/test_db_models.py +7 -0
- tests/unit/test_logger.py +11 -0
api/logger.py
CHANGED
|
@@ -74,6 +74,9 @@ def log_prediction(
|
|
| 74 |
threshold: float,
|
| 75 |
model_version: str,
|
| 76 |
latency_ms: int,
|
|
|
|
|
|
|
|
|
|
| 77 |
status_code: int = 200,
|
| 78 |
error_message: str | None = None,
|
| 79 |
top_shap: dict[str, Any] | None = None,
|
|
@@ -87,6 +90,9 @@ def log_prediction(
|
|
| 87 |
"sk_id_curr": sk_id_curr,
|
| 88 |
"client_known": client_known,
|
| 89 |
"latency_ms": latency_ms,
|
|
|
|
|
|
|
|
|
|
| 90 |
"status_code": status_code,
|
| 91 |
"error_message": error_message,
|
| 92 |
"raw_input": _jsonify_raw_input(raw_input),
|
|
|
|
| 74 |
threshold: float,
|
| 75 |
model_version: str,
|
| 76 |
latency_ms: int,
|
| 77 |
+
feature_assembly_ms: float | None = None,
|
| 78 |
+
inference_ms: float | None = None,
|
| 79 |
+
inference_cpu_ms: float | None = None,
|
| 80 |
status_code: int = 200,
|
| 81 |
error_message: str | None = None,
|
| 82 |
top_shap: dict[str, Any] | None = None,
|
|
|
|
| 90 |
"sk_id_curr": sk_id_curr,
|
| 91 |
"client_known": client_known,
|
| 92 |
"latency_ms": latency_ms,
|
| 93 |
+
"feature_assembly_ms": feature_assembly_ms,
|
| 94 |
+
"inference_ms": inference_ms,
|
| 95 |
+
"inference_cpu_ms": inference_cpu_ms,
|
| 96 |
"status_code": status_code,
|
| 97 |
"error_message": error_message,
|
| 98 |
"raw_input": _jsonify_raw_input(raw_input),
|
api/main.py
CHANGED
|
@@ -158,10 +158,23 @@ async def predict(payload: PredictionRequest, request: Request) -> PredictionRes
|
|
| 158 |
decision: str | None = None
|
| 159 |
status_code = status.HTTP_200_OK
|
| 160 |
error_message: str | None = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
|
| 162 |
try:
|
|
|
|
| 163 |
features, client_known = assemble(raw_inputs, sk_id_curr=sk_id, artefacts=artefacts)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 164 |
proba, decision = predictor.predict(features)
|
|
|
|
|
|
|
|
|
|
| 165 |
return PredictionResponse(
|
| 166 |
sk_id_curr=sk_id,
|
| 167 |
probability_default=proba,
|
|
@@ -193,6 +206,9 @@ async def predict(payload: PredictionRequest, request: Request) -> PredictionRes
|
|
| 193 |
threshold=predictor.threshold,
|
| 194 |
model_version=predictor.model_version,
|
| 195 |
latency_ms=int((time.perf_counter() - started) * 1000),
|
|
|
|
|
|
|
|
|
|
| 196 |
status_code=status_code,
|
| 197 |
error_message=error_message,
|
| 198 |
)
|
|
|
|
| 158 |
decision: str | None = None
|
| 159 |
status_code = status.HTTP_200_OK
|
| 160 |
error_message: str | None = None
|
| 161 |
+
# Fine-grained timings (étape 4). Left as None when the corresponding
|
| 162 |
+
# sub-step did not complete (e.g. assembly raised before inference ran).
|
| 163 |
+
feature_assembly_ms: float | None = None
|
| 164 |
+
inference_ms: float | None = None
|
| 165 |
+
inference_cpu_ms: float | None = None
|
| 166 |
|
| 167 |
try:
|
| 168 |
+
t_asm = time.perf_counter()
|
| 169 |
features, client_known = assemble(raw_inputs, sk_id_curr=sk_id, artefacts=artefacts)
|
| 170 |
+
feature_assembly_ms = (time.perf_counter() - t_asm) * 1000.0
|
| 171 |
+
|
| 172 |
+
t_inf_wall = time.perf_counter()
|
| 173 |
+
t_inf_cpu = time.process_time()
|
| 174 |
proba, decision = predictor.predict(features)
|
| 175 |
+
inference_ms = (time.perf_counter() - t_inf_wall) * 1000.0
|
| 176 |
+
inference_cpu_ms = (time.process_time() - t_inf_cpu) * 1000.0
|
| 177 |
+
|
| 178 |
return PredictionResponse(
|
| 179 |
sk_id_curr=sk_id,
|
| 180 |
probability_default=proba,
|
|
|
|
| 206 |
threshold=predictor.threshold,
|
| 207 |
model_version=predictor.model_version,
|
| 208 |
latency_ms=int((time.perf_counter() - started) * 1000),
|
| 209 |
+
feature_assembly_ms=feature_assembly_ms,
|
| 210 |
+
inference_ms=inference_ms,
|
| 211 |
+
inference_cpu_ms=inference_cpu_ms,
|
| 212 |
status_code=status_code,
|
| 213 |
error_message=error_message,
|
| 214 |
)
|
database/migrations/2026-05-13_add_timing_breakdown.sql
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
-- Étape 4 — Fine-grained timing breakdown of /predict.
|
| 2 |
+
--
|
| 3 |
+
-- Adds three nullable columns capturing the wall-clock cost of feature
|
| 4 |
+
-- assembly and model inference, plus the CPU time spent in inference. The
|
| 5 |
+
-- existing `latency_ms` stays end-to-end; the new columns let us isolate the
|
| 6 |
+
-- actual bottleneck before targeted optimization.
|
| 7 |
+
--
|
| 8 |
+
-- Run once via the Supabase SQL editor (or psql against the pooler URL).
|
| 9 |
+
-- All columns are NULLABLE so legacy rows remain valid and error rows
|
| 10 |
+
-- (status_code != 200) can leave them NULL.
|
| 11 |
+
--
|
| 12 |
+
-- Idempotent: re-running is a no-op thanks to IF NOT EXISTS.
|
| 13 |
+
|
| 14 |
+
ALTER TABLE predictions_log
|
| 15 |
+
ADD COLUMN IF NOT EXISTS feature_assembly_ms DOUBLE PRECISION,
|
| 16 |
+
ADD COLUMN IF NOT EXISTS inference_ms DOUBLE PRECISION,
|
| 17 |
+
ADD COLUMN IF NOT EXISTS inference_cpu_ms DOUBLE PRECISION;
|
| 18 |
+
|
| 19 |
+
ALTER TABLE predictions_log_test
|
| 20 |
+
ADD COLUMN IF NOT EXISTS feature_assembly_ms DOUBLE PRECISION,
|
| 21 |
+
ADD COLUMN IF NOT EXISTS inference_ms DOUBLE PRECISION,
|
| 22 |
+
ADD COLUMN IF NOT EXISTS inference_cpu_ms DOUBLE PRECISION;
|
database/models.py
CHANGED
|
@@ -62,6 +62,12 @@ def build_predictions_log_table(name: str, metadata: MetaData) -> Table:
|
|
| 62 |
Column("client_known", Boolean, nullable=False),
|
| 63 |
# Operational metrics
|
| 64 |
Column("latency_ms", Integer, nullable=False),
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
Column("status_code", Integer, nullable=False, server_default="200"),
|
| 66 |
Column("error_message", Text, nullable=True),
|
| 67 |
# Payloads
|
|
|
|
| 62 |
Column("client_known", Boolean, nullable=False),
|
| 63 |
# Operational metrics
|
| 64 |
Column("latency_ms", Integer, nullable=False),
|
| 65 |
+
# Fine-grained timings added in étape 4. Nullable so legacy rows
|
| 66 |
+
# (pre-instrumentation) remain valid. Populated only on the success
|
| 67 |
+
# path — error rows leave them NULL.
|
| 68 |
+
Column("feature_assembly_ms", Float, nullable=True),
|
| 69 |
+
Column("inference_ms", Float, nullable=True),
|
| 70 |
+
Column("inference_cpu_ms", Float, nullable=True),
|
| 71 |
Column("status_code", Integer, nullable=False, server_default="200"),
|
| 72 |
Column("error_message", Text, nullable=True),
|
| 73 |
# Payloads
|
tests/integration/test_predict_logs_to_db.py
CHANGED
|
@@ -77,7 +77,8 @@ def test_predict_inserts_row(client, valid_payload, test_engine):
|
|
| 77 |
with test_engine.connect() as conn:
|
| 78 |
row = conn.execute(
|
| 79 |
text(
|
| 80 |
-
f"SELECT decision, probability_default, status_code, features "
|
|
|
|
| 81 |
f"FROM {TEST_TABLE} WHERE sk_id_curr = :sk_id"
|
| 82 |
),
|
| 83 |
{"sk_id": sk_id},
|
|
@@ -86,6 +87,14 @@ def test_predict_inserts_row(client, valid_payload, test_engine):
|
|
| 86 |
assert row.decision in ("GRANTED", "REFUSED")
|
| 87 |
assert 0.0 <= row.probability_default <= 1.0
|
| 88 |
assert isinstance(row.features, dict) and len(row.features) > 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 89 |
finally:
|
| 90 |
with test_engine.begin() as conn:
|
| 91 |
conn.execute(
|
|
|
|
| 77 |
with test_engine.connect() as conn:
|
| 78 |
row = conn.execute(
|
| 79 |
text(
|
| 80 |
+
f"SELECT decision, probability_default, status_code, features, "
|
| 81 |
+
f"feature_assembly_ms, inference_ms, inference_cpu_ms "
|
| 82 |
f"FROM {TEST_TABLE} WHERE sk_id_curr = :sk_id"
|
| 83 |
),
|
| 84 |
{"sk_id": sk_id},
|
|
|
|
| 87 |
assert row.decision in ("GRANTED", "REFUSED")
|
| 88 |
assert 0.0 <= row.probability_default <= 1.0
|
| 89 |
assert isinstance(row.features, dict) and len(row.features) > 0
|
| 90 |
+
# Fine-grained timings populated on the success path. Loose bounds —
|
| 91 |
+
# we only assert positivity and a sane upper bound (10 s would already
|
| 92 |
+
# be a major regression on a single-row predict).
|
| 93 |
+
assert row.feature_assembly_ms is not None and 0.0 < row.feature_assembly_ms < 10_000.0
|
| 94 |
+
assert row.inference_ms is not None and 0.0 < row.inference_ms < 10_000.0
|
| 95 |
+
# CPU time can read as 0 on very fast paths (process_time resolution
|
| 96 |
+
# is platform-dependent); only assert non-negative.
|
| 97 |
+
assert row.inference_cpu_ms is not None and row.inference_cpu_ms >= 0.0
|
| 98 |
finally:
|
| 99 |
with test_engine.begin() as conn:
|
| 100 |
conn.execute(
|
tests/unit/test_db_models.py
CHANGED
|
@@ -36,6 +36,9 @@ def test_table_contains_expected_columns() -> None:
|
|
| 36 |
"sk_id_curr",
|
| 37 |
"client_known",
|
| 38 |
"latency_ms",
|
|
|
|
|
|
|
|
|
|
| 39 |
"status_code",
|
| 40 |
"error_message",
|
| 41 |
"raw_input",
|
|
@@ -63,3 +66,7 @@ def test_required_columns_are_not_null() -> None:
|
|
| 63 |
# Ground truth and top_shap are post-hoc data.
|
| 64 |
assert cols["ground_truth"].nullable is True
|
| 65 |
assert cols["top_shap"].nullable is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
"sk_id_curr",
|
| 37 |
"client_known",
|
| 38 |
"latency_ms",
|
| 39 |
+
"feature_assembly_ms",
|
| 40 |
+
"inference_ms",
|
| 41 |
+
"inference_cpu_ms",
|
| 42 |
"status_code",
|
| 43 |
"error_message",
|
| 44 |
"raw_input",
|
|
|
|
| 66 |
# Ground truth and top_shap are post-hoc data.
|
| 67 |
assert cols["ground_truth"].nullable is True
|
| 68 |
assert cols["top_shap"].nullable is True
|
| 69 |
+
# Fine-grained timings (étape 4) are nullable — error rows leave them NULL.
|
| 70 |
+
assert cols["feature_assembly_ms"].nullable is True
|
| 71 |
+
assert cols["inference_ms"].nullable is True
|
| 72 |
+
assert cols["inference_cpu_ms"].nullable is True
|
tests/unit/test_logger.py
CHANGED
|
@@ -67,6 +67,9 @@ def test_insert_payload_contains_expected_fields(monkeypatch) -> None:
|
|
| 67 |
|
| 68 |
_call(
|
| 69 |
features=pd.DataFrame([{"EXT_SOURCE_1": 0.42, "FOO": np.nan, "BAR": np.inf}]),
|
|
|
|
|
|
|
|
|
|
| 70 |
)
|
| 71 |
|
| 72 |
values = captured["values"]
|
|
@@ -78,6 +81,10 @@ def test_insert_payload_contains_expected_fields(monkeypatch) -> None:
|
|
| 78 |
# NaN/Inf scrubbed to None for JSONB compatibility
|
| 79 |
assert values["features"]["FOO"] is None
|
| 80 |
assert values["features"]["BAR"] is None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
|
| 83 |
def test_db_failure_is_swallowed(monkeypatch, caplog) -> None:
|
|
@@ -126,3 +133,7 @@ def test_error_path_logs_status_and_message(monkeypatch) -> None:
|
|
| 126 |
assert values["error_message"] == "boom"
|
| 127 |
assert values["decision"] == "ERROR"
|
| 128 |
assert values["features"] == {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
_call(
|
| 69 |
features=pd.DataFrame([{"EXT_SOURCE_1": 0.42, "FOO": np.nan, "BAR": np.inf}]),
|
| 70 |
+
feature_assembly_ms=12.5,
|
| 71 |
+
inference_ms=3.2,
|
| 72 |
+
inference_cpu_ms=2.9,
|
| 73 |
)
|
| 74 |
|
| 75 |
values = captured["values"]
|
|
|
|
| 81 |
# NaN/Inf scrubbed to None for JSONB compatibility
|
| 82 |
assert values["features"]["FOO"] is None
|
| 83 |
assert values["features"]["BAR"] is None
|
| 84 |
+
# Fine-grained timings propagated through to the insert payload.
|
| 85 |
+
assert values["feature_assembly_ms"] == pytest.approx(12.5)
|
| 86 |
+
assert values["inference_ms"] == pytest.approx(3.2)
|
| 87 |
+
assert values["inference_cpu_ms"] == pytest.approx(2.9)
|
| 88 |
|
| 89 |
|
| 90 |
def test_db_failure_is_swallowed(monkeypatch, caplog) -> None:
|
|
|
|
| 133 |
assert values["error_message"] == "boom"
|
| 134 |
assert values["decision"] == "ERROR"
|
| 135 |
assert values["features"] == {}
|
| 136 |
+
# Error rows leave timing breakdown NULL (defaults to None when omitted).
|
| 137 |
+
assert values["feature_assembly_ms"] is None
|
| 138 |
+
assert values["inference_ms"] is None
|
| 139 |
+
assert values["inference_cpu_ms"] is None
|