Dipti Dhawade commited on
Commit
195674d
·
1 Parent(s): c993217

Retry Sarvam uploads

Browse files
Files changed (1) hide show
  1. backend/library.py +13 -1
backend/library.py CHANGED
@@ -52,6 +52,8 @@ MAX_FILE_BYTES = 200 * 1024 * 1024
52
  TILE_RATIO = 1.35 # height / width of one piece sent to OCR: about a portrait page
53
  OCR_WIDTH_PX = 1600 # render width for OCR: handwriting stays legible, images stay small
54
  STATUS_CACHE_SECONDS = 60
 
 
55
  NOT_READY = "These course notes aren't ready for AI yet. Please try again later."
56
 
57
  FOLDER_NAMES = {
@@ -352,8 +354,13 @@ def _ocr(tiles: list[Tile], mode: str, result: PdfText) -> list[str]:
352
  batch = tiles[i : i + OCR_BATCH]
353
  _check_budget()
354
  got = _sarvam_batch(batch)
 
 
 
 
 
355
  if got is None:
356
- result.notes.append("sarvam_failed_used_tesseract")
357
  got = [_tesseract(t) for t in batch]
358
  else:
359
  result.sarvam_pieces += len(batch)
@@ -653,6 +660,11 @@ def sync(
653
  _sync_lock.release()
654
 
655
 
 
 
 
 
 
656
  def _sync_quietly(**kwargs) -> None:
657
  try:
658
  summary = sync(**kwargs)
 
52
  TILE_RATIO = 1.35 # height / width of one piece sent to OCR: about a portrait page
53
  OCR_WIDTH_PX = 1600 # render width for OCR: handwriting stays legible, images stay small
54
  STATUS_CACHE_SECONDS = 60
55
+ SARVAM_RETRY_DELAYS = (5, 15) # seconds before retrying a failed Sarvam job (e.g. an upload timeout)
56
+ TESSERACT_FALLBACK = "sarvam_failed_used_tesseract"
57
  NOT_READY = "These course notes aren't ready for AI yet. Please try again later."
58
 
59
  FOLDER_NAMES = {
 
354
  batch = tiles[i : i + OCR_BATCH]
355
  _check_budget()
356
  got = _sarvam_batch(batch)
357
+ for delay in SARVAM_RETRY_DELAYS: # failures are mostly network hiccups: try again first
358
+ if got is not None:
359
+ break
360
+ time.sleep(delay)
361
+ got = _sarvam_batch(batch)
362
  if got is None:
363
+ result.notes.append(TESSERACT_FALLBACK)
364
  got = [_tesseract(t) for t in batch]
365
  else:
366
  result.sarvam_pieces += len(batch)
 
660
  _sync_lock.release()
661
 
662
 
663
+ def tesseract_fallbacks() -> list[str]:
664
+ """PDFs where Sarvam failed and Tesseract was used instead (worth reading again)."""
665
+ return sorted(p for p, m in indexed_files().items() if TESSERACT_FALLBACK in (m.get("note") or ""))
666
+
667
+
668
  def _sync_quietly(**kwargs) -> None:
669
  try:
670
  summary = sync(**kwargs)