mohsin-devs commited on
Commit
7c166e1
Β·
1 Parent(s): 1f11d61

Harden scanned PDF OCR fallback

Browse files
Files changed (2) hide show
  1. Dockerfile +2 -0
  2. backend/app/documents/service.py +25 -2
Dockerfile CHANGED
@@ -78,6 +78,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
78
  && curl -fsSL https://nodejs.org/dist/v20.11.0/node-v20.11.0-linux-x64.tar.xz | tar -xJ --strip-components=1 -C /usr/local \
79
  && rm -rf /var/lib/apt/lists/*
80
 
 
 
81
  # ── Python packages ───────────────────────────────────────────────────────────
82
  COPY --from=python-builder /install /usr/local
83
 
 
78
  && curl -fsSL https://nodejs.org/dist/v20.11.0/node-v20.11.0-linux-x64.tar.xz | tar -xJ --strip-components=1 -C /usr/local \
79
  && rm -rf /var/lib/apt/lists/*
80
 
81
+ ENV TESSDATA_PREFIX=/usr/share/tesseract-ocr/5/tessdata
82
+
83
  # ── Python packages ───────────────────────────────────────────────────────────
84
  COPY --from=python-builder /install /usr/local
85
 
backend/app/documents/service.py CHANGED
@@ -5,6 +5,7 @@ Supports PDF, DOCX, TXT, CSV.
5
  import io
6
  import os
7
  import re
 
8
  from typing import Optional
9
 
10
 
@@ -14,6 +15,21 @@ def extract_text_from_pdf(file_bytes: bytes) -> str:
14
  def clean(text: str) -> str:
15
  return re.sub(r"\n{3,}", "\n\n", text).strip()
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  try:
18
  import pypdf
19
  reader = pypdf.PdfReader(io.BytesIO(file_bytes))
@@ -42,8 +58,15 @@ def extract_text_from_pdf(file_bytes: bytes) -> str:
42
 
43
  ocr_pages = []
44
  for page in doc:
45
- textpage = page.get_textpage_ocr(language="eng", dpi=200, full=True)
46
- ocr_pages.append(page.get_text("text", textpage=textpage))
 
 
 
 
 
 
 
47
  return clean("\n".join(ocr_pages))
48
  except Exception as e:
49
  print(f"[documents] PDF fallback extraction error: {e}")
 
5
  import io
6
  import os
7
  import re
8
+ import subprocess
9
  from typing import Optional
10
 
11
 
 
15
  def clean(text: str) -> str:
16
  return re.sub(r"\n{3,}", "\n\n", text).strip()
17
 
18
+ def ocr_page_with_tesseract(page) -> str:
19
+ pix = page.get_pixmap(dpi=200, alpha=False)
20
+ proc = subprocess.run(
21
+ ["tesseract", "stdin", "stdout", "-l", "eng", "--psm", "6"],
22
+ input=pix.tobytes("png"),
23
+ capture_output=True,
24
+ check=False,
25
+ timeout=45,
26
+ )
27
+ if proc.returncode != 0:
28
+ err = proc.stderr.decode("utf-8", errors="replace").strip()
29
+ print(f"[documents] Tesseract OCR error: {err}")
30
+ return ""
31
+ return proc.stdout.decode("utf-8", errors="replace")
32
+
33
  try:
34
  import pypdf
35
  reader = pypdf.PdfReader(io.BytesIO(file_bytes))
 
58
 
59
  ocr_pages = []
60
  for page in doc:
61
+ try:
62
+ textpage = page.get_textpage_ocr(language="eng", dpi=200, full=True)
63
+ ocr_pages.append(page.get_text("text", textpage=textpage))
64
+ except Exception as e:
65
+ print(f"[documents] PyMuPDF OCR error: {e}")
66
+
67
+ if not clean(ocr_pages[-1] if ocr_pages else ""):
68
+ ocr_pages.append(ocr_page_with_tesseract(page))
69
+
70
  return clean("\n".join(ocr_pages))
71
  except Exception as e:
72
  print(f"[documents] PDF fallback extraction error: {e}")