Sdm0 commited on
Commit
2d0f053
·
verified ·
1 Parent(s): f60c9c4

fix: persist terminal ragas status

Browse files
Files changed (1) hide show
  1. api.py +32 -1
api.py CHANGED
@@ -144,6 +144,13 @@ def _with_ragas(job: dict[str, Any]) -> dict[str, Any]:
144
  "reason": "No retrieval context was available for RAGAS evaluation",
145
  }
146
  else:
 
 
 
 
 
 
 
147
  with _ragas_lock:
148
  task = _ragas_tasks.get(task_id)
149
  if not task:
@@ -153,15 +160,33 @@ def _with_ragas(job: dict[str, Any]) -> dict[str, Any]:
153
  result["ragas"] = {"status": "completed", "scores": result["ragas_scores"]}
154
  else:
155
  result["ragas"] = {"status": "unavailable", "error": "RAGAS task was lost during process restart"}
 
 
 
 
 
 
156
  elif not task.get("done"):
157
  timeout = float(os.getenv("OCR_RAGAS_TIMEOUT_SECONDS", "90"))
158
  started = float(task.get("started", 0) or 0)
159
  if started and datetime.now(timezone.utc).timestamp() - started > timeout:
160
  result["ragas"] = {"status": "failed", "error": f"RAGAS exceeded {int(timeout)} second timeout"}
 
 
 
 
 
 
161
  else:
162
  result["ragas"] = {"status": "pending"}
163
  elif task.get("error"):
164
  result["ragas"] = {"status": "failed", "error": task.get("error")}
 
 
 
 
 
 
165
  else:
166
  scores = task.get("scores") or {}
167
  if scores:
@@ -175,6 +200,12 @@ def _with_ragas(job: dict[str, Any]) -> dict[str, Any]:
175
  _save_job(live)
176
  else:
177
  result["ragas"] = {"status": "completed_no_scores", "scores": {}, "error": "RAGAS completed without metric values"}
 
 
 
 
 
 
178
  return result
179
 
180
 
@@ -204,7 +235,7 @@ def _hydrate_jobs() -> None:
204
  continue
205
  status = persisted.get("status")
206
  _jobs[job_id] = persisted
207
- if persisted.get("ragas_inputs") and persisted.get("ragas_task_id") and not persisted.get("ragas_scores"):
208
  new_task = _spawn_ragas_background(
209
  persisted["ragas_inputs"],
210
  persisted.get("repo", ""),
 
144
  "reason": "No retrieval context was available for RAGAS evaluation",
145
  }
146
  else:
147
+ if job.get("ragas_status") in {"failed", "unavailable", "completed_no_scores"}:
148
+ result["ragas"] = {
149
+ "status": job["ragas_status"],
150
+ "scores": job.get("ragas_scores") or {},
151
+ "error": job.get("ragas_error"),
152
+ }
153
+ return result
154
  with _ragas_lock:
155
  task = _ragas_tasks.get(task_id)
156
  if not task:
 
160
  result["ragas"] = {"status": "completed", "scores": result["ragas_scores"]}
161
  else:
162
  result["ragas"] = {"status": "unavailable", "error": "RAGAS task was lost during process restart"}
163
+ with _lock:
164
+ live = _jobs.get(job.get("id"))
165
+ if live is not None:
166
+ live["ragas_status"] = "unavailable"
167
+ live["ragas_error"] = result["ragas"]["error"]
168
+ _save_job(live)
169
  elif not task.get("done"):
170
  timeout = float(os.getenv("OCR_RAGAS_TIMEOUT_SECONDS", "90"))
171
  started = float(task.get("started", 0) or 0)
172
  if started and datetime.now(timezone.utc).timestamp() - started > timeout:
173
  result["ragas"] = {"status": "failed", "error": f"RAGAS exceeded {int(timeout)} second timeout"}
174
+ with _lock:
175
+ live = _jobs.get(job.get("id"))
176
+ if live is not None:
177
+ live["ragas_status"] = "failed"
178
+ live["ragas_error"] = result["ragas"]["error"]
179
+ _save_job(live)
180
  else:
181
  result["ragas"] = {"status": "pending"}
182
  elif task.get("error"):
183
  result["ragas"] = {"status": "failed", "error": task.get("error")}
184
+ with _lock:
185
+ live = _jobs.get(job.get("id"))
186
+ if live is not None:
187
+ live["ragas_status"] = "failed"
188
+ live["ragas_error"] = task.get("error")
189
+ _save_job(live)
190
  else:
191
  scores = task.get("scores") or {}
192
  if scores:
 
200
  _save_job(live)
201
  else:
202
  result["ragas"] = {"status": "completed_no_scores", "scores": {}, "error": "RAGAS completed without metric values"}
203
+ with _lock:
204
+ live = _jobs.get(job.get("id"))
205
+ if live is not None:
206
+ live["ragas_status"] = "completed_no_scores"
207
+ live["ragas_error"] = result["ragas"]["error"]
208
+ _save_job(live)
209
  return result
210
 
211
 
 
235
  continue
236
  status = persisted.get("status")
237
  _jobs[job_id] = persisted
238
+ if persisted.get("ragas_inputs") and persisted.get("ragas_task_id") and not persisted.get("ragas_scores") and not persisted.get("ragas_status"):
239
  new_task = _spawn_ragas_background(
240
  persisted["ragas_inputs"],
241
  persisted.get("repo", ""),