rehan953 commited on
Commit
631e34a
·
verified ·
1 Parent(s): 033be3c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -25
app.py CHANGED
@@ -1166,24 +1166,8 @@ def _patch_ocr_with_textlayer(page_md: str, pdf_path: str, page_num: int) -> str
1166
  return page_md
1167
 
1168
  try:
1169
- # Extract raw page text once — reused by Fix 4D for JB summary sections
1170
- try:
1171
- import pymupdf as _fitz_patch
1172
- _doc_patch = _fitz_patch.open(pdf_path)
1173
- _raw_page_text = _doc_patch[page_num].get_text()
1174
- _doc_patch.close()
1175
- except Exception:
1176
- _raw_page_text = ""
1177
-
1178
  tl_rows = _extract_textlayer_rows(pdf_path, page_num)
1179
  if not tl_rows:
1180
- # Even if no transaction rows, still run Fix 4D for JB summary sections
1181
- needs_checks = "checks" not in page_md.lower() or "<th>Number</th>" not in page_md
1182
- needs_dab = "daily account balance" not in page_md.lower()
1183
- if _raw_page_text and (needs_checks or needs_dab):
1184
- jb_extra = _extract_jb_summary_sections(_raw_page_text, needs_checks, needs_dab)
1185
- if jb_extra:
1186
- return page_md.rstrip() + "\n\n" + jb_extra
1187
  return page_md # scanned PDF or non-transaction page — safe no-op
1188
 
1189
  def _norm(s):
@@ -1391,15 +1375,6 @@ def _patch_ocr_with_textlayer(page_md: str, pdf_path: str, page_num: int) -> str
1391
  log.info("page %d: Case C — new table (%d rows) from text layer",
1392
  page_num, len(_cnew_rows))
1393
 
1394
- # Fix 4D: append Johnson Bank Checks + Daily Account Balance
1395
- # if missing from OCR but present in PDF text layer.
1396
- needs_checks = "checks" not in result.lower() or "<th>Number</th>" not in result
1397
- needs_dab = "daily account balance" not in result.lower()
1398
- if _raw_page_text and (needs_checks or needs_dab):
1399
- jb_extra = _extract_jb_summary_sections(_raw_page_text, needs_checks, needs_dab)
1400
- if jb_extra:
1401
- result = result.rstrip() + "\n\n" + jb_extra
1402
-
1403
  return result
1404
 
1405
  except Exception as e:
@@ -2386,6 +2361,24 @@ def run_ocr(uploaded_file):
2386
  # Safe no-op for scanned PDFs (no text layer) and non-transaction pages.
2387
  if is_pdf:
2388
  stabilized = _patch_ocr_with_textlayer(stabilized, path, page_num)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2389
  parts.append(stabilized)
2390
 
2391
  # Footer extraction: PDF text layer -> band words -> OCR crop
 
1166
  return page_md
1167
 
1168
  try:
 
 
 
 
 
 
 
 
 
1169
  tl_rows = _extract_textlayer_rows(pdf_path, page_num)
1170
  if not tl_rows:
 
 
 
 
 
 
 
1171
  return page_md # scanned PDF or non-transaction page — safe no-op
1172
 
1173
  def _norm(s):
 
1375
  log.info("page %d: Case C — new table (%d rows) from text layer",
1376
  page_num, len(_cnew_rows))
1377
 
 
 
 
 
 
 
 
 
 
1378
  return result
1379
 
1380
  except Exception as e:
 
2361
  # Safe no-op for scanned PDFs (no text layer) and non-transaction pages.
2362
  if is_pdf:
2363
  stabilized = _patch_ocr_with_textlayer(stabilized, path, page_num)
2364
+
2365
+ # Fix 4D: append Johnson Bank Checks + Daily Account Balance
2366
+ # Run OUTSIDE _patch_ocr_with_textlayer so exceptions there don't block it.
2367
+ if is_pdf:
2368
+ try:
2369
+ needs_checks = "checks" not in stabilized.lower() or "<th>Number</th>" not in stabilized
2370
+ needs_dab = "daily account balance" not in stabilized.lower()
2371
+ if needs_checks or needs_dab:
2372
+ import pymupdf as _fitz4d
2373
+ _doc4d = _fitz4d.open(path)
2374
+ _txt4d = _doc4d[page_num].get_text()
2375
+ _doc4d.close()
2376
+ jb_extra = _extract_jb_summary_sections(_txt4d, needs_checks, needs_dab)
2377
+ if jb_extra:
2378
+ stabilized = stabilized.rstrip() + "\n\n" + jb_extra
2379
+ except Exception:
2380
+ pass # safe no-op — never break other PDFs
2381
+
2382
  parts.append(stabilized)
2383
 
2384
  # Footer extraction: PDF text layer -> band words -> OCR crop