Jeevant10 commited on
Commit
fdd0a8f
·
1 Parent(s): dff5943
Files changed (1) hide show
  1. app.py +26 -16
app.py CHANGED
@@ -54,21 +54,25 @@ def _load_model_bg() -> None:
54
  global _pipeline, _model_error
55
  max_attempts = int(os.environ.get("MODEL_LOAD_MAX_ATTEMPTS", "3"))
56
  retry_delay_seconds = int(os.environ.get("MODEL_LOAD_RETRY_DELAY", "8"))
 
57
 
58
  _model_error = None
59
- for attempt in range(1, max_attempts + 1):
60
- try:
61
- _pipeline = PredictionPipeline()
62
- _pipeline.load_model()
63
- _model_error = None
64
- _model_ready.set()
65
- return
66
- except Exception as exc:
67
- _model_error = str(exc)
68
- if attempt < max_attempts:
69
- time.sleep(retry_delay_seconds)
70
-
71
- _model_ready.set() # unblock probes after final failure
 
 
 
72
 
73
 
74
  @asynccontextmanager
@@ -133,8 +137,11 @@ async def health_check():
133
  """
134
  if _model_error:
135
  return JSONResponse(
136
- status_code=500,
137
- content={"status": "error", "detail": _model_error},
 
 
 
138
  )
139
  if not _model_ready.is_set():
140
  return JSONResponse(
@@ -168,7 +175,10 @@ async def predict_route(request: SummarizeRequest):
168
  Returns HTTP 503 while the model is still loading on first startup.
169
  """
170
  if _model_error:
171
- raise HTTPException(status_code=500, detail=f"Model failed to load: {_model_error}")
 
 
 
172
  if not _model_ready.is_set():
173
  raise HTTPException(
174
  status_code=503,
 
54
  global _pipeline, _model_error
55
  max_attempts = int(os.environ.get("MODEL_LOAD_MAX_ATTEMPTS", "3"))
56
  retry_delay_seconds = int(os.environ.get("MODEL_LOAD_RETRY_DELAY", "8"))
57
+ recovery_retry_delay_seconds = int(os.environ.get("MODEL_RECOVERY_RETRY_DELAY", "30"))
58
 
59
  _model_error = None
60
+ while True:
61
+ for attempt in range(1, max_attempts + 1):
62
+ try:
63
+ _pipeline = PredictionPipeline()
64
+ _pipeline.load_model()
65
+ _model_error = None
66
+ _model_ready.set()
67
+ return
68
+ except Exception as exc:
69
+ _model_error = str(exc)
70
+ if attempt < max_attempts:
71
+ time.sleep(retry_delay_seconds)
72
+
73
+ # Keep retrying in the background instead of getting stuck forever.
74
+ _model_ready.clear()
75
+ time.sleep(recovery_retry_delay_seconds)
76
 
77
 
78
  @asynccontextmanager
 
137
  """
138
  if _model_error:
139
  return JSONResponse(
140
+ status_code=503,
141
+ content={
142
+ "status": "loading",
143
+ "detail": f"Model recovery in progress: {_model_error}",
144
+ },
145
  )
146
  if not _model_ready.is_set():
147
  return JSONResponse(
 
175
  Returns HTTP 503 while the model is still loading on first startup.
176
  """
177
  if _model_error:
178
+ raise HTTPException(
179
+ status_code=503,
180
+ detail=f"Model is recovering from a load failure. Please retry shortly. Last error: {_model_error}",
181
+ )
182
  if not _model_ready.is_set():
183
  raise HTTPException(
184
  status_code=503,