Tasks and output
Use the exact prompts in ocr_prompts.json. They request transcription without translation or spelling correction and preserve diacritics and logical Unicode order.
Table with columns: Prompt key, Input and output| Prompt key | Input and output |
|---|
T1_physical | Cropped text region → text, language, script |
T2_physical | Page → text, language, script, preserving physical line breaks |
T2_reflow | Page → text, language, script, with reflowed text |
T3_line, T3_block | Page → ordered regions with bbox_2d and text, plus language and script |
T4_line, T4_block | Page → ordered regions containing only bbox_2d |
bbox_2d is an integer [x1, y1, x2, y2] box on a 0–1000 grid relative to the supplied image, with origin at its top-left. These are normalized corner coordinates, not pixel [x, y, width, height] boxes.
The local BF16 environment uses Transformers 5.20.2 and PyTorch 2.14.1. This example requires a CUDA GPU with enough memory for the BF16 model and generation state.
import jsonimport torchfrom PIL import Image, ImageOpsfrom huggingface_hub import hf_hub_downloadfrom transformers import AutoProcessor, Qwen3_5ForConditionalGeneration model_id = "shbaydadaev/Qwen3.8-27B-ocr-chg_fa-bf16"with open(hf_hub_download(model_id, "ocr_prompts.json"), encoding="utf-8") as f: prompts = json.load(f) processor = AutoProcessor.from_pretrained(model_id, trust_remote_code=False)processor.image_processor.size = { **processor.image_processor.size, "longest_edge": 4_194_304,}model = Qwen3_5ForConditionalGeneration.from_pretrained( model_id, dtype=torch.bfloat16, device_map={"": 0}, attn_implementation="sdpa", trust_remote_code=False,).eval() with Image.open("page.png") as source: rgba = ImageOps.exif_transpose(source).convert("RGBA") image = Image.alpha_composite( Image.new("RGBA", rgba.size, "white"), rgba, ).convert("RGB")messages = [{"role": "user", "content": [ {"type": "image", "image": image}, {"type": "text", "text": prompts["T2_physical"]},]}]inputs = processor.apply_chat_template( messages, tokenize=True, add_generation_prompt=True, enable_thinking=False, return_dict=True, return_tensors="pt", processor_kwargs={"truncation": False},).to(model.device) with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16): output = model.generate( **inputs, max_new_tokens=8192, do_sample=False, use_cache=True, )answer = output[0, inputs["input_ids"].shape[1]:]eos = model.generation_config.eos_token_ideos = [eos] if isinstance(eos, int) else eosif not len(answer) or int(answer[-1]) not in eos: raise RuntimeError("Generation did not finish; retain the raw output as an incomplete result.")raw = processor.decode(answer, skip_special_tokens=True, clean_up_tokenization_spaces=False)print(json.dumps(json.loads(raw), ensure_ascii=False, indent=2))
Use T1_physical for crops and select the page task deliberately. Validate the returned JSON against the chosen task schema. A complete JSON object can still contain transcription or localization errors.
Evaluation status and limitations
Standalone merged BF16 evaluation completed on 2026-11-10 (KST). The principal results below measure this export directly, without loading a separate LoRA adapter. FP8 quality evaluation has not been performed; these results do not establish FP8 quality or speed parity.
The diagnostic used the first 500 records of the prepared Test manifest: 500 of 3,743 examples (13.36%). This is a fixed prefix, not a balanced or randomly sampled benchmark. Raw corpus CER and WER compare generated text with the prepared source labels; the labels have not undergone an independent transcription audit.
Table with columns: Merged BF16 evaluation group, Examples, Raw CER, Raw WER| Merged BF16 evaluation group | Examples | Raw CER | Raw WER |
|---|
| All selected examples | 500 | 4.90% | 15.09% |
Chagatai (chg) | 105 | 37.32% | 80.18% |
Persian (fa) | 381 | 2.61% | 10.85% |
The remaining 14 examples are 13 mixed-language and one Arabic example. The aggregate is heavily weighted toward Persian/Wikisource text and does not describe Chagatai accuracy. JSON validity was 99.8% (499/500); one generation reached the 8,192-token limit and remained a failed prediction. Failed or incomplete predictions are scored as empty transcriptions. See eval_summary.json for aggregate, source, language, and task breakdowns.
For comparison, the unmerged native BF16 base plus its selected LoRA adapter was evaluated on the same 500 examples:
Table with columns: Group, Adapter CER / WER, Merged BF16 CER / WER| Group | Adapter CER / WER | Merged BF16 CER / WER |
|---|
| All selected examples | 4.87% / 15.01% | 4.90% / 15.09% |
Chagatai (chg) | 37.55% / 80.03% | 37.32% / 80.18% |
Persian (fa) | 2.57% / 10.78% | 2.61% / 10.85% |
Overall raw CER increased by 0.026 percentage points after merging on this subset. Chagatai CER decreased by 0.226 percentage points. These observations do not establish statistical equivalence or results on the complete test set.
The evaluated generation settings were greedy decoding, enable_thinking=False, up to 8,192 generated tokens, up to 8,192 input tokens and 4,096 image tokens, and a 4,194,304-pixel image budget. The standalone BF16 inference example above retains the disabled-thinking chat template and uses BF16 autocast. Transparent PNGs are composited onto white before inference.
T3/T4 page-grounding training uses synthetic pages only. There is no completed held-out real-page grounding benchmark, so localization accuracy is unmeasured. Training used the original supplied synthetic images rather than regenerated corrected renderings. Review uncertain text and boxes against the source image before relying on them.
License and provenance
This export follows the upstream Apache-2.0 license; see LICENSE. The publication includes source, merge, and quantization provenance where applicable, with machine-local paths removed, plus SHA256SUMS for the published files. No training images are included.