KLEB38 commited on
Commit
63ee8d8
·
verified ·
1 Parent(s): 582c259

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. api/main.py +18 -3
api/main.py CHANGED
@@ -13,7 +13,7 @@ import time
13
  from contextlib import asynccontextmanager
14
  from pathlib import Path
15
 
16
- from fastapi import FastAPI, HTTPException, Request, status
17
  from fastapi.responses import JSONResponse
18
 
19
  from api import db, settings
@@ -144,7 +144,11 @@ async def model_info(request: Request) -> ModelInfoResponse:
144
 
145
 
146
  @app.post("/predict", response_model=PredictionResponse, tags=["scoring"])
147
- async def predict(payload: PredictionRequest, request: Request) -> PredictionResponse:
 
 
 
 
148
  raw_inputs = payload.model_dump()
149
  sk_id = raw_inputs.pop("SK_ID_CURR")
150
 
@@ -208,7 +212,7 @@ async def predict(payload: PredictionRequest, request: Request) -> PredictionRes
208
  plumbing_ms = max(0, latency_ms - feature_assembly_ms - inference_ms)
209
  else:
210
  plumbing_ms = None
211
- log_prediction(
212
  sk_id_curr=sk_id,
213
  client_known=client_known,
214
  raw_input=raw_inputs,
@@ -225,3 +229,14 @@ async def predict(payload: PredictionRequest, request: Request) -> PredictionRes
225
  status_code=status_code,
226
  error_message=error_message,
227
  )
 
 
 
 
 
 
 
 
 
 
 
 
13
  from contextlib import asynccontextmanager
14
  from pathlib import Path
15
 
16
+ from fastapi import BackgroundTasks, FastAPI, HTTPException, Request, status
17
  from fastapi.responses import JSONResponse
18
 
19
  from api import db, settings
 
144
 
145
 
146
  @app.post("/predict", response_model=PredictionResponse, tags=["scoring"])
147
+ async def predict(
148
+ payload: PredictionRequest,
149
+ request: Request,
150
+ background_tasks: BackgroundTasks,
151
+ ) -> PredictionResponse:
152
  raw_inputs = payload.model_dump()
153
  sk_id = raw_inputs.pop("SK_ID_CURR")
154
 
 
212
  plumbing_ms = max(0, latency_ms - feature_assembly_ms - inference_ms)
213
  else:
214
  plumbing_ms = None
215
+ log_kwargs: dict[str, object | None] = dict(
216
  sk_id_curr=sk_id,
217
  client_known=client_known,
218
  raw_input=raw_inputs,
 
229
  status_code=status_code,
230
  error_message=error_message,
231
  )
232
+ # Success: defer the Supabase round-trip until AFTER the response
233
+ # is sent, so the client never waits on ~350 ms of DB log overhead.
234
+ # Failure: log synchronously — BackgroundTasks scheduled on a route
235
+ # are attached to its Response, and the exception-handler chain
236
+ # builds its own Response, silently dropping pending tasks. Losing
237
+ # observability on errors would be a worse trade-off than the
238
+ # one-shot latency hit paid by failing requests.
239
+ if status_code < 400:
240
+ background_tasks.add_task(log_prediction, **log_kwargs)
241
+ else:
242
+ log_prediction(**log_kwargs)