prosperious commited on
Commit
0cfd653
·
verified ·
1 Parent(s): d82db76

Add 323 files (ocr-tech-mps-integration/tools/prepare_native_spotcheck_bundle.py ...)

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. .gitattributes +4 -0
  2. indra_nepali_exp.bundle +3 -0
  3. jobs/eval_v4_last.sh +19 -0
  4. jobs/route_by_conf.py +39 -0
  5. jobs/show_scores.py +7 -0
  6. k2-ampixa-demo-ocr/models/space/README.md +59 -0
  7. k2-ampixa-demo-ocr/models/space/app.py +1373 -0
  8. k2-ampixa-demo-ocr/models/space/models/deva-v2/artifacts/devanagari-v2.txt +143 -0
  9. k2-ampixa-demo-ocr/models/space/models/deva-v2/artifacts/inference.json +0 -0
  10. k2-ampixa-demo-ocr/models/space/models/deva-v2/artifacts/inference.pdiparams +3 -0
  11. k2-ampixa-demo-ocr/models/space/models/deva-v2/artifacts/inference.yml +186 -0
  12. k2-ampixa-demo-ocr/models/space/models/deva-v2/model-card.json +45 -0
  13. k2-ampixa-demo-ocr/models/space/models/devanagari-ft-v2/MODEL-SOURCE.txt +7 -0
  14. k2-ampixa-demo-ocr/models/space/models/devanagari-ft-v2/artifacts/inference.onnx +3 -0
  15. k2-ampixa-demo-ocr/models/space/models/devanagari-ft-v2/artifacts/inference.yml +611 -0
  16. k2-ampixa-demo-ocr/models/space/models/devanagari-ft-v3/MODEL-SOURCE.txt +7 -0
  17. k2-ampixa-demo-ocr/models/space/models/devanagari-ft-v3/artifacts/inference.onnx +3 -0
  18. k2-ampixa-demo-ocr/models/space/models/devanagari-ft-v3/artifacts/inference.yml +611 -0
  19. k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/MODEL-SOURCE.txt +5 -0
  20. k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/artifacts/README.md +169 -0
  21. k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/artifacts/config.json +665 -0
  22. k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/artifacts/inference.json +0 -0
  23. k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/artifacts/inference.pdiparams +3 -0
  24. k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/artifacts/inference.yml +611 -0
  25. k2-ampixa-demo-ocr/models/space/models/limbu-sirijonga-ft-v2/artifacts/inference.json +0 -0
  26. k2-ampixa-demo-ocr/models/space/models/limbu-sirijonga-ft-v2/artifacts/inference.pdiparams +3 -0
  27. k2-ampixa-demo-ocr/models/space/models/limbu-sirijonga-ft-v2/artifacts/inference.yml +134 -0
  28. k2-ampixa-demo-ocr/models/space/models/limbu-sirijonga-ft-v2/artifacts/limbu-sirijonga-v2.txt +91 -0
  29. k2-ampixa-demo-ocr/models/space/models/limbu-sirijonga-ft-v2/model-card.json +45 -0
  30. k2-ampixa-demo-ocr/models/space/models/line-detector/best.pt +3 -0
  31. k2-ampixa-demo-ocr/models/space/requirements.txt +8 -0
  32. k2-ampixa-demo-ocr/workers-ocr/README.md +314 -0
  33. k2-ampixa-demo-ocr/workers-ocr/requirements.txt +9 -0
  34. k2-ampixa-demo-ocr/workers-ocr/run.sh +16 -0
  35. k2-ampixa-demo-ocr/workers-ocr/server.py +715 -0
  36. k2-ampixa-demo-ocr/workers-ocr/server.py.bak-20260924-deva-v2 +687 -0
  37. k2-ampixa-demo-ocr/workers-ocr/server.py.bak-20260924-ft-v2 +715 -0
  38. k2-ampixa-demo-ocr/workers-ocr/server.py.bak-20260924-official-paddle +694 -0
  39. logs/eval_v4_last.log +47 -0
  40. logs/setup_env.log +405 -0
  41. logs/smoke_train.log +23 -0
  42. logs/train_v4-oom-accum2.log +114 -0
  43. logs/train_v4.log +190 -0
  44. ocr-deva-eval-20260924/newar-test-images.tgz +3 -0
  45. ocr-deva-eval-20260924/predict_both.py +86 -0
  46. ocr-deva-eval-20260924/smoke.tgz +3 -0
  47. ocr-deva-eval-20260924/smoke/english/images/english_0000.png +0 -0
  48. ocr-deva-eval-20260924/smoke/english/images/english_0001.png +0 -0
  49. ocr-deva-eval-20260924/smoke/english/images/english_0002.png +0 -0
  50. ocr-deva-eval-20260924/smoke/english/images/english_0003.png +0 -0
.gitattributes CHANGED
@@ -92,3 +92,7 @@ ocr-tech/paper/figures/limbu-recognizer-results.png filter=lfs diff=lfs merge=lf
92
  ocr-tech/paper/figures/limbu-source-evidence.png filter=lfs diff=lfs merge=lfs -text
93
  ocr-tech/paper/figures/research-program.png filter=lfs diff=lfs merge=lfs -text
94
  ocr-tech/paper/figures/system-architecture.png filter=lfs diff=lfs merge=lfs -text
 
 
 
 
 
92
  ocr-tech/paper/figures/limbu-source-evidence.png filter=lfs diff=lfs merge=lfs -text
93
  ocr-tech/paper/figures/research-program.png filter=lfs diff=lfs merge=lfs -text
94
  ocr-tech/paper/figures/system-architecture.png filter=lfs diff=lfs merge=lfs -text
95
+ k2-ampixa-demo-ocr/models/space/models/deva-v2/artifacts/inference.pdiparams filter=lfs diff=lfs merge=lfs -text
96
+ k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/artifacts/inference.pdiparams filter=lfs diff=lfs merge=lfs -text
97
+ k2-ampixa-demo-ocr/models/space/models/limbu-sirijonga-ft-v2/artifacts/inference.pdiparams filter=lfs diff=lfs merge=lfs -text
98
+ indra_nepali_exp.bundle filter=lfs diff=lfs merge=lfs -text
indra_nepali_exp.bundle ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2865b1a55390019d82d1880abb90e16776e2edc0af591f50a0906860860b935d
3
+ size 43116997
jobs/eval_v4_last.sh ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ # Served-path scoring of v4's final checkpoint against v3 and the official model, CPU only
3
+ # (both T4 GPUs are busy with other projects as of 2026-10-04).
4
+ set -euo pipefail
5
+ R=$HOME/ocr/ocr-tech/tools/deva_rec_torch/recipes/t4
6
+ source "$R/env.sh"
7
+ source "$R/evalsets.sh"
8
+ export CUDA_VISIBLE_DEVICES=""
9
+ OUT=$FT/export/v4-last-onnx
10
+ if [ ! -f "$OUT/inference.onnx" ]; then
11
+ nice -n 10 "$TORCH_PY" "$CODE/export_onnx.py" "$FT/runs/deva-ft-v4-hw/last" "$FT/hf/devanagari_PP-OCRv5_mobile_rec_onnx" "$OUT"
12
+ fi
13
+ H4="h4_dhcd_digits_test:$D/hw-eval/h4_dhcd_digits_test:$D/hw-eval/h4_dhcd_digits_test/labels.txt"
14
+ nice -n 10 "$SERVE_PY" "$CODE/serve_predict.py" "$OUT" onnxruntime "$FT/preds/v4-last" "${EVALSETS[@]}" "${HWSETS[@]}"
15
+ nice -n 10 "$SERVE_PY" "$CODE/serve_predict.py" "$FT/export/v3-final-onnx" onnxruntime "$FT/preds/v3-final" "$H4"
16
+ nice -n 10 "$SERVE_PY" "$CODE/serve_predict.py" "$OCR_ROOT/k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/artifacts" paddle_static "$FT/preds/paddle-official" "$H4"
17
+ "$TORCH_PY" "$CODE/score.py" "$FT/preds/v4last_vs_v3_vs_official.json" \
18
+ "official=$FT/preds/paddle-official" "v3=$FT/preds/v3-final" "v4last=$FT/preds/v4-last" -- "${EVALSETS[@]}" "${HWSETS[@]}"
19
+ echo EVAL_V4_DONE
jobs/route_by_conf.py ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Would 'run v3 and v4-final, keep the more confident reading' get the best of both?"""
2
+ import sys
3
+ from pathlib import Path
4
+
5
+ sys.path.insert(0, str(Path.home() / "ocr" / "ocr-tech" / "tools"))
6
+ from eval_metrics import evaluate # noqa: E402
7
+
8
+ FT = Path.home() / "ocr" / "ocr-deva-ft"
9
+ SETS = {}
10
+ for line in sys.stdin.read().split():
11
+ name, root, labels = line.split(":")
12
+ SETS[name] = Path(labels)
13
+
14
+
15
+ def preds(system, name):
16
+ out = {}
17
+ for row in (FT / "preds" / system / f"{name}.tsv").read_text(encoding="utf-8").splitlines():
18
+ parts = row.split("\t")
19
+ out[parts[0]] = (parts[1] if len(parts) > 1 else "", float(parts[2]) if len(parts) > 2 and parts[2] not in ("", "None") else 0.0)
20
+ return out
21
+
22
+
23
+ print("%-22s %8s %8s %8s %8s %s" % ("set", "v3", "v4last", "router", "oracle", "router picks v4"))
24
+ for name, labels in SETS.items():
25
+ gold = dict(line.split("\t", 1) for line in labels.read_text(encoding="utf-8").splitlines() if "\t" in line)
26
+ a, b = preds("v3-final", name), preds("v4-last", name)
27
+ routed, oracle, picks = [], [], 0
28
+ for rel, text in gold.items():
29
+ (ta, sa), (tb, sb) = a[rel], b[rel]
30
+ pick_b = sb > sa
31
+ picks += pick_b
32
+ routed.append(((tb if pick_b else ta), text))
33
+ ea = evaluate([(ta, text)])["cer_codepoint"]
34
+ eb = evaluate([(tb, text)])["cer_codepoint"]
35
+ oracle.append(((tb if eb < ea else ta), text))
36
+ r = lambda pairs: evaluate(pairs)["cer_codepoint"] * 100
37
+ print("%-22s %7.2f%% %7.2f%% %7.2f%% %7.2f%% %d/%d" % (
38
+ name, r([(a[k][0], v) for k, v in gold.items()]), r([(b[k][0], v) for k, v in gold.items()]),
39
+ r(routed), r(oracle), picks, len(gold)))
jobs/show_scores.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import json
2
+ import sys
3
+ d = json.load(open(sys.argv[1]))
4
+ for name, systems in d.items():
5
+ n = next(iter(systems.values()))["n_lines"]
6
+ cells = " ".join("%s=%6.2f%%/%.2f" % (k, v["cer_codepoint"] * 100, v["exact_line_acc"]) for k, v in systems.items())
7
+ print("%-22s n=%5d %s" % (name, n, cells))
k2-ampixa-demo-ocr/models/space/README.md ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Limbu OCR
3
+ emoji: 📄
4
+ colorFrom: blue
5
+ colorTo: green
6
+ sdk: docker
7
+ app_port: 7860
8
+ pinned: false
9
+ license: apache-2.0
10
+ ---
11
+
12
+ # Limbu OCR
13
+
14
+ Limbu-specific OCR for cropped lines, full page images, and small book/PDF batches.
15
+ It supports Sirijonga Limbu and Devanagari-written Limbu.
16
+
17
+ This Space bundles:
18
+
19
+ - Sirijonga line recognizer: `models/limbu-sirijonga-real-heavy/`
20
+ - Devanagari-written Limbu line recognizer: `models/limbu-devanagari-ppocrv5/`
21
+ - PaddleOCR full-page text detection for primary page and book OCR.
22
+ - Deterministic image-line segmentation as a fallback page/book path.
23
+ - Optional YOLO probe: `models/line-detector/best.pt`
24
+
25
+ The operational page/book path does not depend on YOLO. The primary route uses
26
+ PaddleOCR's full-page detector, orders the detected text lines, routes each crop
27
+ through the bundled Sirijonga/Devanagari Paddle recognizers, and returns editable
28
+ Unicode text with crop and overlay evidence. The Paddle Book OCR tab accepts page
29
+ images or PDFs and returns a ZIP with `book.txt`, `book.json`, annotated pages, and
30
+ line crops.
31
+
32
+ This is an operational OCR system for producing text and review evidence. It is not
33
+ yet a claim-safe benchmark result.
34
+
35
+ ## Local Run
36
+
37
+ ```bash
38
+ cd spaces/limbu-ocr
39
+ docker build -t limbu-ocr-space .
40
+ docker run --rm -p 7860:7860 limbu-ocr-space
41
+ ```
42
+
43
+ Open `http://127.0.0.1:7860`.
44
+
45
+ ## Deploy To Hugging Face
46
+
47
+ This Space has been uploaded to `https://huggingface.co/spaces/ampixa/limbu-ocr`.
48
+ See `DEPLOY.md` for the repeatable `uvx` upload command.
49
+
50
+ ```bash
51
+ cd spaces/limbu-ocr
52
+ git init
53
+ git add .
54
+ git commit -m "Add Limbu OCR Space"
55
+ git remote add origin https://huggingface.co/spaces/ampixa/limbu-ocr
56
+ git push origin main
57
+ ```
58
+
59
+ Use a CPU Space for basic demos. Upgrade hardware if page OCR latency is too high.
k2-ampixa-demo-ocr/models/space/app.py ADDED
@@ -0,0 +1,1373 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import math
5
+ import os
6
+ import re
7
+ import shutil
8
+ import tempfile
9
+ import zipfile
10
+ from dataclasses import dataclass
11
+ from datetime import UTC, datetime
12
+ from functools import lru_cache
13
+ from pathlib import Path
14
+ from typing import Any
15
+
16
+ import gradio as gr
17
+ import numpy as np
18
+ from PIL import Image, ImageDraw, ImageOps
19
+
20
+
21
+ APP_ROOT = Path(__file__).resolve().parent
22
+ SIRIJONGA_MODEL_DIR = APP_ROOT / "models" / "limbu-sirijonga-ft-v2" / "artifacts"
23
+ DEVANAGARI_MODEL_DIR = APP_ROOT / "models" / "deva-v2" / "artifacts"
24
+ DETECTOR_MODEL_PATH = APP_ROOT / "models" / "line-detector" / "best.pt"
25
+
26
+ LIMBU_RE = re.compile(r"[\u1900-\u194F]")
27
+ DEVANAGARI_RE = re.compile(r"[\u0900-\u097F]")
28
+
29
+
30
+ @dataclass(slots=True)
31
+ class Recognition:
32
+ text: str
33
+ confidence: float | None
34
+ engine: str
35
+
36
+ def as_dict(self) -> dict[str, Any]:
37
+ return {
38
+ "text": self.text,
39
+ "confidence": self.confidence,
40
+ "engine": self.engine,
41
+ }
42
+
43
+
44
+ @dataclass(slots=True)
45
+ class Box:
46
+ left: int
47
+ top: int
48
+ right: int
49
+ bottom: int
50
+ confidence: float
51
+
52
+ @property
53
+ def width(self) -> int:
54
+ return max(0, self.right - self.left)
55
+
56
+ @property
57
+ def height(self) -> int:
58
+ return max(0, self.bottom - self.top)
59
+
60
+ @property
61
+ def x_center(self) -> float:
62
+ return (self.left + self.right) / 2.0
63
+
64
+ @property
65
+ def y_center(self) -> float:
66
+ return (self.top + self.bottom) / 2.0
67
+
68
+ def as_dict(self) -> dict[str, Any]:
69
+ return {
70
+ "left": self.left,
71
+ "top": self.top,
72
+ "right": self.right,
73
+ "bottom": self.bottom,
74
+ "confidence": self.confidence,
75
+ }
76
+
77
+
78
+ @dataclass(slots=True)
79
+ class PaddleDetection:
80
+ box: Box
81
+ seed_text: str
82
+ seed_confidence: float | None
83
+
84
+ def as_dict(self) -> dict[str, Any]:
85
+ return {
86
+ "box": self.box.as_dict(),
87
+ "seed_text": self.seed_text,
88
+ "seed_confidence": self.seed_confidence,
89
+ }
90
+
91
+
92
+ def _ensure_models_present(*, include_detector: bool = False) -> None:
93
+ required = [
94
+ SIRIJONGA_MODEL_DIR / "inference.json",
95
+ SIRIJONGA_MODEL_DIR / "inference.pdiparams",
96
+ SIRIJONGA_MODEL_DIR / "inference.yml",
97
+ SIRIJONGA_MODEL_DIR / "limbu-sirijonga-v2.txt",
98
+ DEVANAGARI_MODEL_DIR / "inference.json",
99
+ DEVANAGARI_MODEL_DIR / "inference.pdiparams",
100
+ DEVANAGARI_MODEL_DIR / "inference.yml",
101
+ DEVANAGARI_MODEL_DIR / "nep_eng_dict.txt",
102
+ ]
103
+ if include_detector:
104
+ required.append(DETECTOR_MODEL_PATH)
105
+ missing = [str(path.relative_to(APP_ROOT)) for path in required if not path.exists()]
106
+ if missing:
107
+ raise RuntimeError("Missing bundled OCR artifacts: " + ", ".join(missing))
108
+
109
+
110
+ def _normalize_image(image: Image.Image) -> Image.Image:
111
+ return ImageOps.exif_transpose(image).convert("RGB")
112
+
113
+
114
+ def _save_temp_image(image: Image.Image) -> Path:
115
+ tmp = tempfile.NamedTemporaryFile(prefix="limbu-ocr-", suffix=".png", delete=False)
116
+ tmp_path = Path(tmp.name)
117
+ tmp.close()
118
+ _normalize_image(image).save(tmp_path)
119
+ return tmp_path
120
+
121
+
122
+ @lru_cache(maxsize=1)
123
+ def _text_recognition_class() -> type[Any]:
124
+ try:
125
+ from paddleocr._models.text_recognition import TextRecognition
126
+ except ImportError as exc:
127
+ raise RuntimeError("PaddleOCR TextRecognition is unavailable in this runtime") from exc
128
+ return TextRecognition
129
+
130
+
131
+ @lru_cache(maxsize=2)
132
+ def _recognizer(engine: str) -> Any:
133
+ _ensure_models_present()
134
+ cls = _text_recognition_class()
135
+ if engine == "sirijonga":
136
+ model_dir = SIRIJONGA_MODEL_DIR
137
+ elif engine == "devanagari":
138
+ model_dir = DEVANAGARI_MODEL_DIR
139
+ else:
140
+ raise ValueError(f"unknown recognizer engine: {engine}")
141
+ return cls(
142
+ model_name="PP-OCRv5_mobile_rec",
143
+ model_dir=str(model_dir),
144
+ device=os.environ.get("LIMBU_OCR_PADDLE_DEVICE", "cpu"),
145
+ )
146
+
147
+
148
+ @lru_cache(maxsize=1)
149
+ def _detector() -> Any:
150
+ _ensure_models_present(include_detector=True)
151
+ try:
152
+ from ultralytics import YOLO
153
+ except ImportError as exc:
154
+ raise RuntimeError("ultralytics is unavailable in this runtime") from exc
155
+ return YOLO(str(DETECTOR_MODEL_PATH))
156
+
157
+
158
+ def _result_mapping(item: object) -> dict[str, object] | None:
159
+ payload: object = item
160
+ if not isinstance(payload, dict):
161
+ payload = getattr(item, "json", None)
162
+ if not isinstance(payload, dict):
163
+ return None
164
+ nested = payload.get("res")
165
+ return nested if isinstance(nested, dict) else payload
166
+
167
+
168
+ def _recognize_with_engine(image_path: Path, engine: str) -> Recognition:
169
+ raw_result = _recognizer(engine).predict(str(image_path))
170
+ best_text = ""
171
+ best_confidence: float | None = None
172
+ for item in raw_result if isinstance(raw_result, list) else [raw_result]:
173
+ payload = _result_mapping(item)
174
+ if payload is None:
175
+ continue
176
+ text = payload.get("rec_text") or payload.get("text") or payload.get("transcription") or ""
177
+ score = payload.get("rec_score") or payload.get("score") or payload.get("confidence")
178
+ confidence = float(score) if score is not None else None
179
+ text = str(text).strip()
180
+ if not text:
181
+ continue
182
+ if best_confidence is None or (confidence is not None and confidence > best_confidence):
183
+ best_text = text
184
+ best_confidence = confidence
185
+ return Recognition(text=best_text, confidence=best_confidence, engine=engine)
186
+
187
+
188
+ def _script_score(recognition: Recognition) -> float:
189
+ text = recognition.text or ""
190
+ confidence = recognition.confidence if recognition.confidence is not None else 0.0
191
+ limbu_chars = len(LIMBU_RE.findall(text))
192
+ deva_chars = len(DEVANAGARI_RE.findall(text))
193
+ if recognition.engine == "sirijonga":
194
+ return confidence + min(limbu_chars, 12) * 0.06 - min(deva_chars, 12) * 0.04
195
+ if recognition.engine == "devanagari":
196
+ return confidence + min(deva_chars, 12) * 0.035 - min(limbu_chars, 12) * 0.08
197
+ return confidence
198
+
199
+
200
+ def _recognize_line_path(image_path: Path, mode: str) -> tuple[Recognition, list[Recognition]]:
201
+ if mode == "Sirijonga":
202
+ rec = _recognize_with_engine(image_path, "sirijonga")
203
+ return rec, [rec]
204
+ if mode == "Devanagari":
205
+ rec = _recognize_with_engine(image_path, "devanagari")
206
+ return rec, [rec]
207
+ candidates = [
208
+ _recognize_with_engine(image_path, "sirijonga"),
209
+ _recognize_with_engine(image_path, "devanagari"),
210
+ ]
211
+ return max(candidates, key=_script_score), candidates
212
+
213
+
214
+ def _engine_key(mode: str) -> str:
215
+ if mode == "Sirijonga":
216
+ return "sirijonga"
217
+ if mode == "Devanagari":
218
+ return "devanagari"
219
+ raise ValueError(f"unsupported Paddle seed recognizer: {mode}")
220
+
221
+
222
+ @lru_cache(maxsize=8)
223
+ def _paddle_page_ocr(
224
+ seed_engine: str,
225
+ text_det_limit_side_len: int,
226
+ text_det_thresh: float,
227
+ text_det_box_thresh: float,
228
+ text_det_unclip_ratio: float,
229
+ ) -> Any:
230
+ _ensure_models_present()
231
+ try:
232
+ from paddleocr import PaddleOCR
233
+ except ImportError as exc:
234
+ raise RuntimeError("PaddleOCR is unavailable in this runtime") from exc
235
+ if seed_engine == "sirijonga":
236
+ model_dir = SIRIJONGA_MODEL_DIR
237
+ elif seed_engine == "devanagari":
238
+ model_dir = DEVANAGARI_MODEL_DIR
239
+ else:
240
+ raise ValueError(f"unsupported Paddle seed engine: {seed_engine}")
241
+ return PaddleOCR(
242
+ text_recognition_model_name="PP-OCRv5_mobile_rec",
243
+ text_recognition_model_dir=str(model_dir),
244
+ text_det_limit_side_len=max(256, int(text_det_limit_side_len)),
245
+ text_det_thresh=float(text_det_thresh),
246
+ text_det_box_thresh=float(text_det_box_thresh),
247
+ text_det_unclip_ratio=float(text_det_unclip_ratio),
248
+ use_doc_orientation_classify=False,
249
+ use_doc_unwarping=False,
250
+ use_textline_orientation=False,
251
+ )
252
+
253
+
254
+ def _coerce_point_pair(value: object) -> tuple[float, float] | None:
255
+ if hasattr(value, "tolist"):
256
+ value = value.tolist()
257
+ if isinstance(value, list | tuple) and len(value) >= 2:
258
+ try:
259
+ return float(value[0]), float(value[1])
260
+ except (TypeError, ValueError):
261
+ return None
262
+ return None
263
+
264
+
265
+ def _paddle_box_from_value(value: object, *, image_width: int, image_height: int, confidence: float | None) -> Box | None:
266
+ if hasattr(value, "tolist"):
267
+ value = value.tolist()
268
+ if isinstance(value, list | tuple) and len(value) == 4 and all(isinstance(item, int | float) for item in value):
269
+ left, top, right, bottom = [float(item) for item in value]
270
+ if right < left:
271
+ left, right = right, left
272
+ if bottom < top:
273
+ top, bottom = bottom, top
274
+ elif isinstance(value, list | tuple):
275
+ points = [_coerce_point_pair(point) for point in value]
276
+ clean_points = [point for point in points if point is not None]
277
+ if not clean_points:
278
+ return None
279
+ left = min(point[0] for point in clean_points)
280
+ top = min(point[1] for point in clean_points)
281
+ right = max(point[0] for point in clean_points)
282
+ bottom = max(point[1] for point in clean_points)
283
+ else:
284
+ return None
285
+ box = Box(
286
+ left=max(0, int(math.floor(left))),
287
+ top=max(0, int(math.floor(top))),
288
+ right=min(image_width, int(math.ceil(right))),
289
+ bottom=min(image_height, int(math.ceil(bottom))),
290
+ confidence=float(confidence) if confidence is not None else 1.0,
291
+ )
292
+ return box if box.width > 0 and box.height > 0 else None
293
+
294
+
295
+ def _as_list(value: object) -> list[Any]:
296
+ if value is None:
297
+ return []
298
+ if hasattr(value, "tolist"):
299
+ value = value.tolist()
300
+ return value if isinstance(value, list) else []
301
+
302
+
303
+ def _first_payload_list(payload: dict[str, object], keys: tuple[str, ...]) -> list[Any]:
304
+ for key in keys:
305
+ values = _as_list(payload.get(key))
306
+ if values:
307
+ return values
308
+ return []
309
+
310
+
311
+ def _paddle_detections_from_result(raw_result: object, *, image_width: int, image_height: int) -> list[PaddleDetection]:
312
+ candidates = raw_result
313
+ if isinstance(raw_result, list) and len(raw_result) == 1 and isinstance(raw_result[0], list):
314
+ candidates = raw_result[0]
315
+ if not isinstance(candidates, list):
316
+ candidates = [candidates]
317
+
318
+ detections: list[PaddleDetection] = []
319
+ for item in candidates:
320
+ payload = _result_mapping(item)
321
+ if payload is None:
322
+ continue
323
+ boxes = _first_payload_list(payload, ("rec_polys", "dt_polys", "rec_boxes", "dt_boxes"))
324
+ texts = _first_payload_list(payload, ("rec_texts", "texts"))
325
+ scores = _first_payload_list(payload, ("rec_scores", "scores"))
326
+ for index, raw_box in enumerate(boxes):
327
+ score: float | None = None
328
+ if index < len(scores) and scores[index] is not None:
329
+ try:
330
+ score = float(scores[index])
331
+ except (TypeError, ValueError):
332
+ score = None
333
+ box = _paddle_box_from_value(raw_box, image_width=image_width, image_height=image_height, confidence=score)
334
+ if box is None:
335
+ continue
336
+ seed_text = str(texts[index]).strip() if index < len(texts) and texts[index] is not None else ""
337
+ detections.append(PaddleDetection(box=box, seed_text=seed_text, seed_confidence=score))
338
+ return detections
339
+
340
+
341
+ def _predict_paddle_page_detections(
342
+ image: Image.Image,
343
+ *,
344
+ seed_recognizer: str,
345
+ text_det_limit_side_len: int,
346
+ text_det_thresh: float,
347
+ text_det_box_thresh: float,
348
+ text_det_unclip_ratio: float,
349
+ ) -> list[PaddleDetection]:
350
+ normalized = _normalize_image(image)
351
+ page_path = _save_temp_image(normalized)
352
+ try:
353
+ ocr = _paddle_page_ocr(
354
+ _engine_key(seed_recognizer),
355
+ int(text_det_limit_side_len),
356
+ float(text_det_thresh),
357
+ float(text_det_box_thresh),
358
+ float(text_det_unclip_ratio),
359
+ )
360
+ raw_result = ocr.predict(str(page_path))
361
+ finally:
362
+ page_path.unlink(missing_ok=True)
363
+ return _paddle_detections_from_result(raw_result, image_width=normalized.width, image_height=normalized.height)
364
+
365
+
366
+ def recognize_line(image: Image.Image | None, mode: str) -> tuple[str, str]:
367
+ if image is None:
368
+ return "", "Upload a cropped text line image."
369
+ path = _save_temp_image(image)
370
+ try:
371
+ best, candidates = _recognize_line_path(path, mode)
372
+ finally:
373
+ path.unlink(missing_ok=True)
374
+ details = {
375
+ "selected_engine": best.engine,
376
+ "selected_confidence": best.confidence,
377
+ "candidates": [candidate.as_dict() for candidate in candidates],
378
+ }
379
+ return best.text, json.dumps(details, ensure_ascii=False, indent=2)
380
+
381
+
382
+ def _detect_boxes(image_path: Path, confidence: float) -> list[Box]:
383
+ result = _detector().predict(
384
+ source=str(image_path),
385
+ imgsz=1280,
386
+ conf=float(confidence),
387
+ verbose=False,
388
+ )[0]
389
+ boxes: list[Box] = []
390
+ raw_boxes = getattr(result, "boxes", None)
391
+ if raw_boxes is None:
392
+ return boxes
393
+ xyxy_values = raw_boxes.xyxy.cpu().numpy() if hasattr(raw_boxes.xyxy, "cpu") else np.asarray(raw_boxes.xyxy)
394
+ conf_values = raw_boxes.conf.cpu().numpy() if hasattr(raw_boxes.conf, "cpu") else np.asarray(raw_boxes.conf)
395
+ for xyxy, conf in zip(xyxy_values, conf_values, strict=False):
396
+ left, top, right, bottom = [int(round(float(value))) for value in xyxy[:4]]
397
+ if right <= left or bottom <= top:
398
+ continue
399
+ boxes.append(Box(left=left, top=top, right=right, bottom=bottom, confidence=float(conf)))
400
+ return boxes
401
+
402
+
403
+ def _order_boxes(boxes: list[Box], mode: str, image_width: int) -> list[Box]:
404
+ if mode == "Two columns LTR":
405
+ return sorted(boxes, key=lambda box: (0 if box.x_center < image_width / 2 else 1, box.y_center, box.x_center))
406
+ if mode == "Two columns RTL":
407
+ return sorted(boxes, key=lambda box: (0 if box.x_center >= image_width / 2 else 1, box.y_center, box.x_center))
408
+ row_height = max(12, int(np.median([box.height for box in boxes])) if boxes else 24)
409
+ return sorted(boxes, key=lambda box: (math.floor(box.y_center / row_height), box.x_center))
410
+
411
+
412
+ def _crop_box(image: Image.Image, box: Box, padding: int) -> Image.Image:
413
+ left = max(0, box.left - padding)
414
+ top = max(0, box.top - padding)
415
+ right = min(image.width, box.right + padding)
416
+ bottom = min(image.height, box.bottom + padding)
417
+ return image.crop((left, top, right, bottom))
418
+
419
+
420
+ def _draw_boxes(image: Image.Image, boxes: list[Box]) -> Image.Image:
421
+ annotated = image.copy()
422
+ draw = ImageDraw.Draw(annotated)
423
+ for index, box in enumerate(boxes, start=1):
424
+ draw.rectangle((box.left, box.top, box.right, box.bottom), outline=(28, 120, 92), width=3)
425
+ draw.text((box.left + 4, max(0, box.top - 18)), str(index), fill=(28, 120, 92))
426
+ return annotated
427
+
428
+
429
+ def _box_iou(left: Box, right: Box) -> float:
430
+ overlap_left = max(left.left, right.left)
431
+ overlap_top = max(left.top, right.top)
432
+ overlap_right = min(left.right, right.right)
433
+ overlap_bottom = min(left.bottom, right.bottom)
434
+ overlap_width = max(0, overlap_right - overlap_left)
435
+ overlap_height = max(0, overlap_bottom - overlap_top)
436
+ intersection = overlap_width * overlap_height
437
+ if intersection <= 0:
438
+ return 0.0
439
+ left_area = max(1, left.width * left.height)
440
+ right_area = max(1, right.width * right.height)
441
+ return intersection / float(left_area + right_area - intersection)
442
+
443
+
444
+ def _merge_overlapping_boxes(boxes: list[Box], threshold: float = 0.80) -> list[Box]:
445
+ merged: list[Box] = []
446
+ for box in sorted(boxes, key=lambda item: item.confidence, reverse=True):
447
+ if any(_box_iou(box, kept) >= threshold for kept in merged):
448
+ continue
449
+ merged.append(box)
450
+ return merged
451
+
452
+
453
+ def _cv2_threshold(gray: Image.Image, threshold: int) -> Any:
454
+ try:
455
+ import cv2
456
+ except ImportError as exc:
457
+ raise RuntimeError("OpenCV is unavailable; deterministic page OCR requires opencv-python-headless") from exc
458
+ pixels = np.asarray(gray)
459
+ if threshold <= 0:
460
+ _value, binary = cv2.threshold(pixels, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
461
+ else:
462
+ _value, binary = cv2.threshold(pixels, int(threshold), 255, cv2.THRESH_BINARY_INV)
463
+ return cv2, binary
464
+
465
+
466
+ def _detect_image_line_boxes(
467
+ image: Image.Image,
468
+ *,
469
+ threshold: int,
470
+ horizontal_kernel: int,
471
+ vertical_kernel: int,
472
+ dilation_iterations: int,
473
+ min_width: int,
474
+ min_height: int,
475
+ max_height: int,
476
+ min_area: int,
477
+ padding: int,
478
+ ) -> list[Box]:
479
+ normalized = _normalize_image(image)
480
+ gray = ImageOps.grayscale(normalized)
481
+ cv2, binary = _cv2_threshold(gray, threshold)
482
+ kernel = cv2.getStructuringElement(
483
+ cv2.MORPH_RECT,
484
+ (max(1, int(horizontal_kernel)), max(1, int(vertical_kernel))),
485
+ )
486
+ dilated = cv2.dilate(binary, kernel, iterations=max(1, int(dilation_iterations)))
487
+ contours, _hierarchy = cv2.findContours(dilated, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
488
+ boxes: list[Box] = []
489
+ for contour in contours:
490
+ x, y, width, height = cv2.boundingRect(contour)
491
+ area = int(width * height)
492
+ if width < min_width or height < min_height or area < min_area:
493
+ continue
494
+ if max_height > 0 and height > max_height:
495
+ continue
496
+ left = max(0, int(x) - padding)
497
+ top = max(0, int(y) - padding)
498
+ right = min(normalized.width, int(x + width) + padding)
499
+ bottom = min(normalized.height, int(y + height) + padding)
500
+ if right <= left or bottom <= top:
501
+ continue
502
+ boxes.append(Box(left=left, top=top, right=right, bottom=bottom, confidence=1.0))
503
+ return _merge_overlapping_boxes(boxes)
504
+
505
+
506
+ def _has_two_column_shape(boxes: list[Box], image_width: int) -> bool:
507
+ if len(boxes) < 8 or image_width <= 0:
508
+ return False
509
+ midpoint = image_width / 2.0
510
+ left = [box for box in boxes if box.x_center < midpoint]
511
+ right = [box for box in boxes if box.x_center >= midpoint]
512
+ if len(left) < 3 or len(right) < 3:
513
+ return False
514
+ left_median = float(np.median([box.x_center for box in left]))
515
+ right_median = float(np.median([box.x_center for box in right]))
516
+ return (right_median - left_median) >= image_width * 0.22
517
+
518
+
519
+ def _order_image_line_boxes(boxes: list[Box], mode: str, image_width: int) -> list[Box]:
520
+ if mode == "Auto":
521
+ mode = "Columns LTR" if _has_two_column_shape(boxes, image_width) else "Rows LTR"
522
+ if mode == "Columns LTR":
523
+ return sorted(boxes, key=lambda box: (0 if box.x_center < image_width / 2 else 1, box.y_center, box.x_center))
524
+ if mode == "Columns RTL":
525
+ return sorted(boxes, key=lambda box: (0 if box.x_center >= image_width / 2 else 1, box.y_center, box.x_center))
526
+ row_height = max(12, int(np.median([box.height for box in boxes])) if boxes else 24)
527
+ return sorted(boxes, key=lambda box: (math.floor(box.y_center / row_height), box.x_center))
528
+
529
+
530
+ def _recognize_page_core(
531
+ image: Image.Image,
532
+ *,
533
+ mode: str,
534
+ reading_order: str,
535
+ threshold: int,
536
+ horizontal_kernel: int,
537
+ vertical_kernel: int,
538
+ dilation_iterations: int,
539
+ min_width: int,
540
+ min_height: int,
541
+ max_height: int,
542
+ min_area: int,
543
+ crop_padding: int,
544
+ max_lines: int,
545
+ artifact_dir: Path | None = None,
546
+ page_index: int = 0,
547
+ ) -> dict[str, Any]:
548
+ normalized = _normalize_image(image)
549
+ boxes = _detect_image_line_boxes(
550
+ normalized,
551
+ threshold=threshold,
552
+ horizontal_kernel=horizontal_kernel,
553
+ vertical_kernel=vertical_kernel,
554
+ dilation_iterations=dilation_iterations,
555
+ min_width=min_width,
556
+ min_height=min_height,
557
+ max_height=max_height,
558
+ min_area=min_area,
559
+ padding=max(0, int(crop_padding // 2)),
560
+ )
561
+ boxes = _order_image_line_boxes(boxes, reading_order, normalized.width)
562
+ if max_lines > 0:
563
+ boxes = boxes[: int(max_lines)]
564
+
565
+ lines: list[str] = []
566
+ gallery: list[tuple[Image.Image, str]] = []
567
+ rows: list[dict[str, Any]] = []
568
+ crop_dir = artifact_dir / "crops" / f"page-{page_index + 1:04d}" if artifact_dir is not None else None
569
+ if crop_dir is not None:
570
+ crop_dir.mkdir(parents=True, exist_ok=True)
571
+
572
+ with tempfile.TemporaryDirectory(prefix="limbu-page-image-line-") as tmp_dir_name:
573
+ tmp_dir = Path(tmp_dir_name)
574
+ for index, box in enumerate(boxes, start=1):
575
+ crop = _crop_box(normalized, box, int(crop_padding))
576
+ crop_path = tmp_dir / f"line-{index:04d}.png"
577
+ crop.save(crop_path)
578
+ best, candidates = _recognize_line_path(crop_path, mode)
579
+ if best.text:
580
+ lines.append(best.text)
581
+ saved_crop_path: str | None = None
582
+ if crop_dir is not None:
583
+ saved_crop = crop_dir / f"line-{index:04d}.png"
584
+ crop.save(saved_crop)
585
+ saved_crop_path = str(saved_crop.relative_to(artifact_dir))
586
+ rows.append(
587
+ {
588
+ "page_index": page_index,
589
+ "index": index,
590
+ "box": box.as_dict(),
591
+ "crop_path": saved_crop_path,
592
+ "selected_engine": best.engine,
593
+ "selected_confidence": best.confidence,
594
+ "text": best.text,
595
+ "candidates": [candidate.as_dict() for candidate in candidates],
596
+ }
597
+ )
598
+ caption = f"{index}. {best.text}" if best.text else f"{index}. <empty>"
599
+ gallery.append((crop, caption))
600
+
601
+ annotated = _draw_boxes(normalized, boxes)
602
+ annotated_path: str | None = None
603
+ if artifact_dir is not None:
604
+ page_dir = artifact_dir / "annotated-pages"
605
+ page_dir.mkdir(parents=True, exist_ok=True)
606
+ target = page_dir / f"page-{page_index + 1:04d}.png"
607
+ annotated.save(target)
608
+ annotated_path = str(target.relative_to(artifact_dir))
609
+ return {
610
+ "text": "\n".join(lines),
611
+ "annotated": annotated,
612
+ "annotated_path": annotated_path,
613
+ "gallery": gallery,
614
+ "rows": rows,
615
+ "line_count": len(boxes),
616
+ "detector": "deterministic image-line projection",
617
+ "reading_order": reading_order,
618
+ "threshold": threshold,
619
+ "horizontal_kernel": horizontal_kernel,
620
+ "vertical_kernel": vertical_kernel,
621
+ "dilation_iterations": dilation_iterations,
622
+ }
623
+
624
+
625
+ def _recognize_page_paddle_core(
626
+ image: Image.Image,
627
+ *,
628
+ mode: str,
629
+ seed_recognizer: str,
630
+ reading_order: str,
631
+ text_det_limit_side_len: int,
632
+ text_det_thresh: float,
633
+ text_det_box_thresh: float,
634
+ text_det_unclip_ratio: float,
635
+ crop_padding: int,
636
+ max_lines: int,
637
+ artifact_dir: Path | None = None,
638
+ page_index: int = 0,
639
+ ) -> dict[str, Any]:
640
+ normalized = _normalize_image(image)
641
+ detections = _predict_paddle_page_detections(
642
+ normalized,
643
+ seed_recognizer=seed_recognizer,
644
+ text_det_limit_side_len=text_det_limit_side_len,
645
+ text_det_thresh=text_det_thresh,
646
+ text_det_box_thresh=text_det_box_thresh,
647
+ text_det_unclip_ratio=text_det_unclip_ratio,
648
+ )
649
+ ordered_boxes = _order_image_line_boxes([detection.box for detection in detections], reading_order, normalized.width)
650
+ detection_by_box = {(detection.box.left, detection.box.top, detection.box.right, detection.box.bottom): detection for detection in detections}
651
+ ordered_detections = [
652
+ detection_by_box[(box.left, box.top, box.right, box.bottom)]
653
+ for box in ordered_boxes
654
+ if (box.left, box.top, box.right, box.bottom) in detection_by_box
655
+ ]
656
+ if max_lines > 0:
657
+ ordered_detections = ordered_detections[: int(max_lines)]
658
+
659
+ lines: list[str] = []
660
+ gallery: list[tuple[Image.Image, str]] = []
661
+ rows: list[dict[str, Any]] = []
662
+ crop_dir = artifact_dir / "crops" / f"page-{page_index + 1:04d}" if artifact_dir is not None else None
663
+ if crop_dir is not None:
664
+ crop_dir.mkdir(parents=True, exist_ok=True)
665
+
666
+ with tempfile.TemporaryDirectory(prefix="limbu-page-paddle-") as tmp_dir_name:
667
+ tmp_dir = Path(tmp_dir_name)
668
+ for index, detection in enumerate(ordered_detections, start=1):
669
+ crop = _crop_box(normalized, detection.box, int(crop_padding))
670
+ crop_path = tmp_dir / f"line-{index:04d}.png"
671
+ crop.save(crop_path)
672
+ best, candidates = _recognize_line_path(crop_path, mode)
673
+ if best.text:
674
+ lines.append(best.text)
675
+ saved_crop_path: str | None = None
676
+ if crop_dir is not None:
677
+ saved_crop = crop_dir / f"line-{index:04d}.png"
678
+ crop.save(saved_crop)
679
+ saved_crop_path = str(saved_crop.relative_to(artifact_dir))
680
+ rows.append(
681
+ {
682
+ "page_index": page_index,
683
+ "index": index,
684
+ "box": detection.box.as_dict(),
685
+ "crop_path": saved_crop_path,
686
+ "paddle_seed_recognizer": seed_recognizer,
687
+ "paddle_seed_text": detection.seed_text,
688
+ "paddle_seed_confidence": detection.seed_confidence,
689
+ "selected_engine": best.engine,
690
+ "selected_confidence": best.confidence,
691
+ "text": best.text,
692
+ "candidates": [candidate.as_dict() for candidate in candidates],
693
+ }
694
+ )
695
+ caption = f"{index}. {best.text}" if best.text else f"{index}. <empty>"
696
+ gallery.append((crop, caption))
697
+
698
+ boxes = [detection.box for detection in ordered_detections]
699
+ annotated = _draw_boxes(normalized, boxes)
700
+ annotated_path: str | None = None
701
+ if artifact_dir is not None:
702
+ page_dir = artifact_dir / "annotated-pages"
703
+ page_dir.mkdir(parents=True, exist_ok=True)
704
+ target = page_dir / f"page-{page_index + 1:04d}.png"
705
+ annotated.save(target)
706
+ annotated_path = str(target.relative_to(artifact_dir))
707
+ return {
708
+ "text": "\n".join(lines),
709
+ "annotated": annotated,
710
+ "annotated_path": annotated_path,
711
+ "gallery": gallery,
712
+ "rows": rows,
713
+ "line_count": len(ordered_detections),
714
+ "detector": "PaddleOCR PP-OCRv5 full-page text detector",
715
+ "reading_order": reading_order,
716
+ "seed_recognizer": seed_recognizer,
717
+ "text_det_limit_side_len": text_det_limit_side_len,
718
+ "text_det_thresh": text_det_thresh,
719
+ "text_det_box_thresh": text_det_box_thresh,
720
+ "text_det_unclip_ratio": text_det_unclip_ratio,
721
+ }
722
+
723
+
724
+ def recognize_page_paddle(
725
+ image: Image.Image | None,
726
+ mode: str,
727
+ seed_recognizer: str,
728
+ reading_order: str,
729
+ text_det_limit_side_len: int,
730
+ text_det_thresh: float,
731
+ text_det_box_thresh: float,
732
+ text_det_unclip_ratio: float,
733
+ crop_padding: int,
734
+ max_lines: int,
735
+ ) -> tuple[str, Image.Image | None, list[tuple[Image.Image, str]], str]:
736
+ if image is None:
737
+ return "", None, [], "Upload a page image."
738
+
739
+ result = _recognize_page_paddle_core(
740
+ image,
741
+ mode=mode,
742
+ seed_recognizer=seed_recognizer,
743
+ reading_order=reading_order,
744
+ text_det_limit_side_len=text_det_limit_side_len,
745
+ text_det_thresh=text_det_thresh,
746
+ text_det_box_thresh=text_det_box_thresh,
747
+ text_det_unclip_ratio=text_det_unclip_ratio,
748
+ crop_padding=crop_padding,
749
+ max_lines=max_lines,
750
+ )
751
+ details = {
752
+ "detector": result["detector"],
753
+ "reading_order": result["reading_order"],
754
+ "line_count": result["line_count"],
755
+ "recognition_mode": mode,
756
+ "paddle_seed_recognizer": result["seed_recognizer"],
757
+ "paddle": {
758
+ "text_det_limit_side_len": result["text_det_limit_side_len"],
759
+ "text_det_thresh": result["text_det_thresh"],
760
+ "text_det_box_thresh": result["text_det_box_thresh"],
761
+ "text_det_unclip_ratio": result["text_det_unclip_ratio"],
762
+ },
763
+ "segmentation": {
764
+ "crop_padding": crop_padding,
765
+ },
766
+ "rows": result["rows"],
767
+ }
768
+ return result["text"], result["annotated"], result["gallery"], json.dumps(details, ensure_ascii=False, indent=2)
769
+
770
+
771
+ def recognize_page(
772
+ image: Image.Image | None,
773
+ mode: str,
774
+ reading_order: str,
775
+ threshold: int,
776
+ horizontal_kernel: int,
777
+ vertical_kernel: int,
778
+ dilation_iterations: int,
779
+ min_width: int,
780
+ min_height: int,
781
+ max_height: int,
782
+ min_area: int,
783
+ crop_padding: int,
784
+ max_lines: int,
785
+ ) -> tuple[str, Image.Image | None, list[tuple[Image.Image, str]], str]:
786
+ if image is None:
787
+ return "", None, [], "Upload a page image."
788
+
789
+ result = _recognize_page_core(
790
+ image,
791
+ mode=mode,
792
+ reading_order=reading_order,
793
+ threshold=threshold,
794
+ horizontal_kernel=horizontal_kernel,
795
+ vertical_kernel=vertical_kernel,
796
+ dilation_iterations=dilation_iterations,
797
+ min_width=min_width,
798
+ min_height=min_height,
799
+ max_height=max_height,
800
+ min_area=min_area,
801
+ crop_padding=crop_padding,
802
+ max_lines=max_lines,
803
+ )
804
+ details = {
805
+ "detector": result["detector"],
806
+ "reading_order": result["reading_order"],
807
+ "line_count": result["line_count"],
808
+ "recognition_mode": mode,
809
+ "segmentation": {
810
+ "threshold": result["threshold"],
811
+ "horizontal_kernel": result["horizontal_kernel"],
812
+ "vertical_kernel": result["vertical_kernel"],
813
+ "dilation_iterations": result["dilation_iterations"],
814
+ "min_width": min_width,
815
+ "min_height": min_height,
816
+ "max_height": max_height,
817
+ "min_area": min_area,
818
+ "crop_padding": crop_padding,
819
+ },
820
+ "rows": result["rows"],
821
+ }
822
+ return result["text"], result["annotated"], result["gallery"], json.dumps(details, ensure_ascii=False, indent=2)
823
+
824
+
825
+ def recognize_page_yolo(
826
+ image: Image.Image | None,
827
+ mode: str,
828
+ detector_confidence: float,
829
+ reading_order: str,
830
+ crop_padding: int,
831
+ max_lines: int,
832
+ ) -> tuple[str, Image.Image | None, list[tuple[Image.Image, str]], str]:
833
+ if image is None:
834
+ return "", None, [], "Upload a page image."
835
+
836
+ normalized = _normalize_image(image)
837
+ page_path = _save_temp_image(normalized)
838
+ try:
839
+ boxes = _detect_boxes(page_path, detector_confidence)
840
+ finally:
841
+ page_path.unlink(missing_ok=True)
842
+
843
+ boxes = _order_boxes(boxes, reading_order, normalized.width)
844
+ if max_lines > 0:
845
+ boxes = boxes[: int(max_lines)]
846
+
847
+ lines: list[str] = []
848
+ gallery: list[tuple[Image.Image, str]] = []
849
+ rows: list[dict[str, Any]] = []
850
+ with tempfile.TemporaryDirectory(prefix="limbu-page-ocr-") as tmp_dir_name:
851
+ tmp_dir = Path(tmp_dir_name)
852
+ for index, box in enumerate(boxes, start=1):
853
+ crop = _crop_box(normalized, box, int(crop_padding))
854
+ crop_path = tmp_dir / f"line-{index:04d}.png"
855
+ crop.save(crop_path)
856
+ best, candidates = _recognize_line_path(crop_path, mode)
857
+ if best.text:
858
+ lines.append(best.text)
859
+ rows.append(
860
+ {
861
+ "index": index,
862
+ "box": box.as_dict(),
863
+ "selected_engine": best.engine,
864
+ "selected_confidence": best.confidence,
865
+ "text": best.text,
866
+ "candidates": [candidate.as_dict() for candidate in candidates],
867
+ }
868
+ )
869
+ caption = f"{index}. {best.text}" if best.text else f"{index}. <empty>"
870
+ gallery.append((crop, caption))
871
+
872
+ text = "\n".join(lines)
873
+ annotated = _draw_boxes(normalized, boxes)
874
+ details = {
875
+ "detector": "YOLO cdc-grade1-allbooks-v1",
876
+ "detector_confidence": detector_confidence,
877
+ "reading_order": reading_order,
878
+ "line_count": len(boxes),
879
+ "recognition_mode": mode,
880
+ "rows": rows,
881
+ "warning": "Prototype page OCR. Detector was trained on rendered/augmented CDC pages; verify output before using it as text.",
882
+ }
883
+ return text, annotated, gallery, json.dumps(details, ensure_ascii=False, indent=2)
884
+
885
+
886
+ def _uploaded_path(item: object) -> Path:
887
+ if isinstance(item, str | Path):
888
+ return Path(item)
889
+ name = getattr(item, "name", None)
890
+ if isinstance(name, str):
891
+ return Path(name)
892
+ path = getattr(item, "path", None)
893
+ if isinstance(path, str):
894
+ return Path(path)
895
+ raise ValueError(f"unsupported uploaded file object: {type(item).__name__}")
896
+
897
+
898
+ def _render_pdf_pages(path: Path, *, dpi: int, max_pages: int) -> list[tuple[str, Image.Image]]:
899
+ try:
900
+ import pypdfium2 as pdfium
901
+ except ImportError as exc:
902
+ raise RuntimeError("PDF input requires pypdfium2 in the Space runtime") from exc
903
+ pages: list[tuple[str, Image.Image]] = []
904
+ pdf = pdfium.PdfDocument(str(path))
905
+ limit = len(pdf) if max_pages <= 0 else min(len(pdf), max_pages)
906
+ scale = max(72, int(dpi)) / 72.0
907
+ for page_index in range(limit):
908
+ page = pdf[page_index]
909
+ bitmap = page.render(scale=scale)
910
+ pages.append((f"{path.name}:p{page_index + 1}", bitmap.to_pil().convert("RGB")))
911
+ return pages
912
+
913
+
914
+ def _load_book_pages(files: list[object] | None, *, pdf_dpi: int, max_pages: int) -> list[tuple[str, Image.Image]]:
915
+ if not files:
916
+ return []
917
+ pages: list[tuple[str, Image.Image]] = []
918
+ for item in files:
919
+ path = _uploaded_path(item)
920
+ suffix = path.suffix.lower()
921
+ remaining = 0 if max_pages <= 0 else max_pages - len(pages)
922
+ if max_pages > 0 and remaining <= 0:
923
+ break
924
+ if suffix == ".pdf":
925
+ pages.extend(_render_pdf_pages(path, dpi=pdf_dpi, max_pages=remaining))
926
+ continue
927
+ if suffix not in {".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tif", ".tiff"}:
928
+ raise ValueError(f"unsupported book input file type: {path.name}")
929
+ with Image.open(path) as image:
930
+ pages.append((path.name, _normalize_image(image)))
931
+ return pages[:max_pages] if max_pages > 0 else pages
932
+
933
+
934
+ def _zip_directory(source_dir: Path) -> Path:
935
+ zip_path = Path(tempfile.NamedTemporaryFile(prefix="limbu-book-ocr-", suffix=".zip", delete=False).name)
936
+ with zipfile.ZipFile(zip_path, "w", compression=zipfile.ZIP_DEFLATED) as archive:
937
+ for path in sorted(source_dir.rglob("*")):
938
+ if path.is_file():
939
+ archive.write(path, path.relative_to(source_dir))
940
+ return zip_path
941
+
942
+
943
+ def recognize_book_paddle(
944
+ files: list[object] | None,
945
+ mode: str,
946
+ seed_recognizer: str,
947
+ reading_order: str,
948
+ text_det_limit_side_len: int,
949
+ text_det_thresh: float,
950
+ text_det_box_thresh: float,
951
+ text_det_unclip_ratio: float,
952
+ crop_padding: int,
953
+ max_lines_per_page: int,
954
+ max_pages: int,
955
+ pdf_dpi: int,
956
+ ) -> tuple[str, str | None, str]:
957
+ pages = _load_book_pages(files, pdf_dpi=pdf_dpi, max_pages=max_pages)
958
+ if not pages:
959
+ return "", None, "Upload page images or a PDF."
960
+
961
+ artifact_dir = Path(tempfile.mkdtemp(prefix="limbu-book-paddle-artifacts-"))
962
+ page_payloads: list[dict[str, Any]] = []
963
+ page_texts: list[str] = []
964
+ try:
965
+ for page_index, (source_name, image) in enumerate(pages):
966
+ page_result = _recognize_page_paddle_core(
967
+ image,
968
+ mode=mode,
969
+ seed_recognizer=seed_recognizer,
970
+ reading_order=reading_order,
971
+ text_det_limit_side_len=text_det_limit_side_len,
972
+ text_det_thresh=text_det_thresh,
973
+ text_det_box_thresh=text_det_box_thresh,
974
+ text_det_unclip_ratio=text_det_unclip_ratio,
975
+ crop_padding=crop_padding,
976
+ max_lines=max_lines_per_page,
977
+ artifact_dir=artifact_dir,
978
+ page_index=page_index,
979
+ )
980
+ page_text = page_result["text"]
981
+ page_texts.append(page_text)
982
+ page_payloads.append(
983
+ {
984
+ "page_index": page_index,
985
+ "source_name": source_name,
986
+ "line_count": page_result["line_count"],
987
+ "annotated_path": page_result["annotated_path"],
988
+ "text": page_text,
989
+ "lines": page_result["rows"],
990
+ }
991
+ )
992
+ book_text = "\n\n".join(text for text in page_texts if text.strip())
993
+ payload = {
994
+ "created_at_utc": datetime.now(UTC).isoformat(),
995
+ "system": "limbu-book-ocr-paddle-v1",
996
+ "detector": "PaddleOCR PP-OCRv5 full-page text detector",
997
+ "recognition_mode": mode,
998
+ "paddle_seed_recognizer": seed_recognizer,
999
+ "reading_order": reading_order,
1000
+ "page_count": len(page_payloads),
1001
+ "line_count": sum(int(page["line_count"]) for page in page_payloads),
1002
+ "paddle": {
1003
+ "text_det_limit_side_len": text_det_limit_side_len,
1004
+ "text_det_thresh": text_det_thresh,
1005
+ "text_det_box_thresh": text_det_box_thresh,
1006
+ "text_det_unclip_ratio": text_det_unclip_ratio,
1007
+ },
1008
+ "segmentation": {
1009
+ "crop_padding": crop_padding,
1010
+ },
1011
+ "pages": page_payloads,
1012
+ }
1013
+ (artifact_dir / "book.txt").write_text(book_text + ("\n" if book_text else ""), encoding="utf-8")
1014
+ (artifact_dir / "book.json").write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
1015
+ zip_path = _zip_directory(artifact_dir)
1016
+ finally:
1017
+ shutil.rmtree(artifact_dir, ignore_errors=True)
1018
+
1019
+ details = {
1020
+ "page_count": len(page_payloads),
1021
+ "line_count": sum(int(page["line_count"]) for page in page_payloads),
1022
+ "artifact_zip": str(zip_path),
1023
+ "warning": "Paddle-first operational OCR output with crop evidence; benchmark claims still require frozen-page scoring.",
1024
+ }
1025
+ return book_text, str(zip_path), json.dumps(details, ensure_ascii=False, indent=2)
1026
+
1027
+
1028
+ def recognize_book(
1029
+ files: list[object] | None,
1030
+ mode: str,
1031
+ reading_order: str,
1032
+ threshold: int,
1033
+ horizontal_kernel: int,
1034
+ vertical_kernel: int,
1035
+ dilation_iterations: int,
1036
+ min_width: int,
1037
+ min_height: int,
1038
+ max_height: int,
1039
+ min_area: int,
1040
+ crop_padding: int,
1041
+ max_lines_per_page: int,
1042
+ max_pages: int,
1043
+ pdf_dpi: int,
1044
+ ) -> tuple[str, str | None, str]:
1045
+ pages = _load_book_pages(files, pdf_dpi=pdf_dpi, max_pages=max_pages)
1046
+ if not pages:
1047
+ return "", None, "Upload page images or a PDF."
1048
+
1049
+ artifact_dir = Path(tempfile.mkdtemp(prefix="limbu-book-ocr-artifacts-"))
1050
+ page_payloads: list[dict[str, Any]] = []
1051
+ page_texts: list[str] = []
1052
+ try:
1053
+ for page_index, (source_name, image) in enumerate(pages):
1054
+ page_result = _recognize_page_core(
1055
+ image,
1056
+ mode=mode,
1057
+ reading_order=reading_order,
1058
+ threshold=threshold,
1059
+ horizontal_kernel=horizontal_kernel,
1060
+ vertical_kernel=vertical_kernel,
1061
+ dilation_iterations=dilation_iterations,
1062
+ min_width=min_width,
1063
+ min_height=min_height,
1064
+ max_height=max_height,
1065
+ min_area=min_area,
1066
+ crop_padding=crop_padding,
1067
+ max_lines=max_lines_per_page,
1068
+ artifact_dir=artifact_dir,
1069
+ page_index=page_index,
1070
+ )
1071
+ page_text = page_result["text"]
1072
+ page_texts.append(page_text)
1073
+ page_payloads.append(
1074
+ {
1075
+ "page_index": page_index,
1076
+ "source_name": source_name,
1077
+ "line_count": page_result["line_count"],
1078
+ "annotated_path": page_result["annotated_path"],
1079
+ "text": page_text,
1080
+ "lines": page_result["rows"],
1081
+ }
1082
+ )
1083
+ book_text = "\n\n".join(text for text in page_texts if text.strip())
1084
+ payload = {
1085
+ "created_at_utc": datetime.now(UTC).isoformat(),
1086
+ "system": "limbu-book-ocr-v1",
1087
+ "detector": "deterministic image-line projection",
1088
+ "recognition_mode": mode,
1089
+ "reading_order": reading_order,
1090
+ "page_count": len(page_payloads),
1091
+ "line_count": sum(int(page["line_count"]) for page in page_payloads),
1092
+ "segmentation": {
1093
+ "threshold": threshold,
1094
+ "horizontal_kernel": horizontal_kernel,
1095
+ "vertical_kernel": vertical_kernel,
1096
+ "dilation_iterations": dilation_iterations,
1097
+ "min_width": min_width,
1098
+ "min_height": min_height,
1099
+ "max_height": max_height,
1100
+ "min_area": min_area,
1101
+ "crop_padding": crop_padding,
1102
+ },
1103
+ "pages": page_payloads,
1104
+ }
1105
+ (artifact_dir / "book.txt").write_text(book_text + ("\n" if book_text else ""), encoding="utf-8")
1106
+ (artifact_dir / "book.json").write_text(json.dumps(payload, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
1107
+ zip_path = _zip_directory(artifact_dir)
1108
+ finally:
1109
+ shutil.rmtree(artifact_dir, ignore_errors=True)
1110
+
1111
+ details = {
1112
+ "page_count": len(page_payloads),
1113
+ "line_count": sum(int(page["line_count"]) for page in page_payloads),
1114
+ "artifact_zip": str(zip_path),
1115
+ "warning": "This is operational OCR output with crop evidence, not a benchmark-quality claim.",
1116
+ }
1117
+ return book_text, str(zip_path), json.dumps(details, ensure_ascii=False, indent=2)
1118
+
1119
+
1120
+ def health() -> str:
1121
+ status: dict[str, Any] = {
1122
+ "sirijonga_model": SIRIJONGA_MODEL_DIR.exists(),
1123
+ "devanagari_model": DEVANAGARI_MODEL_DIR.exists(),
1124
+ "line_detector": DETECTOR_MODEL_PATH.exists(),
1125
+ "paddle_device": os.environ.get("LIMBU_OCR_PADDLE_DEVICE", "cpu"),
1126
+ }
1127
+ try:
1128
+ _ensure_models_present()
1129
+ status["artifact_check"] = "pass"
1130
+ except Exception as exc:
1131
+ status["artifact_check"] = f"fail: {type(exc).__name__}: {exc}"
1132
+ return json.dumps(status, ensure_ascii=False, indent=2)
1133
+
1134
+
1135
+ CSS = """
1136
+ .output-markdown textarea { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; }
1137
+ """
1138
+
1139
+
1140
+ with gr.Blocks(title="Limbu OCR", css=CSS) as demo:
1141
+ gr.Markdown(
1142
+ """
1143
+ # Limbu OCR
1144
+
1145
+ OCR for Sirijonga Limbu and Devanagari-written Limbu. The primary page and book
1146
+ paths use PaddleOCR full-page text detection with the bundled Limbu recognizers, and
1147
+ preserve crop evidence beside Unicode output.
1148
+ """
1149
+ )
1150
+
1151
+ with gr.Tab("Line OCR"):
1152
+ with gr.Row():
1153
+ line_image = gr.Image(type="pil", label="Cropped text line")
1154
+ with gr.Column():
1155
+ line_mode = gr.Radio(["Auto", "Sirijonga", "Devanagari"], value="Auto", label="Recognizer")
1156
+ line_button = gr.Button("Recognize Line", variant="primary")
1157
+ line_text = gr.Textbox(label="Unicode text", lines=4)
1158
+ line_details = gr.Code(label="Details", language="json")
1159
+ line_button.click(recognize_line, inputs=[line_image, line_mode], outputs=[line_text, line_details])
1160
+
1161
+ with gr.Tab("Paddle Page OCR"):
1162
+ with gr.Row():
1163
+ paddle_page_image = gr.Image(type="pil", label="Page image")
1164
+ with gr.Column():
1165
+ paddle_page_mode = gr.Radio(["Auto", "Sirijonga", "Devanagari"], value="Auto", label="Recognizer")
1166
+ paddle_page_seed = gr.Radio(["Sirijonga", "Devanagari"], value="Sirijonga", label="Paddle seed recognizer")
1167
+ paddle_page_order = gr.Radio(
1168
+ ["Auto", "Rows LTR", "Columns LTR", "Columns RTL"],
1169
+ value="Rows LTR",
1170
+ label="Reading order",
1171
+ )
1172
+ paddle_page_limit = gr.Slider(512, 2048, value=1280, step=64, label="Paddle detector side limit")
1173
+ paddle_page_thresh = gr.Slider(0.10, 0.90, value=0.30, step=0.05, label="Paddle det threshold")
1174
+ paddle_page_box_thresh = gr.Slider(0.10, 0.90, value=0.60, step=0.05, label="Paddle box threshold")
1175
+ paddle_page_unclip = gr.Slider(1.0, 3.0, value=1.5, step=0.1, label="Paddle unclip ratio")
1176
+ paddle_page_crop_padding = gr.Slider(0, 32, value=12, step=1, label="Crop padding")
1177
+ paddle_page_max_lines = gr.Slider(0, 300, value=0, step=1, label="Max lines (0 = no cap)")
1178
+ paddle_page_button = gr.Button("Run Paddle Page OCR", variant="primary")
1179
+ paddle_page_text = gr.Textbox(label="Draft Unicode text", lines=16)
1180
+ paddle_page_annotated = gr.Image(type="pil", label="Paddle detected lines")
1181
+ paddle_page_gallery = gr.Gallery(label="Line crops", columns=2, height=420)
1182
+ paddle_page_details = gr.Code(label="Details", language="json")
1183
+ paddle_page_button.click(
1184
+ recognize_page_paddle,
1185
+ inputs=[
1186
+ paddle_page_image,
1187
+ paddle_page_mode,
1188
+ paddle_page_seed,
1189
+ paddle_page_order,
1190
+ paddle_page_limit,
1191
+ paddle_page_thresh,
1192
+ paddle_page_box_thresh,
1193
+ paddle_page_unclip,
1194
+ paddle_page_crop_padding,
1195
+ paddle_page_max_lines,
1196
+ ],
1197
+ outputs=[paddle_page_text, paddle_page_annotated, paddle_page_gallery, paddle_page_details],
1198
+ )
1199
+
1200
+ with gr.Tab("Paddle Book OCR"):
1201
+ with gr.Row():
1202
+ paddle_book_files = gr.Files(
1203
+ label="Page images or PDF",
1204
+ file_types=[".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tif", ".tiff", ".pdf"],
1205
+ )
1206
+ with gr.Column():
1207
+ paddle_book_mode = gr.Radio(["Auto", "Sirijonga", "Devanagari"], value="Auto", label="Recognizer")
1208
+ paddle_book_seed = gr.Radio(["Sirijonga", "Devanagari"], value="Sirijonga", label="Paddle seed recognizer")
1209
+ paddle_book_order = gr.Radio(
1210
+ ["Auto", "Rows LTR", "Columns LTR", "Columns RTL"],
1211
+ value="Rows LTR",
1212
+ label="Reading order",
1213
+ )
1214
+ paddle_book_limit = gr.Slider(512, 2048, value=1280, step=64, label="Paddle detector side limit")
1215
+ paddle_book_thresh = gr.Slider(0.10, 0.90, value=0.30, step=0.05, label="Paddle det threshold")
1216
+ paddle_book_box_thresh = gr.Slider(0.10, 0.90, value=0.60, step=0.05, label="Paddle box threshold")
1217
+ paddle_book_unclip = gr.Slider(1.0, 3.0, value=1.5, step=0.1, label="Paddle unclip ratio")
1218
+ paddle_book_crop_padding = gr.Slider(0, 32, value=12, step=1, label="Crop padding")
1219
+ paddle_book_max_lines = gr.Slider(0, 300, value=0, step=1, label="Max lines per page (0 = no cap)")
1220
+ paddle_book_max_pages = gr.Slider(0, 80, value=20, step=1, label="Max pages (0 = no cap)")
1221
+ paddle_book_pdf_dpi = gr.Slider(120, 360, value=240, step=10, label="PDF render DPI")
1222
+ paddle_book_button = gr.Button("Run Paddle Book OCR", variant="primary")
1223
+ paddle_book_text = gr.Textbox(label="Unicode text", lines=18)
1224
+ paddle_book_zip = gr.File(label="OCR artifact zip")
1225
+ paddle_book_details = gr.Code(label="Details", language="json")
1226
+ paddle_book_button.click(
1227
+ recognize_book_paddle,
1228
+ inputs=[
1229
+ paddle_book_files,
1230
+ paddle_book_mode,
1231
+ paddle_book_seed,
1232
+ paddle_book_order,
1233
+ paddle_book_limit,
1234
+ paddle_book_thresh,
1235
+ paddle_book_box_thresh,
1236
+ paddle_book_unclip,
1237
+ paddle_book_crop_padding,
1238
+ paddle_book_max_lines,
1239
+ paddle_book_max_pages,
1240
+ paddle_book_pdf_dpi,
1241
+ ],
1242
+ outputs=[paddle_book_text, paddle_book_zip, paddle_book_details],
1243
+ )
1244
+
1245
+ with gr.Tab("Image-Line Page Fallback"):
1246
+ with gr.Row():
1247
+ page_image = gr.Image(type="pil", label="Page image")
1248
+ with gr.Column():
1249
+ page_mode = gr.Radio(["Auto", "Sirijonga", "Devanagari"], value="Auto", label="Recognizer")
1250
+ reading_order = gr.Radio(
1251
+ ["Auto", "Rows LTR", "Columns LTR", "Columns RTL"],
1252
+ value="Auto",
1253
+ label="Reading order",
1254
+ )
1255
+ threshold = gr.Slider(0, 255, value=0, step=1, label="Ink threshold (0 = Otsu)")
1256
+ horizontal_kernel = gr.Slider(3, 81, value=23, step=2, label="Horizontal merge")
1257
+ vertical_kernel = gr.Slider(1, 15, value=3, step=1, label="Vertical merge")
1258
+ dilation_iterations = gr.Slider(1, 4, value=1, step=1, label="Dilation passes")
1259
+ min_width = gr.Slider(5, 300, value=35, step=5, label="Min line width")
1260
+ min_height = gr.Slider(4, 80, value=10, step=1, label="Min line height")
1261
+ max_height = gr.Slider(0, 260, value=140, step=5, label="Max line height (0 = no cap)")
1262
+ min_area = gr.Slider(20, 5000, value=100, step=20, label="Min line area")
1263
+ crop_padding = gr.Slider(0, 32, value=12, step=1, label="Crop padding")
1264
+ max_lines = gr.Slider(0, 300, value=180, step=1, label="Max lines (0 = no cap)")
1265
+ page_button = gr.Button("Run Image-Line Page OCR", variant="secondary")
1266
+ page_text = gr.Textbox(label="Draft Unicode text", lines=16)
1267
+ annotated_image = gr.Image(type="pil", label="Detected lines")
1268
+ crop_gallery = gr.Gallery(label="Line crops", columns=2, height=420)
1269
+ page_details = gr.Code(label="Details", language="json")
1270
+ page_button.click(
1271
+ recognize_page,
1272
+ inputs=[
1273
+ page_image,
1274
+ page_mode,
1275
+ reading_order,
1276
+ threshold,
1277
+ horizontal_kernel,
1278
+ vertical_kernel,
1279
+ dilation_iterations,
1280
+ min_width,
1281
+ min_height,
1282
+ max_height,
1283
+ min_area,
1284
+ crop_padding,
1285
+ max_lines,
1286
+ ],
1287
+ outputs=[page_text, annotated_image, crop_gallery, page_details],
1288
+ )
1289
+
1290
+ with gr.Tab("Image-Line Book Fallback"):
1291
+ with gr.Row():
1292
+ book_files = gr.Files(
1293
+ label="Page images or PDF",
1294
+ file_types=[".png", ".jpg", ".jpeg", ".webp", ".bmp", ".tif", ".tiff", ".pdf"],
1295
+ )
1296
+ with gr.Column():
1297
+ book_mode = gr.Radio(["Auto", "Sirijonga", "Devanagari"], value="Auto", label="Recognizer")
1298
+ book_order = gr.Radio(
1299
+ ["Auto", "Rows LTR", "Columns LTR", "Columns RTL"],
1300
+ value="Auto",
1301
+ label="Reading order",
1302
+ )
1303
+ book_threshold = gr.Slider(0, 255, value=0, step=1, label="Ink threshold (0 = Otsu)")
1304
+ book_horizontal_kernel = gr.Slider(3, 81, value=23, step=2, label="Horizontal merge")
1305
+ book_vertical_kernel = gr.Slider(1, 15, value=3, step=1, label="Vertical merge")
1306
+ book_dilation_iterations = gr.Slider(1, 4, value=1, step=1, label="Dilation passes")
1307
+ book_min_width = gr.Slider(5, 300, value=35, step=5, label="Min line width")
1308
+ book_min_height = gr.Slider(4, 80, value=10, step=1, label="Min line height")
1309
+ book_max_height = gr.Slider(0, 260, value=140, step=5, label="Max line height (0 = no cap)")
1310
+ book_min_area = gr.Slider(20, 5000, value=100, step=20, label="Min line area")
1311
+ book_crop_padding = gr.Slider(0, 32, value=12, step=1, label="Crop padding")
1312
+ book_max_lines = gr.Slider(0, 300, value=180, step=1, label="Max lines per page (0 = no cap)")
1313
+ book_max_pages = gr.Slider(0, 80, value=20, step=1, label="Max pages (0 = no cap)")
1314
+ pdf_dpi = gr.Slider(120, 360, value=240, step=10, label="PDF render DPI")
1315
+ book_button = gr.Button("Run Image-Line Book OCR", variant="secondary")
1316
+ book_text = gr.Textbox(label="Unicode text", lines=18)
1317
+ book_zip = gr.File(label="OCR artifact zip")
1318
+ book_details = gr.Code(label="Details", language="json")
1319
+ book_button.click(
1320
+ recognize_book,
1321
+ inputs=[
1322
+ book_files,
1323
+ book_mode,
1324
+ book_order,
1325
+ book_threshold,
1326
+ book_horizontal_kernel,
1327
+ book_vertical_kernel,
1328
+ book_dilation_iterations,
1329
+ book_min_width,
1330
+ book_min_height,
1331
+ book_max_height,
1332
+ book_min_area,
1333
+ book_crop_padding,
1334
+ book_max_lines,
1335
+ book_max_pages,
1336
+ pdf_dpi,
1337
+ ],
1338
+ outputs=[book_text, book_zip, book_details],
1339
+ )
1340
+
1341
+ with gr.Tab("YOLO Probe"):
1342
+ with gr.Row():
1343
+ yolo_image = gr.Image(type="pil", label="Page image")
1344
+ with gr.Column():
1345
+ yolo_mode = gr.Radio(["Auto", "Sirijonga", "Devanagari"], value="Auto", label="Recognizer")
1346
+ detector_conf = gr.Slider(0.05, 0.90, value=0.50, step=0.05, label="Detector confidence")
1347
+ yolo_order = gr.Radio(
1348
+ ["Rows LTR", "Two columns LTR", "Two columns RTL"],
1349
+ value="Rows LTR",
1350
+ label="Reading order",
1351
+ )
1352
+ yolo_crop_padding = gr.Slider(0, 24, value=6, step=1, label="Crop padding")
1353
+ yolo_max_lines = gr.Slider(0, 200, value=120, step=1, label="Max lines (0 = no cap)")
1354
+ yolo_button = gr.Button("Run YOLO Probe", variant="secondary")
1355
+ yolo_text = gr.Textbox(label="Draft Unicode text", lines=16)
1356
+ yolo_annotated = gr.Image(type="pil", label="Detected lines")
1357
+ yolo_gallery = gr.Gallery(label="Line crops", columns=2, height=420)
1358
+ yolo_details = gr.Code(label="Details", language="json")
1359
+ yolo_button.click(
1360
+ recognize_page_yolo,
1361
+ inputs=[yolo_image, yolo_mode, detector_conf, yolo_order, yolo_crop_padding, yolo_max_lines],
1362
+ outputs=[yolo_text, yolo_annotated, yolo_gallery, yolo_details],
1363
+ )
1364
+
1365
+ with gr.Tab("Runtime"):
1366
+ health_button = gr.Button("Check OCR Runtime")
1367
+ health_output = gr.Code(label="Status", language="json")
1368
+ health_button.click(health, outputs=health_output)
1369
+ demo.load(health, outputs=health_output)
1370
+
1371
+
1372
+ if __name__ == "__main__":
1373
+ demo.queue(max_size=8).launch()
k2-ampixa-demo-ocr/models/space/models/deva-v2/artifacts/devanagari-v2.txt ADDED
@@ -0,0 +1,143 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ !
2
+ "
3
+ %
4
+ '
5
+ (
6
+ )
7
+ +
8
+ ,
9
+ -
10
+ .
11
+ /
12
+ 0
13
+ 1
14
+ 2
15
+ 3
16
+ 7
17
+ :
18
+ ;
19
+ =
20
+ ?
21
+ A
22
+ C
23
+ D
24
+ N
25
+ O
26
+ [
27
+ ]
28
+ a
29
+ b
30
+ c
31
+ d
32
+ e
33
+ f
34
+ g
35
+ h
36
+ i
37
+ j
38
+ k
39
+ l
40
+ m
41
+ n
42
+ o
43
+ p
44
+ r
45
+ s
46
+ t
47
+ u
48
+ v
49
+ w
50
+ y
51
+ |
52
+ ¥
53
+ ÷
54
+ ʻ
55
+ ँ
56
+ ं
57
+ ः
58
+ अ
59
+ आ
60
+ इ
61
+ ई
62
+ उ
63
+ ऊ
64
+ ऋ
65
+ ऎ
66
+ ए
67
+ ऐ
68
+ ओ
69
+ औ
70
+ क
71
+ ख
72
+ ग
73
+ घ
74
+ ङ
75
+ च
76
+ छ
77
+ ज
78
+ झ
79
+ ञ
80
+ ट
81
+ ठ
82
+ ड
83
+ ढ
84
+ ण
85
+ त
86
+ थ
87
+ द
88
+ ध
89
+ न
90
+ ऩ
91
+ प
92
+ फ
93
+ ब
94
+ भ
95
+ म
96
+ य
97
+ र
98
+ ल
99
+ व
100
+ श
101
+ ष
102
+ स
103
+ ह
104
+ ़
105
+ ऽ
106
+ ा
107
+ ि
108
+ ी
109
+ ु
110
+ ू
111
+ ृ
112
+ ॆ
113
+ े
114
+ ै
115
+ ो
116
+ ौ
117
+ ्
118
+ ।
119
+ ०
120
+ १
121
+ २
122
+ ३
123
+ ४
124
+ ५
125
+ ६
126
+ ७
127
+ ८
128
+ ९
129
+ ॽ
130
+ ‌
131
+ ‍
132
+ –
133
+ —
134
+ ‘
135
+ ’
136
+ ‚
137
+ “
138
+ ”
139
+ …
140
+ √
141
+ ∶
142
+ ॅ
143
+ ॐ
k2-ampixa-demo-ocr/models/space/models/deva-v2/artifacts/inference.json ADDED
The diff for this file is too large to render. See raw diff
 
k2-ampixa-demo-ocr/models/space/models/deva-v2/artifacts/inference.pdiparams ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:32f5256f8a062830be4394606b8307c30dd9b9fb1be4e15af65606abffb479b4
3
+ size 7630503
k2-ampixa-demo-ocr/models/space/models/deva-v2/artifacts/inference.yml ADDED
@@ -0,0 +1,186 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Global:
2
+ model_name: PP-OCRv5_mobile_rec
3
+ Hpi:
4
+ backend_configs:
5
+ paddle_infer:
6
+ trt_dynamic_shapes: &id001
7
+ x:
8
+ - - 1
9
+ - 3
10
+ - 48
11
+ - 160
12
+ - - 1
13
+ - 3
14
+ - 48
15
+ - 320
16
+ - - 8
17
+ - 3
18
+ - 48
19
+ - 3200
20
+ tensorrt:
21
+ dynamic_shapes: *id001
22
+ PreProcess:
23
+ transform_ops:
24
+ - DecodeImage:
25
+ channel_first: false
26
+ img_mode: BGR
27
+ - MultiLabelEncode:
28
+ gtc_encode: NRTRLabelEncode
29
+ - RecResizeImg:
30
+ image_shape:
31
+ - 3
32
+ - 48
33
+ - 320
34
+ - KeepKeys:
35
+ keep_keys:
36
+ - image
37
+ - label_ctc
38
+ - label_gtc
39
+ - length
40
+ - valid_ratio
41
+ PostProcess:
42
+ name: CTCLabelDecode
43
+ character_dict:
44
+ - '!'
45
+ - '"'
46
+ - '%'
47
+ - ''''
48
+ - (
49
+ - )
50
+ - +
51
+ - ','
52
+ - '-'
53
+ - .
54
+ - /
55
+ - '0'
56
+ - '1'
57
+ - '2'
58
+ - '3'
59
+ - '7'
60
+ - ':'
61
+ - ;
62
+ - '='
63
+ - '?'
64
+ - A
65
+ - C
66
+ - D
67
+ - N
68
+ - O
69
+ - '['
70
+ - ']'
71
+ - a
72
+ - b
73
+ - c
74
+ - d
75
+ - e
76
+ - f
77
+ - g
78
+ - h
79
+ - i
80
+ - j
81
+ - k
82
+ - l
83
+ - m
84
+ - n
85
+ - o
86
+ - p
87
+ - r
88
+ - s
89
+ - t
90
+ - u
91
+ - v
92
+ - w
93
+ - y
94
+ - '|'
95
+ - ¥
96
+ - ÷
97
+ - ʻ
98
+ - ँ
99
+ - ं
100
+ - ः
101
+ - अ
102
+ - आ
103
+ - इ
104
+ - ई
105
+ - उ
106
+ - ऊ
107
+ - ऋ
108
+ - ऎ
109
+ - ए
110
+ - ऐ
111
+ - ओ
112
+ - औ
113
+ - क
114
+ - ख
115
+ - ग
116
+ - घ
117
+ - ङ
118
+ - च
119
+ - छ
120
+ - ज
121
+ - झ
122
+ - ञ
123
+ - ट
124
+ - ठ
125
+ - ड
126
+ - ढ
127
+ - ण
128
+ - त
129
+ - थ
130
+ - द
131
+ - ध
132
+ - न
133
+ - ऩ
134
+ - प
135
+ - फ
136
+ - ब
137
+ - भ
138
+ - म
139
+ - य
140
+ - र
141
+ - ल
142
+ - व
143
+ - श
144
+ - ष
145
+ - स
146
+ - ह
147
+ - ़
148
+ - ऽ
149
+ - ा
150
+ - ि
151
+ - ी
152
+ - ु
153
+ - ू
154
+ - ृ
155
+ - ॆ
156
+ - े
157
+ - ै
158
+ - ो
159
+ - ौ
160
+ - ्
161
+ - ।
162
+ - ०
163
+ - १
164
+ - २
165
+ - ३
166
+ - ४
167
+ - ५
168
+ - ६
169
+ - ७
170
+ - ८
171
+ - ९
172
+ - ॽ
173
+ - ‌
174
+ - ‍
175
+ - –
176
+ - —
177
+ - ‘
178
+ - ’
179
+ - ‚
180
+ - “
181
+ - ”
182
+ - …
183
+ - √
184
+ - ∶
185
+ - ॅ
186
+ - ॐ
k2-ampixa-demo-ocr/models/space/models/deva-v2/model-card.json ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_id": "deva-v2-reexport-2026-07-06",
3
+ "backend": "paddleocr",
4
+ "base_model": "deva-v2 training checkpoint (recovered from ashish-experiments 2026-07-05)",
5
+ "created_at_utc": "2026-07-06T07:52:10.588624+00:00",
6
+ "artifacts": [
7
+ {
8
+ "path": "artifacts/inference.json",
9
+ "required": true,
10
+ "sha256": "772e801de4c1cc260058f7c426ab618ef52ecc7ea51442886c8c8fc1606cb88b",
11
+ "kind": "file",
12
+ "size_bytes": 217712
13
+ },
14
+ {
15
+ "path": "artifacts/inference.pdiparams",
16
+ "required": true,
17
+ "sha256": "32f5256f8a062830be4394606b8307c30dd9b9fb1be4e15af65606abffb479b4",
18
+ "kind": "file",
19
+ "size_bytes": 7630503
20
+ },
21
+ {
22
+ "path": "artifacts/inference.yml",
23
+ "required": true,
24
+ "sha256": "4f76c7bd21ea18ec118adef93b98017b55a449072b35f1c3ea2baa3df217c8dc",
25
+ "kind": "file",
26
+ "size_bytes": 1812
27
+ },
28
+ {
29
+ "path": "artifacts/devanagari-v2.txt",
30
+ "required": true,
31
+ "sha256": "96806153df021c99b0195de9757d8fe6443c52f7c8a1c74bf76a5895c37b8c0b",
32
+ "kind": "file",
33
+ "size_bytes": 467
34
+ }
35
+ ],
36
+ "backend_kwargs": {
37
+ "lang": "devanagari",
38
+ "text_recognition_model_name": "PP-OCRv5_mobile_rec",
39
+ "text_recognition_model_dir": "artifacts",
40
+ "recognition_mode": "line"
41
+ },
42
+ "provenance": {},
43
+ "metrics": {},
44
+ "notes": "Re-export of the recovered deva-v2 training checkpoint with the correct 144-char devanagari-v2 dict; replaces the broken limbu-devanagari-ppocrv5 bundle whose truncated dict lacked anusvara/ii-matra/nna and emitted garbage. Verified on the recovered Newar frozen test: cp CER 0.0622 / grapheme 0.1312 / exact 0.158 - exact match to the recorded pre-crash baseline. Silver labels, diagnostic."
45
+ }
k2-ampixa-demo-ocr/models/space/models/devanagari-ft-v2/MODEL-SOURCE.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ devanagari-ft-v2 — fine-tuned from PaddleOCR devanagari_PP-OCRv5_mobile_rec (Apache-2.0), 2026-09-24.
2
+ Training: k2 MPS, transformers PPOCRV5MobileRecForTextRecognition, CTC; v1b (4,000 steps) then v2 (19,540 steps, frozen BN).
3
+ Data: Gorkhapatra real print chunks (internal-use owner determination 2026-07-22), OFL-font synthetic citizenship/office/real-text lines,
4
+ NVIDIA OCR-Synthetic-Multilingual-v1 English (CC BY 4.0), Liberation/DejaVu/Lato English renders. Code: ocr-tech tools/deva_rec_torch.
5
+ Served through PaddleX onnxruntime engine; inference.yml copied unchanged from PaddlePaddle/devanagari_PP-OCRv5_mobile_rec_onnx.
6
+ 69484bdc8dba4e956faf62f860587c4af8948eedb57dbba7409ab46376ad72aa inference.onnx
7
+ 9bd172dd26440c8ce94d1cde5d5baea6aefdc7cf3c5c8492e0beedef656d4e54 inference.yml
k2-ampixa-demo-ocr/models/space/models/devanagari-ft-v2/artifacts/inference.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:69484bdc8dba4e956faf62f860587c4af8948eedb57dbba7409ab46376ad72aa
3
+ size 22253444
k2-ampixa-demo-ocr/models/space/models/devanagari-ft-v2/artifacts/inference.yml ADDED
@@ -0,0 +1,611 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Global:
2
+ model_name: devanagari_PP-OCRv5_mobile_rec
3
+ Hpi:
4
+ backend_configs:
5
+ paddle_infer:
6
+ trt_dynamic_shapes: &id001
7
+ x:
8
+ - - 1
9
+ - 3
10
+ - 48
11
+ - 160
12
+ - - 1
13
+ - 3
14
+ - 48
15
+ - 320
16
+ - - 8
17
+ - 3
18
+ - 48
19
+ - 3200
20
+ tensorrt:
21
+ dynamic_shapes: *id001
22
+ PreProcess:
23
+ transform_ops:
24
+ - DecodeImage:
25
+ channel_first: false
26
+ img_mode: BGR
27
+ - MultiLabelEncode:
28
+ gtc_encode: NRTRLabelEncode
29
+ - RecResizeImg:
30
+ image_shape:
31
+ - 3
32
+ - 48
33
+ - 320
34
+ - KeepKeys:
35
+ keep_keys:
36
+ - image
37
+ - label_ctc
38
+ - label_gtc
39
+ - length
40
+ - valid_ratio
41
+ PostProcess:
42
+ name: CTCLabelDecode
43
+ character_dict:
44
+ - '!'
45
+ - '"'
46
+ - '#'
47
+ - $
48
+ - '%'
49
+ - '&'
50
+ - ''''
51
+ - (
52
+ - )
53
+ - '*'
54
+ - +
55
+ - ','
56
+ - '-'
57
+ - .
58
+ - /
59
+ - '0'
60
+ - '1'
61
+ - '2'
62
+ - '3'
63
+ - '4'
64
+ - '5'
65
+ - '6'
66
+ - '7'
67
+ - '8'
68
+ - '9'
69
+ - ':'
70
+ - ;
71
+ - <
72
+ - '='
73
+ - '>'
74
+ - '?'
75
+ - A
76
+ - B
77
+ - C
78
+ - D
79
+ - E
80
+ - F
81
+ - G
82
+ - H
83
+ - I
84
+ - J
85
+ - K
86
+ - L
87
+ - M
88
+ - N
89
+ - O
90
+ - P
91
+ - Q
92
+ - R
93
+ - S
94
+ - T
95
+ - U
96
+ - V
97
+ - W
98
+ - X
99
+ - Y
100
+ - Z
101
+ - '['
102
+ - ']'
103
+ - _
104
+ - '`'
105
+ - a
106
+ - b
107
+ - c
108
+ - d
109
+ - e
110
+ - f
111
+ - g
112
+ - h
113
+ - i
114
+ - j
115
+ - k
116
+ - l
117
+ - m
118
+ - n
119
+ - o
120
+ - p
121
+ - q
122
+ - r
123
+ - s
124
+ - t
125
+ - u
126
+ - v
127
+ - w
128
+ - x
129
+ - y
130
+ - z
131
+ - ©
132
+ - ‥
133
+ - '{'
134
+ - '}'
135
+ - \
136
+ - '|'
137
+ - '@'
138
+ - ^
139
+ - '~'
140
+ - ÷
141
+ - ∕
142
+ - ∙
143
+ - ⋅
144
+ - ·
145
+ - ±
146
+ - ∓
147
+ - ∩
148
+ - ∪
149
+ - □
150
+ - ←
151
+ - ↔
152
+ - ⇒
153
+ - ⇐
154
+ - ⇔
155
+ - ∀
156
+ - ∃
157
+ - ∄
158
+ - ∴
159
+ - ∵
160
+ - ∝
161
+ - ∞
162
+ - ⊥
163
+ - ∟
164
+ - ∠
165
+ - ∡
166
+ - ∢
167
+ - ′
168
+ - ″
169
+ - ∥
170
+ - ⊾
171
+ - ⊿
172
+ - ∂
173
+ - ∫
174
+ - ∬
175
+ - ∭
176
+ - ∮
177
+ - ∯
178
+ - ∰
179
+ - ∑
180
+ - ∏
181
+ - √
182
+ - ∛
183
+ - ∜
184
+ - ∱
185
+ - ∲
186
+ - ∳
187
+ - ∶
188
+ - ∷
189
+ - ∼
190
+ - ®
191
+ - ℉
192
+ - Ω
193
+ - ℧
194
+ - Å
195
+ - ⌀
196
+ - ℏ
197
+ - ⅀
198
+ - ⍺
199
+ - ⍵
200
+ - ¢
201
+ - €
202
+ - £
203
+ - ¥
204
+ - ₿
205
+ - Ⅰ
206
+ - Ⅱ
207
+ - Ⅲ
208
+ - Ⅳ
209
+ - Ⅴ
210
+ - Ⅵ
211
+ - Ⅶ
212
+ - Ⅷ
213
+ - Ⅸ
214
+ - Ⅹ
215
+ - Ⅺ
216
+ - Ⅻ
217
+ - ⅰ
218
+ - ⅱ
219
+ - ⅲ
220
+ - ⅳ
221
+ - ⅴ
222
+ - ⅵ
223
+ - ⅶ
224
+ - ⅷ
225
+ - ⅸ
226
+ - ⅹ
227
+ - ⅺ
228
+ - ⅻ
229
+ - ➀
230
+ - ➁
231
+ - ➂
232
+ - ➃
233
+ - ➄
234
+ - ➅
235
+ - ➆
236
+ - ➇
237
+ - ➈
238
+ - ➉
239
+ - ➊
240
+ - ➋
241
+ - ➌
242
+ - ➍
243
+ - ➎
244
+ - ➏
245
+ - ➐
246
+ - ➑
247
+ - ➒
248
+ - ➓
249
+ - ❶
250
+ - ❷
251
+ - ❸
252
+ - ❹
253
+ - ❺
254
+ - ❻
255
+ - ❼
256
+ - ❽
257
+ - ❾
258
+ - ❿
259
+ - ①
260
+ - ②
261
+ - ③
262
+ - ④
263
+ - ⑤
264
+ - ⑥
265
+ - ⑦
266
+ - ⑧
267
+ - ⑨
268
+ - ⑩
269
+ - ●
270
+ - ▶
271
+ - 𝑢
272
+ - ︽
273
+ - –
274
+ - ﹥
275
+ - 𝜓
276
+ - •
277
+ - ∋
278
+ - ƒ
279
+ - ⬆
280
+ - Ạ
281
+ - ◀
282
+ - 
283
+ - ▫
284
+ - ︾
285
+ - À
286
+ - Á
287
+ - Â
288
+ - Ã
289
+ - Ä
290
+ - Å
291
+ - Æ
292
+ - Ç
293
+ - È
294
+ - É
295
+ - Ê
296
+ - Ë
297
+ - Ì
298
+ - Í
299
+ - Î
300
+ - Ï
301
+ - Ð
302
+ - Ñ
303
+ - Ò
304
+ - Ó
305
+ - Ô
306
+ - Õ
307
+ - Ö
308
+ - Ø
309
+ - Ù
310
+ - Ú
311
+ - Û
312
+ - Ü
313
+ - Ý
314
+ - Þ
315
+ - à
316
+ - á
317
+ - â
318
+ - ã
319
+ - ä
320
+ - å
321
+ - æ
322
+ - ç
323
+ - è
324
+ - é
325
+ - ê
326
+ - ë
327
+ - ì
328
+ - í
329
+ - î
330
+ - ï
331
+ - ð
332
+ - ñ
333
+ - ò
334
+ - ó
335
+ - ô
336
+ - õ
337
+ - ö
338
+ - ø
339
+ - ù
340
+ - ú
341
+ - û
342
+ - ü
343
+ - ý
344
+ - þ
345
+ - ÿ
346
+ - ¡
347
+ - ¤
348
+ - ¦
349
+ - §
350
+ - ¨
351
+ - ª
352
+ - «
353
+ - ¬
354
+ - ¯
355
+ - °
356
+ - ²
357
+ - ³
358
+ - ´
359
+ - µ
360
+ - ¶
361
+ - ¸
362
+ - ¹
363
+ - º
364
+ - »
365
+ - ¼
366
+ - ½
367
+ - ¾
368
+ - ¿
369
+ - ×
370
+ - ‐
371
+ - ‑
372
+ - ‒
373
+ - —
374
+ - ―
375
+ - ‖
376
+ - ‗
377
+ - ‘
378
+ - ’
379
+ - ‚
380
+ - ‛
381
+ - “
382
+ - ”
383
+ - „
384
+ - ‟
385
+ - †
386
+ - ‡
387
+ - ‣
388
+ - ․
389
+ - …
390
+ - ‧
391
+ - ‰
392
+ - ‴
393
+ - ‵
394
+ - ‶
395
+ - ‷
396
+ - ‸
397
+ - ‹
398
+ - ›
399
+ - ※
400
+ - ‼
401
+ - ‽
402
+ - ‾
403
+ - ₤
404
+ - ₡
405
+ - ₹
406
+ - −
407
+ - ∖
408
+ - ∗
409
+ - ≈
410
+ - ≠
411
+ - ≡
412
+ - ≤
413
+ - ≥
414
+ - ⊂
415
+ - ⊃
416
+ - ↑
417
+ - →
418
+ - ↓
419
+ - ↕
420
+ - ™
421
+ - Ω
422
+ - ℮
423
+ - ∆
424
+ - ✓
425
+ - ✗
426
+ - ✘
427
+ - ▪
428
+ - ◼
429
+ - ✔
430
+ - ✕
431
+ - ☑
432
+ - ☒
433
+ - №
434
+ - ₽
435
+ - ₴
436
+ - Α
437
+ - α
438
+ - Β
439
+ - β
440
+ - Γ
441
+ - γ
442
+ - Δ
443
+ - δ
444
+ - Ε
445
+ - ε
446
+ - Ζ
447
+ - ζ
448
+ - Η
449
+ - η
450
+ - Θ
451
+ - θ
452
+ - Ι
453
+ - ι
454
+ - Κ
455
+ - κ
456
+ - Λ
457
+ - λ
458
+ - Μ
459
+ - μ
460
+ - Ν
461
+ - ν
462
+ - Ξ
463
+ - ξ
464
+ - Ο
465
+ - ο
466
+ - Π
467
+ - π
468
+ - Ρ
469
+ - ρ
470
+ - Σ
471
+ - σ
472
+ - ς
473
+ - Τ
474
+ - τ
475
+ - Υ
476
+ - υ
477
+ - Φ
478
+ - φ
479
+ - Χ
480
+ - χ
481
+ - Ψ
482
+ - ψ
483
+ - ω
484
+ - ऀ
485
+ - ँ
486
+ - ं
487
+ - ः
488
+ - ऄ
489
+ - अ
490
+ - आ
491
+ - इ
492
+ - ई
493
+ - उ
494
+ - ऊ
495
+ - ऋ
496
+ - ऌ
497
+ - ऍ
498
+ - ऎ
499
+ - ए
500
+ - ऐ
501
+ - ऑ
502
+ - ऒ
503
+ - ओ
504
+ - औ
505
+ - क
506
+ - ख
507
+ - ग
508
+ - घ
509
+ - ङ
510
+ - च
511
+ - छ
512
+ - ज
513
+ - झ
514
+ - ञ
515
+ - ट
516
+ - ठ
517
+ - ड
518
+ - ढ
519
+ - ण
520
+ - त
521
+ - थ
522
+ - द
523
+ - ध
524
+ - न
525
+ - ऩ
526
+ - प
527
+ - फ
528
+ - ब
529
+ - भ
530
+ - म
531
+ - य
532
+ - र
533
+ - ऱ
534
+ - ल
535
+ - ळ
536
+ - ऴ
537
+ - व
538
+ - श
539
+ - ष
540
+ - स
541
+ - ह
542
+ - ऺ
543
+ - ऻ
544
+ - ़
545
+ - ऽ
546
+ - ा
547
+ - ि
548
+ - ी
549
+ - ु
550
+ - ू
551
+ - ृ
552
+ - ॄ
553
+ - ॅ
554
+ - ॆ
555
+ - े
556
+ - ै
557
+ - ॉ
558
+ - ॊ
559
+ - ो
560
+ - ौ
561
+ - ्
562
+ - ॎ
563
+ - ॏ
564
+ - ॐ
565
+ - ॑
566
+ - ॒
567
+ - ॓
568
+ - ॔
569
+ - ॕ
570
+ - ॖ
571
+ - ॗ
572
+ - क़
573
+ - ख़
574
+ - ग़
575
+ - ज़
576
+ - ड़
577
+ - ढ़
578
+ - फ़
579
+ - य़
580
+ - ॠ
581
+ - ॡ
582
+ - ॢ
583
+ - ॣ
584
+ - ।
585
+ - ॥
586
+ - ०
587
+ - १
588
+ - २
589
+ - ३
590
+ - ४
591
+ - ५
592
+ - ६
593
+ - ७
594
+ - ८
595
+ - ९
596
+ - ॰
597
+ - ॱ
598
+ - ॲ
599
+ - ॳ
600
+ - ॴ
601
+ - ॵ
602
+ - ॶ
603
+ - ॷ
604
+ - ॸ
605
+ - ॹ
606
+ - ॺ
607
+ - ॻ
608
+ - ॼ
609
+ - ॽ
610
+ - ॾ
611
+ - ॿ
k2-ampixa-demo-ocr/models/space/models/devanagari-ft-v3/MODEL-SOURCE.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ devanagari-ft-v3 — fine-tuned from PaddleOCR devanagari_PP-OCRv5_mobile_rec (Apache-2.0), 2026-09-24.
2
+ Training: k2 MPS, transformers PPOCRV5MobileRecForTextRecognition, CTC; v1b (4,000 steps) then v2 (19,540 steps, frozen BN), then v3 (6,198 steps on converter-fixed labels: nayanepal j|m -> s| for क्र).
3
+ Data: Gorkhapatra real print chunks (internal-use owner determination 2026-07-22), OFL-font synthetic citizenship/office/real-text lines,
4
+ NVIDIA OCR-Synthetic-Multilingual-v1 English (CC BY 4.0), Liberation/DejaVu/Lato English renders. Code: ocr-tech tools/deva_rec_torch.
5
+ Served through PaddleX onnxruntime engine; inference.yml copied unchanged from PaddlePaddle/devanagari_PP-OCRv5_mobile_rec_onnx.
6
+ e6e46f8c2e43732a7b7eed7784e4827e112b9d24d2bba8079501473258e9350a inference.onnx
7
+ 9bd172dd26440c8ce94d1cde5d5baea6aefdc7cf3c5c8492e0beedef656d4e54 inference.yml
k2-ampixa-demo-ocr/models/space/models/devanagari-ft-v3/artifacts/inference.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e6e46f8c2e43732a7b7eed7784e4827e112b9d24d2bba8079501473258e9350a
3
+ size 22253444
k2-ampixa-demo-ocr/models/space/models/devanagari-ft-v3/artifacts/inference.yml ADDED
@@ -0,0 +1,611 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Global:
2
+ model_name: devanagari_PP-OCRv5_mobile_rec
3
+ Hpi:
4
+ backend_configs:
5
+ paddle_infer:
6
+ trt_dynamic_shapes: &id001
7
+ x:
8
+ - - 1
9
+ - 3
10
+ - 48
11
+ - 160
12
+ - - 1
13
+ - 3
14
+ - 48
15
+ - 320
16
+ - - 8
17
+ - 3
18
+ - 48
19
+ - 3200
20
+ tensorrt:
21
+ dynamic_shapes: *id001
22
+ PreProcess:
23
+ transform_ops:
24
+ - DecodeImage:
25
+ channel_first: false
26
+ img_mode: BGR
27
+ - MultiLabelEncode:
28
+ gtc_encode: NRTRLabelEncode
29
+ - RecResizeImg:
30
+ image_shape:
31
+ - 3
32
+ - 48
33
+ - 320
34
+ - KeepKeys:
35
+ keep_keys:
36
+ - image
37
+ - label_ctc
38
+ - label_gtc
39
+ - length
40
+ - valid_ratio
41
+ PostProcess:
42
+ name: CTCLabelDecode
43
+ character_dict:
44
+ - '!'
45
+ - '"'
46
+ - '#'
47
+ - $
48
+ - '%'
49
+ - '&'
50
+ - ''''
51
+ - (
52
+ - )
53
+ - '*'
54
+ - +
55
+ - ','
56
+ - '-'
57
+ - .
58
+ - /
59
+ - '0'
60
+ - '1'
61
+ - '2'
62
+ - '3'
63
+ - '4'
64
+ - '5'
65
+ - '6'
66
+ - '7'
67
+ - '8'
68
+ - '9'
69
+ - ':'
70
+ - ;
71
+ - <
72
+ - '='
73
+ - '>'
74
+ - '?'
75
+ - A
76
+ - B
77
+ - C
78
+ - D
79
+ - E
80
+ - F
81
+ - G
82
+ - H
83
+ - I
84
+ - J
85
+ - K
86
+ - L
87
+ - M
88
+ - N
89
+ - O
90
+ - P
91
+ - Q
92
+ - R
93
+ - S
94
+ - T
95
+ - U
96
+ - V
97
+ - W
98
+ - X
99
+ - Y
100
+ - Z
101
+ - '['
102
+ - ']'
103
+ - _
104
+ - '`'
105
+ - a
106
+ - b
107
+ - c
108
+ - d
109
+ - e
110
+ - f
111
+ - g
112
+ - h
113
+ - i
114
+ - j
115
+ - k
116
+ - l
117
+ - m
118
+ - n
119
+ - o
120
+ - p
121
+ - q
122
+ - r
123
+ - s
124
+ - t
125
+ - u
126
+ - v
127
+ - w
128
+ - x
129
+ - y
130
+ - z
131
+ - ©
132
+ - ‥
133
+ - '{'
134
+ - '}'
135
+ - \
136
+ - '|'
137
+ - '@'
138
+ - ^
139
+ - '~'
140
+ - ÷
141
+ - ∕
142
+ - ∙
143
+ - ⋅
144
+ - ·
145
+ - ±
146
+ - ∓
147
+ - ∩
148
+ - ∪
149
+ - □
150
+ - ←
151
+ - ↔
152
+ - ⇒
153
+ - ⇐
154
+ - ⇔
155
+ - ∀
156
+ - ∃
157
+ - ∄
158
+ - ∴
159
+ - ∵
160
+ - ∝
161
+ - ∞
162
+ - ⊥
163
+ - ∟
164
+ - ∠
165
+ - ∡
166
+ - ∢
167
+ - ′
168
+ - ″
169
+ - ∥
170
+ - ⊾
171
+ - ⊿
172
+ - ∂
173
+ - ∫
174
+ - ∬
175
+ - ∭
176
+ - ∮
177
+ - ∯
178
+ - ∰
179
+ - ∑
180
+ - ∏
181
+ - √
182
+ - ∛
183
+ - ∜
184
+ - ∱
185
+ - ∲
186
+ - ∳
187
+ - ∶
188
+ - ∷
189
+ - ∼
190
+ - ®
191
+ - ℉
192
+ - Ω
193
+ - ℧
194
+ - Å
195
+ - ⌀
196
+ - ℏ
197
+ - ⅀
198
+ - ⍺
199
+ - ⍵
200
+ - ¢
201
+ - €
202
+ - £
203
+ - ¥
204
+ - ₿
205
+ - Ⅰ
206
+ - Ⅱ
207
+ - Ⅲ
208
+ - Ⅳ
209
+ - Ⅴ
210
+ - Ⅵ
211
+ - Ⅶ
212
+ - Ⅷ
213
+ - Ⅸ
214
+ - Ⅹ
215
+ - Ⅺ
216
+ - Ⅻ
217
+ - ⅰ
218
+ - ⅱ
219
+ - ⅲ
220
+ - ⅳ
221
+ - ⅴ
222
+ - ⅵ
223
+ - ⅶ
224
+ - ⅷ
225
+ - ⅸ
226
+ - ⅹ
227
+ - ⅺ
228
+ - ⅻ
229
+ - ➀
230
+ - ➁
231
+ - ➂
232
+ - ➃
233
+ - ➄
234
+ - ➅
235
+ - ➆
236
+ - ➇
237
+ - ➈
238
+ - ➉
239
+ - ➊
240
+ - ➋
241
+ - ➌
242
+ - ➍
243
+ - ➎
244
+ - ➏
245
+ - ➐
246
+ - ➑
247
+ - ➒
248
+ - ➓
249
+ - ❶
250
+ - ❷
251
+ - ❸
252
+ - ❹
253
+ - ❺
254
+ - ❻
255
+ - ❼
256
+ - ❽
257
+ - ❾
258
+ - ❿
259
+ - ①
260
+ - ②
261
+ - ③
262
+ - ④
263
+ - ⑤
264
+ - ⑥
265
+ - ⑦
266
+ - ⑧
267
+ - ⑨
268
+ - ⑩
269
+ - ●
270
+ - ▶
271
+ - 𝑢
272
+ - ︽
273
+ - –
274
+ - ﹥
275
+ - 𝜓
276
+ - •
277
+ - ∋
278
+ - ƒ
279
+ - ⬆
280
+ - Ạ
281
+ - ◀
282
+ - 
283
+ - ▫
284
+ - ︾
285
+ - À
286
+ - Á
287
+ - Â
288
+ - Ã
289
+ - Ä
290
+ - Å
291
+ - Æ
292
+ - Ç
293
+ - È
294
+ - É
295
+ - Ê
296
+ - Ë
297
+ - Ì
298
+ - Í
299
+ - Î
300
+ - Ï
301
+ - Ð
302
+ - Ñ
303
+ - Ò
304
+ - Ó
305
+ - Ô
306
+ - Õ
307
+ - Ö
308
+ - Ø
309
+ - Ù
310
+ - Ú
311
+ - Û
312
+ - Ü
313
+ - Ý
314
+ - Þ
315
+ - à
316
+ - á
317
+ - â
318
+ - ã
319
+ - ä
320
+ - å
321
+ - æ
322
+ - ç
323
+ - è
324
+ - é
325
+ - ê
326
+ - ë
327
+ - ì
328
+ - í
329
+ - î
330
+ - ï
331
+ - ð
332
+ - ñ
333
+ - ò
334
+ - ó
335
+ - ô
336
+ - õ
337
+ - ö
338
+ - ø
339
+ - ù
340
+ - ú
341
+ - û
342
+ - ü
343
+ - ý
344
+ - þ
345
+ - ÿ
346
+ - ¡
347
+ - ¤
348
+ - ¦
349
+ - §
350
+ - ¨
351
+ - ª
352
+ - «
353
+ - ¬
354
+ - ¯
355
+ - °
356
+ - ²
357
+ - ³
358
+ - ´
359
+ - µ
360
+ - ¶
361
+ - ¸
362
+ - ¹
363
+ - º
364
+ - »
365
+ - ¼
366
+ - ½
367
+ - ¾
368
+ - ¿
369
+ - ×
370
+ - ‐
371
+ - ‑
372
+ - ‒
373
+ - —
374
+ - ―
375
+ - ‖
376
+ - ‗
377
+ - ‘
378
+ - ’
379
+ - ‚
380
+ - ‛
381
+ - “
382
+ - ”
383
+ - „
384
+ - ‟
385
+ - †
386
+ - ‡
387
+ - ‣
388
+ - ․
389
+ - …
390
+ - ‧
391
+ - ‰
392
+ - ‴
393
+ - ‵
394
+ - ‶
395
+ - ‷
396
+ - ‸
397
+ - ‹
398
+ - ›
399
+ - ※
400
+ - ‼
401
+ - ‽
402
+ - ‾
403
+ - ₤
404
+ - ₡
405
+ - ₹
406
+ - −
407
+ - ∖
408
+ - ∗
409
+ - ≈
410
+ - ≠
411
+ - ≡
412
+ - ≤
413
+ - ≥
414
+ - ⊂
415
+ - ⊃
416
+ - ↑
417
+ - →
418
+ - ↓
419
+ - ↕
420
+ - ™
421
+ - Ω
422
+ - ℮
423
+ - ∆
424
+ - ✓
425
+ - ✗
426
+ - ✘
427
+ - ▪
428
+ - ◼
429
+ - ✔
430
+ - ✕
431
+ - ☑
432
+ - ☒
433
+ - №
434
+ - ₽
435
+ - ₴
436
+ - Α
437
+ - α
438
+ - Β
439
+ - β
440
+ - Γ
441
+ - γ
442
+ - Δ
443
+ - δ
444
+ - Ε
445
+ - ε
446
+ - Ζ
447
+ - ζ
448
+ - Η
449
+ - η
450
+ - Θ
451
+ - θ
452
+ - Ι
453
+ - ι
454
+ - Κ
455
+ - κ
456
+ - Λ
457
+ - λ
458
+ - Μ
459
+ - μ
460
+ - Ν
461
+ - ν
462
+ - Ξ
463
+ - ξ
464
+ - Ο
465
+ - ο
466
+ - Π
467
+ - π
468
+ - Ρ
469
+ - ρ
470
+ - Σ
471
+ - σ
472
+ - ς
473
+ - Τ
474
+ - τ
475
+ - Υ
476
+ - υ
477
+ - Φ
478
+ - φ
479
+ - Χ
480
+ - χ
481
+ - Ψ
482
+ - ψ
483
+ - ω
484
+ - ऀ
485
+ - ँ
486
+ - ं
487
+ - ः
488
+ - ऄ
489
+ - अ
490
+ - आ
491
+ - इ
492
+ - ई
493
+ - उ
494
+ - ऊ
495
+ - ऋ
496
+ - ऌ
497
+ - ऍ
498
+ - ऎ
499
+ - ए
500
+ - ऐ
501
+ - ऑ
502
+ - ऒ
503
+ - ओ
504
+ - औ
505
+ - क
506
+ - ख
507
+ - ग
508
+ - घ
509
+ - ङ
510
+ - च
511
+ - छ
512
+ - ज
513
+ - झ
514
+ - ञ
515
+ - ट
516
+ - ठ
517
+ - ड
518
+ - ढ
519
+ - ण
520
+ - त
521
+ - थ
522
+ - द
523
+ - ध
524
+ - न
525
+ - ऩ
526
+ - प
527
+ - फ
528
+ - ब
529
+ - भ
530
+ - म
531
+ - य
532
+ - र
533
+ - ऱ
534
+ - ल
535
+ - ळ
536
+ - ऴ
537
+ - व
538
+ - श
539
+ - ष
540
+ - स
541
+ - ह
542
+ - ऺ
543
+ - ऻ
544
+ - ़
545
+ - ऽ
546
+ - ा
547
+ - ि
548
+ - ी
549
+ - ु
550
+ - ू
551
+ - ृ
552
+ - ॄ
553
+ - ॅ
554
+ - ॆ
555
+ - े
556
+ - ै
557
+ - ॉ
558
+ - ॊ
559
+ - ो
560
+ - ौ
561
+ - ्
562
+ - ॎ
563
+ - ॏ
564
+ - ॐ
565
+ - ॑
566
+ - ॒
567
+ - ॓
568
+ - ॔
569
+ - ॕ
570
+ - ॖ
571
+ - ॗ
572
+ - क़
573
+ - ख़
574
+ - ग़
575
+ - ज़
576
+ - ड़
577
+ - ढ़
578
+ - फ़
579
+ - य़
580
+ - ॠ
581
+ - ॡ
582
+ - ॢ
583
+ - ॣ
584
+ - ।
585
+ - ॥
586
+ - ०
587
+ - १
588
+ - २
589
+ - ३
590
+ - ४
591
+ - ५
592
+ - ६
593
+ - ७
594
+ - ८
595
+ - ९
596
+ - ॰
597
+ - ॱ
598
+ - ॲ
599
+ - ॳ
600
+ - ॴ
601
+ - ॵ
602
+ - ॶ
603
+ - ॷ
604
+ - ॸ
605
+ - ॹ
606
+ - ॺ
607
+ - ॻ
608
+ - ॼ
609
+ - ॽ
610
+ - ॾ
611
+ - ॿ
k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/MODEL-SOURCE.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ source: PaddleOCR official devanagari_PP-OCRv5_mobile_rec (Apache-2.0), copied from the PaddleX cache on k2 2026-09-24
2
+ 20d3b121184ec6b7de1705015022f4b22117990f6e5728a5d21c314bac9c1444 inference.json
3
+ 719be7d20bfe9530e2deae324c999e9911087496bce5e70846767c448d023a01 inference.pdiparams
4
+ 9bd172dd26440c8ce94d1cde5d5baea6aefdc7cf3c5c8492e0beedef656d4e54 inference.yml
5
+ b823d922aa6aedc9fda8412b94e2600885bf8442c5d2197f456a4e672d36481c config.json
k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/artifacts/README.md ADDED
@@ -0,0 +1,169 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ library_name: PaddleOCR
4
+ language:
5
+ - en
6
+ pipeline_tag: image-to-text
7
+ tags:
8
+ - OCR
9
+ - PaddlePaddle
10
+ - PaddleOCR
11
+ - textline_recognition
12
+ ---
13
+
14
+ # devanagari_PP-OCRv5_mobile_rec
15
+
16
+ ## Introduction
17
+
18
+ devanagari_PP-OCRv5_mobile_rec is one of the PP-OCRv5_rec that are the latest generation text line recognition models developed by PaddleOCR team. It aims to efficiently and accurately support the recognition of Devanagari. The key accuracy metrics are as follow:
19
+
20
+ | Model | Accuracy (%) |
21
+ |-|-|
22
+ | devanagari_PP-OCRv5_mobile_rec | 84.96|
23
+
24
+
25
+
26
+ **Note**: If any character (including punctuation) in a line was incorrect, the entire line was marked as wrong. This ensures higher accuracy in practical applications.
27
+
28
+ ## Quick Start
29
+
30
+ ### Installation
31
+
32
+ 1. PaddlePaddle
33
+
34
+ Please refer to the following commands to install PaddlePaddle using pip:
35
+
36
+ ```bash
37
+ # for CUDA11.8
38
+ python -m pip install paddlepaddle-gpu==3.0.0 -i https://www.paddlepaddle.org.cn/packages/stable/cu118/
39
+
40
+ # for CUDA12.6
41
+ python -m pip install paddlepaddle-gpu==3.0.0 -i https://www.paddlepaddle.org.cn/packages/stable/cu126/
42
+
43
+ # for CPU
44
+ python -m pip install paddlepaddle==3.0.0 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/
45
+ ```
46
+
47
+ For details about PaddlePaddle installation, please refer to the [PaddlePaddle official website](https://www.paddlepaddle.org.cn/en/install/quick).
48
+
49
+ 2. PaddleOCR
50
+
51
+ Install the latest version of the PaddleOCR inference package from PyPI:
52
+
53
+ ```bash
54
+ python -m pip install paddleocr
55
+ ```
56
+
57
+ ### Model Usage
58
+
59
+ You can quickly experience the functionality with a single command:
60
+
61
+ ```bash
62
+ paddleocr text_recognition \
63
+ --model_name devanagari_PP-OCRv5_mobile_rec \
64
+ -i https://cdn-uploads.huggingface.co/production/uploads/684ad4f6eb7d8ee8f6a92a3a/dtgfI1BdDM7c9BB0X3-xE.png
65
+ ```
66
+
67
+ You can also integrate the model inference of the text recognition module into your project. Before running the following code, please download the sample image to your local machine.
68
+
69
+ ```python
70
+ from paddleocr import TextRecognition
71
+ model = TextRecognition(model_name="devanagari_PP-OCRv5_mobile_rec")
72
+ output = model.predict(input="dtgfI1BdDM7c9BB0X3-xE.png", batch_size=1)
73
+ for res in output:
74
+ res.print()
75
+ res.save_to_img(save_path="./output/")
76
+ res.save_to_json(save_path="./output/res.json")
77
+ ```
78
+
79
+ After running, the obtained result is as follows:
80
+
81
+ ```json
82
+ {'res': {'input_path': '/root/.paddlex/predict_input/dtgfI1BdDM7c9BB0X3-xE.png', 'page_index': None, 'rec_text': 'कख ग घड', 'rec_score': 0.8845340013504028}}
83
+ ```
84
+
85
+ The visualized image is as follows:
86
+
87
+ ![image/jpeg](https://cdn-uploads.huggingface.co/production/uploads/684ad4f6eb7d8ee8f6a92a3a/G6Zqw5OG2kSNopsgBJYWb.png)
88
+
89
+ For details about usage command and descriptions of parameters, please refer to the [Document](https://paddlepaddle.github.io/PaddleOCR/latest/en/version3.x/module_usage/text_recognition.html#iii-quick-start).
90
+
91
+ ### Pipeline Usage
92
+
93
+ The ability of a single model is limited. But the pipeline consists of several models can provide more capacity to resolve difficult problems in real-world scenarios.
94
+
95
+ #### PP-OCRv5
96
+
97
+ The general OCR pipeline is used to solve text recognition tasks by extracting text information from images and outputting it in string format. And there are 5 modules in the pipeline:
98
+ * Document Image Orientation Classification Module (Optional)
99
+ * Text Image Unwarping Module (Optional)
100
+ * Text Line Orientation Classification Module (Optional)
101
+ * Text Detection Module
102
+ * Text Recognition Module
103
+
104
+ Run a single command to quickly experience the OCR pipeline:
105
+
106
+ ```bash
107
+ paddleocr ocr -i https://cdn-uploads.huggingface.co/production/uploads/684ad4f6eb7d8ee8f6a92a3a/4565zt8fsAJ2JCVyP2OEa.png \
108
+ --text_recognition_model_name devanagari_PP-OCRv5_mobile_rec \
109
+ --use_doc_orientation_classify False \
110
+ --use_doc_unwarping False \
111
+ --use_textline_orientation True \
112
+ --save_path ./output \
113
+ --device gpu:0
114
+ ```
115
+
116
+ Results are printed to the terminal:
117
+
118
+ ```json
119
+ {'res': {'input_path': '/root/.paddlex/predict_input/4565zt8fsAJ2JCVyP2OEa.png', 'page_index': None, 'model_settings': {'use_doc_preprocessor': True, 'use_textline_orientation': False}, 'doc_preprocessor_res': {'input_path': None, 'page_index': None, 'model_settings': {'use_doc_orientation_classify': False, 'use_doc_unwarping': False}, 'angle': -1}, 'dt_polys': array([[[ 172, 201],
120
+ ...,
121
+ [ 172, 290]],
122
+
123
+ ...,
124
+
125
+ [[ 795, 2010],
126
+ ...,
127
+ [ 795, 2055]]], shape=(42, 4, 2), dtype=int16), 'text_det_params': {'limit_side_len': 64, 'limit_type': 'min', 'thresh': 0.3, 'max_side_limit': 4000, 'box_thresh': 0.6, 'unclip_ratio': 1.5}, 'text_type': 'general', 'textline_orientation_angles': array([-1, ..., -1], shape=(42,)), 'text_rec_score_thresh': 0.0, 'return_word_box': False, 'rec_texts': ['विषय-सूची', 'क्र.सं.', 'विवरण', 'पृष्ठ सं.', '1', 'आरईसी लिमिटेड और आरईसीआईपीएमट��� के बारे में', '2-3', '2', 'विदयुत क्षत्र के अधिकारियों के लिए राष्टीय नियमित प्रशिक्षण कार्यक्रम', '4-10', '3', 'विदयुत क्षत्र के अधिकारियों के लिए आरईसी द्वारा प्रायोजित प्रशिक्षण कार्यक्रम', '11-14', 'आरईसीआईपीएमटी/ऑफ-कैंपस में आरईसी के कर्मचारियों के लिए इन-हाउस', '4', 'प्रशिक्षण कार्यक्रम', '15-16', '5', 'अनुकूलित प्रशिकषण कैंपस', '17-18', '6', 'आरईसीआईपीएमटी कैंपस', '19-21', '7', 'अंतरिक फैकल्टी के सदस्य', '22', '8', 'बाहरी फैकल्टी के सदस्य', '23', 'संस्थानों और यूटिलिटीज के साथ समझौता ज़ापन', '9', '24', 'मिशन', 'अपने अनुभव, विशेषज्ञता को साझञा करने और बिजली', 'यूटिलिटिज के प्रबंधकीय कर्मियों को प्रबुद्ध करने के लिए', 'बिजली क्षेत्र के मानव संसाधन विकास के लिए वैश्िक', 'उत्कृष्टता की एक संस्था का निर्माण करना।', 'विज़न', 'बिजली इंजीनियरों/्रबंधकों तक पहुंचना, शिक्षित करना,', 'प्रित करना, पोषण करना, प्रबुद करना और सक्रय करना', 'और उच्च उत्पादकता प्रस्त करने के लिए मानव संसाधनों', 'में गुणवता सुधार के लिए प्रयास करना।'], 'rec_scores': array([0.96054012, ..., 0.96115613], shape=(42,)), 'rec_polys': array([[[ 172, 201],
128
+ ...,
129
+ [ 172, 290]],
130
+
131
+ ...,
132
+
133
+ [[ 795, 2010],
134
+ ...,
135
+ [ 795, 2055]]], shape=(42, 4, 2), dtype=int16), 'rec_boxes': array([[ 172, ..., 290],
136
+ ...,
137
+ [ 795, ..., 2061]], shape=(42, 4), dtype=int16)}}
138
+ ```
139
+
140
+ If save_path is specified, the visualization results will be saved under `save_path`. The visualization output is shown below:
141
+
142
+ ![image/jpeg](https://cdn-uploads.huggingface.co/production/uploads/684ad4f6eb7d8ee8f6a92a3a/yS8pRQnX1uIlgfjaqPXlu.png)
143
+
144
+ The command-line method is for quick experience. For project integration, also only a few codes are needed as well:
145
+
146
+ ```python
147
+ from paddleocr import PaddleOCR
148
+
149
+ ocr = PaddleOCR(
150
+ text_recognition_model_name="devanagari_PP-OCRv5_mobile_rec",
151
+ use_doc_orientation_classify=False, # Use use_doc_orientation_classify to enable/disable document orientation classification model
152
+ use_doc_unwarping=False, # Use use_doc_unwarping to enable/disable document unwarping module
153
+ use_textline_orientation=True, # Use use_textline_orientation to enable/disable textline orientation classification model
154
+ device="gpu:0", # Use device to specify GPU for model inference
155
+ )
156
+ result = ocr.predict("https://cdn-uploads.huggingface.co/production/uploads/684ad4f6eb7d8ee8f6a92a3a/4565zt8fsAJ2JCVyP2OEa.png")
157
+ for res in result:
158
+ res.print()
159
+ res.save_to_img("output")
160
+ res.save_to_json("output")
161
+ ```
162
+
163
+ The default model used in pipeline is `PP-OCRv5_server_rec`, so it is needed that specifing to `devanagari_PP-OCRv5_mobile_rec` by argument `text_recognition_model_name`. And you can also use the local model file by argument `text_recognition_model_dir`. For details about usage command and descriptions of parameters, please refer to the [Document](https://paddlepaddle.github.io/PaddleOCR/latest/en/version3.x/pipeline_usage/OCR.html#2-quick-start).
164
+
165
+ ## Links
166
+
167
+ [PaddleOCR Repo](https://github.com/paddlepaddle/paddleocr)
168
+
169
+ [PaddleOCR Documentation](https://paddlepaddle.github.io/PaddleOCR/latest/en/index.html)
k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/artifacts/config.json ADDED
@@ -0,0 +1,665 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "Global": {
3
+ "model_name": "devanagari_PP-OCRv5_mobile_rec"
4
+ },
5
+ "Hpi": {
6
+ "backend_configs": {
7
+ "paddle_infer": {
8
+ "trt_dynamic_shapes": {
9
+ "x": [
10
+ [
11
+ 1,
12
+ 3,
13
+ 48,
14
+ 160
15
+ ],
16
+ [
17
+ 1,
18
+ 3,
19
+ 48,
20
+ 320
21
+ ],
22
+ [
23
+ 8,
24
+ 3,
25
+ 48,
26
+ 3200
27
+ ]
28
+ ]
29
+ }
30
+ },
31
+ "tensorrt": {
32
+ "dynamic_shapes": {
33
+ "x": [
34
+ [
35
+ 1,
36
+ 3,
37
+ 48,
38
+ 160
39
+ ],
40
+ [
41
+ 1,
42
+ 3,
43
+ 48,
44
+ 320
45
+ ],
46
+ [
47
+ 8,
48
+ 3,
49
+ 48,
50
+ 3200
51
+ ]
52
+ ]
53
+ }
54
+ }
55
+ }
56
+ },
57
+ "PreProcess": {
58
+ "transform_ops": [
59
+ {
60
+ "DecodeImage": {
61
+ "channel_first": false,
62
+ "img_mode": "BGR"
63
+ }
64
+ },
65
+ {
66
+ "MultiLabelEncode": {
67
+ "gtc_encode": "NRTRLabelEncode"
68
+ }
69
+ },
70
+ {
71
+ "RecResizeImg": {
72
+ "image_shape": [
73
+ 3,
74
+ 48,
75
+ 320
76
+ ]
77
+ }
78
+ },
79
+ {
80
+ "KeepKeys": {
81
+ "keep_keys": [
82
+ "image",
83
+ "label_ctc",
84
+ "label_gtc",
85
+ "length",
86
+ "valid_ratio"
87
+ ]
88
+ }
89
+ }
90
+ ]
91
+ },
92
+ "PostProcess": {
93
+ "name": "CTCLabelDecode",
94
+ "character_dict": [
95
+ "!",
96
+ "\"",
97
+ "#",
98
+ "$",
99
+ "%",
100
+ "&",
101
+ "'",
102
+ "(",
103
+ ")",
104
+ "*",
105
+ "+",
106
+ ",",
107
+ "-",
108
+ ".",
109
+ "/",
110
+ "0",
111
+ "1",
112
+ "2",
113
+ "3",
114
+ "4",
115
+ "5",
116
+ "6",
117
+ "7",
118
+ "8",
119
+ "9",
120
+ ":",
121
+ ";",
122
+ "<",
123
+ "=",
124
+ ">",
125
+ "?",
126
+ "A",
127
+ "B",
128
+ "C",
129
+ "D",
130
+ "E",
131
+ "F",
132
+ "G",
133
+ "H",
134
+ "I",
135
+ "J",
136
+ "K",
137
+ "L",
138
+ "M",
139
+ "N",
140
+ "O",
141
+ "P",
142
+ "Q",
143
+ "R",
144
+ "S",
145
+ "T",
146
+ "U",
147
+ "V",
148
+ "W",
149
+ "X",
150
+ "Y",
151
+ "Z",
152
+ "[",
153
+ "]",
154
+ "_",
155
+ "`",
156
+ "a",
157
+ "b",
158
+ "c",
159
+ "d",
160
+ "e",
161
+ "f",
162
+ "g",
163
+ "h",
164
+ "i",
165
+ "j",
166
+ "k",
167
+ "l",
168
+ "m",
169
+ "n",
170
+ "o",
171
+ "p",
172
+ "q",
173
+ "r",
174
+ "s",
175
+ "t",
176
+ "u",
177
+ "v",
178
+ "w",
179
+ "x",
180
+ "y",
181
+ "z",
182
+ "©",
183
+ "‥",
184
+ "{",
185
+ "}",
186
+ "\\",
187
+ "|",
188
+ "@",
189
+ "^",
190
+ "~",
191
+ "÷",
192
+ "∕",
193
+ "∙",
194
+ "⋅",
195
+ "·",
196
+ "±",
197
+ "∓",
198
+ "∩",
199
+ "∪",
200
+ "□",
201
+ "←",
202
+ "↔",
203
+ "⇒",
204
+ "⇐",
205
+ "⇔",
206
+ "∀",
207
+ "∃",
208
+ "∄",
209
+ "∴",
210
+ "∵",
211
+ "∝",
212
+ "∞",
213
+ "⊥",
214
+ "∟",
215
+ "∠",
216
+ "∡",
217
+ "∢",
218
+ "′",
219
+ "″",
220
+ "∥",
221
+ "⊾",
222
+ "⊿",
223
+ "∂",
224
+ "∫",
225
+ "∬",
226
+ "∭",
227
+ "∮",
228
+ "∯",
229
+ "∰",
230
+ "∑",
231
+ "∏",
232
+ "√",
233
+ "∛",
234
+ "∜",
235
+ "∱",
236
+ "∲",
237
+ "∳",
238
+ "∶",
239
+ "∷",
240
+ "∼",
241
+ "®",
242
+ "℉",
243
+ "Ω",
244
+ "℧",
245
+ "Å",
246
+ "⌀",
247
+ "ℏ",
248
+ "⅀",
249
+ "⍺",
250
+ "⍵",
251
+ "¢",
252
+ "€",
253
+ "£",
254
+ "¥",
255
+ "₿",
256
+ "Ⅰ",
257
+ "Ⅱ",
258
+ "Ⅲ",
259
+ "Ⅳ",
260
+ "Ⅴ",
261
+ "Ⅵ",
262
+ "Ⅶ",
263
+ "Ⅷ",
264
+ "Ⅸ",
265
+ "Ⅹ",
266
+ "Ⅺ",
267
+ "Ⅻ",
268
+ "ⅰ",
269
+ "ⅱ",
270
+ "ⅲ",
271
+ "ⅳ",
272
+ "ⅴ",
273
+ "ⅵ",
274
+ "ⅶ",
275
+ "ⅷ",
276
+ "ⅸ",
277
+ "ⅹ",
278
+ "ⅺ",
279
+ "ⅻ",
280
+ "➀",
281
+ "➁",
282
+ "➂",
283
+ "➃",
284
+ "➄",
285
+ "➅",
286
+ "➆",
287
+ "➇",
288
+ "➈",
289
+ "➉",
290
+ "➊",
291
+ "➋",
292
+ "➌",
293
+ "➍",
294
+ "➎",
295
+ "➏",
296
+ "➐",
297
+ "➑",
298
+ "➒",
299
+ "➓",
300
+ "❶",
301
+ "❷",
302
+ "❸",
303
+ "❹",
304
+ "❺",
305
+ "❻",
306
+ "❼",
307
+ "❽",
308
+ "❾",
309
+ "❿",
310
+ "①",
311
+ "②",
312
+ "③",
313
+ "④",
314
+ "⑤",
315
+ "⑥",
316
+ "⑦",
317
+ "⑧",
318
+ "⑨",
319
+ "⑩",
320
+ "●",
321
+ "▶",
322
+ "𝑢",
323
+ "︽",
324
+ "–",
325
+ "﹥",
326
+ "𝜓",
327
+ "•",
328
+ "∋",
329
+ "ƒ",
330
+ "⬆",
331
+ "Ạ",
332
+ "◀",
333
+ "",
334
+ "▫",
335
+ "︾",
336
+ "À",
337
+ "Á",
338
+ "Â",
339
+ "Ã",
340
+ "Ä",
341
+ "Å",
342
+ "Æ",
343
+ "Ç",
344
+ "È",
345
+ "É",
346
+ "Ê",
347
+ "Ë",
348
+ "Ì",
349
+ "Í",
350
+ "Î",
351
+ "Ï",
352
+ "Ð",
353
+ "Ñ",
354
+ "Ò",
355
+ "Ó",
356
+ "Ô",
357
+ "Õ",
358
+ "Ö",
359
+ "Ø",
360
+ "Ù",
361
+ "Ú",
362
+ "Û",
363
+ "Ü",
364
+ "Ý",
365
+ "Þ",
366
+ "à",
367
+ "á",
368
+ "â",
369
+ "ã",
370
+ "ä",
371
+ "å",
372
+ "æ",
373
+ "ç",
374
+ "è",
375
+ "é",
376
+ "ê",
377
+ "ë",
378
+ "ì",
379
+ "í",
380
+ "î",
381
+ "ï",
382
+ "ð",
383
+ "ñ",
384
+ "ò",
385
+ "ó",
386
+ "ô",
387
+ "õ",
388
+ "ö",
389
+ "ø",
390
+ "ù",
391
+ "ú",
392
+ "û",
393
+ "ü",
394
+ "ý",
395
+ "þ",
396
+ "ÿ",
397
+ "¡",
398
+ "¤",
399
+ "¦",
400
+ "§",
401
+ "¨",
402
+ "ª",
403
+ "«",
404
+ "¬",
405
+ "¯",
406
+ "°",
407
+ "²",
408
+ "³",
409
+ "´",
410
+ "µ",
411
+ "¶",
412
+ "¸",
413
+ "¹",
414
+ "º",
415
+ "»",
416
+ "¼",
417
+ "½",
418
+ "¾",
419
+ "¿",
420
+ "×",
421
+ "‐",
422
+ "‑",
423
+ "‒",
424
+ "—",
425
+ "―",
426
+ "‖",
427
+ "‗",
428
+ "‘",
429
+ "’",
430
+ "‚",
431
+ "‛",
432
+ "“",
433
+ "”",
434
+ "„",
435
+ "‟",
436
+ "†",
437
+ "‡",
438
+ "‣",
439
+ "․",
440
+ "…",
441
+ "‧",
442
+ "‰",
443
+ "‴",
444
+ "‵",
445
+ "‶",
446
+ "‷",
447
+ "‸",
448
+ "‹",
449
+ "›",
450
+ "※",
451
+ "‼",
452
+ "‽",
453
+ "‾",
454
+ "₤",
455
+ "₡",
456
+ "₹",
457
+ "−",
458
+ "∖",
459
+ "∗",
460
+ "≈",
461
+ "≠",
462
+ "≡",
463
+ "≤",
464
+ "≥",
465
+ "⊂",
466
+ "⊃",
467
+ "↑",
468
+ "→",
469
+ "↓",
470
+ "↕",
471
+ "™",
472
+ "Ω",
473
+ "℮",
474
+ "∆",
475
+ "✓",
476
+ "✗",
477
+ "✘",
478
+ "▪",
479
+ "◼",
480
+ "✔",
481
+ "✕",
482
+ "☑",
483
+ "☒",
484
+ "№",
485
+ "₽",
486
+ "₴",
487
+ "Α",
488
+ "α",
489
+ "Β",
490
+ "β",
491
+ "Γ",
492
+ "γ",
493
+ "Δ",
494
+ "δ",
495
+ "Ε",
496
+ "ε",
497
+ "Ζ",
498
+ "ζ",
499
+ "Η",
500
+ "η",
501
+ "Θ",
502
+ "θ",
503
+ "Ι",
504
+ "ι",
505
+ "Κ",
506
+ "κ",
507
+ "Λ",
508
+ "λ",
509
+ "Μ",
510
+ "μ",
511
+ "Ν",
512
+ "ν",
513
+ "Ξ",
514
+ "ξ",
515
+ "Ο",
516
+ "ο",
517
+ "Π",
518
+ "π",
519
+ "Ρ",
520
+ "ρ",
521
+ "Σ",
522
+ "σ",
523
+ "ς",
524
+ "Τ",
525
+ "τ",
526
+ "Υ",
527
+ "υ",
528
+ "Φ",
529
+ "φ",
530
+ "Χ",
531
+ "χ",
532
+ "Ψ",
533
+ "ψ",
534
+ "ω",
535
+ "ऀ",
536
+ "ँ",
537
+ "ं",
538
+ "ः",
539
+ "ऄ",
540
+ "अ",
541
+ "आ",
542
+ "इ",
543
+ "ई",
544
+ "उ",
545
+ "ऊ",
546
+ "ऋ",
547
+ "ऌ",
548
+ "ऍ",
549
+ "ऎ",
550
+ "ए",
551
+ "ऐ",
552
+ "ऑ",
553
+ "ऒ",
554
+ "ओ",
555
+ "औ",
556
+ "क",
557
+ "ख",
558
+ "ग",
559
+ "घ",
560
+ "ङ",
561
+ "च",
562
+ "छ",
563
+ "ज",
564
+ "झ",
565
+ "ञ",
566
+ "ट",
567
+ "ठ",
568
+ "ड",
569
+ "ढ",
570
+ "ण",
571
+ "त",
572
+ "थ",
573
+ "द",
574
+ "ध",
575
+ "न",
576
+ "ऩ",
577
+ "प",
578
+ "फ",
579
+ "ब",
580
+ "भ",
581
+ "म",
582
+ "य",
583
+ "र",
584
+ "ऱ",
585
+ "ल",
586
+ "ळ",
587
+ "ऴ",
588
+ "व",
589
+ "श",
590
+ "ष",
591
+ "स",
592
+ "ह",
593
+ "ऺ",
594
+ "ऻ",
595
+ "़",
596
+ "ऽ",
597
+ "ा",
598
+ "ि",
599
+ "ी",
600
+ "ु",
601
+ "ू",
602
+ "ृ",
603
+ "ॄ",
604
+ "ॅ",
605
+ "ॆ",
606
+ "े",
607
+ "ै",
608
+ "ॉ",
609
+ "ॊ",
610
+ "ो",
611
+ "ौ",
612
+ "्",
613
+ "ॎ",
614
+ "ॏ",
615
+ "ॐ",
616
+ "॑",
617
+ "॒",
618
+ "॓",
619
+ "॔",
620
+ "ॕ",
621
+ "ॖ",
622
+ "ॗ",
623
+ "क़",
624
+ "ख़",
625
+ "ग़",
626
+ "ज़",
627
+ "ड़",
628
+ "ढ़",
629
+ "फ़",
630
+ "य़",
631
+ "ॠ",
632
+ "ॡ",
633
+ "ॢ",
634
+ "ॣ",
635
+ "।",
636
+ "॥",
637
+ "०",
638
+ "१",
639
+ "२",
640
+ "३",
641
+ "४",
642
+ "५",
643
+ "६",
644
+ "७",
645
+ "८",
646
+ "९",
647
+ "॰",
648
+ "ॱ",
649
+ "ॲ",
650
+ "ॳ",
651
+ "ॴ",
652
+ "ॵ",
653
+ "ॶ",
654
+ "ॷ",
655
+ "ॸ",
656
+ "ॹ",
657
+ "ॺ",
658
+ "ॻ",
659
+ "ॼ",
660
+ "ॽ",
661
+ "ॾ",
662
+ "ॿ"
663
+ ]
664
+ }
665
+ }
k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/artifacts/inference.json ADDED
The diff for this file is too large to render. See raw diff
 
k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/artifacts/inference.pdiparams ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:719be7d20bfe9530e2deae324c999e9911087496bce5e70846767c448d023a01
3
+ size 7836203
k2-ampixa-demo-ocr/models/space/models/devanagari-ppocrv5/artifacts/inference.yml ADDED
@@ -0,0 +1,611 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Global:
2
+ model_name: devanagari_PP-OCRv5_mobile_rec
3
+ Hpi:
4
+ backend_configs:
5
+ paddle_infer:
6
+ trt_dynamic_shapes: &id001
7
+ x:
8
+ - - 1
9
+ - 3
10
+ - 48
11
+ - 160
12
+ - - 1
13
+ - 3
14
+ - 48
15
+ - 320
16
+ - - 8
17
+ - 3
18
+ - 48
19
+ - 3200
20
+ tensorrt:
21
+ dynamic_shapes: *id001
22
+ PreProcess:
23
+ transform_ops:
24
+ - DecodeImage:
25
+ channel_first: false
26
+ img_mode: BGR
27
+ - MultiLabelEncode:
28
+ gtc_encode: NRTRLabelEncode
29
+ - RecResizeImg:
30
+ image_shape:
31
+ - 3
32
+ - 48
33
+ - 320
34
+ - KeepKeys:
35
+ keep_keys:
36
+ - image
37
+ - label_ctc
38
+ - label_gtc
39
+ - length
40
+ - valid_ratio
41
+ PostProcess:
42
+ name: CTCLabelDecode
43
+ character_dict:
44
+ - '!'
45
+ - '"'
46
+ - '#'
47
+ - $
48
+ - '%'
49
+ - '&'
50
+ - ''''
51
+ - (
52
+ - )
53
+ - '*'
54
+ - +
55
+ - ','
56
+ - '-'
57
+ - .
58
+ - /
59
+ - '0'
60
+ - '1'
61
+ - '2'
62
+ - '3'
63
+ - '4'
64
+ - '5'
65
+ - '6'
66
+ - '7'
67
+ - '8'
68
+ - '9'
69
+ - ':'
70
+ - ;
71
+ - <
72
+ - '='
73
+ - '>'
74
+ - '?'
75
+ - A
76
+ - B
77
+ - C
78
+ - D
79
+ - E
80
+ - F
81
+ - G
82
+ - H
83
+ - I
84
+ - J
85
+ - K
86
+ - L
87
+ - M
88
+ - N
89
+ - O
90
+ - P
91
+ - Q
92
+ - R
93
+ - S
94
+ - T
95
+ - U
96
+ - V
97
+ - W
98
+ - X
99
+ - Y
100
+ - Z
101
+ - '['
102
+ - ']'
103
+ - _
104
+ - '`'
105
+ - a
106
+ - b
107
+ - c
108
+ - d
109
+ - e
110
+ - f
111
+ - g
112
+ - h
113
+ - i
114
+ - j
115
+ - k
116
+ - l
117
+ - m
118
+ - n
119
+ - o
120
+ - p
121
+ - q
122
+ - r
123
+ - s
124
+ - t
125
+ - u
126
+ - v
127
+ - w
128
+ - x
129
+ - y
130
+ - z
131
+ - ©
132
+ - ‥
133
+ - '{'
134
+ - '}'
135
+ - \
136
+ - '|'
137
+ - '@'
138
+ - ^
139
+ - '~'
140
+ - ÷
141
+ - ∕
142
+ - ∙
143
+ - ⋅
144
+ - ·
145
+ - ±
146
+ - ∓
147
+ - ∩
148
+ - ∪
149
+ - □
150
+ - ←
151
+ - ↔
152
+ - ⇒
153
+ - ⇐
154
+ - ⇔
155
+ - ∀
156
+ - ∃
157
+ - ∄
158
+ - ∴
159
+ - ∵
160
+ - ∝
161
+ - ∞
162
+ - ⊥
163
+ - ∟
164
+ - ∠
165
+ - ∡
166
+ - ∢
167
+ - ′
168
+ - ″
169
+ - ∥
170
+ - ⊾
171
+ - ⊿
172
+ - ∂
173
+ - ∫
174
+ - ∬
175
+ - ∭
176
+ - ∮
177
+ - ∯
178
+ - ∰
179
+ - ∑
180
+ - ∏
181
+ - √
182
+ - ∛
183
+ - ∜
184
+ - ∱
185
+ - ∲
186
+ - ∳
187
+ - ∶
188
+ - ∷
189
+ - ∼
190
+ - ®
191
+ - ℉
192
+ - Ω
193
+ - ℧
194
+ - Å
195
+ - ⌀
196
+ - ℏ
197
+ - ⅀
198
+ - ⍺
199
+ - ⍵
200
+ - ¢
201
+ - €
202
+ - £
203
+ - ¥
204
+ - ₿
205
+ - Ⅰ
206
+ - Ⅱ
207
+ - Ⅲ
208
+ - Ⅳ
209
+ - Ⅴ
210
+ - Ⅵ
211
+ - Ⅶ
212
+ - Ⅷ
213
+ - Ⅸ
214
+ - Ⅹ
215
+ - Ⅺ
216
+ - Ⅻ
217
+ - ⅰ
218
+ - ⅱ
219
+ - ⅲ
220
+ - ⅳ
221
+ - ⅴ
222
+ - ⅵ
223
+ - ⅶ
224
+ - ⅷ
225
+ - ⅸ
226
+ - ⅹ
227
+ - ⅺ
228
+ - ⅻ
229
+ - ➀
230
+ - ➁
231
+ - ➂
232
+ - ➃
233
+ - ➄
234
+ - ➅
235
+ - ➆
236
+ - ➇
237
+ - ➈
238
+ - ➉
239
+ - ➊
240
+ - ➋
241
+ - ➌
242
+ - ➍
243
+ - ➎
244
+ - ➏
245
+ - ➐
246
+ - ➑
247
+ - ➒
248
+ - ➓
249
+ - ❶
250
+ - ❷
251
+ - ❸
252
+ - ❹
253
+ - ❺
254
+ - ❻
255
+ - ❼
256
+ - ❽
257
+ - ❾
258
+ - ❿
259
+ - ①
260
+ - ②
261
+ - ③
262
+ - ④
263
+ - ⑤
264
+ - ⑥
265
+ - ⑦
266
+ - ⑧
267
+ - ⑨
268
+ - ⑩
269
+ - ●
270
+ - ▶
271
+ - 𝑢
272
+ - ︽
273
+ - –
274
+ - ﹥
275
+ - 𝜓
276
+ - •
277
+ - ∋
278
+ - ƒ
279
+ - ⬆
280
+ - Ạ
281
+ - ◀
282
+ - 
283
+ - ▫
284
+ - ︾
285
+ - À
286
+ - Á
287
+ - Â
288
+ - Ã
289
+ - Ä
290
+ - Å
291
+ - Æ
292
+ - Ç
293
+ - È
294
+ - É
295
+ - Ê
296
+ - Ë
297
+ - Ì
298
+ - Í
299
+ - Î
300
+ - Ï
301
+ - Ð
302
+ - Ñ
303
+ - Ò
304
+ - Ó
305
+ - Ô
306
+ - Õ
307
+ - Ö
308
+ - Ø
309
+ - Ù
310
+ - Ú
311
+ - Û
312
+ - Ü
313
+ - Ý
314
+ - Þ
315
+ - à
316
+ - á
317
+ - â
318
+ - ã
319
+ - ä
320
+ - å
321
+ - æ
322
+ - ç
323
+ - è
324
+ - é
325
+ - ê
326
+ - ë
327
+ - ì
328
+ - í
329
+ - î
330
+ - ï
331
+ - ð
332
+ - ñ
333
+ - ò
334
+ - ó
335
+ - ô
336
+ - õ
337
+ - ö
338
+ - ø
339
+ - ù
340
+ - ú
341
+ - û
342
+ - ü
343
+ - ý
344
+ - þ
345
+ - ÿ
346
+ - ¡
347
+ - ¤
348
+ - ¦
349
+ - §
350
+ - ¨
351
+ - ª
352
+ - «
353
+ - ¬
354
+ - ¯
355
+ - °
356
+ - ²
357
+ - ³
358
+ - ´
359
+ - µ
360
+ - ¶
361
+ - ¸
362
+ - ¹
363
+ - º
364
+ - »
365
+ - ¼
366
+ - ½
367
+ - ¾
368
+ - ¿
369
+ - ×
370
+ - ‐
371
+ - ‑
372
+ - ‒
373
+ - —
374
+ - ―
375
+ - ‖
376
+ - ‗
377
+ - ‘
378
+ - ’
379
+ - ‚
380
+ - ‛
381
+ - “
382
+ - ”
383
+ - „
384
+ - ‟
385
+ - †
386
+ - ‡
387
+ - ‣
388
+ - ․
389
+ - …
390
+ - ‧
391
+ - ‰
392
+ - ‴
393
+ - ‵
394
+ - ‶
395
+ - ‷
396
+ - ‸
397
+ - ‹
398
+ - ›
399
+ - ※
400
+ - ‼
401
+ - ‽
402
+ - ‾
403
+ - ₤
404
+ - ₡
405
+ - ₹
406
+ - −
407
+ - ∖
408
+ - ∗
409
+ - ≈
410
+ - ≠
411
+ - ≡
412
+ - ≤
413
+ - ≥
414
+ - ⊂
415
+ - ⊃
416
+ - ↑
417
+ - →
418
+ - ↓
419
+ - ↕
420
+ - ™
421
+ - Ω
422
+ - ℮
423
+ - ∆
424
+ - ✓
425
+ - ✗
426
+ - ✘
427
+ - ▪
428
+ - ◼
429
+ - ✔
430
+ - ✕
431
+ - ☑
432
+ - ☒
433
+ - №
434
+ - ₽
435
+ - ₴
436
+ - Α
437
+ - α
438
+ - Β
439
+ - β
440
+ - Γ
441
+ - γ
442
+ - Δ
443
+ - δ
444
+ - Ε
445
+ - ε
446
+ - Ζ
447
+ - ζ
448
+ - Η
449
+ - η
450
+ - Θ
451
+ - θ
452
+ - Ι
453
+ - ι
454
+ - Κ
455
+ - κ
456
+ - Λ
457
+ - λ
458
+ - Μ
459
+ - μ
460
+ - Ν
461
+ - ν
462
+ - Ξ
463
+ - ξ
464
+ - Ο
465
+ - ο
466
+ - Π
467
+ - π
468
+ - Ρ
469
+ - ρ
470
+ - Σ
471
+ - σ
472
+ - ς
473
+ - Τ
474
+ - τ
475
+ - Υ
476
+ - υ
477
+ - Φ
478
+ - φ
479
+ - Χ
480
+ - χ
481
+ - Ψ
482
+ - ψ
483
+ - ω
484
+ - ऀ
485
+ - ँ
486
+ - ं
487
+ - ः
488
+ - ऄ
489
+ - अ
490
+ - आ
491
+ - इ
492
+ - ई
493
+ - उ
494
+ - ऊ
495
+ - ऋ
496
+ - ऌ
497
+ - ऍ
498
+ - ऎ
499
+ - ए
500
+ - ऐ
501
+ - ऑ
502
+ - ऒ
503
+ - ओ
504
+ - औ
505
+ - क
506
+ - ख
507
+ - ग
508
+ - घ
509
+ - ङ
510
+ - च
511
+ - छ
512
+ - ज
513
+ - झ
514
+ - ञ
515
+ - ट
516
+ - ठ
517
+ - ड
518
+ - ढ
519
+ - ण
520
+ - त
521
+ - थ
522
+ - द
523
+ - ध
524
+ - न
525
+ - ऩ
526
+ - प
527
+ - फ
528
+ - ब
529
+ - भ
530
+ - म
531
+ - य
532
+ - र
533
+ - ऱ
534
+ - ल
535
+ - ळ
536
+ - ऴ
537
+ - व
538
+ - श
539
+ - ष
540
+ - स
541
+ - ह
542
+ - ऺ
543
+ - ऻ
544
+ - ़
545
+ - ऽ
546
+ - ा
547
+ - ि
548
+ - ी
549
+ - ु
550
+ - ू
551
+ - ृ
552
+ - ॄ
553
+ - ॅ
554
+ - ॆ
555
+ - े
556
+ - ै
557
+ - ॉ
558
+ - ॊ
559
+ - ो
560
+ - ौ
561
+ - ्
562
+ - ॎ
563
+ - ॏ
564
+ - ॐ
565
+ - ॑
566
+ - ॒
567
+ - ॓
568
+ - ॔
569
+ - ॕ
570
+ - ॖ
571
+ - ॗ
572
+ - क़
573
+ - ख़
574
+ - ग़
575
+ - ज़
576
+ - ड़
577
+ - ढ़
578
+ - फ़
579
+ - य़
580
+ - ॠ
581
+ - ॡ
582
+ - ॢ
583
+ - ॣ
584
+ - ।
585
+ - ॥
586
+ - ०
587
+ - १
588
+ - २
589
+ - ३
590
+ - ४
591
+ - ५
592
+ - ६
593
+ - ७
594
+ - ८
595
+ - ९
596
+ - ॰
597
+ - ॱ
598
+ - ॲ
599
+ - ॳ
600
+ - ॴ
601
+ - ॵ
602
+ - ॶ
603
+ - ॷ
604
+ - ॸ
605
+ - ॹ
606
+ - ॺ
607
+ - ॻ
608
+ - ॼ
609
+ - ॽ
610
+ - ॾ
611
+ - ॿ
k2-ampixa-demo-ocr/models/space/models/limbu-sirijonga-ft-v2/artifacts/inference.json ADDED
The diff for this file is too large to render. See raw diff
 
k2-ampixa-demo-ocr/models/space/models/limbu-sirijonga-ft-v2/artifacts/inference.pdiparams ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:97ae5d8121ea4a05de8ac0c98460df9bb66f2965d507da83ed9de5324baf3971
3
+ size 7605333
k2-ampixa-demo-ocr/models/space/models/limbu-sirijonga-ft-v2/artifacts/inference.yml ADDED
@@ -0,0 +1,134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ Global:
2
+ model_name: PP-OCRv5_mobile_rec
3
+ Hpi:
4
+ backend_configs:
5
+ paddle_infer:
6
+ trt_dynamic_shapes: &id001
7
+ x:
8
+ - - 1
9
+ - 3
10
+ - 48
11
+ - 160
12
+ - - 1
13
+ - 3
14
+ - 48
15
+ - 320
16
+ - - 8
17
+ - 3
18
+ - 48
19
+ - 3200
20
+ tensorrt:
21
+ dynamic_shapes: *id001
22
+ PreProcess:
23
+ transform_ops:
24
+ - DecodeImage:
25
+ channel_first: false
26
+ img_mode: BGR
27
+ - MultiLabelEncode:
28
+ gtc_encode: NRTRLabelEncode
29
+ - RecResizeImg:
30
+ image_shape:
31
+ - 3
32
+ - 48
33
+ - 320
34
+ - KeepKeys:
35
+ keep_keys:
36
+ - image
37
+ - label_ctc
38
+ - label_gtc
39
+ - length
40
+ - valid_ratio
41
+ PostProcess:
42
+ name: CTCLabelDecode
43
+ character_dict:
44
+ - '!'
45
+ - '"'
46
+ - ''''
47
+ - (
48
+ - )
49
+ - '*'
50
+ - +
51
+ - ','
52
+ - '-'
53
+ - .
54
+ - /
55
+ - ':'
56
+ - '='
57
+ - '?'
58
+ - S
59
+ - X
60
+ - '['
61
+ - ']'
62
+ - a
63
+ - c
64
+ - i
65
+ - s
66
+ - t
67
+ - '|'
68
+ - ।
69
+ - ॥
70
+ - ᤀ
71
+ - ᤁ
72
+ - ᤂ
73
+ - ᤃ
74
+ - ᤄ
75
+ - ᤅ
76
+ - ᤆ
77
+ - ᤇ
78
+ - ᤈ
79
+ - ᤋ
80
+ - ᤌ
81
+ - ᤍ
82
+ - ᤎ
83
+ - ᤏ
84
+ - ᤐ
85
+ - ᤑ
86
+ - ᤒ
87
+ - ᤓ
88
+ - ᤔ
89
+ - ᤕ
90
+ - ᤖ
91
+ - ᤗ
92
+ - ᤘ
93
+ - ᤙ
94
+ - ᤛ
95
+ - ᤜ
96
+ - ᤠ
97
+ - ᤡ
98
+ - ᤢ
99
+ - ᤣ
100
+ - ᤤ
101
+ - ᤥ
102
+ - ᤦ
103
+ - ᤧ
104
+ - ᤨ
105
+ - ᤩ
106
+ - ᤪ
107
+ - ᤫ
108
+ - ᤰ
109
+ - ᤱ
110
+ - ᤳ
111
+ - ᤴ
112
+ - ᤵ
113
+ - ᤶ
114
+ - ᤷ
115
+ - ᤸ
116
+ - ᤹
117
+ - ᤺
118
+ - ᤻
119
+ - ᥄
120
+ - ᥅
121
+ - ᥆
122
+ - ᥇
123
+ - ᥈
124
+ - ᥉
125
+ - ᥊
126
+ - ᥋
127
+ - ᥌
128
+ - ᥍
129
+ - ᥎
130
+ - ᥏
131
+ - ‌
132
+ - ‘
133
+ - ’
134
+ - …
k2-ampixa-demo-ocr/models/space/models/limbu-sirijonga-ft-v2/artifacts/limbu-sirijonga-v2.txt ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ !
2
+ "
3
+ '
4
+ (
5
+ )
6
+ *
7
+ +
8
+ ,
9
+ -
10
+ .
11
+ /
12
+ :
13
+ =
14
+ ?
15
+ S
16
+ X
17
+ [
18
+ ]
19
+ a
20
+ c
21
+ i
22
+ s
23
+ t
24
+ |
25
+ ।
26
+ ॥
27
+ ᤀ
28
+ ᤁ
29
+ ᤂ
30
+ ᤃ
31
+ ᤄ
32
+ ᤅ
33
+ ᤆ
34
+ ᤇ
35
+ ᤈ
36
+ ᤋ
37
+ ᤌ
38
+ ᤍ
39
+ ᤎ
40
+ ᤏ
41
+ ᤐ
42
+ ᤑ
43
+ ᤒ
44
+ ᤓ
45
+ ᤔ
46
+ ᤕ
47
+ ᤖ
48
+ ᤗ
49
+ ᤘ
50
+ ᤙ
51
+ ᤛ
52
+ ᤜ
53
+ ᤠ
54
+ ᤡ
55
+ ᤢ
56
+ ᤣ
57
+ ᤤ
58
+ ᤥ
59
+ ᤦ
60
+ ᤧ
61
+ ᤨ
62
+ ᤩ
63
+ ᤪ
64
+ ᤫ
65
+ ᤰ
66
+ ᤱ
67
+ ᤳ
68
+ ᤴ
69
+ ᤵ
70
+ ᤶ
71
+ ᤷ
72
+ ᤸ
73
+ ᤹
74
+ ᤺
75
+ ᤻
76
+ ᥄
77
+ ᥅
78
+ ᥆
79
+ ᥇
80
+ ᥈
81
+ ᥉
82
+ ᥊
83
+ ᥋
84
+ ᥌
85
+ ᥍
86
+ ᥎
87
+ ᥏
88
+ ‌
89
+ ‘
90
+ ’
91
+ …
k2-ampixa-demo-ocr/models/space/models/limbu-sirijonga-ft-v2/model-card.json ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "model_id": "limbu-sirijonga-ft-v2-best",
3
+ "backend": "paddleocr",
4
+ "base_model": "PP-OCRv5_mobile_rec_pretrained (generic; champion training checkpoint lost 2026-07-04)",
5
+ "created_at_utc": "2026-07-06T07:19:31.363403+00:00",
6
+ "artifacts": [
7
+ {
8
+ "path": "artifacts/inference.json",
9
+ "required": true,
10
+ "sha256": "0b5ca684a4e1acc77bad0af537670800f21923e69ea1a79cfb899f25dd54f0eb",
11
+ "kind": "file",
12
+ "size_bytes": 217706
13
+ },
14
+ {
15
+ "path": "artifacts/inference.pdiparams",
16
+ "required": true,
17
+ "sha256": "97ae5d8121ea4a05de8ac0c98460df9bb66f2965d507da83ed9de5324baf3971",
18
+ "kind": "file",
19
+ "size_bytes": 7605333
20
+ },
21
+ {
22
+ "path": "artifacts/inference.yml",
23
+ "required": true,
24
+ "sha256": "8701ae3c6d1586bf8db570a009374cf88d94bb3bf733c665e03146aee2b854c9",
25
+ "kind": "file",
26
+ "size_bytes": 1443
27
+ },
28
+ {
29
+ "path": "artifacts/limbu-sirijonga-v2.txt",
30
+ "required": true,
31
+ "sha256": "a708e4e6c74ac17097a2c8fe24e3f3fd057482969959f5f17950150523693d7d",
32
+ "kind": "file",
33
+ "size_bytes": 316
34
+ }
35
+ ],
36
+ "backend_kwargs": {
37
+ "lang": "limbu-sirijonga",
38
+ "text_recognition_model_name": "PP-OCRv5_mobile_rec",
39
+ "text_recognition_model_dir": "artifacts",
40
+ "recognition_mode": "line"
41
+ },
42
+ "provenance": {},
43
+ "metrics": {},
44
+ "notes": "ft-v2 2026-07-06: trained on rebuilt 2x corpus (46,753 rows, real x8 replay), frozen 902-line test cp CER 0.0047 / grapheme 0.0098 / exact 0.859 vs prior champion 0.0138/0.0284/0.637; page-eval-v2 end-to-end 0.0272 cp (prior 0.043). Silver labels, diagnostic. Run artifacts: outputs/limbu-ft-v2-run-2026-07-06 + HF ampixa/ocr-tech-artifacts."
45
+ }
k2-ampixa-demo-ocr/models/space/models/line-detector/best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:bc57415eb53e4c0372a590196ea3de045bbcdabfc823cd06955a38a3e88dfec3
3
+ size 6362922
k2-ampixa-demo-ocr/models/space/requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ gradio==5.49.1
2
+ pillow>=10.0.0
3
+ numpy>=1.26.0,<2.3
4
+ opencv-python-headless>=4.10.0.84,<4.13
5
+ pypdfium2>=4.30.0,<5
6
+ paddlepaddle==3.3.1
7
+ paddleocr==3.7.0
8
+ ultralytics==8.3.0
k2-ampixa-demo-ocr/workers-ocr/README.md ADDED
@@ -0,0 +1,314 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # OCR worker (Limbu Sirijonga + Devanagari)
2
+
3
+ Port 8906, worker name `ocr`. Line and page OCR on CPU with PaddleOCR 3.7.0 /
4
+ paddlepaddle 3.3.1, ported from the non-Gradio inference paths of the private
5
+ Hugging Face Space `ampixa/limbu-ocr`.
6
+
7
+ ## Endpoints
8
+
9
+ ### `GET /health`
10
+
11
+ ```json
12
+ {
13
+ "ok": true,
14
+ "worker": "ocr",
15
+ "loaded": true,
16
+ "models": {
17
+ "sirijonga": "/Users/k2/ampixa-demo/models/ocr/space/models/limbu-sirijonga-ft-v2/artifacts",
18
+ "devanagari": "/Users/k2/ampixa-demo/models/ocr/space/models/deva-v2/artifacts",
19
+ "recognizer_model_name": "PP-OCRv5_mobile_rec",
20
+ "detector_model_name": "PP-OCRv6_medium_det",
21
+ "detector_limit": "max/1280",
22
+ "max_lines": 120,
23
+ "max_image_side": 2000,
24
+ "load_seconds": 1.19
25
+ },
26
+ "device": "cpu"
27
+ }
28
+ ```
29
+
30
+ `/health` answers as soon as uvicorn binds; the Paddle predictors are built in a
31
+ background thread and `loaded` flips to `true` when they are ready. If the load
32
+ fails, `/health` returns 503 with `ok: false` and an `error` string.
33
+
34
+ ### `POST /recognize`
35
+
36
+ `multipart/form-data`:
37
+
38
+ | field | required | values | default |
39
+ |----------|----------|-----------------------------------------|---------|
40
+ | `file` | yes | PNG / JPEG / WebP, <= 10 MB | - |
41
+ | `mode` | no | `line`, `page` | `page` |
42
+ | `script` | no | `auto`, `sirijonga`, `devanagari` | `auto` |
43
+
44
+ The image is EXIF-transposed, converted to RGB and downscaled so the longest
45
+ side is at most 2000 px. Every box in the response is in **processed-image**
46
+ pixels (`image_size` reports those dimensions), not the original upload's.
47
+
48
+ Response:
49
+
50
+ ```json
51
+ {
52
+ "text": "line 1\nline 2",
53
+ "lines": [
54
+ {"text": "line 1", "confidence": 0.918, "box": [13, 0, 300, 14], "script": "sirijonga"}
55
+ ],
56
+ "script": "sirijonga",
57
+ "mode": "page",
58
+ "elapsed_s": 4.94,
59
+ "image_size": [316, 600],
60
+ "truncated": false,
61
+ "overlay_png_b64": "iVBORw0..."
62
+ }
63
+ ```
64
+
65
+ Two keys extend the contract in `PLAN.md`, both additive:
66
+
67
+ - `image_size` — the processed width/height the `box` values refer to.
68
+ - `truncated` — true when the page had more detected lines than
69
+ `AMPIXA_OCR_MAX_LINES` (default 120) and the tail was dropped.
70
+
71
+ `overlay_png_b64` is `null` in `line` mode. In `page` mode it is a base64 PNG of
72
+ the processed image with every ordered box drawn and numbered, downscaled so its
73
+ longest side is at most 1400 px (`AMPIXA_OCR_OVERLAY_MAX_SIDE`). A full
74
+ newspaper page produces roughly 1.4 MB of PNG, i.e. ~1.85 MB of base64; the
75
+ website should prefer column-sized crops.
76
+
77
+ Errors are always `{"error": "..."}`:
78
+
79
+ | case | status |
80
+ |------------------------------|--------|
81
+ | missing `file` field | 400 |
82
+ | `mode`/`script` not in the enum | 400 |
83
+ | empty or undecodable file | 400 |
84
+ | GIF/BMP/TIFF/... upload | 415 |
85
+ | upload above 10 MB | 413 |
86
+ | models still loading / failed| 503 |
87
+ | anything unhandled | 500 |
88
+
89
+ ## How the two modes work
90
+
91
+ **`line`** — the whole (downscaled) image is one crop, fed straight to the
92
+ recognizer(s). No detector runs, so this is fast (~0.07 s) and the returned box
93
+ is the full image.
94
+
95
+ **`page`** — PaddleOCR full-page text detection (`TextDetection`, PaddleOCR
96
+ 3.7.0's default `PP-OCRv6_medium_det`, auto-downloaded to
97
+ `~/.paddlex/official_models`) produces line polygons. They are reduced to
98
+ axis-aligned boxes, sorted in the Space's `Rows LTR` order, cropped with 12 px
99
+ padding and recognized in batches of 16.
100
+
101
+ ## Script routing
102
+
103
+ `sirijonga` and `devanagari` run exactly one recognizer.
104
+
105
+ `auto` copies the Space's `_script_score` verbatim: both recognizers run on each
106
+ crop and the higher score wins, ties going to Sirijonga.
107
+
108
+ ```
109
+ sirijonga : confidence + 0.06 * min(limbu_chars, 12) - 0.04 * min(deva_chars, 12)
110
+ devanagari: confidence + 0.035 * min(deva_chars, 12) - 0.08 * min(limbu_chars, 12)
111
+ ```
112
+
113
+ Two things are worth knowing before wiring the site:
114
+
115
+ 1. **`auto` is strongly biased towards Sirijonga.** The Sirijonga bonus is
116
+ 0.06/char against 0.035/char for Devanagari, so a Sirijonga candidate with
117
+ 12+ Limbu characters starts 0.72 ahead. Measured on a real Devanagari
118
+ newspaper column (`docs/smoke/ocr/page-column-devanagari-*.json`): forcing
119
+ `script=devanagari` reads the text, while `script=auto` routes 18 of 19 lines
120
+ to Sirijonga and returns transliteration noise. Expose the script selector in
121
+ the UI; do not rely on `auto` for Devanagari input.
122
+ 2. **The "fall back when the output has no Limbu codepoints" heuristic cannot
123
+ work here.** The Sirijonga recognizer's character dictionary (in
124
+ `inference.yml`) contains only Limbu, digits and punctuation, so it can never
125
+ emit a Devanagari codepoint and its output essentially always contains Limbu
126
+ characters. Score comparison is the only routing signal available.
127
+
128
+ `auto` skips the Devanagari pass whenever the Sirijonga candidate already scores
129
+ at least `1.0 + 0.035 * 12 = 1.42`, which is the largest score any Devanagari
130
+ candidate can reach. That is an exact prune: it never changes which text is
131
+ selected, it only avoids work.
132
+
133
+ ## Deviations from the Space's `app.py`
134
+
135
+ | what | Space | here | why |
136
+ |------|-------|------|-----|
137
+ | page pipeline | `PaddleOCR(...)` pipeline, then re-recognize each crop | `TextDetection` + batched recognizers | The pipeline recognizes every box with the seed recognizer and the Space then throws that away and recognizes each crop again. Dropping the wasted pass saves ~40 s on a 305-line page with identical boxes (`use_textline_orientation=False` makes `rec_polys == dt_polys`). |
138
+ | detector `limit_type` | PaddleX default `min` | `max` | With `min`, `limit_side_len=1280` scales the *shortest* side up to 1280 and a page costs 7.7 s to detect regardless of its size. With `max` the same page takes 3.2 s and a column crop 1.6 s, for 303 boxes vs 305. Override with `AMPIXA_OCR_DET_LIMIT_TYPE=min` to match the Space exactly. |
139
+ | line cap | Space default 0 (uncapped) | 120 | Bounds worst-case request time. A 305-line broadsheet would otherwise take ~90 s in `auto`. |
140
+ | model presence check | requires `models/deva-v2/artifacts/nep_eng_dict.txt` | requires `inference.{json,pdiparams,yml}` | **That file does not exist in the Space.** The deployed `app.py` (sha256 `cffc507f...`) checks for `nep_eng_dict.txt` in `deva-v2/artifacts`, but the directory only ships `devanagari-v2.txt` (the repo copy of `app.py` in `ampixa/ocr-tech` still names `devanagari-v2.txt`, so this is a regression in the Space upload). `_ensure_models_present()` therefore raises on the live Space for every route. Worth fixing there. Neither dictionary file is read at inference time anyway — the character dictionary is embedded in `inference.yml` under `PostProcess.character_dict`. |
141
+ | YOLO line detector | optional probe tab | not ported | `models/line-detector/best.pt` is downloaded (6.4 MB) but unused; the Space calls the Paddle detector the primary route and the YOLO route needs `ultralytics` + torch. |
142
+ | PDF / book batch | Paddle Book OCR tab | not ported | Out of scope for the demo contract. |
143
+
144
+ Detector thresholds match the Space's Paddle page defaults: `thresh=0.30`,
145
+ `box_thresh=0.60`, `unclip_ratio=1.5`, `crop_padding=12`, reading order
146
+ `Rows LTR`.
147
+
148
+ ## Models
149
+
150
+ Fetched on k2 with `huggingface_hub.snapshot_download("ampixa/limbu-ocr",
151
+ repo_type="space", ...)` into `~/ampixa-demo/models/ocr/space` (21 MB total, not
152
+ in git). The recognizer weights are PP-OCRv5_mobile_rec fine-tunes.
153
+
154
+ ```
155
+ 96806153df021c99b0195de9757d8fe6443c52f7c8a1c74bf76a5895c37b8c0b models/deva-v2/artifacts/devanagari-v2.txt
156
+ 772e801de4c1cc260058f7c426ab618ef52ecc7ea51442886c8c8fc1606cb88b models/deva-v2/artifacts/inference.json
157
+ 32f5256f8a062830be4394606b8307c30dd9b9fb1be4e15af65606abffb479b4 models/deva-v2/artifacts/inference.pdiparams
158
+ 4f76c7bd21ea18ec118adef93b98017b55a449072b35f1c3ea2baa3df217c8dc models/deva-v2/artifacts/inference.yml
159
+ f6e7dae5427ce893f8b15824060d29e46bd9981218d587b86a13591d4871d588 models/deva-v2/model-card.json
160
+ 0b5ca684a4e1acc77bad0af537670800f21923e69ea1a79cfb899f25dd54f0eb models/limbu-sirijonga-ft-v2/artifacts/inference.json
161
+ 97ae5d8121ea4a05de8ac0c98460df9bb66f2965d507da83ed9de5324baf3971 models/limbu-sirijonga-ft-v2/artifacts/inference.pdiparams
162
+ 8701ae3c6d1586bf8db570a009374cf88d94bb3bf733c665e03146aee2b854c9 models/limbu-sirijonga-ft-v2/artifacts/inference.yml
163
+ a708e4e6c74ac17097a2c8fe24e3f3fd057482969959f5f17950150523693d7d models/limbu-sirijonga-ft-v2/artifacts/limbu-sirijonga-v2.txt
164
+ 1265a26d557ae752743e7ec56d3970e419aa5fe6ecc34785d3e9d3a1e49c58a4 models/limbu-sirijonga-ft-v2/model-card.json
165
+ bc57415eb53e4c0372a590196ea3de045bbcdabfc823cd06955a38a3e88dfec3 models/line-detector/best.pt (downloaded, unused)
166
+ cffc507fd79f1ee31b6469315c92ff6b8e2360affe617ae8e2f8fa709b92fa01 app.py (reference only)
167
+ ```
168
+
169
+ The text detector is PaddleOCR's own `PP-OCRv6_medium_det`, already cached at
170
+ `~/.paddlex/official_models/PP-OCRv6_medium_det`
171
+ (`inference.pdiparams` sha256 `85218d2e3d98f5a21c58b4220627be923a97aee5db3cc71f39536ab31ac53960`).
172
+
173
+ Model-card notes carried over from the Space, unchanged and worth repeating on
174
+ the site: the Sirijonga recognizer reports frozen-test codepoint CER 0.0047 on
175
+ **silver** (machine-derived) labels and is described as diagnostic, not a
176
+ claim-safe benchmark. The Devanagari model has not been validated on
177
+ human-reviewed Devanagari-written Limbu pages.
178
+
179
+ ## Device
180
+
181
+ CPU only. paddlepaddle has no Metal/MPS backend on macOS arm64, so there is no
182
+ GPU option on k2. `cpu_threads` (4/8/16) and `enable_mkldnn` (on/off) were
183
+ measured and changed nothing on this host (all within 3% of each other), so both
184
+ are left at the PaddleOCR defaults.
185
+
186
+ ## Install and run on k2
187
+
188
+ The worker reuses `~/ocr-tech/.venv` (python 3.11.14), which already has the
189
+ exact stack the Space pins: `paddlepaddle==3.3.1`, `paddleocr==3.7.0`, plus
190
+ pillow 10.4.0, opencv-python 4.11.0 and numpy 2.3.5. Per the venv policy,
191
+ `uv pip install --dry-run` was run first and showed the four additions as pure
192
+ installs with no upgrade or downgrade of an existing package:
193
+
194
+ ```sh
195
+ export PATH=/opt/homebrew/bin:$PATH
196
+ uv pip install --python ~/ocr-tech/.venv/bin/python -r ~/ampixa-demo/workers/ocr/requirements.txt
197
+ ```
198
+
199
+ Start it:
200
+
201
+ ```sh
202
+ cd ~/ampixa-demo && nohup ./workers/ocr/run.sh > /dev/null 2>&1 &
203
+ ```
204
+
205
+ `run.sh` binds 127.0.0.1:8906 and appends to `~/ampixa-demo/logs/ocr.log`. It
206
+ honours `AMPIXA_DEMO_ROOT`, `AMPIXA_OCR_VENV`, `AMPIXA_OCR_HOST`,
207
+ `AMPIXA_OCR_PORT` and `AMPIXA_OCR_MODEL_ROOT`.
208
+
209
+ Other tunables, all environment variables read at import time:
210
+ `AMPIXA_OCR_MAX_LINES` (120), `AMPIXA_OCR_OVERLAY_MAX_SIDE` (1400),
211
+ `AMPIXA_OCR_REC_BATCH_SIZE` (16), `AMPIXA_OCR_DET_LIMIT_SIDE_LEN` (1280),
212
+ `AMPIXA_OCR_DET_LIMIT_TYPE` (`max`), `AMPIXA_OCR_DET_THRESH` (0.30),
213
+ `AMPIXA_OCR_DET_BOX_THRESH` (0.60), `AMPIXA_OCR_DET_UNCLIP_RATIO` (1.5).
214
+
215
+ Smoke test:
216
+
217
+ ```sh
218
+ curl -s http://127.0.0.1:8906/health
219
+ curl -s -F "file=@$HOME/ampixa-demo/logs/ocr-smoke/samples/limbu-line-sirijonga.png" \
220
+ -F mode=line -F script=auto http://127.0.0.1:8906/recognize
221
+ curl -s -F "file=@$HOME/ampixa-demo/logs/ocr-smoke/samples/limbu-column-sirijonga.png" \
222
+ -F mode=page -F script=auto http://127.0.0.1:8906/recognize
223
+ ```
224
+
225
+ ## Measured latency (k2, Mac Studio M2 Ultra, CPU)
226
+
227
+ Model load after the process starts: 1.2 s (plus ~9 s to import paddle before
228
+ uvicorn binds). Full table in `docs/smoke/ocr/latency.json`.
229
+
230
+ | case | mode | script | input px | lines | server `elapsed_s` |
231
+ |------|------|--------|----------|-------|--------------------|
232
+ | Sirijonga headline crop | line | auto | 1176x183 | 1 | 0.07 |
233
+ | Sirijonga headline crop | line | sirijonga | 1176x183 | 1 | 0.07 |
234
+ | Devanagari line crop | line | devanagari | 822x141 | 1 | 0.07 |
235
+ | Sirijonga newspaper column | page | auto | 316x600 | 23 | 4.94 |
236
+ | Devanagari newspaper column | page | devanagari | 316x600 | 19 | 2.76 |
237
+ | Devanagari newspaper column | page | auto | 316x600 | 19 | 4.84 |
238
+ | Sirijonga article block (3.5 columns) | page | auto | 930x600 | 90 | 18.70 |
239
+ | Full broadsheet page (capped) | page | sirijonga | 1297x2000 | 120 | 17.12 |
240
+ | Full broadsheet page (capped) | page | auto | 1297x2000 | 120 | 29.93 |
241
+
242
+ Cost model: detection ~1.6 s for a column and ~3.2 s for a full page, then
243
+ ~130 ms per line per recognizer pass. `auto` is up to 2x a forced script. A full
244
+ uncapped 305-line broadsheet would be ~90 s in `auto`, which is why
245
+ `AMPIXA_OCR_MAX_LINES` defaults to 120 and why the website samples are single
246
+ newspaper columns.
247
+
248
+ ## Sample images
249
+
250
+ `site/assets/ocr-samples/` holds three images for the website's "try a sample"
251
+ buttons, all cut from Gorkhapatra pages already present read-only on k2 under
252
+ `~/ocr-tech/data`:
253
+
254
+ | file | bytes | px | cut from |
255
+ |------|-------|----|----------|
256
+ | `limbu-line-sirijonga.png` | 26 754 | 1176x183 | `data/limbu-gorkhapatra-language-page-pack-2026-06-16/inputs/gorkhapatra-language-page-0001-01.png` crop `(76, 369, 1252, 552)` |
257
+ | `limbu-column-sirijonga.png` | 91 080 | 316x600 | same page, crop `(360, 1150, 676, 1750)` |
258
+ | `deva-column-devanagari.png` | 105 069 | 316x600 | `data/gorkhapatra-language-page-pack-smoke/inputs/gorkhapatra-language-page-0001-02.png` crop `(360, 1150, 676, 1750)` |
259
+
260
+ **Provenance and licence.** Both source pages are language-supplement pages of
261
+ *Gorkhapatra* ("नयाँ नेपाल"), the Nepali state-owned daily, taken from its public
262
+ e-paper — the first via
263
+ `epaper.gorkhapatraonline.com` (manifest
264
+ `data/gorkhapatra-pack-smoke/manifests/gorkhapatra-pack.jsonl` records the source
265
+ PDF URLs and sha256s). The content is **copyright Gorkhapatra Sansthan; it is not
266
+ openly licensed.** These are small excerpts (one headline, two single columns)
267
+ used as OCR demo input. The ocr-tech manifests mark every one of these rows
268
+ `claim_evidence_eligible: false` / `reference_status: pending_manual_label`, so
269
+ they are demo material, not benchmark evidence. If the public site needs
270
+ unambiguously redistributable samples, replace them with text rendered from
271
+ Noto Sans Limbu (present on k2 at
272
+ `/System/Library/Fonts/Supplemental/NotoSansLimbu-Regular.ttf`, SIL OFL) or with
273
+ material the project owns.
274
+
275
+ ## Ground-truthed check on rendered text
276
+
277
+ k2 ships `/System/Library/Fonts/Supplemental/NotoSansLimbu-Regular.ttf`, so three
278
+ Sirijonga lines were rendered at 64 px with PIL and pushed through `mode=line`,
279
+ `script=sirijonga`. All three came back **exact**, codepoint CER 0.000,
280
+ confidence 0.953-0.970, 0.07-0.09 s each:
281
+
282
+ | reference | recognized | exact |
283
+ |-----------|------------|-------|
284
+ | `ᤕᤠᤰᤌᤢᤱ ᤐᤠᤴ` | `ᤕᤠᤰᤌᤢᤱ ᤐᤠᤴ` | yes |
285
+ | `ᤗᤡᤶᤒᤢ ᤛᤡᤖᤡᤈᤥᤱᤀᤥ ᤛᤠᤵᤐᤴ` | `ᤗᤡᤶᤒᤢ ᤛᤡᤖᤡᤈᤥᤱᤀᤥ ᤛᤠᤵᤐᤴ` | yes |
286
+ | `᥇᥈᥉᥊᥋ ᤀᤠᤱ ᥌᥍᥎᥏᥆` | `᥇᥈᥉᥊᥋ ᤀᤠᤱ ᥌᥍᥎᥏᥆` | yes |
287
+
288
+ Caveat: this Pillow build reports `features.check("raqm") == False`, so the text
289
+ is drawn without complex-script shaping. The glyphs are correct but positioned
290
+ more simply than properly typeset Limbu, and the images are clean synthetic
291
+ renders. Treat this as "the pipeline and the character dictionary are wired up
292
+ correctly", not as an accuracy result. Artifacts in `docs/smoke/ocr/rendered/`.
293
+
294
+ ## Smoke evidence
295
+
296
+ `docs/smoke/ocr/` contains the request JSON for all ten cases, the decoded box
297
+ overlays, `latency.json`, the input crops under `docs/smoke/ocr/inputs/` and the
298
+ rendered-text check under `docs/smoke/ocr/rendered/`. The same files live on k2
299
+ in `~/ampixa-demo/logs/ocr-smoke/` together with the two full-page inputs that
300
+ are too large for git.
301
+
302
+ ## Known gaps
303
+
304
+ - There is no ground-truth transcription for the scanned pages used, so the page
305
+ smoke tests show the worker runs end to end and produces plausible Limbu at
306
+ high recognizer confidence (median 0.845 over a 23-line Sirijonga column,
307
+ 0.83-0.97 on headline lines). They are **not** an accuracy measurement on real
308
+ scans. The Space's own model card is the only accuracy claim, and it is on
309
+ silver labels.
310
+ - The only ground-truthed measurement here is the rendered-text check below, and
311
+ it is easier than a real scan.
312
+ - The `auto` route is unreliable for Devanagari input (see Script routing).
313
+ - Page mode is slow on dense pages; a broadsheet is 18-30 s. Anything more
314
+ responsive needs either a smaller input or batching across requests.
k2-ampixa-demo-ocr/workers-ocr/requirements.txt ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ # Added on top of the existing ~/ocr-tech/.venv on k2 (python 3.11).
2
+ # That venv already provides the heavy stack the Space pins:
3
+ # paddlepaddle==3.3.1, paddleocr==3.7.0, pillow 10.4.0, opencv-python 4.11.0, numpy 2.3.5
4
+ # `uv pip install --dry-run` against it showed these four as pure additions with
5
+ # no upgrade or downgrade of any existing package.
6
+ fastapi==0.141.1
7
+ starlette==1.6.0
8
+ uvicorn==0.52.4
9
+ python-multipart==0.0.32
k2-ampixa-demo-ocr/workers-ocr/run.sh ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/sh
2
+ # Start the Ampixa OCR worker on k2 (127.0.0.1:8906), logging to ~/ampixa-demo/logs/ocr.log.
3
+ set -eu
4
+
5
+ ROOT="${AMPIXA_DEMO_ROOT:-$HOME/ampixa-demo}"
6
+ VENV="${AMPIXA_OCR_VENV:-$HOME/ocr-tech/.venv}"
7
+
8
+ AMPIXA_OCR_MODEL_ROOT="${AMPIXA_OCR_MODEL_ROOT:-$ROOT/models/ocr/space/models}"
9
+ export AMPIXA_OCR_MODEL_ROOT
10
+
11
+ mkdir -p "$ROOT/logs"
12
+
13
+ exec "$VENV/bin/python" "$ROOT/workers/ocr/server.py" \
14
+ --host "${AMPIXA_OCR_HOST:-127.0.0.1}" \
15
+ --port "${AMPIXA_OCR_PORT:-8906}" \
16
+ >> "$ROOT/logs/ocr.log" 2>&1
k2-ampixa-demo-ocr/workers-ocr/server.py ADDED
@@ -0,0 +1,715 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ampixa OCR worker: Limbu Sirijonga + Devanagari OCR on CPU via PaddleOCR.
2
+
3
+ Ported from the non-Gradio inference paths of the private Hugging Face Space
4
+ ``ampixa/limbu-ocr`` (``app.py``, sha256 cffc507fd79f1ee31b6469315c92ff6b8e2360affe617ae8e2f8fa709b92fa01).
5
+
6
+ Two routes are exposed:
7
+
8
+ ``line``
9
+ The uploaded image is treated as a single cropped text line and sent
10
+ straight to the recognizer(s).
11
+
12
+ ``page``
13
+ PaddleOCR full-page text detection produces line boxes, the boxes are put
14
+ into reading order, every crop is recognized, and a PNG overlay with the
15
+ boxes drawn on it is returned alongside the text.
16
+
17
+ Everything runs on CPU: paddlepaddle has no Metal/MPS backend on macOS arm64.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import argparse
23
+ import base64
24
+ import io
25
+ import logging
26
+ import math
27
+ import os
28
+ import re
29
+ import sys
30
+ import threading
31
+ import time
32
+ import traceback
33
+ from collections.abc import AsyncIterator
34
+ from contextlib import asynccontextmanager
35
+ from dataclasses import dataclass
36
+ from pathlib import Path
37
+ from typing import Any
38
+
39
+ import numpy as np
40
+ import uvicorn
41
+ from fastapi import FastAPI, File, Form, Request, UploadFile
42
+ from fastapi.exceptions import RequestValidationError
43
+ from fastapi.responses import JSONResponse
44
+ from paddleocr import TextDetection
45
+ from paddleocr._models.text_recognition import TextRecognition
46
+ from paddlex import create_model
47
+ from PIL import Image, ImageDraw, ImageOps, UnidentifiedImageError
48
+ from starlette.concurrency import run_in_threadpool
49
+ from starlette.exceptions import HTTPException as StarletteHTTPException
50
+
51
+ WORKER_NAME = "ocr"
52
+ DEFAULT_HOST = os.environ.get("AMPIXA_OCR_HOST", "127.0.0.1")
53
+ DEFAULT_PORT = int(os.environ.get("AMPIXA_OCR_PORT", "8906"))
54
+
55
+ MODEL_ROOT = Path(
56
+ os.environ.get(
57
+ "AMPIXA_OCR_MODEL_ROOT",
58
+ str(Path.home() / "ampixa-demo" / "models" / "ocr" / "space" / "models"),
59
+ )
60
+ ).expanduser()
61
+ SIRIJONGA_MODEL_DIR = MODEL_ROOT / "limbu-sirijonga-ft-v2" / "artifacts"
62
+ # devanagari-ft-v3 (2026-09-24): official PaddleOCR devanagari_PP-OCRv5_mobile_rec fine-tuned
63
+ # on real Gorkhapatra print, synthetic citizenship/office lines, and English replay; served as
64
+ # ONNX (see ../models/.../devanagari-ft-v3/MODEL-SOURCE.txt). Same 568-entry dictionary
65
+ # (Devanagari + all Latin letters + 0-9). Roll back to the official model by pointing
66
+ # AMPIXA_OCR_DEVANAGARI_DIR at devanagari-ppocrv5/artifacts.
67
+ DEVANAGARI_MODEL_DIR = Path(
68
+ os.environ.get("AMPIXA_OCR_DEVANAGARI_DIR", str(MODEL_ROOT / "devanagari-ft-v3" / "artifacts"))
69
+ ).expanduser()
70
+
71
+ # The PP-OCRv5 recognizer packs live under model_dir; the character dictionary is
72
+ # embedded in inference.yml (PostProcess.character_dict), so the standalone .txt
73
+ # dictionaries shipped next to them are documentation only and are not loaded.
74
+ # Each model_name must match Global.model_name in that model's inference.yml.
75
+ SIRIJONGA_MODEL_NAME = "PP-OCRv5_mobile_rec"
76
+ DEVANAGARI_MODEL_NAME = "devanagari_PP-OCRv5_mobile_rec"
77
+ REQUIRED_RECOGNIZER_FILES = ("inference.json", "inference.pdiparams", "inference.yml")
78
+ # A directory holding inference.onnx (tools/deva_rec_torch/export_onnx.py output) is served
79
+ # through PaddleX's onnxruntime engine with the same pre/post-processing as Paddle.
80
+ REQUIRED_ONNX_FILES = ("inference.onnx", "inference.yml")
81
+
82
+
83
+ def recognizer_engine(model_dir: Path) -> str:
84
+ return "onnxruntime" if (model_dir / "inference.onnx").is_file() else "paddle_static"
85
+
86
+ MAX_UPLOAD_BYTES = 10 * 1024 * 1024
87
+ MAX_IMAGE_SIDE = 2000
88
+ OVERLAY_MAX_SIDE = int(os.environ.get("AMPIXA_OCR_OVERLAY_MAX_SIDE", "1400"))
89
+ MAX_LINES = int(os.environ.get("AMPIXA_OCR_MAX_LINES", "120"))
90
+ RECOGNIZER_BATCH_SIZE = int(os.environ.get("AMPIXA_OCR_REC_BATCH_SIZE", "16"))
91
+
92
+ # Detector settings. The Space leaves limit_type at the PaddleX default ("min"),
93
+ # which upscales every page so its *shortest* side is >= limit_side_len and costs
94
+ # ~8 s per page on this host. "max" bounds the *longest* side instead: 2.4x faster
95
+ # on a full newspaper page for the same box count (303 vs 305 boxes measured).
96
+ DETECTOR_LIMIT_SIDE_LEN = int(os.environ.get("AMPIXA_OCR_DET_LIMIT_SIDE_LEN", "1280"))
97
+ DETECTOR_LIMIT_TYPE = os.environ.get("AMPIXA_OCR_DET_LIMIT_TYPE", "max")
98
+ DETECTOR_THRESH = float(os.environ.get("AMPIXA_OCR_DET_THRESH", "0.30"))
99
+ DETECTOR_BOX_THRESH = float(os.environ.get("AMPIXA_OCR_DET_BOX_THRESH", "0.60"))
100
+ DETECTOR_UNCLIP_RATIO = float(os.environ.get("AMPIXA_OCR_DET_UNCLIP_RATIO", "1.5"))
101
+ DETECTOR_MODEL_NAME = "PP-OCRv6_medium_det" # PaddleOCR 3.7.0 default text detector.
102
+
103
+ CROP_PADDING = 12 # Space default for the Paddle page tab.
104
+ READING_ORDER = "Rows LTR" # Space default for the Paddle page tab.
105
+
106
+ SUPPORTED_MODES = ("line", "page")
107
+ SUPPORTED_SCRIPTS = ("auto", "sirijonga", "devanagari")
108
+ SUPPORTED_IMAGE_FORMATS = {"PNG": "image/png", "JPEG": "image/jpeg", "WEBP": "image/webp"}
109
+
110
+ LIMBU_RE = re.compile("[\u1900-\u194F]")
111
+ DEVANAGARI_RE = re.compile("[\u0900-\u097F]")
112
+
113
+ # Upper bound of _script_score() for a Devanagari candidate: rec_score is a
114
+ # probability (<= 1.0) and the Devanagari bonus saturates at 12 * 0.035. If the
115
+ # Sirijonga candidate already scores at least this much, the Devanagari pass
116
+ # cannot change the outcome and is skipped. This is an exact prune, not an
117
+ # approximation: it never changes the selected text.
118
+ MAX_DEVANAGARI_SCRIPT_SCORE = 1.0 + 0.035 * 12
119
+
120
+ LOGGER = logging.getLogger("ampixa.ocr")
121
+
122
+
123
+ @dataclass(slots=True)
124
+ class Recognition:
125
+ """One recognizer output for one crop."""
126
+
127
+ text: str
128
+ confidence: float | None
129
+ engine: str
130
+
131
+
132
+ @dataclass(slots=True)
133
+ class Box:
134
+ """Axis-aligned line box in processed-image pixel coordinates."""
135
+
136
+ left: int
137
+ top: int
138
+ right: int
139
+ bottom: int
140
+ confidence: float
141
+
142
+ @property
143
+ def width(self) -> int:
144
+ return max(0, self.right - self.left)
145
+
146
+ @property
147
+ def height(self) -> int:
148
+ return max(0, self.bottom - self.top)
149
+
150
+ @property
151
+ def x_center(self) -> float:
152
+ return (self.left + self.right) / 2.0
153
+
154
+ @property
155
+ def y_center(self) -> float:
156
+ return (self.top + self.bottom) / 2.0
157
+
158
+ def as_list(self) -> list[int]:
159
+ return [self.left, self.top, self.right, self.bottom]
160
+
161
+
162
+ class OcrError(Exception):
163
+ """Client-visible OCR failure carrying an HTTP status code."""
164
+
165
+ def __init__(self, message: str, status_code: int) -> None:
166
+ super().__init__(message)
167
+ self.message = message
168
+ self.status_code = status_code
169
+
170
+
171
+ class Engines:
172
+ """Holds the Paddle predictors and serializes every call into them.
173
+
174
+ Paddle predictors are not documented as thread-safe, and uvicorn runs request
175
+ handlers in a thread pool, so every predict() call goes through ``lock``.
176
+ """
177
+
178
+ def __init__(self) -> None:
179
+ self.lock = threading.Lock()
180
+ self.recognizers: dict[str, Any] = {}
181
+ self.detector: Any = None
182
+ self.loaded = False
183
+ self.load_seconds: float | None = None
184
+ self.load_error: str | None = None
185
+
186
+ def ensure_model_files(self) -> None:
187
+ missing: list[str] = []
188
+ for model_dir in (SIRIJONGA_MODEL_DIR, DEVANAGARI_MODEL_DIR):
189
+ required = REQUIRED_ONNX_FILES if recognizer_engine(model_dir) == "onnxruntime" else REQUIRED_RECOGNIZER_FILES
190
+ for name in required:
191
+ path = model_dir / name
192
+ if not path.is_file():
193
+ missing.append(str(path))
194
+ if missing:
195
+ raise RuntimeError("missing OCR model artifacts: " + ", ".join(missing))
196
+
197
+ def load(self) -> None:
198
+ """Build every predictor once. Called from a background thread at startup."""
199
+ started = time.perf_counter()
200
+ try:
201
+ self.ensure_model_files()
202
+ for engine, model_name, model_dir in (
203
+ ("sirijonga", SIRIJONGA_MODEL_NAME, SIRIJONGA_MODEL_DIR),
204
+ ("devanagari", DEVANAGARI_MODEL_NAME, DEVANAGARI_MODEL_DIR),
205
+ ):
206
+ step = time.perf_counter()
207
+ backend = recognizer_engine(model_dir)
208
+ if backend == "onnxruntime":
209
+ self.recognizers[engine] = create_model(
210
+ model_name=model_name, model_dir=str(model_dir), engine="onnxruntime", device="cpu"
211
+ )
212
+ else:
213
+ self.recognizers[engine] = TextRecognition(
214
+ model_name=model_name,
215
+ model_dir=str(model_dir),
216
+ device="cpu",
217
+ )
218
+ LOGGER.info(
219
+ "loaded recognizer engine=%s backend=%s model=%s dir=%s in %.2fs",
220
+ engine,
221
+ backend,
222
+ model_name,
223
+ model_dir,
224
+ time.perf_counter() - step,
225
+ )
226
+ step = time.perf_counter()
227
+ self.detector = TextDetection(
228
+ limit_side_len=DETECTOR_LIMIT_SIDE_LEN,
229
+ limit_type=DETECTOR_LIMIT_TYPE,
230
+ thresh=DETECTOR_THRESH,
231
+ box_thresh=DETECTOR_BOX_THRESH,
232
+ unclip_ratio=DETECTOR_UNCLIP_RATIO,
233
+ device="cpu",
234
+ )
235
+ LOGGER.info("loaded text detector in %.2fs", time.perf_counter() - step)
236
+ except Exception as exc: # noqa: BLE001 - recorded and surfaced through /health
237
+ self.load_error = f"{type(exc).__name__}: {exc}"
238
+ LOGGER.error("model load failed: %s\n%s", self.load_error, traceback.format_exc())
239
+ return
240
+ self.load_seconds = time.perf_counter() - started
241
+ self.loaded = True
242
+ LOGGER.info("all OCR models loaded in %.2fs", self.load_seconds)
243
+
244
+
245
+ ENGINES = Engines()
246
+
247
+
248
+ def _payload(item: object) -> dict[str, Any] | None:
249
+ """Unwrap a PaddleX result object into its plain result mapping."""
250
+ candidate: object = item
251
+ if not isinstance(candidate, dict):
252
+ candidate = getattr(item, "json", None)
253
+ if not isinstance(candidate, dict):
254
+ return None
255
+ nested = candidate.get("res")
256
+ return nested if isinstance(nested, dict) else candidate
257
+
258
+
259
+ def _script_score(recognition: Recognition) -> float:
260
+ """Score copied verbatim from the Space's app.py ``_script_score``.
261
+
262
+ The Sirijonga bonus (0.06/char, capped at 12 chars) is larger than the
263
+ Devanagari one (0.035/char), so this rule leans towards Sirijonga on mixed
264
+ pages. Callers that know the script should pass it explicitly.
265
+ """
266
+ text = recognition.text or ""
267
+ confidence = recognition.confidence if recognition.confidence is not None else 0.0
268
+ limbu_chars = len(LIMBU_RE.findall(text))
269
+ deva_chars = len(DEVANAGARI_RE.findall(text))
270
+ if recognition.engine == "sirijonga":
271
+ return confidence + min(limbu_chars, 12) * 0.06 - min(deva_chars, 12) * 0.04
272
+ if recognition.engine == "devanagari":
273
+ return confidence + min(deva_chars, 12) * 0.035 - min(limbu_chars, 12) * 0.08
274
+ return confidence
275
+
276
+
277
+ def _recognize_batch(engine: str, crops: list[np.ndarray]) -> list[Recognition]:
278
+ """Run one recognizer over a list of BGR crops, preserving input order."""
279
+ if not crops:
280
+ return []
281
+ predictor = ENGINES.recognizers.get(engine)
282
+ if predictor is None:
283
+ raise OcrError(f"recognizer '{engine}' is not loaded", 503)
284
+ raw = list(predictor.predict(crops, batch_size=RECOGNIZER_BATCH_SIZE))
285
+ if len(raw) != len(crops):
286
+ raise OcrError(
287
+ f"recognizer '{engine}' returned {len(raw)} results for {len(crops)} crops",
288
+ 500,
289
+ )
290
+ results: list[Recognition] = []
291
+ for item in raw:
292
+ payload = _payload(item)
293
+ if payload is None:
294
+ results.append(Recognition(text="", confidence=None, engine=engine))
295
+ continue
296
+ text = payload.get("rec_text") or payload.get("text") or ""
297
+ score = payload.get("rec_score")
298
+ if score is None:
299
+ score = payload.get("score")
300
+ confidence: float | None
301
+ try:
302
+ confidence = float(score) if score is not None else None
303
+ except (TypeError, ValueError):
304
+ confidence = None
305
+ results.append(Recognition(text=str(text).strip(), confidence=confidence, engine=engine))
306
+ return results
307
+
308
+
309
+ def _recognize_routed(crops: list[np.ndarray], script: str) -> list[Recognition]:
310
+ """Recognize crops under the requested script routing.
311
+
312
+ ``auto`` reproduces the Space: both engines run and the higher ``_script_score``
313
+ wins, with ties going to Sirijonga. The Devanagari pass is skipped for crops
314
+ whose Sirijonga score already exceeds every score Devanagari could reach.
315
+ """
316
+ if script in ("sirijonga", "devanagari"):
317
+ return _recognize_batch(script, crops)
318
+
319
+ sirijonga = _recognize_batch("sirijonga", crops)
320
+ scores = [_script_score(rec) for rec in sirijonga]
321
+ pending = [i for i, score in enumerate(scores) if score < MAX_DEVANAGARI_SCRIPT_SCORE]
322
+ if not pending:
323
+ return sirijonga
324
+ devanagari = _recognize_batch("devanagari", [crops[i] for i in pending])
325
+ selected = list(sirijonga)
326
+ for index, candidate in zip(pending, devanagari, strict=True):
327
+ if _script_score(candidate) > scores[index]:
328
+ selected[index] = candidate
329
+ return selected
330
+
331
+
332
+ def _to_bgr(image: Image.Image) -> np.ndarray:
333
+ """PIL RGB image -> contiguous BGR ndarray, the layout PaddleX expects."""
334
+ return np.asarray(image)[:, :, ::-1].copy()
335
+
336
+
337
+ def _box_from_polygon(polygon: object, *, width: int, height: int, confidence: float) -> Box | None:
338
+ array = np.asarray(polygon, dtype=np.float64).reshape(-1, 2)
339
+ if array.size == 0:
340
+ return None
341
+ box = Box(
342
+ left=max(0, int(math.floor(float(array[:, 0].min())))),
343
+ top=max(0, int(math.floor(float(array[:, 1].min())))),
344
+ right=min(width, int(math.ceil(float(array[:, 0].max())))),
345
+ bottom=min(height, int(math.ceil(float(array[:, 1].max())))),
346
+ confidence=confidence,
347
+ )
348
+ return box if box.width > 0 and box.height > 0 else None
349
+
350
+
351
+ def _detect_boxes(image: Image.Image) -> list[Box]:
352
+ """Full-page text detection -> axis-aligned line boxes."""
353
+ if ENGINES.detector is None:
354
+ raise OcrError("text detector is not loaded", 503)
355
+ raw = list(ENGINES.detector.predict(_to_bgr(image)))
356
+ boxes: list[Box] = []
357
+ for item in raw:
358
+ payload = _payload(item)
359
+ if payload is None:
360
+ continue
361
+ polygons = payload.get("dt_polys")
362
+ if polygons is None:
363
+ continue
364
+ # dt_scores comes back as an ndarray, so never use truthiness on it.
365
+ scores = payload.get("dt_scores")
366
+ if scores is None:
367
+ scores = []
368
+ for index, polygon in enumerate(polygons):
369
+ confidence = 1.0
370
+ if index < len(scores) and scores[index] is not None:
371
+ try:
372
+ confidence = float(scores[index])
373
+ except (TypeError, ValueError):
374
+ confidence = 1.0
375
+ box = _box_from_polygon(
376
+ polygon, width=image.width, height=image.height, confidence=confidence
377
+ )
378
+ if box is not None:
379
+ boxes.append(box)
380
+ return boxes
381
+
382
+
383
+ def _has_two_column_shape(boxes: list[Box], image_width: int) -> bool:
384
+ """Copied from the Space's app.py."""
385
+ if len(boxes) < 8 or image_width <= 0:
386
+ return False
387
+ midpoint = image_width / 2.0
388
+ left = [box for box in boxes if box.x_center < midpoint]
389
+ right = [box for box in boxes if box.x_center >= midpoint]
390
+ if len(left) < 3 or len(right) < 3:
391
+ return False
392
+ left_median = float(np.median([box.x_center for box in left]))
393
+ right_median = float(np.median([box.x_center for box in right]))
394
+ return (right_median - left_median) >= image_width * 0.22
395
+
396
+
397
+ def _order_boxes(boxes: list[Box], mode: str, image_width: int) -> list[Box]:
398
+ """Copied from the Space's app.py ``_order_image_line_boxes``."""
399
+ if mode == "Auto":
400
+ mode = "Columns LTR" if _has_two_column_shape(boxes, image_width) else "Rows LTR"
401
+ if mode == "Columns LTR":
402
+ return sorted(
403
+ boxes,
404
+ key=lambda box: (0 if box.x_center < image_width / 2 else 1, box.y_center, box.x_center),
405
+ )
406
+ if mode == "Columns RTL":
407
+ return sorted(
408
+ boxes,
409
+ key=lambda box: (0 if box.x_center >= image_width / 2 else 1, box.y_center, box.x_center),
410
+ )
411
+ row_height = max(12, int(np.median([box.height for box in boxes])) if boxes else 24)
412
+ return sorted(boxes, key=lambda box: (math.floor(box.y_center / row_height), box.x_center))
413
+
414
+
415
+ def _crop(image: Image.Image, box: Box, padding: int) -> Image.Image:
416
+ return image.crop(
417
+ (
418
+ max(0, box.left - padding),
419
+ max(0, box.top - padding),
420
+ min(image.width, box.right + padding),
421
+ min(image.height, box.bottom + padding),
422
+ )
423
+ )
424
+
425
+
426
+ def _overlay_png_b64(image: Image.Image, boxes: list[Box]) -> str:
427
+ """Draw the ordered boxes on the page and return a base64 PNG."""
428
+ annotated = image.copy()
429
+ draw = ImageDraw.Draw(annotated)
430
+ for index, box in enumerate(boxes, start=1):
431
+ draw.rectangle(
432
+ (box.left, box.top, box.right, box.bottom), outline=(28, 120, 92), width=3
433
+ )
434
+ draw.text((box.left + 4, max(0, box.top - 12)), str(index), fill=(28, 120, 92))
435
+ longest = max(annotated.size)
436
+ if longest > OVERLAY_MAX_SIDE:
437
+ scale = OVERLAY_MAX_SIDE / longest
438
+ annotated = annotated.resize(
439
+ (max(1, round(annotated.width * scale)), max(1, round(annotated.height * scale))),
440
+ Image.LANCZOS,
441
+ )
442
+ buffer = io.BytesIO()
443
+ annotated.save(buffer, format="PNG", optimize=True)
444
+ return base64.b64encode(buffer.getvalue()).decode("ascii")
445
+
446
+
447
+ def _decode_image(raw: bytes) -> Image.Image:
448
+ """Validate and normalize an uploaded image; downscale the longest side."""
449
+ try:
450
+ probe = Image.open(io.BytesIO(raw))
451
+ image_format = probe.format
452
+ probe.load()
453
+ image = ImageOps.exif_transpose(probe).convert("RGB")
454
+ probe.close()
455
+ except UnidentifiedImageError as exc:
456
+ raise OcrError("file is not a decodable image", 400) from exc
457
+ except (OSError, ValueError) as exc:
458
+ raise OcrError(f"could not decode image: {exc}", 400) from exc
459
+ if image_format not in SUPPORTED_IMAGE_FORMATS:
460
+ raise OcrError(
461
+ f"unsupported image format {image_format!r}; expected one of "
462
+ + ", ".join(sorted(SUPPORTED_IMAGE_FORMATS)),
463
+ 415,
464
+ )
465
+ longest = max(image.size)
466
+ if longest > MAX_IMAGE_SIDE:
467
+ scale = MAX_IMAGE_SIDE / longest
468
+ image = image.resize(
469
+ (max(1, round(image.width * scale)), max(1, round(image.height * scale))),
470
+ Image.LANCZOS,
471
+ )
472
+ if image.width < 4 or image.height < 4:
473
+ raise OcrError("image is too small to recognize", 400)
474
+ return image
475
+
476
+
477
+ def _resolved_script(recognitions: list[Recognition], requested: str) -> str:
478
+ if requested in ("sirijonga", "devanagari"):
479
+ return requested
480
+ engines = [rec.engine for rec in recognitions if rec.text]
481
+ if not engines:
482
+ return "sirijonga"
483
+ return max(set(engines), key=engines.count)
484
+
485
+
486
+ def _run_line(image: Image.Image, script: str) -> dict[str, Any]:
487
+ crops = [_to_bgr(image)]
488
+ with ENGINES.lock:
489
+ recognitions = _recognize_routed(crops, script)
490
+ recognition = recognitions[0]
491
+ line = {
492
+ "text": recognition.text,
493
+ "confidence": recognition.confidence,
494
+ "box": [0, 0, image.width, image.height],
495
+ "script": recognition.engine,
496
+ }
497
+ return {
498
+ "text": recognition.text,
499
+ "lines": [line],
500
+ "script": _resolved_script(recognitions, script),
501
+ "truncated": False,
502
+ "overlay_png_b64": None,
503
+ }
504
+
505
+
506
+ def _run_page(image: Image.Image, script: str) -> dict[str, Any]:
507
+ with ENGINES.lock:
508
+ boxes = _detect_boxes(image)
509
+ ordered = _order_boxes(boxes, READING_ORDER, image.width)
510
+ truncated = MAX_LINES > 0 and len(ordered) > MAX_LINES
511
+ if truncated:
512
+ LOGGER.warning(
513
+ "page has %d detected lines, truncating to AMPIXA_OCR_MAX_LINES=%d",
514
+ len(ordered),
515
+ MAX_LINES,
516
+ )
517
+ ordered = ordered[:MAX_LINES]
518
+ crops = [_to_bgr(_crop(image, box, CROP_PADDING)) for box in ordered]
519
+ recognitions = _recognize_routed(crops, script)
520
+ overlay = _overlay_png_b64(image, ordered)
521
+
522
+ lines: list[dict[str, Any]] = []
523
+ texts: list[str] = []
524
+ for box, recognition in zip(ordered, recognitions, strict=True):
525
+ lines.append(
526
+ {
527
+ "text": recognition.text,
528
+ "confidence": recognition.confidence,
529
+ "box": box.as_list(),
530
+ "script": recognition.engine,
531
+ }
532
+ )
533
+ if recognition.text:
534
+ texts.append(recognition.text)
535
+ return {
536
+ "text": "\n".join(texts),
537
+ "lines": lines,
538
+ "script": _resolved_script(recognitions, script),
539
+ "truncated": truncated,
540
+ "overlay_png_b64": overlay,
541
+ }
542
+
543
+
544
+ @asynccontextmanager
545
+ async def _lifespan(_app: FastAPI) -> AsyncIterator[None]:
546
+ """Serve /health immediately; build the Paddle predictors in the background."""
547
+ LOGGER.info(
548
+ "starting OCR worker: model_root=%s detector=%s (%s/%d) max_lines=%d",
549
+ MODEL_ROOT,
550
+ DETECTOR_MODEL_NAME,
551
+ DETECTOR_LIMIT_TYPE,
552
+ DETECTOR_LIMIT_SIDE_LEN,
553
+ MAX_LINES,
554
+ )
555
+ loader = threading.Thread(target=ENGINES.load, name="ocr-model-load", daemon=True)
556
+ loader.start()
557
+ yield
558
+ LOGGER.info("OCR worker shutting down")
559
+
560
+
561
+ app = FastAPI(title="Ampixa OCR worker", docs_url=None, redoc_url=None, lifespan=_lifespan)
562
+
563
+
564
+ @app.exception_handler(OcrError)
565
+ async def _ocr_error_handler(_request: Request, exc: OcrError) -> JSONResponse:
566
+ return JSONResponse(status_code=exc.status_code, content={"error": exc.message})
567
+
568
+
569
+ @app.exception_handler(RequestValidationError)
570
+ async def _validation_error_handler(
571
+ _request: Request, exc: RequestValidationError
572
+ ) -> JSONResponse:
573
+ """Report malformed multipart bodies as {"error": ...} instead of FastAPI's 422."""
574
+ problems = "; ".join(
575
+ f"{'.'.join(str(part) for part in error.get('loc', ()))}: {error.get('msg', 'invalid')}"
576
+ for error in exc.errors()
577
+ )
578
+ return JSONResponse(status_code=400, content={"error": f"invalid request: {problems}"})
579
+
580
+
581
+ @app.exception_handler(StarletteHTTPException)
582
+ async def _http_error_handler(_request: Request, exc: StarletteHTTPException) -> JSONResponse:
583
+ """Keep 404/405 and friends on the {"error": ...} contract."""
584
+ return JSONResponse(status_code=exc.status_code, content={"error": str(exc.detail)})
585
+
586
+
587
+ @app.exception_handler(Exception)
588
+ async def _unhandled_error_handler(_request: Request, exc: Exception) -> JSONResponse:
589
+ """Last resort: log the traceback, return JSON, never a bare stack trace."""
590
+ LOGGER.error("unhandled error: %s\n%s", exc, traceback.format_exc())
591
+ return JSONResponse(
592
+ status_code=500, content={"error": f"internal error: {type(exc).__name__}"}
593
+ )
594
+
595
+
596
+ @app.get("/health")
597
+ def health() -> JSONResponse:
598
+ body: dict[str, Any] = {
599
+ "ok": ENGINES.load_error is None,
600
+ "worker": WORKER_NAME,
601
+ "loaded": ENGINES.loaded,
602
+ "models": {
603
+ "sirijonga": str(SIRIJONGA_MODEL_DIR),
604
+ "devanagari": str(DEVANAGARI_MODEL_DIR),
605
+ "sirijonga_model_name": SIRIJONGA_MODEL_NAME,
606
+ "devanagari_model_name": DEVANAGARI_MODEL_NAME,
607
+ "devanagari_backend": recognizer_engine(DEVANAGARI_MODEL_DIR),
608
+ "detector_model_name": DETECTOR_MODEL_NAME,
609
+ "detector_limit": f"{DETECTOR_LIMIT_TYPE}/{DETECTOR_LIMIT_SIDE_LEN}",
610
+ "max_lines": MAX_LINES,
611
+ "max_image_side": MAX_IMAGE_SIDE,
612
+ "load_seconds": round(ENGINES.load_seconds, 2) if ENGINES.load_seconds else None,
613
+ },
614
+ "device": "cpu",
615
+ }
616
+ if ENGINES.load_error is not None:
617
+ body["error"] = ENGINES.load_error
618
+ return JSONResponse(status_code=503, content=body)
619
+ return JSONResponse(status_code=200, content=body)
620
+
621
+
622
+ @app.post("/recognize")
623
+ async def recognize(
624
+ file: UploadFile = File(...),
625
+ mode: str = Form("page"),
626
+ script: str = Form("auto"),
627
+ ) -> JSONResponse:
628
+ started = time.perf_counter()
629
+ mode = (mode or "page").strip().lower()
630
+ script = (script or "auto").strip().lower()
631
+ if mode not in SUPPORTED_MODES:
632
+ raise OcrError(f"mode must be one of {', '.join(SUPPORTED_MODES)}", 400)
633
+ if script not in SUPPORTED_SCRIPTS:
634
+ raise OcrError(f"script must be one of {', '.join(SUPPORTED_SCRIPTS)}", 400)
635
+ if not ENGINES.loaded:
636
+ if ENGINES.load_error is not None:
637
+ raise OcrError(f"OCR models failed to load: {ENGINES.load_error}", 503)
638
+ raise OcrError("OCR models are still loading", 503)
639
+
640
+ try:
641
+ raw = await file.read()
642
+ except Exception as exc: # noqa: BLE001 - upload stream failures are client errors
643
+ LOGGER.error("upload read failed: %s\n%s", exc, traceback.format_exc())
644
+ raise OcrError(f"could not read uploaded file: {exc}", 400) from exc
645
+ finally:
646
+ await file.close()
647
+
648
+ if not raw:
649
+ raise OcrError("uploaded file is empty", 400)
650
+ if len(raw) > MAX_UPLOAD_BYTES:
651
+ raise OcrError(
652
+ f"image is {len(raw)} bytes, limit is {MAX_UPLOAD_BYTES} bytes", 413
653
+ )
654
+
655
+ image = _decode_image(raw)
656
+ runner = _run_line if mode == "line" else _run_page
657
+ try:
658
+ result = await run_in_threadpool(runner, image, script)
659
+ except OcrError:
660
+ raise
661
+ except Exception as exc: # noqa: BLE001 - converted to a JSON 500 below
662
+ LOGGER.error(
663
+ "recognition failed mode=%s script=%s: %s\n%s",
664
+ mode,
665
+ script,
666
+ exc,
667
+ traceback.format_exc(),
668
+ )
669
+ raise OcrError(f"recognition failed: {type(exc).__name__}: {exc}", 500) from exc
670
+
671
+ elapsed = time.perf_counter() - started
672
+ LOGGER.info(
673
+ "recognize mode=%s script=%s -> %s bytes=%d size=%dx%d lines=%d truncated=%s chars=%d %.2fs",
674
+ mode,
675
+ script,
676
+ result["script"],
677
+ len(raw),
678
+ image.width,
679
+ image.height,
680
+ len(result["lines"]),
681
+ result["truncated"],
682
+ len(result["text"]),
683
+ elapsed,
684
+ )
685
+ return JSONResponse(
686
+ status_code=200,
687
+ content={
688
+ "text": result["text"],
689
+ "lines": result["lines"],
690
+ "script": result["script"],
691
+ "mode": mode,
692
+ "elapsed_s": round(elapsed, 3),
693
+ "image_size": [image.width, image.height],
694
+ "truncated": result["truncated"],
695
+ "overlay_png_b64": result["overlay_png_b64"],
696
+ },
697
+ )
698
+
699
+
700
+ def main() -> int:
701
+ parser = argparse.ArgumentParser(description="Ampixa OCR worker (Limbu Sirijonga + Devanagari)")
702
+ parser.add_argument("--host", default=DEFAULT_HOST)
703
+ parser.add_argument("--port", type=int, default=DEFAULT_PORT)
704
+ args = parser.parse_args()
705
+ logging.basicConfig(
706
+ level=logging.INFO,
707
+ format="%(asctime)s %(levelname)s %(name)s %(message)s",
708
+ stream=sys.stdout,
709
+ )
710
+ uvicorn.run(app, host=args.host, port=args.port, log_level="info", access_log=False)
711
+ return 0
712
+
713
+
714
+ if __name__ == "__main__":
715
+ raise SystemExit(main())
k2-ampixa-demo-ocr/workers-ocr/server.py.bak-20260924-deva-v2 ADDED
@@ -0,0 +1,687 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ampixa OCR worker: Limbu Sirijonga + Devanagari OCR on CPU via PaddleOCR.
2
+
3
+ Ported from the non-Gradio inference paths of the private Hugging Face Space
4
+ ``ampixa/limbu-ocr`` (``app.py``, sha256 cffc507fd79f1ee31b6469315c92ff6b8e2360affe617ae8e2f8fa709b92fa01).
5
+
6
+ Two routes are exposed:
7
+
8
+ ``line``
9
+ The uploaded image is treated as a single cropped text line and sent
10
+ straight to the recognizer(s).
11
+
12
+ ``page``
13
+ PaddleOCR full-page text detection produces line boxes, the boxes are put
14
+ into reading order, every crop is recognized, and a PNG overlay with the
15
+ boxes drawn on it is returned alongside the text.
16
+
17
+ Everything runs on CPU: paddlepaddle has no Metal/MPS backend on macOS arm64.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import argparse
23
+ import base64
24
+ import io
25
+ import logging
26
+ import math
27
+ import os
28
+ import re
29
+ import sys
30
+ import threading
31
+ import time
32
+ import traceback
33
+ from collections.abc import AsyncIterator
34
+ from contextlib import asynccontextmanager
35
+ from dataclasses import dataclass
36
+ from pathlib import Path
37
+ from typing import Any
38
+
39
+ import numpy as np
40
+ import uvicorn
41
+ from fastapi import FastAPI, File, Form, Request, UploadFile
42
+ from fastapi.exceptions import RequestValidationError
43
+ from fastapi.responses import JSONResponse
44
+ from paddleocr import TextDetection
45
+ from paddleocr._models.text_recognition import TextRecognition
46
+ from PIL import Image, ImageDraw, ImageOps, UnidentifiedImageError
47
+ from starlette.concurrency import run_in_threadpool
48
+ from starlette.exceptions import HTTPException as StarletteHTTPException
49
+
50
+ WORKER_NAME = "ocr"
51
+ DEFAULT_HOST = os.environ.get("AMPIXA_OCR_HOST", "127.0.0.1")
52
+ DEFAULT_PORT = int(os.environ.get("AMPIXA_OCR_PORT", "8906"))
53
+
54
+ MODEL_ROOT = Path(
55
+ os.environ.get(
56
+ "AMPIXA_OCR_MODEL_ROOT",
57
+ str(Path.home() / "ampixa-demo" / "models" / "ocr" / "space" / "models"),
58
+ )
59
+ ).expanduser()
60
+ SIRIJONGA_MODEL_DIR = MODEL_ROOT / "limbu-sirijonga-ft-v2" / "artifacts"
61
+ DEVANAGARI_MODEL_DIR = MODEL_ROOT / "deva-v2" / "artifacts"
62
+
63
+ # The PP-OCRv5 recognizer packs live under model_dir; the character dictionary is
64
+ # embedded in inference.yml (PostProcess.character_dict), so the standalone .txt
65
+ # dictionaries shipped next to them are documentation only and are not loaded.
66
+ RECOGNIZER_MODEL_NAME = "PP-OCRv5_mobile_rec"
67
+ REQUIRED_RECOGNIZER_FILES = ("inference.json", "inference.pdiparams", "inference.yml")
68
+
69
+ MAX_UPLOAD_BYTES = 10 * 1024 * 1024
70
+ MAX_IMAGE_SIDE = 2000
71
+ OVERLAY_MAX_SIDE = int(os.environ.get("AMPIXA_OCR_OVERLAY_MAX_SIDE", "1400"))
72
+ MAX_LINES = int(os.environ.get("AMPIXA_OCR_MAX_LINES", "120"))
73
+ RECOGNIZER_BATCH_SIZE = int(os.environ.get("AMPIXA_OCR_REC_BATCH_SIZE", "16"))
74
+
75
+ # Detector settings. The Space leaves limit_type at the PaddleX default ("min"),
76
+ # which upscales every page so its *shortest* side is >= limit_side_len and costs
77
+ # ~8 s per page on this host. "max" bounds the *longest* side instead: 2.4x faster
78
+ # on a full newspaper page for the same box count (303 vs 305 boxes measured).
79
+ DETECTOR_LIMIT_SIDE_LEN = int(os.environ.get("AMPIXA_OCR_DET_LIMIT_SIDE_LEN", "1280"))
80
+ DETECTOR_LIMIT_TYPE = os.environ.get("AMPIXA_OCR_DET_LIMIT_TYPE", "max")
81
+ DETECTOR_THRESH = float(os.environ.get("AMPIXA_OCR_DET_THRESH", "0.30"))
82
+ DETECTOR_BOX_THRESH = float(os.environ.get("AMPIXA_OCR_DET_BOX_THRESH", "0.60"))
83
+ DETECTOR_UNCLIP_RATIO = float(os.environ.get("AMPIXA_OCR_DET_UNCLIP_RATIO", "1.5"))
84
+ DETECTOR_MODEL_NAME = "PP-OCRv6_medium_det" # PaddleOCR 3.7.0 default text detector.
85
+
86
+ CROP_PADDING = 12 # Space default for the Paddle page tab.
87
+ READING_ORDER = "Rows LTR" # Space default for the Paddle page tab.
88
+
89
+ SUPPORTED_MODES = ("line", "page")
90
+ SUPPORTED_SCRIPTS = ("auto", "sirijonga", "devanagari")
91
+ SUPPORTED_IMAGE_FORMATS = {"PNG": "image/png", "JPEG": "image/jpeg", "WEBP": "image/webp"}
92
+
93
+ LIMBU_RE = re.compile("[\u1900-\u194F]")
94
+ DEVANAGARI_RE = re.compile("[\u0900-\u097F]")
95
+
96
+ # Upper bound of _script_score() for a Devanagari candidate: rec_score is a
97
+ # probability (<= 1.0) and the Devanagari bonus saturates at 12 * 0.035. If the
98
+ # Sirijonga candidate already scores at least this much, the Devanagari pass
99
+ # cannot change the outcome and is skipped. This is an exact prune, not an
100
+ # approximation: it never changes the selected text.
101
+ MAX_DEVANAGARI_SCRIPT_SCORE = 1.0 + 0.035 * 12
102
+
103
+ LOGGER = logging.getLogger("ampixa.ocr")
104
+
105
+
106
+ @dataclass(slots=True)
107
+ class Recognition:
108
+ """One recognizer output for one crop."""
109
+
110
+ text: str
111
+ confidence: float | None
112
+ engine: str
113
+
114
+
115
+ @dataclass(slots=True)
116
+ class Box:
117
+ """Axis-aligned line box in processed-image pixel coordinates."""
118
+
119
+ left: int
120
+ top: int
121
+ right: int
122
+ bottom: int
123
+ confidence: float
124
+
125
+ @property
126
+ def width(self) -> int:
127
+ return max(0, self.right - self.left)
128
+
129
+ @property
130
+ def height(self) -> int:
131
+ return max(0, self.bottom - self.top)
132
+
133
+ @property
134
+ def x_center(self) -> float:
135
+ return (self.left + self.right) / 2.0
136
+
137
+ @property
138
+ def y_center(self) -> float:
139
+ return (self.top + self.bottom) / 2.0
140
+
141
+ def as_list(self) -> list[int]:
142
+ return [self.left, self.top, self.right, self.bottom]
143
+
144
+
145
+ class OcrError(Exception):
146
+ """Client-visible OCR failure carrying an HTTP status code."""
147
+
148
+ def __init__(self, message: str, status_code: int) -> None:
149
+ super().__init__(message)
150
+ self.message = message
151
+ self.status_code = status_code
152
+
153
+
154
+ class Engines:
155
+ """Holds the Paddle predictors and serializes every call into them.
156
+
157
+ Paddle predictors are not documented as thread-safe, and uvicorn runs request
158
+ handlers in a thread pool, so every predict() call goes through ``lock``.
159
+ """
160
+
161
+ def __init__(self) -> None:
162
+ self.lock = threading.Lock()
163
+ self.recognizers: dict[str, Any] = {}
164
+ self.detector: Any = None
165
+ self.loaded = False
166
+ self.load_seconds: float | None = None
167
+ self.load_error: str | None = None
168
+
169
+ def ensure_model_files(self) -> None:
170
+ missing: list[str] = []
171
+ for model_dir in (SIRIJONGA_MODEL_DIR, DEVANAGARI_MODEL_DIR):
172
+ for name in REQUIRED_RECOGNIZER_FILES:
173
+ path = model_dir / name
174
+ if not path.is_file():
175
+ missing.append(str(path))
176
+ if missing:
177
+ raise RuntimeError("missing OCR model artifacts: " + ", ".join(missing))
178
+
179
+ def load(self) -> None:
180
+ """Build every predictor once. Called from a background thread at startup."""
181
+ started = time.perf_counter()
182
+ try:
183
+ self.ensure_model_files()
184
+ for engine, model_dir in (
185
+ ("sirijonga", SIRIJONGA_MODEL_DIR),
186
+ ("devanagari", DEVANAGARI_MODEL_DIR),
187
+ ):
188
+ step = time.perf_counter()
189
+ self.recognizers[engine] = TextRecognition(
190
+ model_name=RECOGNIZER_MODEL_NAME,
191
+ model_dir=str(model_dir),
192
+ device="cpu",
193
+ )
194
+ LOGGER.info(
195
+ "loaded recognizer engine=%s dir=%s in %.2fs",
196
+ engine,
197
+ model_dir,
198
+ time.perf_counter() - step,
199
+ )
200
+ step = time.perf_counter()
201
+ self.detector = TextDetection(
202
+ limit_side_len=DETECTOR_LIMIT_SIDE_LEN,
203
+ limit_type=DETECTOR_LIMIT_TYPE,
204
+ thresh=DETECTOR_THRESH,
205
+ box_thresh=DETECTOR_BOX_THRESH,
206
+ unclip_ratio=DETECTOR_UNCLIP_RATIO,
207
+ device="cpu",
208
+ )
209
+ LOGGER.info("loaded text detector in %.2fs", time.perf_counter() - step)
210
+ except Exception as exc: # noqa: BLE001 - recorded and surfaced through /health
211
+ self.load_error = f"{type(exc).__name__}: {exc}"
212
+ LOGGER.error("model load failed: %s\n%s", self.load_error, traceback.format_exc())
213
+ return
214
+ self.load_seconds = time.perf_counter() - started
215
+ self.loaded = True
216
+ LOGGER.info("all OCR models loaded in %.2fs", self.load_seconds)
217
+
218
+
219
+ ENGINES = Engines()
220
+
221
+
222
+ def _payload(item: object) -> dict[str, Any] | None:
223
+ """Unwrap a PaddleX result object into its plain result mapping."""
224
+ candidate: object = item
225
+ if not isinstance(candidate, dict):
226
+ candidate = getattr(item, "json", None)
227
+ if not isinstance(candidate, dict):
228
+ return None
229
+ nested = candidate.get("res")
230
+ return nested if isinstance(nested, dict) else candidate
231
+
232
+
233
+ def _script_score(recognition: Recognition) -> float:
234
+ """Score copied verbatim from the Space's app.py ``_script_score``.
235
+
236
+ The Sirijonga bonus (0.06/char, capped at 12 chars) is larger than the
237
+ Devanagari one (0.035/char), so this rule leans towards Sirijonga on mixed
238
+ pages. Callers that know the script should pass it explicitly.
239
+ """
240
+ text = recognition.text or ""
241
+ confidence = recognition.confidence if recognition.confidence is not None else 0.0
242
+ limbu_chars = len(LIMBU_RE.findall(text))
243
+ deva_chars = len(DEVANAGARI_RE.findall(text))
244
+ if recognition.engine == "sirijonga":
245
+ return confidence + min(limbu_chars, 12) * 0.06 - min(deva_chars, 12) * 0.04
246
+ if recognition.engine == "devanagari":
247
+ return confidence + min(deva_chars, 12) * 0.035 - min(limbu_chars, 12) * 0.08
248
+ return confidence
249
+
250
+
251
+ def _recognize_batch(engine: str, crops: list[np.ndarray]) -> list[Recognition]:
252
+ """Run one recognizer over a list of BGR crops, preserving input order."""
253
+ if not crops:
254
+ return []
255
+ predictor = ENGINES.recognizers.get(engine)
256
+ if predictor is None:
257
+ raise OcrError(f"recognizer '{engine}' is not loaded", 503)
258
+ raw = list(predictor.predict(crops, batch_size=RECOGNIZER_BATCH_SIZE))
259
+ if len(raw) != len(crops):
260
+ raise OcrError(
261
+ f"recognizer '{engine}' returned {len(raw)} results for {len(crops)} crops",
262
+ 500,
263
+ )
264
+ results: list[Recognition] = []
265
+ for item in raw:
266
+ payload = _payload(item)
267
+ if payload is None:
268
+ results.append(Recognition(text="", confidence=None, engine=engine))
269
+ continue
270
+ text = payload.get("rec_text") or payload.get("text") or ""
271
+ score = payload.get("rec_score")
272
+ if score is None:
273
+ score = payload.get("score")
274
+ confidence: float | None
275
+ try:
276
+ confidence = float(score) if score is not None else None
277
+ except (TypeError, ValueError):
278
+ confidence = None
279
+ results.append(Recognition(text=str(text).strip(), confidence=confidence, engine=engine))
280
+ return results
281
+
282
+
283
+ def _recognize_routed(crops: list[np.ndarray], script: str) -> list[Recognition]:
284
+ """Recognize crops under the requested script routing.
285
+
286
+ ``auto`` reproduces the Space: both engines run and the higher ``_script_score``
287
+ wins, with ties going to Sirijonga. The Devanagari pass is skipped for crops
288
+ whose Sirijonga score already exceeds every score Devanagari could reach.
289
+ """
290
+ if script in ("sirijonga", "devanagari"):
291
+ return _recognize_batch(script, crops)
292
+
293
+ sirijonga = _recognize_batch("sirijonga", crops)
294
+ scores = [_script_score(rec) for rec in sirijonga]
295
+ pending = [i for i, score in enumerate(scores) if score < MAX_DEVANAGARI_SCRIPT_SCORE]
296
+ if not pending:
297
+ return sirijonga
298
+ devanagari = _recognize_batch("devanagari", [crops[i] for i in pending])
299
+ selected = list(sirijonga)
300
+ for index, candidate in zip(pending, devanagari, strict=True):
301
+ if _script_score(candidate) > scores[index]:
302
+ selected[index] = candidate
303
+ return selected
304
+
305
+
306
+ def _to_bgr(image: Image.Image) -> np.ndarray:
307
+ """PIL RGB image -> contiguous BGR ndarray, the layout PaddleX expects."""
308
+ return np.asarray(image)[:, :, ::-1].copy()
309
+
310
+
311
+ def _box_from_polygon(polygon: object, *, width: int, height: int, confidence: float) -> Box | None:
312
+ array = np.asarray(polygon, dtype=np.float64).reshape(-1, 2)
313
+ if array.size == 0:
314
+ return None
315
+ box = Box(
316
+ left=max(0, int(math.floor(float(array[:, 0].min())))),
317
+ top=max(0, int(math.floor(float(array[:, 1].min())))),
318
+ right=min(width, int(math.ceil(float(array[:, 0].max())))),
319
+ bottom=min(height, int(math.ceil(float(array[:, 1].max())))),
320
+ confidence=confidence,
321
+ )
322
+ return box if box.width > 0 and box.height > 0 else None
323
+
324
+
325
+ def _detect_boxes(image: Image.Image) -> list[Box]:
326
+ """Full-page text detection -> axis-aligned line boxes."""
327
+ if ENGINES.detector is None:
328
+ raise OcrError("text detector is not loaded", 503)
329
+ raw = list(ENGINES.detector.predict(_to_bgr(image)))
330
+ boxes: list[Box] = []
331
+ for item in raw:
332
+ payload = _payload(item)
333
+ if payload is None:
334
+ continue
335
+ polygons = payload.get("dt_polys")
336
+ if polygons is None:
337
+ continue
338
+ # dt_scores comes back as an ndarray, so never use truthiness on it.
339
+ scores = payload.get("dt_scores")
340
+ if scores is None:
341
+ scores = []
342
+ for index, polygon in enumerate(polygons):
343
+ confidence = 1.0
344
+ if index < len(scores) and scores[index] is not None:
345
+ try:
346
+ confidence = float(scores[index])
347
+ except (TypeError, ValueError):
348
+ confidence = 1.0
349
+ box = _box_from_polygon(
350
+ polygon, width=image.width, height=image.height, confidence=confidence
351
+ )
352
+ if box is not None:
353
+ boxes.append(box)
354
+ return boxes
355
+
356
+
357
+ def _has_two_column_shape(boxes: list[Box], image_width: int) -> bool:
358
+ """Copied from the Space's app.py."""
359
+ if len(boxes) < 8 or image_width <= 0:
360
+ return False
361
+ midpoint = image_width / 2.0
362
+ left = [box for box in boxes if box.x_center < midpoint]
363
+ right = [box for box in boxes if box.x_center >= midpoint]
364
+ if len(left) < 3 or len(right) < 3:
365
+ return False
366
+ left_median = float(np.median([box.x_center for box in left]))
367
+ right_median = float(np.median([box.x_center for box in right]))
368
+ return (right_median - left_median) >= image_width * 0.22
369
+
370
+
371
+ def _order_boxes(boxes: list[Box], mode: str, image_width: int) -> list[Box]:
372
+ """Copied from the Space's app.py ``_order_image_line_boxes``."""
373
+ if mode == "Auto":
374
+ mode = "Columns LTR" if _has_two_column_shape(boxes, image_width) else "Rows LTR"
375
+ if mode == "Columns LTR":
376
+ return sorted(
377
+ boxes,
378
+ key=lambda box: (0 if box.x_center < image_width / 2 else 1, box.y_center, box.x_center),
379
+ )
380
+ if mode == "Columns RTL":
381
+ return sorted(
382
+ boxes,
383
+ key=lambda box: (0 if box.x_center >= image_width / 2 else 1, box.y_center, box.x_center),
384
+ )
385
+ row_height = max(12, int(np.median([box.height for box in boxes])) if boxes else 24)
386
+ return sorted(boxes, key=lambda box: (math.floor(box.y_center / row_height), box.x_center))
387
+
388
+
389
+ def _crop(image: Image.Image, box: Box, padding: int) -> Image.Image:
390
+ return image.crop(
391
+ (
392
+ max(0, box.left - padding),
393
+ max(0, box.top - padding),
394
+ min(image.width, box.right + padding),
395
+ min(image.height, box.bottom + padding),
396
+ )
397
+ )
398
+
399
+
400
+ def _overlay_png_b64(image: Image.Image, boxes: list[Box]) -> str:
401
+ """Draw the ordered boxes on the page and return a base64 PNG."""
402
+ annotated = image.copy()
403
+ draw = ImageDraw.Draw(annotated)
404
+ for index, box in enumerate(boxes, start=1):
405
+ draw.rectangle(
406
+ (box.left, box.top, box.right, box.bottom), outline=(28, 120, 92), width=3
407
+ )
408
+ draw.text((box.left + 4, max(0, box.top - 12)), str(index), fill=(28, 120, 92))
409
+ longest = max(annotated.size)
410
+ if longest > OVERLAY_MAX_SIDE:
411
+ scale = OVERLAY_MAX_SIDE / longest
412
+ annotated = annotated.resize(
413
+ (max(1, round(annotated.width * scale)), max(1, round(annotated.height * scale))),
414
+ Image.LANCZOS,
415
+ )
416
+ buffer = io.BytesIO()
417
+ annotated.save(buffer, format="PNG", optimize=True)
418
+ return base64.b64encode(buffer.getvalue()).decode("ascii")
419
+
420
+
421
+ def _decode_image(raw: bytes) -> Image.Image:
422
+ """Validate and normalize an uploaded image; downscale the longest side."""
423
+ try:
424
+ probe = Image.open(io.BytesIO(raw))
425
+ image_format = probe.format
426
+ probe.load()
427
+ image = ImageOps.exif_transpose(probe).convert("RGB")
428
+ probe.close()
429
+ except UnidentifiedImageError as exc:
430
+ raise OcrError("file is not a decodable image", 400) from exc
431
+ except (OSError, ValueError) as exc:
432
+ raise OcrError(f"could not decode image: {exc}", 400) from exc
433
+ if image_format not in SUPPORTED_IMAGE_FORMATS:
434
+ raise OcrError(
435
+ f"unsupported image format {image_format!r}; expected one of "
436
+ + ", ".join(sorted(SUPPORTED_IMAGE_FORMATS)),
437
+ 415,
438
+ )
439
+ longest = max(image.size)
440
+ if longest > MAX_IMAGE_SIDE:
441
+ scale = MAX_IMAGE_SIDE / longest
442
+ image = image.resize(
443
+ (max(1, round(image.width * scale)), max(1, round(image.height * scale))),
444
+ Image.LANCZOS,
445
+ )
446
+ if image.width < 4 or image.height < 4:
447
+ raise OcrError("image is too small to recognize", 400)
448
+ return image
449
+
450
+
451
+ def _resolved_script(recognitions: list[Recognition], requested: str) -> str:
452
+ if requested in ("sirijonga", "devanagari"):
453
+ return requested
454
+ engines = [rec.engine for rec in recognitions if rec.text]
455
+ if not engines:
456
+ return "sirijonga"
457
+ return max(set(engines), key=engines.count)
458
+
459
+
460
+ def _run_line(image: Image.Image, script: str) -> dict[str, Any]:
461
+ crops = [_to_bgr(image)]
462
+ with ENGINES.lock:
463
+ recognitions = _recognize_routed(crops, script)
464
+ recognition = recognitions[0]
465
+ line = {
466
+ "text": recognition.text,
467
+ "confidence": recognition.confidence,
468
+ "box": [0, 0, image.width, image.height],
469
+ "script": recognition.engine,
470
+ }
471
+ return {
472
+ "text": recognition.text,
473
+ "lines": [line],
474
+ "script": _resolved_script(recognitions, script),
475
+ "truncated": False,
476
+ "overlay_png_b64": None,
477
+ }
478
+
479
+
480
+ def _run_page(image: Image.Image, script: str) -> dict[str, Any]:
481
+ with ENGINES.lock:
482
+ boxes = _detect_boxes(image)
483
+ ordered = _order_boxes(boxes, READING_ORDER, image.width)
484
+ truncated = MAX_LINES > 0 and len(ordered) > MAX_LINES
485
+ if truncated:
486
+ LOGGER.warning(
487
+ "page has %d detected lines, truncating to AMPIXA_OCR_MAX_LINES=%d",
488
+ len(ordered),
489
+ MAX_LINES,
490
+ )
491
+ ordered = ordered[:MAX_LINES]
492
+ crops = [_to_bgr(_crop(image, box, CROP_PADDING)) for box in ordered]
493
+ recognitions = _recognize_routed(crops, script)
494
+ overlay = _overlay_png_b64(image, ordered)
495
+
496
+ lines: list[dict[str, Any]] = []
497
+ texts: list[str] = []
498
+ for box, recognition in zip(ordered, recognitions, strict=True):
499
+ lines.append(
500
+ {
501
+ "text": recognition.text,
502
+ "confidence": recognition.confidence,
503
+ "box": box.as_list(),
504
+ "script": recognition.engine,
505
+ }
506
+ )
507
+ if recognition.text:
508
+ texts.append(recognition.text)
509
+ return {
510
+ "text": "\n".join(texts),
511
+ "lines": lines,
512
+ "script": _resolved_script(recognitions, script),
513
+ "truncated": truncated,
514
+ "overlay_png_b64": overlay,
515
+ }
516
+
517
+
518
+ @asynccontextmanager
519
+ async def _lifespan(_app: FastAPI) -> AsyncIterator[None]:
520
+ """Serve /health immediately; build the Paddle predictors in the background."""
521
+ LOGGER.info(
522
+ "starting OCR worker: model_root=%s detector=%s (%s/%d) max_lines=%d",
523
+ MODEL_ROOT,
524
+ DETECTOR_MODEL_NAME,
525
+ DETECTOR_LIMIT_TYPE,
526
+ DETECTOR_LIMIT_SIDE_LEN,
527
+ MAX_LINES,
528
+ )
529
+ loader = threading.Thread(target=ENGINES.load, name="ocr-model-load", daemon=True)
530
+ loader.start()
531
+ yield
532
+ LOGGER.info("OCR worker shutting down")
533
+
534
+
535
+ app = FastAPI(title="Ampixa OCR worker", docs_url=None, redoc_url=None, lifespan=_lifespan)
536
+
537
+
538
+ @app.exception_handler(OcrError)
539
+ async def _ocr_error_handler(_request: Request, exc: OcrError) -> JSONResponse:
540
+ return JSONResponse(status_code=exc.status_code, content={"error": exc.message})
541
+
542
+
543
+ @app.exception_handler(RequestValidationError)
544
+ async def _validation_error_handler(
545
+ _request: Request, exc: RequestValidationError
546
+ ) -> JSONResponse:
547
+ """Report malformed multipart bodies as {"error": ...} instead of FastAPI's 422."""
548
+ problems = "; ".join(
549
+ f"{'.'.join(str(part) for part in error.get('loc', ()))}: {error.get('msg', 'invalid')}"
550
+ for error in exc.errors()
551
+ )
552
+ return JSONResponse(status_code=400, content={"error": f"invalid request: {problems}"})
553
+
554
+
555
+ @app.exception_handler(StarletteHTTPException)
556
+ async def _http_error_handler(_request: Request, exc: StarletteHTTPException) -> JSONResponse:
557
+ """Keep 404/405 and friends on the {"error": ...} contract."""
558
+ return JSONResponse(status_code=exc.status_code, content={"error": str(exc.detail)})
559
+
560
+
561
+ @app.exception_handler(Exception)
562
+ async def _unhandled_error_handler(_request: Request, exc: Exception) -> JSONResponse:
563
+ """Last resort: log the traceback, return JSON, never a bare stack trace."""
564
+ LOGGER.error("unhandled error: %s\n%s", exc, traceback.format_exc())
565
+ return JSONResponse(
566
+ status_code=500, content={"error": f"internal error: {type(exc).__name__}"}
567
+ )
568
+
569
+
570
+ @app.get("/health")
571
+ def health() -> JSONResponse:
572
+ body: dict[str, Any] = {
573
+ "ok": ENGINES.load_error is None,
574
+ "worker": WORKER_NAME,
575
+ "loaded": ENGINES.loaded,
576
+ "models": {
577
+ "sirijonga": str(SIRIJONGA_MODEL_DIR),
578
+ "devanagari": str(DEVANAGARI_MODEL_DIR),
579
+ "recognizer_model_name": RECOGNIZER_MODEL_NAME,
580
+ "detector_model_name": DETECTOR_MODEL_NAME,
581
+ "detector_limit": f"{DETECTOR_LIMIT_TYPE}/{DETECTOR_LIMIT_SIDE_LEN}",
582
+ "max_lines": MAX_LINES,
583
+ "max_image_side": MAX_IMAGE_SIDE,
584
+ "load_seconds": round(ENGINES.load_seconds, 2) if ENGINES.load_seconds else None,
585
+ },
586
+ "device": "cpu",
587
+ }
588
+ if ENGINES.load_error is not None:
589
+ body["error"] = ENGINES.load_error
590
+ return JSONResponse(status_code=503, content=body)
591
+ return JSONResponse(status_code=200, content=body)
592
+
593
+
594
+ @app.post("/recognize")
595
+ async def recognize(
596
+ file: UploadFile = File(...),
597
+ mode: str = Form("page"),
598
+ script: str = Form("auto"),
599
+ ) -> JSONResponse:
600
+ started = time.perf_counter()
601
+ mode = (mode or "page").strip().lower()
602
+ script = (script or "auto").strip().lower()
603
+ if mode not in SUPPORTED_MODES:
604
+ raise OcrError(f"mode must be one of {', '.join(SUPPORTED_MODES)}", 400)
605
+ if script not in SUPPORTED_SCRIPTS:
606
+ raise OcrError(f"script must be one of {', '.join(SUPPORTED_SCRIPTS)}", 400)
607
+ if not ENGINES.loaded:
608
+ if ENGINES.load_error is not None:
609
+ raise OcrError(f"OCR models failed to load: {ENGINES.load_error}", 503)
610
+ raise OcrError("OCR models are still loading", 503)
611
+
612
+ try:
613
+ raw = await file.read()
614
+ except Exception as exc: # noqa: BLE001 - upload stream failures are client errors
615
+ LOGGER.error("upload read failed: %s\n%s", exc, traceback.format_exc())
616
+ raise OcrError(f"could not read uploaded file: {exc}", 400) from exc
617
+ finally:
618
+ await file.close()
619
+
620
+ if not raw:
621
+ raise OcrError("uploaded file is empty", 400)
622
+ if len(raw) > MAX_UPLOAD_BYTES:
623
+ raise OcrError(
624
+ f"image is {len(raw)} bytes, limit is {MAX_UPLOAD_BYTES} bytes", 413
625
+ )
626
+
627
+ image = _decode_image(raw)
628
+ runner = _run_line if mode == "line" else _run_page
629
+ try:
630
+ result = await run_in_threadpool(runner, image, script)
631
+ except OcrError:
632
+ raise
633
+ except Exception as exc: # noqa: BLE001 - converted to a JSON 500 below
634
+ LOGGER.error(
635
+ "recognition failed mode=%s script=%s: %s\n%s",
636
+ mode,
637
+ script,
638
+ exc,
639
+ traceback.format_exc(),
640
+ )
641
+ raise OcrError(f"recognition failed: {type(exc).__name__}: {exc}", 500) from exc
642
+
643
+ elapsed = time.perf_counter() - started
644
+ LOGGER.info(
645
+ "recognize mode=%s script=%s -> %s bytes=%d size=%dx%d lines=%d truncated=%s chars=%d %.2fs",
646
+ mode,
647
+ script,
648
+ result["script"],
649
+ len(raw),
650
+ image.width,
651
+ image.height,
652
+ len(result["lines"]),
653
+ result["truncated"],
654
+ len(result["text"]),
655
+ elapsed,
656
+ )
657
+ return JSONResponse(
658
+ status_code=200,
659
+ content={
660
+ "text": result["text"],
661
+ "lines": result["lines"],
662
+ "script": result["script"],
663
+ "mode": mode,
664
+ "elapsed_s": round(elapsed, 3),
665
+ "image_size": [image.width, image.height],
666
+ "truncated": result["truncated"],
667
+ "overlay_png_b64": result["overlay_png_b64"],
668
+ },
669
+ )
670
+
671
+
672
+ def main() -> int:
673
+ parser = argparse.ArgumentParser(description="Ampixa OCR worker (Limbu Sirijonga + Devanagari)")
674
+ parser.add_argument("--host", default=DEFAULT_HOST)
675
+ parser.add_argument("--port", type=int, default=DEFAULT_PORT)
676
+ args = parser.parse_args()
677
+ logging.basicConfig(
678
+ level=logging.INFO,
679
+ format="%(asctime)s %(levelname)s %(name)s %(message)s",
680
+ stream=sys.stdout,
681
+ )
682
+ uvicorn.run(app, host=args.host, port=args.port, log_level="info", access_log=False)
683
+ return 0
684
+
685
+
686
+ if __name__ == "__main__":
687
+ raise SystemExit(main())
k2-ampixa-demo-ocr/workers-ocr/server.py.bak-20260924-ft-v2 ADDED
@@ -0,0 +1,715 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ampixa OCR worker: Limbu Sirijonga + Devanagari OCR on CPU via PaddleOCR.
2
+
3
+ Ported from the non-Gradio inference paths of the private Hugging Face Space
4
+ ``ampixa/limbu-ocr`` (``app.py``, sha256 cffc507fd79f1ee31b6469315c92ff6b8e2360affe617ae8e2f8fa709b92fa01).
5
+
6
+ Two routes are exposed:
7
+
8
+ ``line``
9
+ The uploaded image is treated as a single cropped text line and sent
10
+ straight to the recognizer(s).
11
+
12
+ ``page``
13
+ PaddleOCR full-page text detection produces line boxes, the boxes are put
14
+ into reading order, every crop is recognized, and a PNG overlay with the
15
+ boxes drawn on it is returned alongside the text.
16
+
17
+ Everything runs on CPU: paddlepaddle has no Metal/MPS backend on macOS arm64.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import argparse
23
+ import base64
24
+ import io
25
+ import logging
26
+ import math
27
+ import os
28
+ import re
29
+ import sys
30
+ import threading
31
+ import time
32
+ import traceback
33
+ from collections.abc import AsyncIterator
34
+ from contextlib import asynccontextmanager
35
+ from dataclasses import dataclass
36
+ from pathlib import Path
37
+ from typing import Any
38
+
39
+ import numpy as np
40
+ import uvicorn
41
+ from fastapi import FastAPI, File, Form, Request, UploadFile
42
+ from fastapi.exceptions import RequestValidationError
43
+ from fastapi.responses import JSONResponse
44
+ from paddleocr import TextDetection
45
+ from paddleocr._models.text_recognition import TextRecognition
46
+ from paddlex import create_model
47
+ from PIL import Image, ImageDraw, ImageOps, UnidentifiedImageError
48
+ from starlette.concurrency import run_in_threadpool
49
+ from starlette.exceptions import HTTPException as StarletteHTTPException
50
+
51
+ WORKER_NAME = "ocr"
52
+ DEFAULT_HOST = os.environ.get("AMPIXA_OCR_HOST", "127.0.0.1")
53
+ DEFAULT_PORT = int(os.environ.get("AMPIXA_OCR_PORT", "8906"))
54
+
55
+ MODEL_ROOT = Path(
56
+ os.environ.get(
57
+ "AMPIXA_OCR_MODEL_ROOT",
58
+ str(Path.home() / "ampixa-demo" / "models" / "ocr" / "space" / "models"),
59
+ )
60
+ ).expanduser()
61
+ SIRIJONGA_MODEL_DIR = MODEL_ROOT / "limbu-sirijonga-ft-v2" / "artifacts"
62
+ # devanagari-ft-v2 (2026-09-24): official PaddleOCR devanagari_PP-OCRv5_mobile_rec fine-tuned
63
+ # on real Gorkhapatra print, synthetic citizenship/office lines, and English replay; served as
64
+ # ONNX (see ../models/.../devanagari-ft-v2/MODEL-SOURCE.txt). Same 568-entry dictionary
65
+ # (Devanagari + all Latin letters + 0-9). Roll back to the official model by pointing
66
+ # AMPIXA_OCR_DEVANAGARI_DIR at devanagari-ppocrv5/artifacts.
67
+ DEVANAGARI_MODEL_DIR = Path(
68
+ os.environ.get("AMPIXA_OCR_DEVANAGARI_DIR", str(MODEL_ROOT / "devanagari-ft-v2" / "artifacts"))
69
+ ).expanduser()
70
+
71
+ # The PP-OCRv5 recognizer packs live under model_dir; the character dictionary is
72
+ # embedded in inference.yml (PostProcess.character_dict), so the standalone .txt
73
+ # dictionaries shipped next to them are documentation only and are not loaded.
74
+ # Each model_name must match Global.model_name in that model's inference.yml.
75
+ SIRIJONGA_MODEL_NAME = "PP-OCRv5_mobile_rec"
76
+ DEVANAGARI_MODEL_NAME = "devanagari_PP-OCRv5_mobile_rec"
77
+ REQUIRED_RECOGNIZER_FILES = ("inference.json", "inference.pdiparams", "inference.yml")
78
+ # A directory holding inference.onnx (tools/deva_rec_torch/export_onnx.py output) is served
79
+ # through PaddleX's onnxruntime engine with the same pre/post-processing as Paddle.
80
+ REQUIRED_ONNX_FILES = ("inference.onnx", "inference.yml")
81
+
82
+
83
+ def recognizer_engine(model_dir: Path) -> str:
84
+ return "onnxruntime" if (model_dir / "inference.onnx").is_file() else "paddle_static"
85
+
86
+ MAX_UPLOAD_BYTES = 10 * 1024 * 1024
87
+ MAX_IMAGE_SIDE = 2000
88
+ OVERLAY_MAX_SIDE = int(os.environ.get("AMPIXA_OCR_OVERLAY_MAX_SIDE", "1400"))
89
+ MAX_LINES = int(os.environ.get("AMPIXA_OCR_MAX_LINES", "120"))
90
+ RECOGNIZER_BATCH_SIZE = int(os.environ.get("AMPIXA_OCR_REC_BATCH_SIZE", "16"))
91
+
92
+ # Detector settings. The Space leaves limit_type at the PaddleX default ("min"),
93
+ # which upscales every page so its *shortest* side is >= limit_side_len and costs
94
+ # ~8 s per page on this host. "max" bounds the *longest* side instead: 2.4x faster
95
+ # on a full newspaper page for the same box count (303 vs 305 boxes measured).
96
+ DETECTOR_LIMIT_SIDE_LEN = int(os.environ.get("AMPIXA_OCR_DET_LIMIT_SIDE_LEN", "1280"))
97
+ DETECTOR_LIMIT_TYPE = os.environ.get("AMPIXA_OCR_DET_LIMIT_TYPE", "max")
98
+ DETECTOR_THRESH = float(os.environ.get("AMPIXA_OCR_DET_THRESH", "0.30"))
99
+ DETECTOR_BOX_THRESH = float(os.environ.get("AMPIXA_OCR_DET_BOX_THRESH", "0.60"))
100
+ DETECTOR_UNCLIP_RATIO = float(os.environ.get("AMPIXA_OCR_DET_UNCLIP_RATIO", "1.5"))
101
+ DETECTOR_MODEL_NAME = "PP-OCRv6_medium_det" # PaddleOCR 3.7.0 default text detector.
102
+
103
+ CROP_PADDING = 12 # Space default for the Paddle page tab.
104
+ READING_ORDER = "Rows LTR" # Space default for the Paddle page tab.
105
+
106
+ SUPPORTED_MODES = ("line", "page")
107
+ SUPPORTED_SCRIPTS = ("auto", "sirijonga", "devanagari")
108
+ SUPPORTED_IMAGE_FORMATS = {"PNG": "image/png", "JPEG": "image/jpeg", "WEBP": "image/webp"}
109
+
110
+ LIMBU_RE = re.compile("[\u1900-\u194F]")
111
+ DEVANAGARI_RE = re.compile("[\u0900-\u097F]")
112
+
113
+ # Upper bound of _script_score() for a Devanagari candidate: rec_score is a
114
+ # probability (<= 1.0) and the Devanagari bonus saturates at 12 * 0.035. If the
115
+ # Sirijonga candidate already scores at least this much, the Devanagari pass
116
+ # cannot change the outcome and is skipped. This is an exact prune, not an
117
+ # approximation: it never changes the selected text.
118
+ MAX_DEVANAGARI_SCRIPT_SCORE = 1.0 + 0.035 * 12
119
+
120
+ LOGGER = logging.getLogger("ampixa.ocr")
121
+
122
+
123
+ @dataclass(slots=True)
124
+ class Recognition:
125
+ """One recognizer output for one crop."""
126
+
127
+ text: str
128
+ confidence: float | None
129
+ engine: str
130
+
131
+
132
+ @dataclass(slots=True)
133
+ class Box:
134
+ """Axis-aligned line box in processed-image pixel coordinates."""
135
+
136
+ left: int
137
+ top: int
138
+ right: int
139
+ bottom: int
140
+ confidence: float
141
+
142
+ @property
143
+ def width(self) -> int:
144
+ return max(0, self.right - self.left)
145
+
146
+ @property
147
+ def height(self) -> int:
148
+ return max(0, self.bottom - self.top)
149
+
150
+ @property
151
+ def x_center(self) -> float:
152
+ return (self.left + self.right) / 2.0
153
+
154
+ @property
155
+ def y_center(self) -> float:
156
+ return (self.top + self.bottom) / 2.0
157
+
158
+ def as_list(self) -> list[int]:
159
+ return [self.left, self.top, self.right, self.bottom]
160
+
161
+
162
+ class OcrError(Exception):
163
+ """Client-visible OCR failure carrying an HTTP status code."""
164
+
165
+ def __init__(self, message: str, status_code: int) -> None:
166
+ super().__init__(message)
167
+ self.message = message
168
+ self.status_code = status_code
169
+
170
+
171
+ class Engines:
172
+ """Holds the Paddle predictors and serializes every call into them.
173
+
174
+ Paddle predictors are not documented as thread-safe, and uvicorn runs request
175
+ handlers in a thread pool, so every predict() call goes through ``lock``.
176
+ """
177
+
178
+ def __init__(self) -> None:
179
+ self.lock = threading.Lock()
180
+ self.recognizers: dict[str, Any] = {}
181
+ self.detector: Any = None
182
+ self.loaded = False
183
+ self.load_seconds: float | None = None
184
+ self.load_error: str | None = None
185
+
186
+ def ensure_model_files(self) -> None:
187
+ missing: list[str] = []
188
+ for model_dir in (SIRIJONGA_MODEL_DIR, DEVANAGARI_MODEL_DIR):
189
+ required = REQUIRED_ONNX_FILES if recognizer_engine(model_dir) == "onnxruntime" else REQUIRED_RECOGNIZER_FILES
190
+ for name in required:
191
+ path = model_dir / name
192
+ if not path.is_file():
193
+ missing.append(str(path))
194
+ if missing:
195
+ raise RuntimeError("missing OCR model artifacts: " + ", ".join(missing))
196
+
197
+ def load(self) -> None:
198
+ """Build every predictor once. Called from a background thread at startup."""
199
+ started = time.perf_counter()
200
+ try:
201
+ self.ensure_model_files()
202
+ for engine, model_name, model_dir in (
203
+ ("sirijonga", SIRIJONGA_MODEL_NAME, SIRIJONGA_MODEL_DIR),
204
+ ("devanagari", DEVANAGARI_MODEL_NAME, DEVANAGARI_MODEL_DIR),
205
+ ):
206
+ step = time.perf_counter()
207
+ backend = recognizer_engine(model_dir)
208
+ if backend == "onnxruntime":
209
+ self.recognizers[engine] = create_model(
210
+ model_name=model_name, model_dir=str(model_dir), engine="onnxruntime", device="cpu"
211
+ )
212
+ else:
213
+ self.recognizers[engine] = TextRecognition(
214
+ model_name=model_name,
215
+ model_dir=str(model_dir),
216
+ device="cpu",
217
+ )
218
+ LOGGER.info(
219
+ "loaded recognizer engine=%s backend=%s model=%s dir=%s in %.2fs",
220
+ engine,
221
+ backend,
222
+ model_name,
223
+ model_dir,
224
+ time.perf_counter() - step,
225
+ )
226
+ step = time.perf_counter()
227
+ self.detector = TextDetection(
228
+ limit_side_len=DETECTOR_LIMIT_SIDE_LEN,
229
+ limit_type=DETECTOR_LIMIT_TYPE,
230
+ thresh=DETECTOR_THRESH,
231
+ box_thresh=DETECTOR_BOX_THRESH,
232
+ unclip_ratio=DETECTOR_UNCLIP_RATIO,
233
+ device="cpu",
234
+ )
235
+ LOGGER.info("loaded text detector in %.2fs", time.perf_counter() - step)
236
+ except Exception as exc: # noqa: BLE001 - recorded and surfaced through /health
237
+ self.load_error = f"{type(exc).__name__}: {exc}"
238
+ LOGGER.error("model load failed: %s\n%s", self.load_error, traceback.format_exc())
239
+ return
240
+ self.load_seconds = time.perf_counter() - started
241
+ self.loaded = True
242
+ LOGGER.info("all OCR models loaded in %.2fs", self.load_seconds)
243
+
244
+
245
+ ENGINES = Engines()
246
+
247
+
248
+ def _payload(item: object) -> dict[str, Any] | None:
249
+ """Unwrap a PaddleX result object into its plain result mapping."""
250
+ candidate: object = item
251
+ if not isinstance(candidate, dict):
252
+ candidate = getattr(item, "json", None)
253
+ if not isinstance(candidate, dict):
254
+ return None
255
+ nested = candidate.get("res")
256
+ return nested if isinstance(nested, dict) else candidate
257
+
258
+
259
+ def _script_score(recognition: Recognition) -> float:
260
+ """Score copied verbatim from the Space's app.py ``_script_score``.
261
+
262
+ The Sirijonga bonus (0.06/char, capped at 12 chars) is larger than the
263
+ Devanagari one (0.035/char), so this rule leans towards Sirijonga on mixed
264
+ pages. Callers that know the script should pass it explicitly.
265
+ """
266
+ text = recognition.text or ""
267
+ confidence = recognition.confidence if recognition.confidence is not None else 0.0
268
+ limbu_chars = len(LIMBU_RE.findall(text))
269
+ deva_chars = len(DEVANAGARI_RE.findall(text))
270
+ if recognition.engine == "sirijonga":
271
+ return confidence + min(limbu_chars, 12) * 0.06 - min(deva_chars, 12) * 0.04
272
+ if recognition.engine == "devanagari":
273
+ return confidence + min(deva_chars, 12) * 0.035 - min(limbu_chars, 12) * 0.08
274
+ return confidence
275
+
276
+
277
+ def _recognize_batch(engine: str, crops: list[np.ndarray]) -> list[Recognition]:
278
+ """Run one recognizer over a list of BGR crops, preserving input order."""
279
+ if not crops:
280
+ return []
281
+ predictor = ENGINES.recognizers.get(engine)
282
+ if predictor is None:
283
+ raise OcrError(f"recognizer '{engine}' is not loaded", 503)
284
+ raw = list(predictor.predict(crops, batch_size=RECOGNIZER_BATCH_SIZE))
285
+ if len(raw) != len(crops):
286
+ raise OcrError(
287
+ f"recognizer '{engine}' returned {len(raw)} results for {len(crops)} crops",
288
+ 500,
289
+ )
290
+ results: list[Recognition] = []
291
+ for item in raw:
292
+ payload = _payload(item)
293
+ if payload is None:
294
+ results.append(Recognition(text="", confidence=None, engine=engine))
295
+ continue
296
+ text = payload.get("rec_text") or payload.get("text") or ""
297
+ score = payload.get("rec_score")
298
+ if score is None:
299
+ score = payload.get("score")
300
+ confidence: float | None
301
+ try:
302
+ confidence = float(score) if score is not None else None
303
+ except (TypeError, ValueError):
304
+ confidence = None
305
+ results.append(Recognition(text=str(text).strip(), confidence=confidence, engine=engine))
306
+ return results
307
+
308
+
309
+ def _recognize_routed(crops: list[np.ndarray], script: str) -> list[Recognition]:
310
+ """Recognize crops under the requested script routing.
311
+
312
+ ``auto`` reproduces the Space: both engines run and the higher ``_script_score``
313
+ wins, with ties going to Sirijonga. The Devanagari pass is skipped for crops
314
+ whose Sirijonga score already exceeds every score Devanagari could reach.
315
+ """
316
+ if script in ("sirijonga", "devanagari"):
317
+ return _recognize_batch(script, crops)
318
+
319
+ sirijonga = _recognize_batch("sirijonga", crops)
320
+ scores = [_script_score(rec) for rec in sirijonga]
321
+ pending = [i for i, score in enumerate(scores) if score < MAX_DEVANAGARI_SCRIPT_SCORE]
322
+ if not pending:
323
+ return sirijonga
324
+ devanagari = _recognize_batch("devanagari", [crops[i] for i in pending])
325
+ selected = list(sirijonga)
326
+ for index, candidate in zip(pending, devanagari, strict=True):
327
+ if _script_score(candidate) > scores[index]:
328
+ selected[index] = candidate
329
+ return selected
330
+
331
+
332
+ def _to_bgr(image: Image.Image) -> np.ndarray:
333
+ """PIL RGB image -> contiguous BGR ndarray, the layout PaddleX expects."""
334
+ return np.asarray(image)[:, :, ::-1].copy()
335
+
336
+
337
+ def _box_from_polygon(polygon: object, *, width: int, height: int, confidence: float) -> Box | None:
338
+ array = np.asarray(polygon, dtype=np.float64).reshape(-1, 2)
339
+ if array.size == 0:
340
+ return None
341
+ box = Box(
342
+ left=max(0, int(math.floor(float(array[:, 0].min())))),
343
+ top=max(0, int(math.floor(float(array[:, 1].min())))),
344
+ right=min(width, int(math.ceil(float(array[:, 0].max())))),
345
+ bottom=min(height, int(math.ceil(float(array[:, 1].max())))),
346
+ confidence=confidence,
347
+ )
348
+ return box if box.width > 0 and box.height > 0 else None
349
+
350
+
351
+ def _detect_boxes(image: Image.Image) -> list[Box]:
352
+ """Full-page text detection -> axis-aligned line boxes."""
353
+ if ENGINES.detector is None:
354
+ raise OcrError("text detector is not loaded", 503)
355
+ raw = list(ENGINES.detector.predict(_to_bgr(image)))
356
+ boxes: list[Box] = []
357
+ for item in raw:
358
+ payload = _payload(item)
359
+ if payload is None:
360
+ continue
361
+ polygons = payload.get("dt_polys")
362
+ if polygons is None:
363
+ continue
364
+ # dt_scores comes back as an ndarray, so never use truthiness on it.
365
+ scores = payload.get("dt_scores")
366
+ if scores is None:
367
+ scores = []
368
+ for index, polygon in enumerate(polygons):
369
+ confidence = 1.0
370
+ if index < len(scores) and scores[index] is not None:
371
+ try:
372
+ confidence = float(scores[index])
373
+ except (TypeError, ValueError):
374
+ confidence = 1.0
375
+ box = _box_from_polygon(
376
+ polygon, width=image.width, height=image.height, confidence=confidence
377
+ )
378
+ if box is not None:
379
+ boxes.append(box)
380
+ return boxes
381
+
382
+
383
+ def _has_two_column_shape(boxes: list[Box], image_width: int) -> bool:
384
+ """Copied from the Space's app.py."""
385
+ if len(boxes) < 8 or image_width <= 0:
386
+ return False
387
+ midpoint = image_width / 2.0
388
+ left = [box for box in boxes if box.x_center < midpoint]
389
+ right = [box for box in boxes if box.x_center >= midpoint]
390
+ if len(left) < 3 or len(right) < 3:
391
+ return False
392
+ left_median = float(np.median([box.x_center for box in left]))
393
+ right_median = float(np.median([box.x_center for box in right]))
394
+ return (right_median - left_median) >= image_width * 0.22
395
+
396
+
397
+ def _order_boxes(boxes: list[Box], mode: str, image_width: int) -> list[Box]:
398
+ """Copied from the Space's app.py ``_order_image_line_boxes``."""
399
+ if mode == "Auto":
400
+ mode = "Columns LTR" if _has_two_column_shape(boxes, image_width) else "Rows LTR"
401
+ if mode == "Columns LTR":
402
+ return sorted(
403
+ boxes,
404
+ key=lambda box: (0 if box.x_center < image_width / 2 else 1, box.y_center, box.x_center),
405
+ )
406
+ if mode == "Columns RTL":
407
+ return sorted(
408
+ boxes,
409
+ key=lambda box: (0 if box.x_center >= image_width / 2 else 1, box.y_center, box.x_center),
410
+ )
411
+ row_height = max(12, int(np.median([box.height for box in boxes])) if boxes else 24)
412
+ return sorted(boxes, key=lambda box: (math.floor(box.y_center / row_height), box.x_center))
413
+
414
+
415
+ def _crop(image: Image.Image, box: Box, padding: int) -> Image.Image:
416
+ return image.crop(
417
+ (
418
+ max(0, box.left - padding),
419
+ max(0, box.top - padding),
420
+ min(image.width, box.right + padding),
421
+ min(image.height, box.bottom + padding),
422
+ )
423
+ )
424
+
425
+
426
+ def _overlay_png_b64(image: Image.Image, boxes: list[Box]) -> str:
427
+ """Draw the ordered boxes on the page and return a base64 PNG."""
428
+ annotated = image.copy()
429
+ draw = ImageDraw.Draw(annotated)
430
+ for index, box in enumerate(boxes, start=1):
431
+ draw.rectangle(
432
+ (box.left, box.top, box.right, box.bottom), outline=(28, 120, 92), width=3
433
+ )
434
+ draw.text((box.left + 4, max(0, box.top - 12)), str(index), fill=(28, 120, 92))
435
+ longest = max(annotated.size)
436
+ if longest > OVERLAY_MAX_SIDE:
437
+ scale = OVERLAY_MAX_SIDE / longest
438
+ annotated = annotated.resize(
439
+ (max(1, round(annotated.width * scale)), max(1, round(annotated.height * scale))),
440
+ Image.LANCZOS,
441
+ )
442
+ buffer = io.BytesIO()
443
+ annotated.save(buffer, format="PNG", optimize=True)
444
+ return base64.b64encode(buffer.getvalue()).decode("ascii")
445
+
446
+
447
+ def _decode_image(raw: bytes) -> Image.Image:
448
+ """Validate and normalize an uploaded image; downscale the longest side."""
449
+ try:
450
+ probe = Image.open(io.BytesIO(raw))
451
+ image_format = probe.format
452
+ probe.load()
453
+ image = ImageOps.exif_transpose(probe).convert("RGB")
454
+ probe.close()
455
+ except UnidentifiedImageError as exc:
456
+ raise OcrError("file is not a decodable image", 400) from exc
457
+ except (OSError, ValueError) as exc:
458
+ raise OcrError(f"could not decode image: {exc}", 400) from exc
459
+ if image_format not in SUPPORTED_IMAGE_FORMATS:
460
+ raise OcrError(
461
+ f"unsupported image format {image_format!r}; expected one of "
462
+ + ", ".join(sorted(SUPPORTED_IMAGE_FORMATS)),
463
+ 415,
464
+ )
465
+ longest = max(image.size)
466
+ if longest > MAX_IMAGE_SIDE:
467
+ scale = MAX_IMAGE_SIDE / longest
468
+ image = image.resize(
469
+ (max(1, round(image.width * scale)), max(1, round(image.height * scale))),
470
+ Image.LANCZOS,
471
+ )
472
+ if image.width < 4 or image.height < 4:
473
+ raise OcrError("image is too small to recognize", 400)
474
+ return image
475
+
476
+
477
+ def _resolved_script(recognitions: list[Recognition], requested: str) -> str:
478
+ if requested in ("sirijonga", "devanagari"):
479
+ return requested
480
+ engines = [rec.engine for rec in recognitions if rec.text]
481
+ if not engines:
482
+ return "sirijonga"
483
+ return max(set(engines), key=engines.count)
484
+
485
+
486
+ def _run_line(image: Image.Image, script: str) -> dict[str, Any]:
487
+ crops = [_to_bgr(image)]
488
+ with ENGINES.lock:
489
+ recognitions = _recognize_routed(crops, script)
490
+ recognition = recognitions[0]
491
+ line = {
492
+ "text": recognition.text,
493
+ "confidence": recognition.confidence,
494
+ "box": [0, 0, image.width, image.height],
495
+ "script": recognition.engine,
496
+ }
497
+ return {
498
+ "text": recognition.text,
499
+ "lines": [line],
500
+ "script": _resolved_script(recognitions, script),
501
+ "truncated": False,
502
+ "overlay_png_b64": None,
503
+ }
504
+
505
+
506
+ def _run_page(image: Image.Image, script: str) -> dict[str, Any]:
507
+ with ENGINES.lock:
508
+ boxes = _detect_boxes(image)
509
+ ordered = _order_boxes(boxes, READING_ORDER, image.width)
510
+ truncated = MAX_LINES > 0 and len(ordered) > MAX_LINES
511
+ if truncated:
512
+ LOGGER.warning(
513
+ "page has %d detected lines, truncating to AMPIXA_OCR_MAX_LINES=%d",
514
+ len(ordered),
515
+ MAX_LINES,
516
+ )
517
+ ordered = ordered[:MAX_LINES]
518
+ crops = [_to_bgr(_crop(image, box, CROP_PADDING)) for box in ordered]
519
+ recognitions = _recognize_routed(crops, script)
520
+ overlay = _overlay_png_b64(image, ordered)
521
+
522
+ lines: list[dict[str, Any]] = []
523
+ texts: list[str] = []
524
+ for box, recognition in zip(ordered, recognitions, strict=True):
525
+ lines.append(
526
+ {
527
+ "text": recognition.text,
528
+ "confidence": recognition.confidence,
529
+ "box": box.as_list(),
530
+ "script": recognition.engine,
531
+ }
532
+ )
533
+ if recognition.text:
534
+ texts.append(recognition.text)
535
+ return {
536
+ "text": "\n".join(texts),
537
+ "lines": lines,
538
+ "script": _resolved_script(recognitions, script),
539
+ "truncated": truncated,
540
+ "overlay_png_b64": overlay,
541
+ }
542
+
543
+
544
+ @asynccontextmanager
545
+ async def _lifespan(_app: FastAPI) -> AsyncIterator[None]:
546
+ """Serve /health immediately; build the Paddle predictors in the background."""
547
+ LOGGER.info(
548
+ "starting OCR worker: model_root=%s detector=%s (%s/%d) max_lines=%d",
549
+ MODEL_ROOT,
550
+ DETECTOR_MODEL_NAME,
551
+ DETECTOR_LIMIT_TYPE,
552
+ DETECTOR_LIMIT_SIDE_LEN,
553
+ MAX_LINES,
554
+ )
555
+ loader = threading.Thread(target=ENGINES.load, name="ocr-model-load", daemon=True)
556
+ loader.start()
557
+ yield
558
+ LOGGER.info("OCR worker shutting down")
559
+
560
+
561
+ app = FastAPI(title="Ampixa OCR worker", docs_url=None, redoc_url=None, lifespan=_lifespan)
562
+
563
+
564
+ @app.exception_handler(OcrError)
565
+ async def _ocr_error_handler(_request: Request, exc: OcrError) -> JSONResponse:
566
+ return JSONResponse(status_code=exc.status_code, content={"error": exc.message})
567
+
568
+
569
+ @app.exception_handler(RequestValidationError)
570
+ async def _validation_error_handler(
571
+ _request: Request, exc: RequestValidationError
572
+ ) -> JSONResponse:
573
+ """Report malformed multipart bodies as {"error": ...} instead of FastAPI's 422."""
574
+ problems = "; ".join(
575
+ f"{'.'.join(str(part) for part in error.get('loc', ()))}: {error.get('msg', 'invalid')}"
576
+ for error in exc.errors()
577
+ )
578
+ return JSONResponse(status_code=400, content={"error": f"invalid request: {problems}"})
579
+
580
+
581
+ @app.exception_handler(StarletteHTTPException)
582
+ async def _http_error_handler(_request: Request, exc: StarletteHTTPException) -> JSONResponse:
583
+ """Keep 404/405 and friends on the {"error": ...} contract."""
584
+ return JSONResponse(status_code=exc.status_code, content={"error": str(exc.detail)})
585
+
586
+
587
+ @app.exception_handler(Exception)
588
+ async def _unhandled_error_handler(_request: Request, exc: Exception) -> JSONResponse:
589
+ """Last resort: log the traceback, return JSON, never a bare stack trace."""
590
+ LOGGER.error("unhandled error: %s\n%s", exc, traceback.format_exc())
591
+ return JSONResponse(
592
+ status_code=500, content={"error": f"internal error: {type(exc).__name__}"}
593
+ )
594
+
595
+
596
+ @app.get("/health")
597
+ def health() -> JSONResponse:
598
+ body: dict[str, Any] = {
599
+ "ok": ENGINES.load_error is None,
600
+ "worker": WORKER_NAME,
601
+ "loaded": ENGINES.loaded,
602
+ "models": {
603
+ "sirijonga": str(SIRIJONGA_MODEL_DIR),
604
+ "devanagari": str(DEVANAGARI_MODEL_DIR),
605
+ "sirijonga_model_name": SIRIJONGA_MODEL_NAME,
606
+ "devanagari_model_name": DEVANAGARI_MODEL_NAME,
607
+ "devanagari_backend": recognizer_engine(DEVANAGARI_MODEL_DIR),
608
+ "detector_model_name": DETECTOR_MODEL_NAME,
609
+ "detector_limit": f"{DETECTOR_LIMIT_TYPE}/{DETECTOR_LIMIT_SIDE_LEN}",
610
+ "max_lines": MAX_LINES,
611
+ "max_image_side": MAX_IMAGE_SIDE,
612
+ "load_seconds": round(ENGINES.load_seconds, 2) if ENGINES.load_seconds else None,
613
+ },
614
+ "device": "cpu",
615
+ }
616
+ if ENGINES.load_error is not None:
617
+ body["error"] = ENGINES.load_error
618
+ return JSONResponse(status_code=503, content=body)
619
+ return JSONResponse(status_code=200, content=body)
620
+
621
+
622
+ @app.post("/recognize")
623
+ async def recognize(
624
+ file: UploadFile = File(...),
625
+ mode: str = Form("page"),
626
+ script: str = Form("auto"),
627
+ ) -> JSONResponse:
628
+ started = time.perf_counter()
629
+ mode = (mode or "page").strip().lower()
630
+ script = (script or "auto").strip().lower()
631
+ if mode not in SUPPORTED_MODES:
632
+ raise OcrError(f"mode must be one of {', '.join(SUPPORTED_MODES)}", 400)
633
+ if script not in SUPPORTED_SCRIPTS:
634
+ raise OcrError(f"script must be one of {', '.join(SUPPORTED_SCRIPTS)}", 400)
635
+ if not ENGINES.loaded:
636
+ if ENGINES.load_error is not None:
637
+ raise OcrError(f"OCR models failed to load: {ENGINES.load_error}", 503)
638
+ raise OcrError("OCR models are still loading", 503)
639
+
640
+ try:
641
+ raw = await file.read()
642
+ except Exception as exc: # noqa: BLE001 - upload stream failures are client errors
643
+ LOGGER.error("upload read failed: %s\n%s", exc, traceback.format_exc())
644
+ raise OcrError(f"could not read uploaded file: {exc}", 400) from exc
645
+ finally:
646
+ await file.close()
647
+
648
+ if not raw:
649
+ raise OcrError("uploaded file is empty", 400)
650
+ if len(raw) > MAX_UPLOAD_BYTES:
651
+ raise OcrError(
652
+ f"image is {len(raw)} bytes, limit is {MAX_UPLOAD_BYTES} bytes", 413
653
+ )
654
+
655
+ image = _decode_image(raw)
656
+ runner = _run_line if mode == "line" else _run_page
657
+ try:
658
+ result = await run_in_threadpool(runner, image, script)
659
+ except OcrError:
660
+ raise
661
+ except Exception as exc: # noqa: BLE001 - converted to a JSON 500 below
662
+ LOGGER.error(
663
+ "recognition failed mode=%s script=%s: %s\n%s",
664
+ mode,
665
+ script,
666
+ exc,
667
+ traceback.format_exc(),
668
+ )
669
+ raise OcrError(f"recognition failed: {type(exc).__name__}: {exc}", 500) from exc
670
+
671
+ elapsed = time.perf_counter() - started
672
+ LOGGER.info(
673
+ "recognize mode=%s script=%s -> %s bytes=%d size=%dx%d lines=%d truncated=%s chars=%d %.2fs",
674
+ mode,
675
+ script,
676
+ result["script"],
677
+ len(raw),
678
+ image.width,
679
+ image.height,
680
+ len(result["lines"]),
681
+ result["truncated"],
682
+ len(result["text"]),
683
+ elapsed,
684
+ )
685
+ return JSONResponse(
686
+ status_code=200,
687
+ content={
688
+ "text": result["text"],
689
+ "lines": result["lines"],
690
+ "script": result["script"],
691
+ "mode": mode,
692
+ "elapsed_s": round(elapsed, 3),
693
+ "image_size": [image.width, image.height],
694
+ "truncated": result["truncated"],
695
+ "overlay_png_b64": result["overlay_png_b64"],
696
+ },
697
+ )
698
+
699
+
700
+ def main() -> int:
701
+ parser = argparse.ArgumentParser(description="Ampixa OCR worker (Limbu Sirijonga + Devanagari)")
702
+ parser.add_argument("--host", default=DEFAULT_HOST)
703
+ parser.add_argument("--port", type=int, default=DEFAULT_PORT)
704
+ args = parser.parse_args()
705
+ logging.basicConfig(
706
+ level=logging.INFO,
707
+ format="%(asctime)s %(levelname)s %(name)s %(message)s",
708
+ stream=sys.stdout,
709
+ )
710
+ uvicorn.run(app, host=args.host, port=args.port, log_level="info", access_log=False)
711
+ return 0
712
+
713
+
714
+ if __name__ == "__main__":
715
+ raise SystemExit(main())
k2-ampixa-demo-ocr/workers-ocr/server.py.bak-20260924-official-paddle ADDED
@@ -0,0 +1,694 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Ampixa OCR worker: Limbu Sirijonga + Devanagari OCR on CPU via PaddleOCR.
2
+
3
+ Ported from the non-Gradio inference paths of the private Hugging Face Space
4
+ ``ampixa/limbu-ocr`` (``app.py``, sha256 cffc507fd79f1ee31b6469315c92ff6b8e2360affe617ae8e2f8fa709b92fa01).
5
+
6
+ Two routes are exposed:
7
+
8
+ ``line``
9
+ The uploaded image is treated as a single cropped text line and sent
10
+ straight to the recognizer(s).
11
+
12
+ ``page``
13
+ PaddleOCR full-page text detection produces line boxes, the boxes are put
14
+ into reading order, every crop is recognized, and a PNG overlay with the
15
+ boxes drawn on it is returned alongside the text.
16
+
17
+ Everything runs on CPU: paddlepaddle has no Metal/MPS backend on macOS arm64.
18
+ """
19
+
20
+ from __future__ import annotations
21
+
22
+ import argparse
23
+ import base64
24
+ import io
25
+ import logging
26
+ import math
27
+ import os
28
+ import re
29
+ import sys
30
+ import threading
31
+ import time
32
+ import traceback
33
+ from collections.abc import AsyncIterator
34
+ from contextlib import asynccontextmanager
35
+ from dataclasses import dataclass
36
+ from pathlib import Path
37
+ from typing import Any
38
+
39
+ import numpy as np
40
+ import uvicorn
41
+ from fastapi import FastAPI, File, Form, Request, UploadFile
42
+ from fastapi.exceptions import RequestValidationError
43
+ from fastapi.responses import JSONResponse
44
+ from paddleocr import TextDetection
45
+ from paddleocr._models.text_recognition import TextRecognition
46
+ from PIL import Image, ImageDraw, ImageOps, UnidentifiedImageError
47
+ from starlette.concurrency import run_in_threadpool
48
+ from starlette.exceptions import HTTPException as StarletteHTTPException
49
+
50
+ WORKER_NAME = "ocr"
51
+ DEFAULT_HOST = os.environ.get("AMPIXA_OCR_HOST", "127.0.0.1")
52
+ DEFAULT_PORT = int(os.environ.get("AMPIXA_OCR_PORT", "8906"))
53
+
54
+ MODEL_ROOT = Path(
55
+ os.environ.get(
56
+ "AMPIXA_OCR_MODEL_ROOT",
57
+ str(Path.home() / "ampixa-demo" / "models" / "ocr" / "space" / "models"),
58
+ )
59
+ ).expanduser()
60
+ SIRIJONGA_MODEL_DIR = MODEL_ROOT / "limbu-sirijonga-ft-v2" / "artifacts"
61
+ # Official PaddleOCR devanagari_PP-OCRv5_mobile_rec (replaced deva-v2 on 2026-09-24).
62
+ # Its dictionary covers Devanagari plus all 52 Latin letters and 0-9, so mixed
63
+ # Nepali/English lines are readable; deva-v2 had only 28 Latin letters and 5 digits.
64
+ DEVANAGARI_MODEL_DIR = MODEL_ROOT / "devanagari-ppocrv5" / "artifacts"
65
+
66
+ # The PP-OCRv5 recognizer packs live under model_dir; the character dictionary is
67
+ # embedded in inference.yml (PostProcess.character_dict), so the standalone .txt
68
+ # dictionaries shipped next to them are documentation only and are not loaded.
69
+ # Each model_name must match Global.model_name in that model's inference.yml.
70
+ SIRIJONGA_MODEL_NAME = "PP-OCRv5_mobile_rec"
71
+ DEVANAGARI_MODEL_NAME = "devanagari_PP-OCRv5_mobile_rec"
72
+ REQUIRED_RECOGNIZER_FILES = ("inference.json", "inference.pdiparams", "inference.yml")
73
+
74
+ MAX_UPLOAD_BYTES = 10 * 1024 * 1024
75
+ MAX_IMAGE_SIDE = 2000
76
+ OVERLAY_MAX_SIDE = int(os.environ.get("AMPIXA_OCR_OVERLAY_MAX_SIDE", "1400"))
77
+ MAX_LINES = int(os.environ.get("AMPIXA_OCR_MAX_LINES", "120"))
78
+ RECOGNIZER_BATCH_SIZE = int(os.environ.get("AMPIXA_OCR_REC_BATCH_SIZE", "16"))
79
+
80
+ # Detector settings. The Space leaves limit_type at the PaddleX default ("min"),
81
+ # which upscales every page so its *shortest* side is >= limit_side_len and costs
82
+ # ~8 s per page on this host. "max" bounds the *longest* side instead: 2.4x faster
83
+ # on a full newspaper page for the same box count (303 vs 305 boxes measured).
84
+ DETECTOR_LIMIT_SIDE_LEN = int(os.environ.get("AMPIXA_OCR_DET_LIMIT_SIDE_LEN", "1280"))
85
+ DETECTOR_LIMIT_TYPE = os.environ.get("AMPIXA_OCR_DET_LIMIT_TYPE", "max")
86
+ DETECTOR_THRESH = float(os.environ.get("AMPIXA_OCR_DET_THRESH", "0.30"))
87
+ DETECTOR_BOX_THRESH = float(os.environ.get("AMPIXA_OCR_DET_BOX_THRESH", "0.60"))
88
+ DETECTOR_UNCLIP_RATIO = float(os.environ.get("AMPIXA_OCR_DET_UNCLIP_RATIO", "1.5"))
89
+ DETECTOR_MODEL_NAME = "PP-OCRv6_medium_det" # PaddleOCR 3.7.0 default text detector.
90
+
91
+ CROP_PADDING = 12 # Space default for the Paddle page tab.
92
+ READING_ORDER = "Rows LTR" # Space default for the Paddle page tab.
93
+
94
+ SUPPORTED_MODES = ("line", "page")
95
+ SUPPORTED_SCRIPTS = ("auto", "sirijonga", "devanagari")
96
+ SUPPORTED_IMAGE_FORMATS = {"PNG": "image/png", "JPEG": "image/jpeg", "WEBP": "image/webp"}
97
+
98
+ LIMBU_RE = re.compile("[\u1900-\u194F]")
99
+ DEVANAGARI_RE = re.compile("[\u0900-\u097F]")
100
+
101
+ # Upper bound of _script_score() for a Devanagari candidate: rec_score is a
102
+ # probability (<= 1.0) and the Devanagari bonus saturates at 12 * 0.035. If the
103
+ # Sirijonga candidate already scores at least this much, the Devanagari pass
104
+ # cannot change the outcome and is skipped. This is an exact prune, not an
105
+ # approximation: it never changes the selected text.
106
+ MAX_DEVANAGARI_SCRIPT_SCORE = 1.0 + 0.035 * 12
107
+
108
+ LOGGER = logging.getLogger("ampixa.ocr")
109
+
110
+
111
+ @dataclass(slots=True)
112
+ class Recognition:
113
+ """One recognizer output for one crop."""
114
+
115
+ text: str
116
+ confidence: float | None
117
+ engine: str
118
+
119
+
120
+ @dataclass(slots=True)
121
+ class Box:
122
+ """Axis-aligned line box in processed-image pixel coordinates."""
123
+
124
+ left: int
125
+ top: int
126
+ right: int
127
+ bottom: int
128
+ confidence: float
129
+
130
+ @property
131
+ def width(self) -> int:
132
+ return max(0, self.right - self.left)
133
+
134
+ @property
135
+ def height(self) -> int:
136
+ return max(0, self.bottom - self.top)
137
+
138
+ @property
139
+ def x_center(self) -> float:
140
+ return (self.left + self.right) / 2.0
141
+
142
+ @property
143
+ def y_center(self) -> float:
144
+ return (self.top + self.bottom) / 2.0
145
+
146
+ def as_list(self) -> list[int]:
147
+ return [self.left, self.top, self.right, self.bottom]
148
+
149
+
150
+ class OcrError(Exception):
151
+ """Client-visible OCR failure carrying an HTTP status code."""
152
+
153
+ def __init__(self, message: str, status_code: int) -> None:
154
+ super().__init__(message)
155
+ self.message = message
156
+ self.status_code = status_code
157
+
158
+
159
+ class Engines:
160
+ """Holds the Paddle predictors and serializes every call into them.
161
+
162
+ Paddle predictors are not documented as thread-safe, and uvicorn runs request
163
+ handlers in a thread pool, so every predict() call goes through ``lock``.
164
+ """
165
+
166
+ def __init__(self) -> None:
167
+ self.lock = threading.Lock()
168
+ self.recognizers: dict[str, Any] = {}
169
+ self.detector: Any = None
170
+ self.loaded = False
171
+ self.load_seconds: float | None = None
172
+ self.load_error: str | None = None
173
+
174
+ def ensure_model_files(self) -> None:
175
+ missing: list[str] = []
176
+ for model_dir in (SIRIJONGA_MODEL_DIR, DEVANAGARI_MODEL_DIR):
177
+ for name in REQUIRED_RECOGNIZER_FILES:
178
+ path = model_dir / name
179
+ if not path.is_file():
180
+ missing.append(str(path))
181
+ if missing:
182
+ raise RuntimeError("missing OCR model artifacts: " + ", ".join(missing))
183
+
184
+ def load(self) -> None:
185
+ """Build every predictor once. Called from a background thread at startup."""
186
+ started = time.perf_counter()
187
+ try:
188
+ self.ensure_model_files()
189
+ for engine, model_name, model_dir in (
190
+ ("sirijonga", SIRIJONGA_MODEL_NAME, SIRIJONGA_MODEL_DIR),
191
+ ("devanagari", DEVANAGARI_MODEL_NAME, DEVANAGARI_MODEL_DIR),
192
+ ):
193
+ step = time.perf_counter()
194
+ self.recognizers[engine] = TextRecognition(
195
+ model_name=model_name,
196
+ model_dir=str(model_dir),
197
+ device="cpu",
198
+ )
199
+ LOGGER.info(
200
+ "loaded recognizer engine=%s model=%s dir=%s in %.2fs",
201
+ engine,
202
+ model_name,
203
+ model_dir,
204
+ time.perf_counter() - step,
205
+ )
206
+ step = time.perf_counter()
207
+ self.detector = TextDetection(
208
+ limit_side_len=DETECTOR_LIMIT_SIDE_LEN,
209
+ limit_type=DETECTOR_LIMIT_TYPE,
210
+ thresh=DETECTOR_THRESH,
211
+ box_thresh=DETECTOR_BOX_THRESH,
212
+ unclip_ratio=DETECTOR_UNCLIP_RATIO,
213
+ device="cpu",
214
+ )
215
+ LOGGER.info("loaded text detector in %.2fs", time.perf_counter() - step)
216
+ except Exception as exc: # noqa: BLE001 - recorded and surfaced through /health
217
+ self.load_error = f"{type(exc).__name__}: {exc}"
218
+ LOGGER.error("model load failed: %s\n%s", self.load_error, traceback.format_exc())
219
+ return
220
+ self.load_seconds = time.perf_counter() - started
221
+ self.loaded = True
222
+ LOGGER.info("all OCR models loaded in %.2fs", self.load_seconds)
223
+
224
+
225
+ ENGINES = Engines()
226
+
227
+
228
+ def _payload(item: object) -> dict[str, Any] | None:
229
+ """Unwrap a PaddleX result object into its plain result mapping."""
230
+ candidate: object = item
231
+ if not isinstance(candidate, dict):
232
+ candidate = getattr(item, "json", None)
233
+ if not isinstance(candidate, dict):
234
+ return None
235
+ nested = candidate.get("res")
236
+ return nested if isinstance(nested, dict) else candidate
237
+
238
+
239
+ def _script_score(recognition: Recognition) -> float:
240
+ """Score copied verbatim from the Space's app.py ``_script_score``.
241
+
242
+ The Sirijonga bonus (0.06/char, capped at 12 chars) is larger than the
243
+ Devanagari one (0.035/char), so this rule leans towards Sirijonga on mixed
244
+ pages. Callers that know the script should pass it explicitly.
245
+ """
246
+ text = recognition.text or ""
247
+ confidence = recognition.confidence if recognition.confidence is not None else 0.0
248
+ limbu_chars = len(LIMBU_RE.findall(text))
249
+ deva_chars = len(DEVANAGARI_RE.findall(text))
250
+ if recognition.engine == "sirijonga":
251
+ return confidence + min(limbu_chars, 12) * 0.06 - min(deva_chars, 12) * 0.04
252
+ if recognition.engine == "devanagari":
253
+ return confidence + min(deva_chars, 12) * 0.035 - min(limbu_chars, 12) * 0.08
254
+ return confidence
255
+
256
+
257
+ def _recognize_batch(engine: str, crops: list[np.ndarray]) -> list[Recognition]:
258
+ """Run one recognizer over a list of BGR crops, preserving input order."""
259
+ if not crops:
260
+ return []
261
+ predictor = ENGINES.recognizers.get(engine)
262
+ if predictor is None:
263
+ raise OcrError(f"recognizer '{engine}' is not loaded", 503)
264
+ raw = list(predictor.predict(crops, batch_size=RECOGNIZER_BATCH_SIZE))
265
+ if len(raw) != len(crops):
266
+ raise OcrError(
267
+ f"recognizer '{engine}' returned {len(raw)} results for {len(crops)} crops",
268
+ 500,
269
+ )
270
+ results: list[Recognition] = []
271
+ for item in raw:
272
+ payload = _payload(item)
273
+ if payload is None:
274
+ results.append(Recognition(text="", confidence=None, engine=engine))
275
+ continue
276
+ text = payload.get("rec_text") or payload.get("text") or ""
277
+ score = payload.get("rec_score")
278
+ if score is None:
279
+ score = payload.get("score")
280
+ confidence: float | None
281
+ try:
282
+ confidence = float(score) if score is not None else None
283
+ except (TypeError, ValueError):
284
+ confidence = None
285
+ results.append(Recognition(text=str(text).strip(), confidence=confidence, engine=engine))
286
+ return results
287
+
288
+
289
+ def _recognize_routed(crops: list[np.ndarray], script: str) -> list[Recognition]:
290
+ """Recognize crops under the requested script routing.
291
+
292
+ ``auto`` reproduces the Space: both engines run and the higher ``_script_score``
293
+ wins, with ties going to Sirijonga. The Devanagari pass is skipped for crops
294
+ whose Sirijonga score already exceeds every score Devanagari could reach.
295
+ """
296
+ if script in ("sirijonga", "devanagari"):
297
+ return _recognize_batch(script, crops)
298
+
299
+ sirijonga = _recognize_batch("sirijonga", crops)
300
+ scores = [_script_score(rec) for rec in sirijonga]
301
+ pending = [i for i, score in enumerate(scores) if score < MAX_DEVANAGARI_SCRIPT_SCORE]
302
+ if not pending:
303
+ return sirijonga
304
+ devanagari = _recognize_batch("devanagari", [crops[i] for i in pending])
305
+ selected = list(sirijonga)
306
+ for index, candidate in zip(pending, devanagari, strict=True):
307
+ if _script_score(candidate) > scores[index]:
308
+ selected[index] = candidate
309
+ return selected
310
+
311
+
312
+ def _to_bgr(image: Image.Image) -> np.ndarray:
313
+ """PIL RGB image -> contiguous BGR ndarray, the layout PaddleX expects."""
314
+ return np.asarray(image)[:, :, ::-1].copy()
315
+
316
+
317
+ def _box_from_polygon(polygon: object, *, width: int, height: int, confidence: float) -> Box | None:
318
+ array = np.asarray(polygon, dtype=np.float64).reshape(-1, 2)
319
+ if array.size == 0:
320
+ return None
321
+ box = Box(
322
+ left=max(0, int(math.floor(float(array[:, 0].min())))),
323
+ top=max(0, int(math.floor(float(array[:, 1].min())))),
324
+ right=min(width, int(math.ceil(float(array[:, 0].max())))),
325
+ bottom=min(height, int(math.ceil(float(array[:, 1].max())))),
326
+ confidence=confidence,
327
+ )
328
+ return box if box.width > 0 and box.height > 0 else None
329
+
330
+
331
+ def _detect_boxes(image: Image.Image) -> list[Box]:
332
+ """Full-page text detection -> axis-aligned line boxes."""
333
+ if ENGINES.detector is None:
334
+ raise OcrError("text detector is not loaded", 503)
335
+ raw = list(ENGINES.detector.predict(_to_bgr(image)))
336
+ boxes: list[Box] = []
337
+ for item in raw:
338
+ payload = _payload(item)
339
+ if payload is None:
340
+ continue
341
+ polygons = payload.get("dt_polys")
342
+ if polygons is None:
343
+ continue
344
+ # dt_scores comes back as an ndarray, so never use truthiness on it.
345
+ scores = payload.get("dt_scores")
346
+ if scores is None:
347
+ scores = []
348
+ for index, polygon in enumerate(polygons):
349
+ confidence = 1.0
350
+ if index < len(scores) and scores[index] is not None:
351
+ try:
352
+ confidence = float(scores[index])
353
+ except (TypeError, ValueError):
354
+ confidence = 1.0
355
+ box = _box_from_polygon(
356
+ polygon, width=image.width, height=image.height, confidence=confidence
357
+ )
358
+ if box is not None:
359
+ boxes.append(box)
360
+ return boxes
361
+
362
+
363
+ def _has_two_column_shape(boxes: list[Box], image_width: int) -> bool:
364
+ """Copied from the Space's app.py."""
365
+ if len(boxes) < 8 or image_width <= 0:
366
+ return False
367
+ midpoint = image_width / 2.0
368
+ left = [box for box in boxes if box.x_center < midpoint]
369
+ right = [box for box in boxes if box.x_center >= midpoint]
370
+ if len(left) < 3 or len(right) < 3:
371
+ return False
372
+ left_median = float(np.median([box.x_center for box in left]))
373
+ right_median = float(np.median([box.x_center for box in right]))
374
+ return (right_median - left_median) >= image_width * 0.22
375
+
376
+
377
+ def _order_boxes(boxes: list[Box], mode: str, image_width: int) -> list[Box]:
378
+ """Copied from the Space's app.py ``_order_image_line_boxes``."""
379
+ if mode == "Auto":
380
+ mode = "Columns LTR" if _has_two_column_shape(boxes, image_width) else "Rows LTR"
381
+ if mode == "Columns LTR":
382
+ return sorted(
383
+ boxes,
384
+ key=lambda box: (0 if box.x_center < image_width / 2 else 1, box.y_center, box.x_center),
385
+ )
386
+ if mode == "Columns RTL":
387
+ return sorted(
388
+ boxes,
389
+ key=lambda box: (0 if box.x_center >= image_width / 2 else 1, box.y_center, box.x_center),
390
+ )
391
+ row_height = max(12, int(np.median([box.height for box in boxes])) if boxes else 24)
392
+ return sorted(boxes, key=lambda box: (math.floor(box.y_center / row_height), box.x_center))
393
+
394
+
395
+ def _crop(image: Image.Image, box: Box, padding: int) -> Image.Image:
396
+ return image.crop(
397
+ (
398
+ max(0, box.left - padding),
399
+ max(0, box.top - padding),
400
+ min(image.width, box.right + padding),
401
+ min(image.height, box.bottom + padding),
402
+ )
403
+ )
404
+
405
+
406
+ def _overlay_png_b64(image: Image.Image, boxes: list[Box]) -> str:
407
+ """Draw the ordered boxes on the page and return a base64 PNG."""
408
+ annotated = image.copy()
409
+ draw = ImageDraw.Draw(annotated)
410
+ for index, box in enumerate(boxes, start=1):
411
+ draw.rectangle(
412
+ (box.left, box.top, box.right, box.bottom), outline=(28, 120, 92), width=3
413
+ )
414
+ draw.text((box.left + 4, max(0, box.top - 12)), str(index), fill=(28, 120, 92))
415
+ longest = max(annotated.size)
416
+ if longest > OVERLAY_MAX_SIDE:
417
+ scale = OVERLAY_MAX_SIDE / longest
418
+ annotated = annotated.resize(
419
+ (max(1, round(annotated.width * scale)), max(1, round(annotated.height * scale))),
420
+ Image.LANCZOS,
421
+ )
422
+ buffer = io.BytesIO()
423
+ annotated.save(buffer, format="PNG", optimize=True)
424
+ return base64.b64encode(buffer.getvalue()).decode("ascii")
425
+
426
+
427
+ def _decode_image(raw: bytes) -> Image.Image:
428
+ """Validate and normalize an uploaded image; downscale the longest side."""
429
+ try:
430
+ probe = Image.open(io.BytesIO(raw))
431
+ image_format = probe.format
432
+ probe.load()
433
+ image = ImageOps.exif_transpose(probe).convert("RGB")
434
+ probe.close()
435
+ except UnidentifiedImageError as exc:
436
+ raise OcrError("file is not a decodable image", 400) from exc
437
+ except (OSError, ValueError) as exc:
438
+ raise OcrError(f"could not decode image: {exc}", 400) from exc
439
+ if image_format not in SUPPORTED_IMAGE_FORMATS:
440
+ raise OcrError(
441
+ f"unsupported image format {image_format!r}; expected one of "
442
+ + ", ".join(sorted(SUPPORTED_IMAGE_FORMATS)),
443
+ 415,
444
+ )
445
+ longest = max(image.size)
446
+ if longest > MAX_IMAGE_SIDE:
447
+ scale = MAX_IMAGE_SIDE / longest
448
+ image = image.resize(
449
+ (max(1, round(image.width * scale)), max(1, round(image.height * scale))),
450
+ Image.LANCZOS,
451
+ )
452
+ if image.width < 4 or image.height < 4:
453
+ raise OcrError("image is too small to recognize", 400)
454
+ return image
455
+
456
+
457
+ def _resolved_script(recognitions: list[Recognition], requested: str) -> str:
458
+ if requested in ("sirijonga", "devanagari"):
459
+ return requested
460
+ engines = [rec.engine for rec in recognitions if rec.text]
461
+ if not engines:
462
+ return "sirijonga"
463
+ return max(set(engines), key=engines.count)
464
+
465
+
466
+ def _run_line(image: Image.Image, script: str) -> dict[str, Any]:
467
+ crops = [_to_bgr(image)]
468
+ with ENGINES.lock:
469
+ recognitions = _recognize_routed(crops, script)
470
+ recognition = recognitions[0]
471
+ line = {
472
+ "text": recognition.text,
473
+ "confidence": recognition.confidence,
474
+ "box": [0, 0, image.width, image.height],
475
+ "script": recognition.engine,
476
+ }
477
+ return {
478
+ "text": recognition.text,
479
+ "lines": [line],
480
+ "script": _resolved_script(recognitions, script),
481
+ "truncated": False,
482
+ "overlay_png_b64": None,
483
+ }
484
+
485
+
486
+ def _run_page(image: Image.Image, script: str) -> dict[str, Any]:
487
+ with ENGINES.lock:
488
+ boxes = _detect_boxes(image)
489
+ ordered = _order_boxes(boxes, READING_ORDER, image.width)
490
+ truncated = MAX_LINES > 0 and len(ordered) > MAX_LINES
491
+ if truncated:
492
+ LOGGER.warning(
493
+ "page has %d detected lines, truncating to AMPIXA_OCR_MAX_LINES=%d",
494
+ len(ordered),
495
+ MAX_LINES,
496
+ )
497
+ ordered = ordered[:MAX_LINES]
498
+ crops = [_to_bgr(_crop(image, box, CROP_PADDING)) for box in ordered]
499
+ recognitions = _recognize_routed(crops, script)
500
+ overlay = _overlay_png_b64(image, ordered)
501
+
502
+ lines: list[dict[str, Any]] = []
503
+ texts: list[str] = []
504
+ for box, recognition in zip(ordered, recognitions, strict=True):
505
+ lines.append(
506
+ {
507
+ "text": recognition.text,
508
+ "confidence": recognition.confidence,
509
+ "box": box.as_list(),
510
+ "script": recognition.engine,
511
+ }
512
+ )
513
+ if recognition.text:
514
+ texts.append(recognition.text)
515
+ return {
516
+ "text": "\n".join(texts),
517
+ "lines": lines,
518
+ "script": _resolved_script(recognitions, script),
519
+ "truncated": truncated,
520
+ "overlay_png_b64": overlay,
521
+ }
522
+
523
+
524
+ @asynccontextmanager
525
+ async def _lifespan(_app: FastAPI) -> AsyncIterator[None]:
526
+ """Serve /health immediately; build the Paddle predictors in the background."""
527
+ LOGGER.info(
528
+ "starting OCR worker: model_root=%s detector=%s (%s/%d) max_lines=%d",
529
+ MODEL_ROOT,
530
+ DETECTOR_MODEL_NAME,
531
+ DETECTOR_LIMIT_TYPE,
532
+ DETECTOR_LIMIT_SIDE_LEN,
533
+ MAX_LINES,
534
+ )
535
+ loader = threading.Thread(target=ENGINES.load, name="ocr-model-load", daemon=True)
536
+ loader.start()
537
+ yield
538
+ LOGGER.info("OCR worker shutting down")
539
+
540
+
541
+ app = FastAPI(title="Ampixa OCR worker", docs_url=None, redoc_url=None, lifespan=_lifespan)
542
+
543
+
544
+ @app.exception_handler(OcrError)
545
+ async def _ocr_error_handler(_request: Request, exc: OcrError) -> JSONResponse:
546
+ return JSONResponse(status_code=exc.status_code, content={"error": exc.message})
547
+
548
+
549
+ @app.exception_handler(RequestValidationError)
550
+ async def _validation_error_handler(
551
+ _request: Request, exc: RequestValidationError
552
+ ) -> JSONResponse:
553
+ """Report malformed multipart bodies as {"error": ...} instead of FastAPI's 422."""
554
+ problems = "; ".join(
555
+ f"{'.'.join(str(part) for part in error.get('loc', ()))}: {error.get('msg', 'invalid')}"
556
+ for error in exc.errors()
557
+ )
558
+ return JSONResponse(status_code=400, content={"error": f"invalid request: {problems}"})
559
+
560
+
561
+ @app.exception_handler(StarletteHTTPException)
562
+ async def _http_error_handler(_request: Request, exc: StarletteHTTPException) -> JSONResponse:
563
+ """Keep 404/405 and friends on the {"error": ...} contract."""
564
+ return JSONResponse(status_code=exc.status_code, content={"error": str(exc.detail)})
565
+
566
+
567
+ @app.exception_handler(Exception)
568
+ async def _unhandled_error_handler(_request: Request, exc: Exception) -> JSONResponse:
569
+ """Last resort: log the traceback, return JSON, never a bare stack trace."""
570
+ LOGGER.error("unhandled error: %s\n%s", exc, traceback.format_exc())
571
+ return JSONResponse(
572
+ status_code=500, content={"error": f"internal error: {type(exc).__name__}"}
573
+ )
574
+
575
+
576
+ @app.get("/health")
577
+ def health() -> JSONResponse:
578
+ body: dict[str, Any] = {
579
+ "ok": ENGINES.load_error is None,
580
+ "worker": WORKER_NAME,
581
+ "loaded": ENGINES.loaded,
582
+ "models": {
583
+ "sirijonga": str(SIRIJONGA_MODEL_DIR),
584
+ "devanagari": str(DEVANAGARI_MODEL_DIR),
585
+ "sirijonga_model_name": SIRIJONGA_MODEL_NAME,
586
+ "devanagari_model_name": DEVANAGARI_MODEL_NAME,
587
+ "detector_model_name": DETECTOR_MODEL_NAME,
588
+ "detector_limit": f"{DETECTOR_LIMIT_TYPE}/{DETECTOR_LIMIT_SIDE_LEN}",
589
+ "max_lines": MAX_LINES,
590
+ "max_image_side": MAX_IMAGE_SIDE,
591
+ "load_seconds": round(ENGINES.load_seconds, 2) if ENGINES.load_seconds else None,
592
+ },
593
+ "device": "cpu",
594
+ }
595
+ if ENGINES.load_error is not None:
596
+ body["error"] = ENGINES.load_error
597
+ return JSONResponse(status_code=503, content=body)
598
+ return JSONResponse(status_code=200, content=body)
599
+
600
+
601
+ @app.post("/recognize")
602
+ async def recognize(
603
+ file: UploadFile = File(...),
604
+ mode: str = Form("page"),
605
+ script: str = Form("auto"),
606
+ ) -> JSONResponse:
607
+ started = time.perf_counter()
608
+ mode = (mode or "page").strip().lower()
609
+ script = (script or "auto").strip().lower()
610
+ if mode not in SUPPORTED_MODES:
611
+ raise OcrError(f"mode must be one of {', '.join(SUPPORTED_MODES)}", 400)
612
+ if script not in SUPPORTED_SCRIPTS:
613
+ raise OcrError(f"script must be one of {', '.join(SUPPORTED_SCRIPTS)}", 400)
614
+ if not ENGINES.loaded:
615
+ if ENGINES.load_error is not None:
616
+ raise OcrError(f"OCR models failed to load: {ENGINES.load_error}", 503)
617
+ raise OcrError("OCR models are still loading", 503)
618
+
619
+ try:
620
+ raw = await file.read()
621
+ except Exception as exc: # noqa: BLE001 - upload stream failures are client errors
622
+ LOGGER.error("upload read failed: %s\n%s", exc, traceback.format_exc())
623
+ raise OcrError(f"could not read uploaded file: {exc}", 400) from exc
624
+ finally:
625
+ await file.close()
626
+
627
+ if not raw:
628
+ raise OcrError("uploaded file is empty", 400)
629
+ if len(raw) > MAX_UPLOAD_BYTES:
630
+ raise OcrError(
631
+ f"image is {len(raw)} bytes, limit is {MAX_UPLOAD_BYTES} bytes", 413
632
+ )
633
+
634
+ image = _decode_image(raw)
635
+ runner = _run_line if mode == "line" else _run_page
636
+ try:
637
+ result = await run_in_threadpool(runner, image, script)
638
+ except OcrError:
639
+ raise
640
+ except Exception as exc: # noqa: BLE001 - converted to a JSON 500 below
641
+ LOGGER.error(
642
+ "recognition failed mode=%s script=%s: %s\n%s",
643
+ mode,
644
+ script,
645
+ exc,
646
+ traceback.format_exc(),
647
+ )
648
+ raise OcrError(f"recognition failed: {type(exc).__name__}: {exc}", 500) from exc
649
+
650
+ elapsed = time.perf_counter() - started
651
+ LOGGER.info(
652
+ "recognize mode=%s script=%s -> %s bytes=%d size=%dx%d lines=%d truncated=%s chars=%d %.2fs",
653
+ mode,
654
+ script,
655
+ result["script"],
656
+ len(raw),
657
+ image.width,
658
+ image.height,
659
+ len(result["lines"]),
660
+ result["truncated"],
661
+ len(result["text"]),
662
+ elapsed,
663
+ )
664
+ return JSONResponse(
665
+ status_code=200,
666
+ content={
667
+ "text": result["text"],
668
+ "lines": result["lines"],
669
+ "script": result["script"],
670
+ "mode": mode,
671
+ "elapsed_s": round(elapsed, 3),
672
+ "image_size": [image.width, image.height],
673
+ "truncated": result["truncated"],
674
+ "overlay_png_b64": result["overlay_png_b64"],
675
+ },
676
+ )
677
+
678
+
679
+ def main() -> int:
680
+ parser = argparse.ArgumentParser(description="Ampixa OCR worker (Limbu Sirijonga + Devanagari)")
681
+ parser.add_argument("--host", default=DEFAULT_HOST)
682
+ parser.add_argument("--port", type=int, default=DEFAULT_PORT)
683
+ args = parser.parse_args()
684
+ logging.basicConfig(
685
+ level=logging.INFO,
686
+ format="%(asctime)s %(levelname)s %(name)s %(message)s",
687
+ stream=sys.stdout,
688
+ )
689
+ uvicorn.run(app, host=args.host, port=args.port, log_level="info", access_log=False)
690
+ return 0
691
+
692
+
693
+ if __name__ == "__main__":
694
+ raise SystemExit(main())
logs/eval_v4_last.log ADDED
@@ -0,0 +1,47 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ /home/ampixa/ocr/ocr-tech/tools/deva_rec_torch/export_onnx.py:38: DeprecationWarning: You are using the legacy TorchScript-based ONNX export. Starting in PyTorch 2.9, the new torch.export-based ONNX exporter has become the default. Learn more about the new export logic: https://docs.pytorch.org/docs/stable/onnx_export.html. For exporting control flow: https://pytorch.org/tutorials/beginner/onnx/export_control_flow_model_to_onnx_tutorial.html
3
+ torch.onnx.export(
4
+ /home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/transformers/integrations/sdpa_attention.py:124: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
5
+ is_causal = q_length > 1 and attention_mask is None and is_causal
6
+ batch 1 width 320: T=40 max|diff|=5.96e-07 argmax agree=1.0000
7
+ batch 4 width 480: T=60 max|diff|=1.10e-06 argmax agree=1.0000
8
+ batch 2 width 1200: T=150 max|diff|=6.56e-07 argmax agree=1.0000
9
+ exported and verified: /home/ampixa/ocr/ocr-deva-ft/export/v4-last-onnx
10
+ e1v4_chunks: 3000 predictions
11
+ e7_kra: 530 predictions
12
+ e1b_lines_kfix: 1500 predictions
13
+ e2_newar: 1139 predictions
14
+ e3_cit_ne: 800 predictions
15
+ e3_office_ne: 300 predictions
16
+ e3_cit_en: 300 predictions
17
+ e3_realtext: 400 predictions
18
+ e4_english: 120 predictions
19
+ e4_mixed: 120 predictions
20
+ e5_handwriting_probe: 450 predictions
21
+ e6_nvidia_en: 419 predictions
22
+ h1_nepali_cc0_test: 871 predictions
23
+ h2_iiit_test: 2000 predictions
24
+ h3_indicscan: 835 predictions
25
+ h4_dhcd_digits_test: 800 predictions
26
+ h4_dhcd_digits_test: 800 predictions
27
+ /home/ampixa/ocr/.venv-serve/lib/python3.11/site-packages/paddle/utils/cpp_extension/extension_utils.py:712: UserWarning: No ccache found. Please be aware that recompiling all source files may be required. You can download and install ccache from: https://github.com/ccache/ccache/blob/master/doc/INSTALL.md
28
+ warnings.warn(warning_message)
29
+ h4_dhcd_digits_test: 800 predictions
30
+ set official v3 v4last
31
+ e1v4_chunks cp 0.0513 gr 0.0817 ex 0.47 cp 0.0057 gr 0.0098 ex 0.93 cp 0.0063 gr 0.0111 ex 0.93 n=3000
32
+ e7_kra cp 0.0552 gr 0.0996 ex 0.33 cp 0.0086 gr 0.0150 ex 0.89 cp 0.0087 gr 0.0149 ex 0.88 n=530
33
+ e1b_lines_kfix cp 0.0769 gr 0.1265 ex 0.13 cp 0.0257 gr 0.0500 ex 0.54 cp 0.0266 gr 0.0518 ex 0.53 n=1500
34
+ e2_newar cp 0.0911 gr 0.1742 ex 0.06 cp 0.0354 gr 0.0779 ex 0.35 cp 0.0367 gr 0.0802 ex 0.34 n=1139
35
+ e3_cit_ne cp 0.0678 gr 0.1155 ex 0.56 cp 0.0075 gr 0.0125 ex 0.94 cp 0.0083 gr 0.0152 ex 0.92 n=800
36
+ e3_office_ne cp 0.0777 gr 0.1089 ex 0.54 cp 0.0092 gr 0.0138 ex 0.92 cp 0.0111 gr 0.0167 ex 0.90 n=300
37
+ e3_cit_en cp 0.0194 gr 0.0194 ex 0.83 cp 0.0064 gr 0.0064 ex 0.95 cp 0.0055 gr 0.0055 ex 0.96 n=300
38
+ e3_realtext cp 0.0857 gr 0.1388 ex 0.47 cp 0.0120 gr 0.0216 ex 0.89 cp 0.0166 gr 0.0344 ex 0.84 n=400
39
+ e4_english cp 0.0257 gr 0.0257 ex 0.62 cp 0.0164 gr 0.0164 ex 0.72 cp 0.0235 gr 0.0235 ex 0.66 n=120
40
+ e4_mixed cp 0.0678 gr 0.0785 ex 0.36 cp 0.0267 gr 0.0346 ex 0.70 cp 0.0325 gr 0.0415 ex 0.62 n=120
41
+ e5_handwriting_probe cp 0.6465 gr 0.7112 ex 0.01 cp 0.6102 gr 0.7176 ex 0.00 cp 0.5301 gr 0.6529 ex 0.04 n=450
42
+ e6_nvidia_en cp 0.1062 gr 0.1062 ex 0.71 cp 0.0284 gr 0.0284 ex 0.84 cp 0.0357 gr 0.0357 ex 0.81 n=419
43
+ h1_nepali_cc0_test cp 0.4075 gr 0.5477 ex 0.20 cp 0.6886 gr 0.9664 ex 0.04 cp 0.1554 gr 0.2426 ex 0.55 n=871
44
+ h2_iiit_test cp 0.4944 gr 0.6582 ex 0.07 cp 0.6051 gr 0.8296 ex 0.03 cp 0.0911 gr 0.1521 ex 0.64 n=2000
45
+ h3_indicscan cp 0.2520 gr 0.3649 ex 0.38 cp 0.4580 gr 0.6264 ex 0.14 cp 0.1337 gr 0.2053 ex 0.59 n=835
46
+ h4_dhcd_digits_test cp 0.7185 gr 0.7185 ex 0.02 cp 0.5295 gr 0.5295 ex 0.05 cp 0.0021 gr 0.0021 ex 0.98 n=800
47
+ EVAL_V4_DONE
logs/setup_env.log ADDED
@@ -0,0 +1,405 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ + OCR_ROOT=/home/ampixa/ocr
2
+ + export UV_INSTALL_DIR=/home/ampixa/ocr/bin UV_NO_MODIFY_PATH=1
3
+ + UV_INSTALL_DIR=/home/ampixa/ocr/bin
4
+ + UV_NO_MODIFY_PATH=1
5
+ + export UV_CACHE_DIR=/home/ampixa/ocr/.uv-cache UV_PYTHON_INSTALL_DIR=/home/ampixa/ocr/.uv-python
6
+ + UV_CACHE_DIR=/home/ampixa/ocr/.uv-cache
7
+ + UV_PYTHON_INSTALL_DIR=/home/ampixa/ocr/.uv-python
8
+ + REQ=/home/ampixa/ocr/ocr-tech/tools/deva_rec_torch/recipes/k2
9
+ + cd /home/ampixa/ocr
10
+ + '[' '!' -x /home/ampixa/ocr/bin/uv ']'
11
+ + curl -LsSf https://astral.sh/uv/install.sh
12
+ + sh
13
+ downloading uv 0.12.21 x86_64-unknown-linux-gnu
14
+ installing to /home/ampixa/ocr/bin
15
+ uv
16
+ uvx
17
+ everything's installed!
18
+ + UV=/home/ampixa/ocr/bin/uv
19
+ + /home/ampixa/ocr/bin/uv venv --allow-existing --python 3.11 .venv-torch
20
+ Downloading cpython-3.11.16-linux-x86_64-gnu (download) (29.4MiB)
21
+ Downloaded cpython-3.11.16-linux-x86_64-gnu (download)
22
+ Using CPython 3.11.16
23
+ Creating virtual environment at: .venv-torch
24
+ Activate with: source .venv-torch/bin/activate
25
+ + /home/ampixa/ocr/bin/uv pip install --python .venv-torch/bin/python --index-url https://download.pytorch.org/whl/cu126 torch==2.14.0
26
+ Using Python 3.11.16 environment at: .venv-torch
27
+ Resolved 29 packages in 3.85s
28
+ Downloading nvidia-nvshmem-cu12 (132.7MiB)
29
+ Downloading setuptools (1.2MiB)
30
+ Downloading sympy (6.0MiB)
31
+ Downloading networkx (2.0MiB)
32
+ Downloading cuda-bindings (7.3MiB)
33
+ Downloading nvidia-nvjitlink-cu12 (37.9MiB)
34
+ Downloading torch (828.9MiB)
35
+ Downloading triton (235.4MiB)
36
+ Downloading nvidia-cusparselt-cu12 (273.9MiB)
37
+ Downloading nvidia-curand-cu12 (53.7MiB)
38
+ Downloading nvidia-cuda-cupti-cu12 (8.5MiB)
39
+ Downloading nvidia-cusparse-cu12 (206.5MiB)
40
+ Downloading nvidia-nccl-cu12 (276.3MiB)
41
+ Downloading nvidia-cudnn-cu12 (674.0MiB)
42
+ Downloading nvidia-cusolver-cu12 (150.9MiB)
43
+ Downloading nvidia-cublas-cu12 (374.9MiB)
44
+ Downloading nvidia-cufile-cu12 (1.1MiB)
45
+ Downloading nvidia-cuda-nvrtc-cu12 (22.6MiB)
46
+ Downloading nvidia-cufft-cu12 (190.9MiB)
47
+ Downloaded setuptools
48
+ Downloaded triton
49
+ Downloaded nvidia-cufile-cu12
50
+ Downloaded torch
51
+ Downloaded nvidia-nvjitlink-cu12
52
+ Downloaded networkx
53
+ Downloaded nvidia-cuda-cupti-cu12
54
+ Downloaded nvidia-nvshmem-cu12
55
+ Downloaded nvidia-curand-cu12
56
+ Downloaded sympy
57
+ Downloaded cuda-bindings
58
+ Downloaded nvidia-cusparselt-cu12
59
+ Downloaded nvidia-cusparse-cu12
60
+ Downloaded nvidia-nccl-cu12
61
+ Downloaded nvidia-cuda-nvrtc-cu12
62
+ Downloaded nvidia-cusolver-cu12
63
+ Downloaded nvidia-cudnn-cu12
64
+ Downloaded nvidia-cublas-cu12
65
+ Downloaded nvidia-cufft-cu12
66
+ Prepared 29 packages in 4m 17s
67
+ Installed 29 packages in 424ms
68
+ + cuda-bindings==12.9.7
69
+ + cuda-pathfinder==1.6.0
70
+ + cuda-toolkit==12.6.3
71
+ + filelock==3.32.3
72
+ + fsspec==2026.7.0
73
+ + jinja2==3.1.6
74
+ + markupsafe==3.0.3
75
+ + mpmath==1.3.0
76
+ + networkx==3.6.1
77
+ + nvidia-cublas-cu12==12.6.4.1
78
+ + nvidia-cuda-cupti-cu12==12.6.80
79
+ + nvidia-cuda-nvrtc-cu12==12.6.85
80
+ + nvidia-cuda-runtime-cu12==12.6.77
81
+ + nvidia-cudnn-cu12==9.10.2.21
82
+ + nvidia-cufft-cu12==11.3.0.4
83
+ + nvidia-cufile-cu12==1.11.1.6
84
+ + nvidia-curand-cu12==10.3.7.77
85
+ + nvidia-cusolver-cu12==11.7.1.2
86
+ + nvidia-cusparse-cu12==12.5.4.2
87
+ + nvidia-cusparselt-cu12==0.7.1
88
+ + nvidia-nccl-cu12==2.29.3
89
+ + nvidia-nvjitlink-cu12==12.9.86
90
+ + nvidia-nvshmem-cu12==3.4.5
91
+ + nvidia-nvtx-cu12==12.6.77
92
+ + setuptools==78.1.0
93
+ + sympy==1.14.0
94
+ + torch==2.14.0+cu126
95
+ + triton==3.8.0
96
+ + typing-extensions==4.16.0
97
+ + /home/ampixa/ocr/bin/uv pip install --python .venv-torch/bin/python transformers==5.17.0 safetensors==0.8.0 onnx==1.23.0 onnxruntime==1.30.0 numpy==2.4.6 pillow huggingface_hub rapidfuzz regex pyyaml opencv-python-headless
98
+ Using Python 3.11.16 environment at: .venv-torch
99
+ Resolved 35 packages in 784ms
100
+ Downloading tokenizers (3.2MiB)
101
+ Downloading pygments (1.2MiB)
102
+ Downloading pillow (6.6MiB)
103
+ Downloading opencv-python-headless (58.4MiB)
104
+ Downloading transformers (11.7MiB)
105
+ Downloading rapidfuzz (3.0MiB)
106
+ Downloading numpy (16.1MiB)
107
+ Downloading hf-xet (4.3MiB)
108
+ Downloading onnxruntime (22.5MiB)
109
+ Downloading onnx (8.5MiB)
110
+ Downloaded pygments
111
+ Downloaded rapidfuzz
112
+ Downloaded tokenizers
113
+ Downloaded hf-xet
114
+ Downloaded pillow
115
+ Downloaded onnx
116
+ Downloaded transformers
117
+ Downloaded numpy
118
+ Downloaded onnxruntime
119
+ Downloaded opencv-python-headless
120
+ Prepared 32 packages in 14.74s
121
+ Installed 32 packages in 1.14s
122
+ + annotated-doc==0.0.5
123
+ + anyio==4.15.1
124
+ + certifi==2026.7.22
125
+ + click==8.5.0
126
+ + flatbuffers==25.12.19
127
+ + h11==0.16.0
128
+ + hf-xet==1.6.0
129
+ + httpcore==1.0.9
130
+ + httpx==0.28.1
131
+ + huggingface-hub==1.33.0
132
+ + idna==3.20
133
+ + markdown-it-py==4.2.0
134
+ + mdurl==0.1.2
135
+ + ml-dtypes==0.6.0
136
+ + numpy==2.4.6
137
+ + onnx==1.23.0
138
+ + onnxruntime==1.30.0
139
+ + opencv-python-headless==5.0.0.93
140
+ + packaging==26.3
141
+ + pillow==12.3.0
142
+ + protobuf==7.36.2
143
+ + pygments==2.21.0
144
+ + pyyaml==6.0.3
145
+ + rapidfuzz==3.14.6
146
+ + regex==2026.9.29
147
+ + rich==15.0.0
148
+ + safetensors==0.8.0
149
+ + shellingham==1.5.4
150
+ + tokenizers==0.23.2
151
+ + tqdm==4.70.1
152
+ + transformers==5.17.0
153
+ + typer==0.27.2
154
+ + .venv-torch/bin/python -c 'import torch, transformers; print(torch.__version__, transformers.__version__, torch.version.cuda, torch.cuda.is_available(), [torch.cuda.get_device_name(i) for i in range(torch.cuda.device_count())])'
155
+ 2.14.0+cu126 5.17.0 12.6 True ['Tesla T4', 'Tesla T4']
156
+ + /home/ampixa/ocr/bin/uv venv --allow-existing --python 3.11 .venv-serve
157
+ Using CPython 3.11.16
158
+ Creating virtual environment at: .venv-serve
159
+ Activate with: source .venv-serve/bin/activate
160
+ + /home/ampixa/ocr/bin/uv pip install --python .venv-serve/bin/python -r /home/ampixa/ocr/ocr-tech/tools/deva_rec_torch/recipes/k2/serve-requirements.txt
161
+ Using Python 3.11.16 environment at: .venv-serve
162
+ Resolved 145 packages in 1.23s
163
+ Downloading pycryptodome (2.2MiB)
164
+ Downloading pygments (1.2MiB)
165
+ Downloading nvidia-cufile (1.2MiB)
166
+ Downloading networkx (2.0MiB)
167
+ Downloading tiktoken (1.1MiB)
168
+ Downloading nvidia-cusolver (191.6MiB)
169
+ Downloading nvidia-cusparse (139.2MiB)
170
+ Downloading nvidia-nvjitlink (38.8MiB)
171
+ Downloading sympy (6.0MiB)
172
+ Downloading nvidia-cuda-runtime (2.1MiB)
173
+ Downloading sentencepiece (1.3MiB)
174
+ Downloading setuptools (1.0MiB)
175
+ Downloading lxml (5.0MiB)
176
+ Downloading nvidia-cublas (403.5MiB)
177
+ Downloading opencv-python-headless (47.7MiB)
178
+ Downloading numpy (16.1MiB)
179
+ Downloading pydantic-core (2.0MiB)
180
+ Downloading tokenizers (3.1MiB)
181
+ Downloading pyarrow (46.6MiB)
182
+ Downloading pandas (10.8MiB)
183
+ Downloading scikit-learn (8.9MiB)
184
+ Downloading aiohttp (1.7MiB)
185
+ Downloading paddlepaddle (185.8MiB)
186
+ Downloading shapely (3.0MiB)
187
+ Downloading nvidia-cufft (204.2MiB)
188
+ Downloading cuda-bindings (6.8MiB)
189
+ Downloading transformers (11.4MiB)
190
+ Downloading nvidia-cuda-nvrtc (86.0MiB)
191
+ Downloading opencv-contrib-python (65.5MiB)
192
+ Downloading pillow (4.3MiB)
193
+ Downloading nvidia-nvshmem-cu13 (57.6MiB)
194
+ Downloading h5py (4.8MiB)
195
+ Downloading scipy (33.7MiB)
196
+ Downloading nvidia-curand (56.8MiB)
197
+ Downloading nvidia-cusparselt-cu13 (162.3MiB)
198
+ Downloading nvidia-cuda-cupti (10.2MiB)
199
+ Downloading pymupdf (23.8MiB)
200
+ Downloading hf-xet (4.3MiB)
201
+ Downloading nvidia-nccl-cu13 (196.4MiB)
202
+ Downloading triton (192.1MiB)
203
+ Downloading modelscope (5.8MiB)
204
+ Downloading pypdfium2 (2.7MiB)
205
+ Downloading nvidia-cudnn-cu13 (349.2MiB)
206
+ Downloading torch (507.5MiB)
207
+ Downloading paddlex (2.1MiB)
208
+ Downloaded setuptools
209
+ Downloaded tiktoken
210
+ Downloaded nvidia-cufile
211
+ Downloaded pygments
212
+ Downloaded sentencepiece
213
+ Downloaded aiohttp
214
+ Downloaded networkx
215
+ Downloaded pydantic-core
216
+ Downloaded pycryptodome
217
+ Downloaded paddlex
218
+ Downloaded nvidia-cuda-runtime
219
+ Downloaded pypdfium2
220
+ Downloaded shapely
221
+ Downloaded tokenizers
222
+ Downloaded pillow
223
+ Downloaded hf-xet
224
+ Downloaded h5py
225
+ Downloaded lxml
226
+ Downloaded modelscope
227
+ Downloaded sympy
228
+ Downloaded cuda-bindings
229
+ Downloaded scikit-learn
230
+ Downloaded nvidia-cuda-cupti
231
+ Downloaded pandas
232
+ Downloaded transformers
233
+ Downloaded numpy
234
+ Downloaded pymupdf
235
+ Downloaded scipy
236
+ Downloaded nvidia-nvjitlink
237
+ Downloaded pyarrow
238
+ Downloaded opencv-python-headless
239
+ Downloaded nvidia-curand
240
+ Downloaded nvidia-nvshmem-cu13
241
+ Downloaded opencv-contrib-python
242
+ Downloaded nvidia-cuda-nvrtc
243
+ Downloaded nvidia-cusparse
244
+ Downloaded nvidia-cusparselt-cu13
245
+ Downloaded paddlepaddle
246
+ Downloaded triton
247
+ Downloaded nvidia-cusolver
248
+ Downloaded nvidia-nccl-cu13
249
+ Downloaded nvidia-cufft
250
+ Downloaded nvidia-cudnn-cu13
251
+ Downloaded nvidia-cublas
252
+ Downloaded torch
253
+ Prepared 136 packages in 5m 34s
254
+ Installed 145 packages in 998ms
255
+ + accelerate==1.14.0
256
+ + aiohappyeyeballs==2.6.2
257
+ + aiohttp==3.14.1
258
+ + aiosignal==1.4.0
259
+ + aistudio-sdk==0.3.8
260
+ + annotated-doc==0.0.4
261
+ + annotated-types==0.7.0
262
+ + anyio==4.13.0
263
+ + attrs==26.1.0
264
+ + bce-python-sdk==0.9.72
265
+ + beautifulsoup4==4.15.0
266
+ + cachetools==7.1.4
267
+ + certifi==2026.5.20
268
+ + chardet==7.4.3
269
+ + charset-normalizer==3.4.7
270
+ + click==8.4.1
271
+ + colorlog==6.10.1
272
+ + crc32c==2.8
273
+ + cssselect==1.4.0
274
+ + cssutils==2.15.0
275
+ + cuda-bindings==13.4.3
276
+ + cuda-pathfinder==1.8.2
277
+ + cuda-toolkit==13.0.2
278
+ + datasets==5.0.0
279
+ + dill==0.4.1
280
+ + distro==1.9.0
281
+ + einops==0.8.2
282
+ + encutils==1.0.0
283
+ + et-xmlfile==2.0.0
284
+ + fastapi==0.141.1
285
+ + filelock==3.29.4
286
+ + filetype==1.2.0
287
+ + flatbuffers==25.12.19
288
+ + frozenlist==1.8.0
289
+ + fsspec==2026.4.0
290
+ + ftfy==6.3.1
291
+ + future==1.0.0
292
+ + h11==0.16.0
293
+ + h5py==3.16.0
294
+ + hf-xet==1.5.1
295
+ + httpcore==1.0.9
296
+ + httpx==0.27.2
297
+ + huggingface-hub==0.36.2
298
+ + idna==3.18
299
+ + imagesize==2.0.0
300
+ + iniconfig==2.3.0
301
+ + jinja2==3.1.6
302
+ + jiter==0.15.0
303
+ + joblib==1.5.3
304
+ + latex2mathml==3.81.0
305
+ + lxml==6.1.1
306
+ + markdown-it-py==4.2.0
307
+ + markupsafe==3.0.3
308
+ + mdurl==0.1.2
309
+ + modelscope==1.37.1
310
+ + more-itertools==11.1.0
311
+ + mpmath==1.3.0
312
+ + multidict==6.7.1
313
+ + multiprocess==0.70.19
314
+ + narwhals==2.22.1
315
+ + networkx==3.6.1
316
+ + numpy==2.3.5
317
+ + nvidia-cublas==13.1.1.3
318
+ + nvidia-cuda-cupti==13.0.85
319
+ + nvidia-cuda-nvrtc==13.0.88
320
+ + nvidia-cuda-runtime==13.0.96
321
+ + nvidia-cudnn-cu13==9.20.0.48
322
+ + nvidia-cufft==12.0.0.61
323
+ + nvidia-cufile==1.15.1.6
324
+ + nvidia-curand==10.4.0.35
325
+ + nvidia-cusolver==12.0.4.66
326
+ + nvidia-cusparse==12.6.3.3
327
+ + nvidia-cusparselt-cu13==0.8.1
328
+ + nvidia-nccl-cu13==2.29.7
329
+ + nvidia-nvjitlink==13.0.88
330
+ + nvidia-nvshmem-cu13==3.4.5
331
+ + nvidia-nvtx==13.0.85
332
+ + onnxruntime==1.30.0
333
+ + openai==1.109.1
334
+ + opencv-contrib-python==4.10.0.84
335
+ + opencv-python-headless==4.11.0.86
336
+ + openpyxl==3.1.5
337
+ + opt-einsum==3.3.0
338
+ + packaging==26.2
339
+ + paddleocr==3.7.0
340
+ + paddlepaddle==3.3.1
341
+ + paddlex==3.7.1
342
+ + pandas==3.0.3
343
+ + pillow==10.4.0
344
+ + platformdirs==4.10.0
345
+ + pluggy==1.6.0
346
+ + premailer==3.10.0
347
+ + prettytable==3.17.0
348
+ + propcache==0.5.2
349
+ + protobuf==7.35.1
350
+ + psutil==7.2.2
351
+ + py-cpuinfo==9.0.0
352
+ + pyarrow==24.0.0
353
+ + pyclipper==1.4.0
354
+ + pycryptodome==3.23.0
355
+ + pydantic==2.13.4
356
+ + pydantic-core==2.46.4
357
+ + pydantic-settings==2.14.1
358
+ + pygments==2.20.0
359
+ + pymupdf==1.27.2.3
360
+ + pypdfium2==4.30.0
361
+ + pytest==9.1.0
362
+ + python-bidi==0.6.10
363
+ + python-dateutil==2.9.0.post0
364
+ + python-dotenv==1.2.2
365
+ + python-multipart==0.0.32
366
+ + pyyaml==6.0.2
367
+ + regex==2026.5.9
368
+ + requests==2.34.2
369
+ + rich==15.0.0
370
+ + ruamel-yaml==0.19.1
371
+ + safetensors==0.8.0
372
+ + scikit-learn==1.9.0
373
+ + scipy==1.17.1
374
+ + sentencepiece==0.2.1
375
+ + setuptools==81.0.0
376
+ + shapely==2.1.2
377
+ + shellingham==1.5.4
378
+ + six==1.17.0
379
+ + sniffio==1.3.1
380
+ + soupsieve==2.8.4
381
+ + starlette==1.6.0
382
+ + surya-ocr==0.20.0
383
+ + sympy==1.14.0
384
+ + threadpoolctl==3.6.0
385
+ + tiktoken==0.13.0
386
+ + tokenizers==0.22.2
387
+ + torch==2.12.0
388
+ + tqdm==4.68.2
389
+ + transformers==4.57.6
390
+ + triton==3.7.0
391
+ + typer==0.25.1
392
+ + typing-extensions==4.15.0
393
+ + typing-inspection==0.4.2
394
+ + ujson==5.12.1
395
+ + urllib3==2.7.0
396
+ + uvicorn==0.52.4
397
+ + wcwidth==0.8.1
398
+ + xxhash==3.7.0
399
+ + yarl==1.24.2
400
+ + .venv-serve/bin/python -c 'import paddle, paddlex, paddleocr, onnxruntime; print(paddle.__version__, paddlex.__version__, onnxruntime.__version__)'
401
+ /home/ampixa/ocr/.venv-serve/lib/python3.11/site-packages/paddle/utils/cpp_extension/extension_utils.py:712: UserWarning: No ccache found. Please be aware that recompiling all source files may be required. You can download and install ccache from: https://github.com/ccache/ccache/blob/master/doc/INSTALL.md
402
+ warnings.warn(warning_message)
403
+ 3.3.1 3.7.1 1.30.0
404
+ + echo T4_SETUP_DONE
405
+ T4_SETUP_DONE
logs/smoke_train.log ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ctc cpu=24.265554 cuda=24.265553 |dloss|=1.91e-06 max|dgrad|=1.04e-06
2
+
3
+ grad-accum: max |full - 2x8| / max |full| = 1.90e-07
4
+ {
5
+ "real_kept": 4000,
6
+ "hw_kept": 2000,
7
+ "real_dev_kept": 300,
8
+ "hw_dev_kept": 300
9
+ }
10
+ reading image sizes...
11
+
12
+ device=cuda train_items=6000 steps/epoch=93 total=93
13
+ {"step": 0, "dev_mean_cp_cer": 0.3183555169212171, "target_mean_cp_cer": 0.6313131313131313, "eligible": true, "guard_regressions": {}, "real_dev_cer_codepoint": 0.0053979025293029, "real_dev_cer_grapheme": 0.008409785932721712, "real_dev_exact_line_acc": 0.9366666666666666, "hw_dev_cer_codepoint": 0.6313131313131313, "hw_dev_cer_grapheme": 0.8311425682507584, "hw_dev_exact_line_acc": 0.03333333333333333}
14
+
15
+ [W1001 19:07:31.645636384 CUDACachingAllocator.cpp:3934] memory allocation failed with OOM on device 0 while trying to allocate 65011712 bytes (free: 65011712, total: 15640231936).
16
+ epoch 0 step 50/93 loss 0.3706 lr 3.19e-05 50 img/s
17
+ {"step": 50, "dev_mean_cp_cer": 0.15958604137197596, "target_mean_cp_cer": 0.30976430976430974, "eligible": false, "guard_regressions": {"real_dev": 0.00401}, "real_dev_cer_codepoint": 0.009407772979642195, "real_dev_cer_grapheme": 0.015035677879714576, "real_dev_exact_line_acc": 0.8666666666666667, "hw_dev_cer_codepoint": 0.30976430976430974, "hw_dev_cer_grapheme": 0.4368048533872599, "hw_dev_exact_line_acc": 0.21666666666666667}
18
+ step 50: not eligible as best, guard sets regressed: {'real_dev': 0.00401}
19
+
20
+ {"step": 93, "dev_mean_cp_cer": 0.14468075089090923, "target_mean_cp_cer": 0.2822671156004489, "eligible": true, "guard_regressions": {}, "real_dev_cer_codepoint": 0.007094386181369525, "real_dev_cer_grapheme": 0.010703363914373088, "real_dev_exact_line_acc": 0.91, "hw_dev_cer_codepoint": 0.2822671156004489, "hw_dev_cer_grapheme": 0.40040444893832156, "hw_dev_exact_line_acc": 0.26666666666666666}
21
+
22
+ done: best target mean cp CER 0.2823 (targets ['hw_dev'], guards ['real_dev'])
23
+ SMOKE_DONE
logs/train_v4-oom-accum2.log ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "iiit_hw_kept": 69853,
3
+ "nep_hw_kept": 3187,
4
+ "dhcd_digits_kept": 15000,
5
+ "real_v4_kept": 125017,
6
+ "cit_ne_kept": 30000,
7
+ "office_ne_kept": 12000,
8
+ "cit_en_v1_kept": 10000,
9
+ "realtext_v1_kept": 29959,
10
+ "longne_kept": 19930,
11
+ "latin_text_kept": 18881,
12
+ "latin_cit_en_kept": 10000,
13
+ "nvidia_en_kept": 31934,
14
+ "real_dev_kept": 15570,
15
+ "synth_dev_kept": 800,
16
+ "en_dev_kept": 600,
17
+ "hw_hindi_dev_kept": 2000,
18
+ "hw_nep_dev_kept": 300
19
+ }
20
+ reading image sizes...
21
+
22
+ device=cuda train_items=375761 steps/epoch=2511 total=7533
23
+ {"step": 0, "dev_mean_cp_cer": 0.2924696292460386, "target_mean_cp_cer": 0.6624097401587867, "eligible": true, "guard_regressions": {}, "real_dev_cer_codepoint": 0.08906285977434952, "real_dev_cer_grapheme": 0.10007883178798004, "real_dev_exact_line_acc": 0.8045, "synth_dev_cer_codepoint": 0.007164179104477612, "synth_dev_cer_grapheme": 0.010831267127756753, "synth_dev_exact_line_acc": 0.935, "en_dev_cer_codepoint": 0.04130162703379224, "en_dev_cer_grapheme": 0.04130162703379224, "en_dev_exact_line_acc": 0.7333333333333333, "hw_hindi_dev_cer_codepoint": 0.6391239422598307, "hw_hindi_dev_cer_grapheme": 0.8593396653098145, "hw_hindi_dev_exact_line_acc": 0.022, "hw_nep_dev_cer_codepoint": 0.6856955380577427, "hw_nep_dev_cer_grapheme": 0.9724770642201835, "hw_nep_dev_exact_line_acc": 0.043333333333333335}
24
+
25
+ epoch 0 step 50/7533 loss 0.1331 lr 8.50e-06 47 img/s
26
+ [W1001 19:15:47.191063854 CUDACachingAllocator.cpp:3934] memory allocation failed with OOM on device 0 while trying to allocate 90177536 bytes (free: 88080384, total: 15640231936).
27
+ [W1001 19:15:48.523346228 CUDACachingAllocator.cpp:3934] memory allocation failed with OOM on device 0 while trying to allocate 90177536 bytes (free: 60817408, total: 15640231936).
28
+ [W1001 19:15:48.523487257 CUDACachingAllocator.cpp:3934] memory allocation failed with OOM on device 0 while trying to allocate 90177536 bytes (free: 60817408, total: 15640231936).
29
+ Traceback (most recent call last):
30
+ File "/home/ampixa/ocr/ocr-tech/tools/deva_rec_torch/train.py", line 345, in <module>
31
+ sys.exit(main())
32
+ ^^^^^^
33
+ File "/home/ampixa/ocr/ocr-tech/tools/deva_rec_torch/train.py", line 319, in main
34
+ part_loss = ctc_loss(logits(model, pixel_values[part].to(device)), [ids[i] for i in rows])
35
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36
+ File "/home/ampixa/ocr/ocr-tech/tools/deva_rec_torch/common.py", line 104, in logits
37
+ features = model.model(pixel_values).last_hidden_state
38
+ ^^^^^^^^^^^^^^^^^^^^^^^^^
39
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1783, in _wrapped_call_impl
40
+ return self._call_impl(*args, **kwargs)
41
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1794, in _call_impl
43
+ return forward_call(*args, **kwargs)
44
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
45
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/transformers/utils/generic.py", line 939, in wrapper
46
+ output = func(self, *args, **kwargs)
47
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
48
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/transformers/models/pp_ocrv5_mobile_rec/modeling_pp_ocrv5_mobile_rec.py", line 337, in forward
49
+ outputs = self.backbone(pixel_values, **kwargs)
50
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
51
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1783, in _wrapped_call_impl
52
+ return self._call_impl(*args, **kwargs)
53
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
54
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1794, in _call_impl
55
+ return forward_call(*args, **kwargs)
56
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
57
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/transformers/utils/generic.py", line 939, in wrapper
58
+ output = func(self, *args, **kwargs)
59
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
60
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/transformers/backbone_utils.py", line 172, in wrapper
61
+ output = forward_function(self, *args, **kwargs)
62
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
63
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/transformers/models/pp_lcnet_v3/modeling_pp_lcnet_v3.py", line 365, in forward
64
+ hidden_states = self.encoder(pixel_values, **kwargs).hidden_states
65
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1783, in _wrapped_call_impl
67
+ return self._call_impl(*args, **kwargs)
68
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
69
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1794, in _call_impl
70
+ return forward_call(*args, **kwargs)
71
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
72
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/transformers/utils/generic.py", line 1068, in wrapper
73
+ output = func(self, *args, **kwargs)
74
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
75
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/transformers/utils/output_capturing.py", line 286, in wrapper
76
+ outputs = func(self, *args, **kwargs)
77
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^
78
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/transformers/models/pp_lcnet_v3/modeling_pp_lcnet_v3.py", line 404, in forward
79
+ hidden_state = block(hidden_state)
80
+ ^^^^^^^^^^^^^^^^^^^
81
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1783, in _wrapped_call_impl
82
+ return self._call_impl(*args, **kwargs)
83
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
84
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1889, in _call_impl
85
+ return inner()
86
+ ^^^^^^^
87
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1837, in inner
88
+ result = forward_call(*args, **kwargs)
89
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
90
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/transformers/models/pp_lcnet_v3/modeling_pp_lcnet_v3.py", line 291, in forward
91
+ hidden_states = layer(hidden_states)
92
+ ^^^^^^^^^^^^^^^^^^^^
93
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/transformers/modeling_layers.py", line 109, in __call__
94
+ return super().__call__(*args, **kwargs)
95
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
96
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1783, in _wrapped_call_impl
97
+ return self._call_impl(*args, **kwargs)
98
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
99
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1794, in _call_impl
100
+ return forward_call(*args, **kwargs)
101
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/transformers/models/pp_lcnet_v3/modeling_pp_lcnet_v3.py", line 247, in forward
103
+ hidden_state = self.depthwise_convolution(hidden_state)
104
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
105
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1783, in _wrapped_call_impl
106
+ return self._call_impl(*args, **kwargs)
107
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
108
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1794, in _call_impl
109
+ return forward_call(*args, **kwargs)
110
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
111
+ File "/home/ampixa/ocr/.venv-torch/lib/python3.11/site-packages/transformers/models/pp_lcnet_v3/modeling_pp_lcnet_v3.py", line 157, in forward
112
+ output = residual if output is None else output + residual
113
+ ~~~~~~~^~~~~~~~~~
114
+ torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 86.00 MiB. GPU 0 has a total capacity of 14.57 GiB of which 58.00 MiB is free. Including non-PyTorch memory, this process has 14.50 GiB memory in use. Of the allocated memory 11.38 GiB is allocated by PyTorch, and 2.99 GiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation. See documentation for Memory Management (https://docs.pytorch.org/docs/stable/notes/cuda.html#optimizing-memory-usage-with-pytorch-cuda-alloc-conf)
logs/train_v4.log ADDED
@@ -0,0 +1,190 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "iiit_hw_kept": 69853,
3
+ "nep_hw_kept": 3187,
4
+ "dhcd_digits_kept": 15000,
5
+ "real_v4_kept": 125017,
6
+ "cit_ne_kept": 30000,
7
+ "office_ne_kept": 12000,
8
+ "cit_en_v1_kept": 10000,
9
+ "realtext_v1_kept": 29959,
10
+ "longne_kept": 19930,
11
+ "latin_text_kept": 18881,
12
+ "latin_cit_en_kept": 10000,
13
+ "nvidia_en_kept": 31934,
14
+ "real_dev_kept": 15570,
15
+ "synth_dev_kept": 800,
16
+ "en_dev_kept": 600,
17
+ "hw_hindi_dev_kept": 2000,
18
+ "hw_nep_dev_kept": 300
19
+ }
20
+ reading image sizes...
21
+
22
+ device=cuda train_items=375761 steps/epoch=2511 total=7533
23
+ {"step": 0, "dev_mean_cp_cer": 0.2924696292460386, "target_mean_cp_cer": 0.6624097401587867, "eligible": true, "guard_regressions": {}, "real_dev_cer_codepoint": 0.08906285977434952, "real_dev_cer_grapheme": 0.10007883178798004, "real_dev_exact_line_acc": 0.8045, "synth_dev_cer_codepoint": 0.007164179104477612, "synth_dev_cer_grapheme": 0.010831267127756753, "synth_dev_exact_line_acc": 0.935, "en_dev_cer_codepoint": 0.04130162703379224, "en_dev_cer_grapheme": 0.04130162703379224, "en_dev_exact_line_acc": 0.7333333333333333, "hw_hindi_dev_cer_codepoint": 0.6391239422598307, "hw_hindi_dev_cer_grapheme": 0.8593396653098145, "hw_hindi_dev_exact_line_acc": 0.022, "hw_nep_dev_cer_codepoint": 0.6856955380577427, "hw_nep_dev_cer_grapheme": 0.9724770642201835, "hw_nep_dev_exact_line_acc": 0.043333333333333335}
24
+
25
+ epoch 0 step 50/7533 loss 0.1057 lr 8.50e-06 47 img/s
26
+ epoch 0 step 100/7533 loss 0.8932 lr 1.68e-05 46 img/s
27
+ epoch 0 step 150/7533 loss 0.6944 lr 2.52e-05 47 img/s
28
+ epoch 0 step 200/7533 loss 0.5371 lr 3.35e-05 48 img/s
29
+ epoch 0 step 250/7533 loss 0.4007 lr 4.18e-05 48 img/s
30
+ epoch 0 step 300/7533 loss 0.3354 lr 5.00e-05 48 img/s
31
+ epoch 0 step 350/7533 loss 0.3486 lr 5.00e-05 48 img/s
32
+ epoch 0 step 400/7533 loss 0.5050 lr 5.00e-05 48 img/s
33
+ epoch 0 step 450/7533 loss 0.5424 lr 4.99e-05 47 img/s
34
+ epoch 0 step 500/7533 loss 0.0772 lr 4.99e-05 47 img/s
35
+ epoch 0 step 550/7533 loss 0.2343 lr 4.99e-05 47 img/s
36
+ epoch 0 step 600/7533 loss 0.2554 lr 4.98e-05 47 img/s
37
+ epoch 0 step 650/7533 loss 0.2746 lr 4.97e-05 47 img/s
38
+ epoch 0 step 700/7533 loss 0.3602 lr 4.96e-05 47 img/s
39
+ epoch 0 step 750/7533 loss 0.2427 lr 4.95e-05 47 img/s
40
+ epoch 0 step 800/7533 loss 0.1184 lr 4.94e-05 47 img/s
41
+ epoch 0 step 850/7533 loss 0.2567 lr 4.93e-05 47 img/s
42
+ epoch 0 step 900/7533 loss 0.1891 lr 4.92e-05 47 img/s
43
+ epoch 0 step 950/7533 loss 0.1782 lr 4.90e-05 47 img/s
44
+ epoch 0 step 1000/7533 loss 0.0722 lr 4.89e-05 47 img/s
45
+ epoch 0 step 1050/7533 loss 0.2994 lr 4.87e-05 47 img/s
46
+ epoch 0 step 1100/7533 loss 0.3314 lr 4.85e-05 47 img/s
47
+ epoch 0 step 1150/7533 loss 0.2300 lr 4.83e-05 47 img/s
48
+ epoch 0 step 1200/7533 loss 0.1252 lr 4.81e-05 47 img/s
49
+ epoch 0 step 1250/7533 loss 0.3062 lr 4.79e-05 47 img/s
50
+ epoch 0 step 1300/7533 loss 0.2066 lr 4.77e-05 47 img/s
51
+ epoch 0 step 1350/7533 loss 0.2541 lr 4.74e-05 47 img/s
52
+ epoch 0 step 1400/7533 loss 0.2309 lr 4.72e-05 47 img/s
53
+ epoch 0 step 1450/7533 loss 0.1560 lr 4.69e-05 47 img/s
54
+ epoch 0 step 1500/7533 loss 0.1918 lr 4.67e-05 47 img/s
55
+ {"step": 1500, "dev_mean_cp_cer": 0.09661153968172269, "target_mean_cp_cer": 0.15615393459092874, "eligible": false, "guard_regressions": {"real_dev": 0.00495, "synth_dev": 0.01582, "en_dev": 0.01245}, "real_dev_cer_codepoint": 0.09401335482385448, "real_dev_cer_grapheme": 0.10664814745298247, "real_dev_exact_line_acc": 0.7725, "synth_dev_cer_codepoint": 0.02298507462686567, "synth_dev_cer_grapheme": 0.03249380138327026, "synth_dev_exact_line_acc": 0.78625, "en_dev_cer_codepoint": 0.05375139977603583, "en_dev_cer_grapheme": 0.05375139977603583, "en_dev_exact_line_acc": 0.6683333333333333, "hw_hindi_dev_cer_codepoint": 0.12070681931309109, "hw_hindi_dev_cer_grapheme": 0.19071310116086235, "hw_hindi_dev_exact_line_acc": 0.5655, "hw_nep_dev_cer_codepoint": 0.19160104986876642, "hw_nep_dev_cer_grapheme": 0.3038990825688073, "hw_nep_dev_exact_line_acc": 0.4766666666666667}
56
+ step 1500: not eligible as best, guard sets regressed: {'real_dev': 0.00495, 'synth_dev': 0.01582, 'en_dev': 0.01245}
57
+ epoch 0 step 1550/7533 loss 0.0390 lr 4.64e-05 46 img/s
58
+ epoch 0 step 1600/7533 loss 0.1754 lr 4.61e-05 46 img/s
59
+ epoch 0 step 1650/7533 loss 0.0595 lr 4.58e-05 46 img/s
60
+ epoch 0 step 1700/7533 loss 0.2074 lr 4.55e-05 46 img/s
61
+ epoch 0 step 1750/7533 loss 0.1467 lr 4.52e-05 46 img/s
62
+ epoch 0 step 1800/7533 loss 0.2243 lr 4.49e-05 46 img/s
63
+ epoch 0 step 1850/7533 loss 0.1996 lr 4.45e-05 46 img/s
64
+ epoch 0 step 1900/7533 loss 0.2966 lr 4.42e-05 46 img/s
65
+ epoch 0 step 1950/7533 loss 0.1680 lr 4.39e-05 46 img/s
66
+ epoch 0 step 2000/7533 loss 0.1996 lr 4.35e-05 46 img/s
67
+ epoch 0 step 2050/7533 loss 0.0544 lr 4.31e-05 46 img/s
68
+ epoch 0 step 2100/7533 loss 0.1143 lr 4.27e-05 46 img/s
69
+ epoch 0 step 2150/7533 loss 0.1108 lr 4.24e-05 46 img/s
70
+ epoch 0 step 2200/7533 loss 0.0920 lr 4.20e-05 46 img/s
71
+ epoch 0 step 2250/7533 loss 0.2657 lr 4.16e-05 46 img/s
72
+ epoch 0 step 2300/7533 loss 0.0966 lr 4.11e-05 46 img/s
73
+ epoch 0 step 2350/7533 loss 0.1935 lr 4.07e-05 46 img/s
74
+ epoch 0 step 2400/7533 loss 0.2806 lr 4.03e-05 46 img/s
75
+ epoch 0 step 2450/7533 loss 0.0774 lr 3.99e-05 46 img/s
76
+ epoch 0 step 2500/7533 loss 0.1280 lr 3.94e-05 46 img/s
77
+
78
+ epoch 1 step 2550/7533 loss 0.2241 lr 3.90e-05 46 img/s
79
+ epoch 1 step 2600/7533 loss 0.1630 lr 3.85e-05 46 img/s
80
+ epoch 1 step 2650/7533 loss 0.2530 lr 3.81e-05 46 img/s
81
+ epoch 1 step 2700/7533 loss 0.1961 lr 3.76e-05 46 img/s
82
+ epoch 1 step 2750/7533 loss 0.1071 lr 3.71e-05 46 img/s
83
+ epoch 1 step 2800/7533 loss 0.1324 lr 3.67e-05 46 img/s
84
+ epoch 1 step 2850/7533 loss 0.0896 lr 3.62e-05 46 img/s
85
+ epoch 1 step 2900/7533 loss 0.1086 lr 3.57e-05 46 img/s
86
+ epoch 1 step 2950/7533 loss 0.1422 lr 3.52e-05 46 img/s
87
+ epoch 1 step 3000/7533 loss 0.1706 lr 3.47e-05 46 img/s
88
+ {"step": 3000, "dev_mean_cp_cer": 0.08493811709652706, "target_mean_cp_cer": 0.13522090226526562, "eligible": false, "guard_regressions": {"synth_dev": 0.00577, "en_dev": 0.00922}, "real_dev_cer_codepoint": 0.0907897766520838, "real_dev_cer_grapheme": 0.10413303802695296, "real_dev_exact_line_acc": 0.7855, "synth_dev_cer_codepoint": 0.012935323383084577, "synth_dev_cer_grapheme": 0.018139110009134804, "synth_dev_exact_line_acc": 0.88875, "en_dev_cer_codepoint": 0.05052368091693564, "en_dev_cer_grapheme": 0.05052368091693564, "en_dev_exact_line_acc": 0.6633333333333333, "hw_hindi_dev_cer_codepoint": 0.11033681765389082, "hw_hindi_dev_cer_grapheme": 0.17699381878486356, "hw_hindi_dev_exact_line_acc": 0.588, "hw_nep_dev_cer_codepoint": 0.16010498687664043, "hw_nep_dev_cer_grapheme": 0.268348623853211, "hw_nep_dev_exact_line_acc": 0.5066666666666667}
89
+ step 3000: not eligible as best, guard sets regressed: {'synth_dev': 0.00577, 'en_dev': 0.00922}
90
+ epoch 1 step 3050/7533 loss 0.1887 lr 3.42e-05 46 img/s
91
+ epoch 1 step 3100/7533 loss 0.0935 lr 3.37e-05 46 img/s
92
+ epoch 1 step 3150/7533 loss 0.3523 lr 3.32e-05 46 img/s
93
+ epoch 1 step 3200/7533 loss 0.0220 lr 3.27e-05 46 img/s
94
+ epoch 1 step 3250/7533 loss 0.1017 lr 3.21e-05 46 img/s
95
+ epoch 1 step 3300/7533 loss 0.1258 lr 3.16e-05 46 img/s
96
+ epoch 1 step 3350/7533 loss 0.1463 lr 3.11e-05 46 img/s
97
+ epoch 1 step 3400/7533 loss 0.1624 lr 3.06e-05 46 img/s
98
+ epoch 1 step 3450/7533 loss 0.1128 lr 3.00e-05 46 img/s
99
+ epoch 1 step 3500/7533 loss 0.1161 lr 2.95e-05 46 img/s
100
+ epoch 1 step 3550/7533 loss 0.0616 lr 2.90e-05 46 img/s
101
+ epoch 1 step 3600/7533 loss 0.1002 lr 2.84e-05 46 img/s
102
+ epoch 1 step 3650/7533 loss 0.1801 lr 2.79e-05 46 img/s
103
+ epoch 1 step 3700/7533 loss 0.1133 lr 2.73e-05 46 img/s
104
+ epoch 1 step 3750/7533 loss 0.1695 lr 2.68e-05 46 img/s
105
+ epoch 1 step 3800/7533 loss 0.0795 lr 2.63e-05 46 img/s
106
+ epoch 1 step 3850/7533 loss 0.0978 lr 2.57e-05 46 img/s
107
+ epoch 1 step 3900/7533 loss 0.1472 lr 2.52e-05 46 img/s
108
+ epoch 1 step 3950/7533 loss 0.1886 lr 2.46e-05 46 img/s
109
+ epoch 1 step 4000/7533 loss 0.0458 lr 2.41e-05 46 img/s
110
+ epoch 1 step 4050/7533 loss 0.2461 lr 2.36e-05 46 img/s
111
+ epoch 1 step 4100/7533 loss 0.1376 lr 2.30e-05 46 img/s
112
+ epoch 1 step 4150/7533 loss 0.2569 lr 2.25e-05 46 img/s
113
+ epoch 1 step 4200/7533 loss 0.0535 lr 2.19e-05 46 img/s
114
+ epoch 1 step 4250/7533 loss 0.1314 lr 2.14e-05 46 img/s
115
+ epoch 1 step 4300/7533 loss 0.1623 lr 2.09e-05 46 img/s
116
+ epoch 1 step 4350/7533 loss 0.0353 lr 2.03e-05 46 img/s
117
+ epoch 1 step 4400/7533 loss 0.0905 lr 1.98e-05 46 img/s
118
+ epoch 1 step 4450/7533 loss 0.1442 lr 1.93e-05 46 img/s
119
+ epoch 1 step 4500/7533 loss 0.0349 lr 1.87e-05 46 img/s
120
+ {"step": 4500, "dev_mean_cp_cer": 0.08053855864456454, "target_mean_cp_cer": 0.12392674565505095, "eligible": false, "guard_regressions": {"real_dev": 0.00502, "synth_dev": 0.00557, "en_dev": 0.00672}, "real_dev_cer_codepoint": 0.09408243149896385, "real_dev_cer_grapheme": 0.10086714966778032, "real_dev_exact_line_acc": 0.7975, "synth_dev_cer_codepoint": 0.012736318407960199, "synth_dev_cer_grapheme": 0.018269607203445126, "synth_dev_exact_line_acc": 0.885, "en_dev_cer_codepoint": 0.04802055200579672, "en_dev_cer_grapheme": 0.04802055200579672, "en_dev_exact_line_acc": 0.6883333333333334, "hw_hindi_dev_cer_codepoint": 0.10021569603451136, "hw_hindi_dev_cer_grapheme": 0.16116387758178802, "hw_hindi_dev_exact_line_acc": 0.622, "hw_nep_dev_cer_codepoint": 0.14763779527559054, "hw_nep_dev_cer_grapheme": 0.24311926605504589, "hw_nep_dev_exact_line_acc": 0.5533333333333333}
121
+ step 4500: not eligible as best, guard sets regressed: {'real_dev': 0.00502, 'synth_dev': 0.00557, 'en_dev': 0.00672}
122
+ epoch 1 step 4550/7533 loss 0.0738 lr 1.82e-05 46 img/s
123
+ epoch 1 step 4600/7533 loss 0.1781 lr 1.77e-05 46 img/s
124
+ epoch 1 step 4650/7533 loss 0.1016 lr 1.72e-05 46 img/s
125
+ epoch 1 step 4700/7533 loss 0.1298 lr 1.67e-05 46 img/s
126
+ epoch 1 step 4750/7533 loss 0.0706 lr 1.61e-05 46 img/s
127
+ epoch 1 step 4800/7533 loss 0.0295 lr 1.56e-05 46 img/s
128
+ epoch 1 step 4850/7533 loss 0.0863 lr 1.51e-05 46 img/s
129
+ epoch 1 step 4900/7533 loss 0.1081 lr 1.46e-05 46 img/s
130
+ epoch 1 step 4950/7533 loss 0.1039 lr 1.42e-05 46 img/s
131
+ epoch 1 step 5000/7533 loss 0.1666 lr 1.37e-05 46 img/s
132
+
133
+ epoch 2 step 5050/7533 loss 0.1926 lr 1.32e-05 46 img/s
134
+ epoch 2 step 5100/7533 loss 0.0928 lr 1.27e-05 46 img/s
135
+ epoch 2 step 5150/7533 loss 0.0940 lr 1.22e-05 46 img/s
136
+ epoch 2 step 5200/7533 loss 0.1490 lr 1.18e-05 46 img/s
137
+ epoch 2 step 5250/7533 loss 0.1423 lr 1.13e-05 46 img/s
138
+ epoch 2 step 5300/7533 loss 0.1777 lr 1.09e-05 46 img/s
139
+ epoch 2 step 5350/7533 loss 0.0948 lr 1.04e-05 46 img/s
140
+ epoch 2 step 5400/7533 loss 0.0859 lr 9.98e-06 46 img/s
141
+ epoch 2 step 5450/7533 loss 0.1263 lr 9.55e-06 46 img/s
142
+ epoch 2 step 5500/7533 loss 0.0829 lr 9.13e-06 46 img/s
143
+ epoch 2 step 5550/7533 loss 0.0303 lr 8.71e-06 46 img/s
144
+ epoch 2 step 5600/7533 loss 0.1681 lr 8.31e-06 46 img/s
145
+ epoch 2 step 5650/7533 loss 0.1837 lr 7.91e-06 46 img/s
146
+ epoch 2 step 5700/7533 loss 0.0849 lr 7.51e-06 46 img/s
147
+ epoch 2 step 5750/7533 loss 0.0916 lr 7.13e-06 46 img/s
148
+ epoch 2 step 5800/7533 loss 0.0786 lr 6.75e-06 46 img/s
149
+ epoch 2 step 5850/7533 loss 0.0904 lr 6.39e-06 46 img/s
150
+ epoch 2 step 5900/7533 loss 0.0553 lr 6.03e-06 46 img/s
151
+ epoch 2 step 5950/7533 loss 0.0400 lr 5.68e-06 46 img/s
152
+ epoch 2 step 6000/7533 loss 0.1533 lr 5.34e-06 46 img/s
153
+ {"step": 6000, "dev_mean_cp_cer": 0.07768151544600826, "target_mean_cp_cer": 0.1194127737517131, "eligible": false, "guard_regressions": {"synth_dev": 0.00338, "en_dev": 0.00731}, "real_dev_cer_codepoint": 0.09042136771816717, "real_dev_cer_grapheme": 0.10199331806749502, "real_dev_exact_line_acc": 0.7965, "synth_dev_cer_codepoint": 0.01054726368159204, "synth_dev_cer_grapheme": 0.014876680151376746, "synth_dev_exact_line_acc": 0.9075, "en_dev_cer_codepoint": 0.04861339832685594, "en_dev_cer_grapheme": 0.04861339832685594, "en_dev_exact_line_acc": 0.6833333333333333, "hw_hindi_dev_cer_codepoint": 0.09971793595486975, "hw_hindi_dev_cer_grapheme": 0.16010854816824965, "hw_hindi_dev_exact_line_acc": 0.6215, "hw_nep_dev_cer_codepoint": 0.13910761154855644, "hw_nep_dev_cer_grapheme": 0.22362385321100917, "hw_nep_dev_exact_line_acc": 0.5866666666666667}
154
+ step 6000: not eligible as best, guard sets regressed: {'synth_dev': 0.00338, 'en_dev': 0.00731}
155
+ epoch 2 step 6050/7533 loss 0.0614 lr 5.01e-06 46 img/s
156
+ epoch 2 step 6100/7533 loss 0.0431 lr 4.69e-06 46 img/s
157
+ epoch 2 step 6150/7533 loss 0.1456 lr 4.38e-06 46 img/s
158
+ epoch 2 step 6200/7533 loss 0.1677 lr 4.07e-06 46 img/s
159
+ epoch 2 step 6250/7533 loss 0.1213 lr 3.78e-06 46 img/s
160
+ epoch 2 step 6300/7533 loss 0.2170 lr 3.50e-06 46 img/s
161
+ epoch 2 step 6350/7533 loss 0.1796 lr 3.23e-06 46 img/s
162
+ epoch 2 step 6400/7533 loss 0.1099 lr 2.97e-06 46 img/s
163
+ epoch 2 step 6450/7533 loss 0.0548 lr 2.72e-06 46 img/s
164
+ epoch 2 step 6500/7533 loss 0.0591 lr 2.47e-06 46 img/s
165
+ epoch 2 step 6550/7533 loss 0.0452 lr 2.24e-06 46 img/s
166
+ epoch 2 step 6600/7533 loss 0.0724 lr 2.02e-06 46 img/s
167
+ epoch 2 step 6650/7533 loss 0.0689 lr 1.82e-06 46 img/s
168
+ epoch 2 step 6700/7533 loss 0.0991 lr 1.62e-06 46 img/s
169
+ epoch 2 step 6750/7533 loss 0.1863 lr 1.43e-06 46 img/s
170
+ epoch 2 step 6800/7533 loss 0.0972 lr 1.26e-06 46 img/s
171
+ epoch 2 step 6850/7533 loss 0.1552 lr 1.09e-06 46 img/s
172
+ epoch 2 step 6900/7533 loss 0.1172 lr 9.39e-07 46 img/s
173
+ epoch 2 step 6950/7533 loss 0.0592 lr 7.97e-07 46 img/s
174
+ epoch 2 step 7000/7533 loss 0.0559 lr 6.67e-07 46 img/s
175
+ epoch 2 step 7050/7533 loss 0.1107 lr 5.48e-07 46 img/s
176
+ epoch 2 step 7100/7533 loss 0.1407 lr 4.41e-07 46 img/s
177
+ epoch 2 step 7150/7533 loss 0.0731 lr 3.45e-07 46 img/s
178
+ epoch 2 step 7200/7533 loss 0.0602 lr 2.61e-07 46 img/s
179
+ epoch 2 step 7250/7533 loss 0.1410 lr 1.89e-07 46 img/s
180
+ epoch 2 step 7300/7533 loss 0.0628 lr 1.28e-07 46 img/s
181
+ epoch 2 step 7350/7533 loss 0.2054 lr 7.89e-08 46 img/s
182
+ epoch 2 step 7400/7533 loss 0.0823 lr 4.17e-08 46 img/s
183
+ epoch 2 step 7450/7533 loss 0.0271 lr 1.62e-08 46 img/s
184
+ epoch 2 step 7500/7533 loss 0.0816 lr 2.57e-09 46 img/s
185
+ {"step": 7500, "dev_mean_cp_cer": 0.07654357377734125, "target_mean_cp_cer": 0.11799494139887566, "eligible": false, "guard_regressions": {"synth_dev": 0.00209, "en_dev": 0.00626}, "real_dev_cer_codepoint": 0.08991480543403177, "real_dev_cer_grapheme": 0.10000375389466572, "real_dev_exact_line_acc": 0.8, "synth_dev_cer_codepoint": 0.009253731343283582, "synth_dev_cer_grapheme": 0.012658227848101266, "synth_dev_exact_line_acc": 0.9175, "en_dev_cer_codepoint": 0.04755944931163955, "en_dev_cer_grapheme": 0.04755944931163955, "en_dev_exact_line_acc": 0.69, "hw_hindi_dev_cer_codepoint": 0.09556993529118965, "hw_hindi_dev_cer_grapheme": 0.1519674355495251, "hw_hindi_dev_exact_line_acc": 0.6365, "hw_nep_dev_cer_codepoint": 0.14041994750656167, "hw_nep_dev_cer_grapheme": 0.22821100917431192, "hw_nep_dev_exact_line_acc": 0.58}
186
+ step 7500: not eligible as best, guard sets regressed: {'synth_dev': 0.00209, 'en_dev': 0.00626}
187
+
188
+ {"step": 7533, "dev_mean_cp_cer": 0.07654357377734125, "target_mean_cp_cer": 0.11799494139887566, "eligible": false, "guard_regressions": {"synth_dev": 0.00209, "en_dev": 0.00626}, "real_dev_cer_codepoint": 0.08991480543403177, "real_dev_cer_grapheme": 0.10000375389466572, "real_dev_exact_line_acc": 0.8, "synth_dev_cer_codepoint": 0.009253731343283582, "synth_dev_cer_grapheme": 0.012658227848101266, "synth_dev_exact_line_acc": 0.9175, "en_dev_cer_codepoint": 0.04755944931163955, "en_dev_cer_grapheme": 0.04755944931163955, "en_dev_exact_line_acc": 0.69, "hw_hindi_dev_cer_codepoint": 0.09556993529118965, "hw_hindi_dev_cer_grapheme": 0.1519674355495251, "hw_hindi_dev_exact_line_acc": 0.6365, "hw_nep_dev_cer_codepoint": 0.14041994750656167, "hw_nep_dev_cer_grapheme": 0.22821100917431192, "hw_nep_dev_exact_line_acc": 0.58}
189
+ step 7533: not eligible as best, guard sets regressed: {'synth_dev': 0.00209, 'en_dev': 0.00626}
190
+ done: best target mean cp CER 0.6624 (targets ['hw_hindi_dev', 'hw_nep_dev'], guards ['real_dev', 'synth_dev', 'en_dev'])
ocr-deva-eval-20260924/newar-test-images.tgz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:829c70b7109944a84ef276a7f3ea2768a61151cfb230a281ee567b19bea72387
3
+ size 7136717
ocr-deva-eval-20260924/predict_both.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """Run the deployed deva-v2 recognizer and the official devanagari_PP-OCRv5_mobile_rec
3
+ over labelled line sets, with the same preprocessing as the k2 OCR worker.
4
+
5
+ Writes <out>/<model>/<set>.tsv rows: image_path, reference, prediction, score.
6
+ Scoring is done separately with tools/eval_metrics.py.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import json
12
+ import sys
13
+ import time
14
+ from pathlib import Path
15
+
16
+ import numpy as np
17
+ from paddleocr._models.text_recognition import TextRecognition
18
+ from PIL import Image, ImageOps
19
+
20
+ HOME = Path.home()
21
+ MODELS = {
22
+ "deva-v2": ("PP-OCRv5_mobile_rec", HOME / "ampixa-demo/models/ocr/space/models/deva-v2/artifacts"),
23
+ "official": ("devanagari_PP-OCRv5_mobile_rec", HOME / ".paddlex/official_models/devanagari_PP-OCRv5_mobile_rec"),
24
+ }
25
+ BATCH_SIZE = 16
26
+
27
+
28
+ def load_set(root: Path, labels: Path) -> list[tuple[str, str]]:
29
+ rows: list[tuple[str, str]] = []
30
+ for line_no, line in enumerate(labels.read_text(encoding="utf-8").splitlines(), start=1):
31
+ if not line.strip():
32
+ continue
33
+ parts = line.split("\t", 1)
34
+ if len(parts) != 2:
35
+ raise ValueError(f"{labels}:{line_no}: expected '<path>\\t<text>'")
36
+ image_path = root / parts[0]
37
+ if not image_path.is_file():
38
+ raise FileNotFoundError(f"{labels}:{line_no}: missing image {image_path}")
39
+ rows.append((parts[0], parts[1]))
40
+ return rows
41
+
42
+
43
+ def to_bgr(path: Path) -> np.ndarray:
44
+ # Mirrors the worker: exif_transpose -> RGB -> BGR contiguous ndarray.
45
+ with Image.open(path) as probe:
46
+ image = ImageOps.exif_transpose(probe).convert("RGB")
47
+ return np.asarray(image)[:, :, ::-1].copy()
48
+
49
+
50
+ def main() -> int:
51
+ out = Path(sys.argv[1])
52
+ sets = {
53
+ name: (Path(root), Path(labels))
54
+ for name, root, labels in (arg.split(":", 2) for arg in sys.argv[2:])
55
+ }
56
+ timings: dict[str, dict[str, float]] = {}
57
+ for model, (model_name, model_dir) in MODELS.items():
58
+ for required in ("inference.json", "inference.pdiparams", "inference.yml"):
59
+ if not (model_dir / required).is_file():
60
+ raise FileNotFoundError(f"{model}: missing {model_dir / required}")
61
+ predictor = TextRecognition(model_name=model_name, model_dir=str(model_dir), device="cpu")
62
+ timings[model] = {}
63
+ for set_name, (root, labels) in sets.items():
64
+ rows = load_set(root, labels)
65
+ crops = [to_bgr(root / rel) for rel, _ in rows]
66
+ started = time.perf_counter()
67
+ results = list(predictor.predict(crops, batch_size=BATCH_SIZE))
68
+ elapsed = time.perf_counter() - started
69
+ if len(results) != len(rows):
70
+ raise RuntimeError(f"{model}/{set_name}: {len(results)} results for {len(rows)} crops")
71
+ (out / model).mkdir(parents=True, exist_ok=True)
72
+ with (out / model / f"{set_name}.tsv").open("w", encoding="utf-8") as handle:
73
+ for (rel, reference), result in zip(rows, results, strict=True):
74
+ payload = result.json.get("res", result.json) if hasattr(result, "json") else result
75
+ text = str(payload.get("rec_text") or "").strip()
76
+ score = payload.get("rec_score")
77
+ clean_ref = reference.replace("\t", " ")
78
+ handle.write(f"{rel}\t{clean_ref}\t{text.replace(chr(9), ' ')}\t{score}\n")
79
+ timings[model][set_name] = round(elapsed / len(rows) * 1000, 2)
80
+ print(f"{model} {set_name}: {len(rows)} lines, {timings[model][set_name]} ms/line", flush=True)
81
+ (out / "timings_ms_per_line.json").write_text(json.dumps(timings, indent=2), encoding="utf-8")
82
+ return 0
83
+
84
+
85
+ if __name__ == "__main__":
86
+ raise SystemExit(main())
ocr-deva-eval-20260924/smoke.tgz ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4e1304b689cdad1fa8f97d83fbbdb823f0d788caec56cbb2bc5b80bb79417ac3
3
+ size 2815497
ocr-deva-eval-20260924/smoke/english/images/english_0000.png ADDED
ocr-deva-eval-20260924/smoke/english/images/english_0001.png ADDED
ocr-deva-eval-20260924/smoke/english/images/english_0002.png ADDED
ocr-deva-eval-20260924/smoke/english/images/english_0003.png ADDED