Sync from GitHub via hub-sync
Browse files- README.md +1 -1
- models.json +2 -2
- unlimited-ocr2.py +54 -7
README.md
CHANGED
|
@@ -230,7 +230,7 @@ hf jobs uv run --detach \
|
|
| 230 |
your-username/britannica-1771
|
| 231 |
```
|
| 232 |
|
| 233 |
-
The header picks `rtx-pro-6000` (the fastest per dollar in our tests; 24 GB GPUs are too small for 16-page requests). Results are one row per 16-page window. About 2% of pages in 18th-century print (mostly mathematics) send the model into a loop; `unlimited-ocr2-saturate.py`, an experimental tuned version, reads those pages again one at a time and adds PDF input and
|
| 234 |
|
| 235 |
```bash
|
| 236 |
hf jobs uv run --detach --timeout 4h \
|
|
|
|
| 230 |
your-username/britannica-1771
|
| 231 |
```
|
| 232 |
|
| 233 |
+
The header picks `rtx-pro-6000` (the fastest per dollar in our tests; 24 GB GPUs are too small for 16-page requests). Results are one row per 16-page window. About 2% of pages in 18th-century print (mostly mathematics) send the model into a loop; `unlimited-ocr2-saturate.py`, an experimental tuned version, reads those pages again one at a time and adds PDF input and Baidu's Markdown post-processing. `unlimited-ocr2-live.py`, also experimental, is the minimal script plus a live view you can open while the Job runs (add `--live-port 7860` and `--expose-public 7860`): the page being read with its text streaming in, finished pages, pages per minute and GPU load.
|
| 234 |
|
| 235 |
```bash
|
| 236 |
hf jobs uv run --detach --timeout 4h \
|
models.json
CHANGED
|
@@ -700,7 +700,7 @@
|
|
| 700 |
"task": "ocr (multi-page documents: book volumes, PDFs; cross-page continuations)",
|
| 701 |
"output": "markdown per page and per window + layout-grounded raw",
|
| 702 |
"license": "mit",
|
| 703 |
-
"notes": "Tuned version of unlimited-ocr2.py: repair pass for looping pages (fixed 28/33 in testing),
|
| 704 |
"languages": {
|
| 705 |
"evidence": "multilingual-unspecified"
|
| 706 |
},
|
|
@@ -723,7 +723,7 @@
|
|
| 723 |
"task": "ocr (multi-page documents) with a live view of the run",
|
| 724 |
"output": "markdown per page + layout-grounded raw, one row per 16-page window",
|
| 725 |
"license": "mit",
|
| 726 |
-
"notes": "unlimited-ocr2.py plus --live-port: a dashboard served from the Job (start-up steps, the page being read with its text streaming in, finished pages, pages/min, GPU load) for watching or screen-recording a run.
|
| 727 |
"languages": {
|
| 728 |
"evidence": "multilingual-unspecified"
|
| 729 |
},
|
|
|
|
| 700 |
"task": "ocr (multi-page documents: book volumes, PDFs; cross-page continuations)",
|
| 701 |
"output": "markdown per page and per window + layout-grounded raw",
|
| 702 |
"license": "mit",
|
| 703 |
+
"notes": "Tuned version of unlimited-ocr2.py: repair pass for looping pages (fixed 28/33 in testing), PDF input, Baidu's own Markdown post-processing, speed flags.",
|
| 704 |
"languages": {
|
| 705 |
"evidence": "multilingual-unspecified"
|
| 706 |
},
|
|
|
|
| 723 |
"task": "ocr (multi-page documents) with a live view of the run",
|
| 724 |
"output": "markdown per page + layout-grounded raw, one row per 16-page window",
|
| 725 |
"license": "mit",
|
| 726 |
+
"notes": "unlimited-ocr2.py plus --live-port: a dashboard served from the Job (start-up steps, the page being read with its text streaming in, finished pages, pages/min, GPU load) for watching or screen-recording a run. Measured on 1,456 Wellcome Library pages: ~130 pages/min on rtx-pro-6000.",
|
| 727 |
"languages": {
|
| 728 |
"evidence": "multilingual-unspecified"
|
| 729 |
},
|
unlimited-ocr2.py
CHANGED
|
@@ -40,8 +40,8 @@ ran out of budget or lost a page boundary (then `page_markdown` is empty and onl
|
|
| 40 |
`raw` is kept). Re-running the same command resumes where it stopped.
|
| 41 |
|
| 42 |
About 2% of dense 18th-century pages (mostly mathematics) send the model into a
|
| 43 |
-
loop. `unlimited-ocr2-saturate.py` is the
|
| 44 |
-
re-reads those pages one at a time,
|
| 45 |
Baidu's own Markdown post-processing. It may change more often.
|
| 46 |
|
| 47 |
How it runs: Unlimited-OCR-2 needs Baidu's own SGLang build (no vLLM support).
|
|
@@ -73,7 +73,8 @@ SERVING = {
|
|
| 73 |
"SGLANG_UOCR_EANABLE_DOC_LIMIT": "1", # (sic) upstream spelling
|
| 74 |
"SGLANG_UOCR_DOC_GUNDAM_PAGE_WINDOW": "10",
|
| 75 |
"SGLANG_UOCR_DOC_BASE_PAGE_WINDOW": "10",
|
| 76 |
-
|
|
|
|
| 77 |
"SGLANG_OPT_UOCR_SAM_REL_POS_BIAS_MB": "64",
|
| 78 |
"SGLANG_UOCR_SHM_WRAP_MODEL_DATA": "1",
|
| 79 |
"SGLANG_UOCR_SHM_WRAP_MIN_MB": "1",
|
|
@@ -158,7 +159,26 @@ def load_page(fs, path: str, max_side: int) -> str:
|
|
| 158 |
from PIL import Image
|
| 159 |
|
| 160 |
with fs.open(path, "rb") as f:
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
img.load()
|
| 163 |
img = img.convert("RGB")
|
| 164 |
img.thumbnail((max_side, max_side), Image.LANCZOS)
|
|
@@ -193,6 +213,24 @@ def clean_page(raw_page: str, keep_furniture: bool) -> str:
|
|
| 193 |
return "\n\n".join(blocks)
|
| 194 |
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
def start_server_cmd() -> list[str]:
|
| 197 |
"""Install Baidu's wheel into the image's sglang Python and return the server command."""
|
| 198 |
# The SGLang image keeps sglang in its own venv (/opt/sglang/bin), first on PATH;
|
|
@@ -227,8 +265,17 @@ def main():
|
|
| 227 |
"1024 px view, so more pixels only make the requests bigger")
|
| 228 |
ap.add_argument("--keep-furniture", action="store_true",
|
| 229 |
help="Keep page numbers and running heads in page_markdown")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
ap.add_argument("--retry-errors", action="store_true", help="Retry windows recorded as errors")
|
| 231 |
args = ap.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
import fsspec
|
| 234 |
import saturate.transport
|
|
@@ -249,14 +296,14 @@ def main():
|
|
| 249 |
return {
|
| 250 |
"model": SERVING["served_name"],
|
| 251 |
"messages": [{"role": "user", "content": content}],
|
| 252 |
-
"max_tokens": 8000 * n, # output budget for the whole window
|
| 253 |
"temperature": 0,
|
| 254 |
"skip_special_tokens": False, # <PAGE>/<BLOCK> markers are the structure
|
| 255 |
"images_config": {"crop_mode": False}, # base mode: one 1024 px view per page
|
| 256 |
"custom_logit_processor": NGRAM_PROCESSOR,
|
| 257 |
"custom_params": {
|
| 258 |
"ngram_size": 35, "window_size": 1024 if n > 1 else 128,
|
| 259 |
-
"page_token_id": 128827, "eos_token_id": 1, "max_page_tokens":
|
| 260 |
},
|
| 261 |
}
|
| 262 |
|
|
@@ -294,7 +341,7 @@ def main():
|
|
| 294 |
stats = pump(rows, to_request, parse, endpoint, output,
|
| 295 |
window=Auto(initial=32, max_limit=64), # long requests: start wide
|
| 296 |
flush_every=4, # each row is minutes of GPU time; write often
|
| 297 |
-
read_timeout=3600, prepare_workers=
|
| 298 |
print(f"{output}: {stats.rows_processed} windows written, {stats.rows_failed} errors", file=sys.stderr)
|
| 299 |
print("UNLIMITED_OCR2 " + stats.to_json(), flush=True)
|
| 300 |
|
|
|
|
| 40 |
`raw` is kept). Re-running the same command resumes where it stopped.
|
| 41 |
|
| 42 |
About 2% of dense 18th-century pages (mostly mathematics) send the model into a
|
| 43 |
+
loop; a page stops at `--max-page-tokens` (8000). `unlimited-ocr2-saturate.py` is the
|
| 44 |
+
tuned version of this script: it re-reads those pages one at a time, reads PDFs and adds
|
| 45 |
Baidu's own Markdown post-processing. It may change more often.
|
| 46 |
|
| 47 |
How it runs: Unlimited-OCR-2 needs Baidu's own SGLang build (no vLLM support).
|
|
|
|
| 73 |
"SGLANG_UOCR_EANABLE_DOC_LIMIT": "1", # (sic) upstream spelling
|
| 74 |
"SGLANG_UOCR_DOC_GUNDAM_PAGE_WINDOW": "10",
|
| 75 |
"SGLANG_UOCR_DOC_BASE_PAGE_WINDOW": "10",
|
| 76 |
+
# Per-page output cap; --max-page-tokens sets it (default 8000).
|
| 77 |
+
"SGLANG_UOCR_MAX_PAGE_TOKENS": "8000",
|
| 78 |
"SGLANG_OPT_UOCR_SAM_REL_POS_BIAS_MB": "64",
|
| 79 |
"SGLANG_UOCR_SHM_WRAP_MODEL_DATA": "1",
|
| 80 |
"SGLANG_UOCR_SHM_WRAP_MIN_MB": "1",
|
|
|
|
| 159 |
from PIL import Image
|
| 160 |
|
| 161 |
with fs.open(path, "rb") as f:
|
| 162 |
+
data = f.read()
|
| 163 |
+
img = Image.open(io.BytesIO(data))
|
| 164 |
+
if img.format == "JPEG2000":
|
| 165 |
+
# Decode straight at a lower wavelet level while the page stays >= max_side: close
|
| 166 |
+
# to the full decode after downscaling, for a fraction of the CPU. Some codestreams fail
|
| 167 |
+
# at a reduced level, so step back towards a full decode until one works.
|
| 168 |
+
reduce = 0
|
| 169 |
+
while max(img.size) >> (reduce + 1) >= max_side:
|
| 170 |
+
reduce += 1
|
| 171 |
+
while True:
|
| 172 |
+
try:
|
| 173 |
+
img = Image.open(io.BytesIO(data))
|
| 174 |
+
img.reduce = reduce
|
| 175 |
+
img.load()
|
| 176 |
+
break
|
| 177 |
+
except OSError:
|
| 178 |
+
if reduce == 0:
|
| 179 |
+
raise
|
| 180 |
+
reduce -= 1
|
| 181 |
+
else:
|
| 182 |
img.load()
|
| 183 |
img = img.convert("RGB")
|
| 184 |
img.thumbnail((max_side, max_side), Image.LANCZOS)
|
|
|
|
| 213 |
return "\n\n".join(blocks)
|
| 214 |
|
| 215 |
|
| 216 |
+
def prepare_workers() -> int:
|
| 217 |
+
"""Threads that decode and encode pages: enough to keep ~40 requests fed, within the Job's CPU quota.
|
| 218 |
+
|
| 219 |
+
os.cpu_count() reports the host's cores (190 on a Jobs node), not the quota; that many
|
| 220 |
+
threads starve the loop that sends requests. Jobs sets CPU_CORES to the quota.
|
| 221 |
+
"""
|
| 222 |
+
try:
|
| 223 |
+
cores = int(os.environ.get("CPU_CORES", ""))
|
| 224 |
+
except ValueError:
|
| 225 |
+
cores = 0
|
| 226 |
+
if cores <= 0:
|
| 227 |
+
try:
|
| 228 |
+
cores = len(os.sched_getaffinity(0))
|
| 229 |
+
except AttributeError: # not Linux
|
| 230 |
+
cores = os.cpu_count() or 1
|
| 231 |
+
return max(4, min(16, cores - 6)) # leave cores for SGLang's tokenizer and scheduler
|
| 232 |
+
|
| 233 |
+
|
| 234 |
def start_server_cmd() -> list[str]:
|
| 235 |
"""Install Baidu's wheel into the image's sglang Python and return the server command."""
|
| 236 |
# The SGLang image keeps sglang in its own venv (/opt/sglang/bin), first on PATH;
|
|
|
|
| 265 |
"1024 px view, so more pixels only make the requests bigger")
|
| 266 |
ap.add_argument("--keep-furniture", action="store_true",
|
| 267 |
help="Keep page numbers and running heads in page_markdown")
|
| 268 |
+
ap.add_argument("--max-page-tokens", type=int, default=8000,
|
| 269 |
+
help="Output cap per page (default 8000). A book page is ~2,000 characters; "
|
| 270 |
+
"past 8000 tokens it is almost always a repetition loop. Raise it (up to "
|
| 271 |
+
"20000) for very dense pages: large tables, mathematics")
|
| 272 |
ap.add_argument("--retry-errors", action="store_true", help="Retry windows recorded as errors")
|
| 273 |
args = ap.parse_args()
|
| 274 |
+
if args.max_side <= 0:
|
| 275 |
+
ap.error("--max-side must be a positive number of pixels")
|
| 276 |
+
if not 0 < args.max_page_tokens <= 20000:
|
| 277 |
+
ap.error("--max-page-tokens must be between 1 and 20000")
|
| 278 |
+
SERVING["env"]["SGLANG_UOCR_MAX_PAGE_TOKENS"] = str(args.max_page_tokens)
|
| 279 |
|
| 280 |
import fsspec
|
| 281 |
import saturate.transport
|
|
|
|
| 296 |
return {
|
| 297 |
"model": SERVING["served_name"],
|
| 298 |
"messages": [{"role": "user", "content": content}],
|
| 299 |
+
"max_tokens": max(8000, args.max_page_tokens) * n, # output budget for the whole window
|
| 300 |
"temperature": 0,
|
| 301 |
"skip_special_tokens": False, # <PAGE>/<BLOCK> markers are the structure
|
| 302 |
"images_config": {"crop_mode": False}, # base mode: one 1024 px view per page
|
| 303 |
"custom_logit_processor": NGRAM_PROCESSOR,
|
| 304 |
"custom_params": {
|
| 305 |
"ngram_size": 35, "window_size": 1024 if n > 1 else 128,
|
| 306 |
+
"page_token_id": 128827, "eos_token_id": 1, "max_page_tokens": args.max_page_tokens,
|
| 307 |
},
|
| 308 |
}
|
| 309 |
|
|
|
|
| 341 |
stats = pump(rows, to_request, parse, endpoint, output,
|
| 342 |
window=Auto(initial=32, max_limit=64), # long requests: start wide
|
| 343 |
flush_every=4, # each row is minutes of GPU time; write often
|
| 344 |
+
read_timeout=3600, prepare_workers=prepare_workers(), retry_errors=args.retry_errors)
|
| 345 |
print(f"{output}: {stats.rows_processed} windows written, {stats.rows_failed} errors", file=sys.stderr)
|
| 346 |
print("UNLIMITED_OCR2 " + stats.to_json(), flush=True)
|
| 347 |
|