Spaces:
Runtime error
Runtime error
Support PaddleOCR v3 and legacy init args
Browse files- Dockerfile +10 -1
- simpliscribe/ocr.py +43 -2
Dockerfile
CHANGED
|
@@ -18,7 +18,16 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
|
| 18 |
COPY requirements.txt .
|
| 19 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 20 |
|
| 21 |
-
RUN mkdir -p /app/.paddleocr && python -
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
COPY . .
|
| 24 |
|
|
|
|
| 18 |
COPY requirements.txt .
|
| 19 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 20 |
|
| 21 |
+
RUN mkdir -p /app/.paddleocr && python - <<'PY'
|
| 22 |
+
from paddleocr import PaddleOCR
|
| 23 |
+
|
| 24 |
+
try:
|
| 25 |
+
PaddleOCR(lang="en", device="cpu", use_textline_orientation=True, show_log=False)
|
| 26 |
+
except (TypeError, ValueError):
|
| 27 |
+
PaddleOCR(lang="en", use_angle_cls=True, use_gpu=False, show_log=False)
|
| 28 |
+
|
| 29 |
+
print("PaddleOCR models ready")
|
| 30 |
+
PY
|
| 31 |
|
| 32 |
COPY . .
|
| 33 |
|
simpliscribe/ocr.py
CHANGED
|
@@ -48,7 +48,45 @@ def get_ocr_reader() -> Any:
|
|
| 48 |
"PaddleOCR is not installed. Install `paddlepaddle` and `paddleocr` before running OCR."
|
| 49 |
) from exc
|
| 50 |
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
return _ocr_reader
|
| 53 |
|
| 54 |
|
|
@@ -84,7 +122,10 @@ def extract_ocr_text(file_path: Path) -> str:
|
|
| 84 |
|
| 85 |
segments: list[str] = []
|
| 86 |
for path in input_paths:
|
| 87 |
-
|
|
|
|
|
|
|
|
|
|
| 88 |
segments.extend(_collect_paddle_text(results))
|
| 89 |
return " ".join(segments)
|
| 90 |
finally:
|
|
|
|
| 48 |
"PaddleOCR is not installed. Install `paddlepaddle` and `paddleocr` before running OCR."
|
| 49 |
) from exc
|
| 50 |
|
| 51 |
+
reader = None
|
| 52 |
+
init_errors: list[Exception] = []
|
| 53 |
+
|
| 54 |
+
# PaddleOCR v3 dropped use_gpu/use_angle_cls in favor of device/use_textline_orientation.
|
| 55 |
+
for kwargs in (
|
| 56 |
+
{
|
| 57 |
+
"lang": settings.ocr_language,
|
| 58 |
+
"device": "gpu" if settings.ocr_use_gpu else "cpu",
|
| 59 |
+
"use_textline_orientation": True,
|
| 60 |
+
"show_log": False,
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
"lang": settings.ocr_language,
|
| 64 |
+
"device": "gpu" if settings.ocr_use_gpu else "cpu",
|
| 65 |
+
"use_textline_orientation": True,
|
| 66 |
+
},
|
| 67 |
+
{
|
| 68 |
+
"lang": settings.ocr_language,
|
| 69 |
+
"use_angle_cls": True,
|
| 70 |
+
"use_gpu": settings.ocr_use_gpu,
|
| 71 |
+
"show_log": False,
|
| 72 |
+
},
|
| 73 |
+
{
|
| 74 |
+
"lang": settings.ocr_language,
|
| 75 |
+
"use_angle_cls": True,
|
| 76 |
+
"use_gpu": settings.ocr_use_gpu,
|
| 77 |
+
},
|
| 78 |
+
):
|
| 79 |
+
try:
|
| 80 |
+
reader = PaddleOCR(**kwargs)
|
| 81 |
+
break
|
| 82 |
+
except (TypeError, ValueError) as exc:
|
| 83 |
+
init_errors.append(exc)
|
| 84 |
+
|
| 85 |
+
if reader is None:
|
| 86 |
+
last_error = init_errors[-1] if init_errors else RuntimeError("Unknown PaddleOCR initialization failure.")
|
| 87 |
+
raise RuntimeError(f"Failed to initialize PaddleOCR: {last_error}") from last_error
|
| 88 |
+
|
| 89 |
+
_ocr_reader = reader
|
| 90 |
return _ocr_reader
|
| 91 |
|
| 92 |
|
|
|
|
| 122 |
|
| 123 |
segments: list[str] = []
|
| 124 |
for path in input_paths:
|
| 125 |
+
try:
|
| 126 |
+
results = reader.ocr(str(path), cls=True)
|
| 127 |
+
except TypeError:
|
| 128 |
+
results = reader.ocr(str(path))
|
| 129 |
segments.extend(_collect_paddle_text(results))
|
| 130 |
return " ".join(segments)
|
| 131 |
finally:
|