File size: 3,582 Bytes
d82db76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
92
93
94
#!/usr/bin/env python3
"""Run GLM-OCR on one image or page-bundle directory."""

from __future__ import annotations

import argparse
import json
from pathlib import Path

from ocrtech.external_outputs import bundle_page_paths, materialize_external_markdown


def build_parser() -> argparse.ArgumentParser:
    parser = argparse.ArgumentParser(description="Run GLM-OCR and write ocrtech-compatible outputs.")
    parser.add_argument("--input", required=True, help="image path or directory of page images")
    parser.add_argument("--out", required=True, help="output directory")
    parser.add_argument("--model-id", default="zai-org/GLM-OCR")
    parser.add_argument("--prompt", default="Text Recognition:")
    parser.add_argument("--max-new-tokens", type=int, default=8192)
    return parser


def main() -> int:
    args = build_parser().parse_args()
    input_path = Path(args.input)
    output_dir = Path(args.out)
    output_dir.mkdir(parents=True, exist_ok=True)
    try:
        from transformers import AutoModelForImageTextToText, AutoProcessor
    except ImportError as exc:
        raise RuntimeError("GLM-OCR wrapper requires transformers with multimodal support installed.") from exc

    try:
        processor = AutoProcessor.from_pretrained(args.model_id, trust_remote_code=True, use_fast=False)
        model = AutoModelForImageTextToText.from_pretrained(
            pretrained_model_name_or_path=args.model_id,
            torch_dtype="auto",
            device_map="auto",
            trust_remote_code=True,
        )
    except Exception as exc:
        raise RuntimeError(
            "GLM-OCR processor/model initialization failed. The upstream model card currently requires a git "
            "transformers build, for example `pip install git+https://github.com/huggingface/transformers.git`, "
            f"plus any remote-code dependencies such as torchvision. Root cause: {type(exc).__name__}: {exc}"
        ) from exc

    page_payloads: list[dict[str, str]] = []
    markdown_pages: list[str] = []
    for page_path in bundle_page_paths(input_path):
        messages = [
            {
                "role": "user",
                "content": [
                    {"type": "image", "url": str(page_path)},
                    {"type": "text", "text": args.prompt},
                ],
            }
        ]
        inputs = processor.apply_chat_template(
            messages,
            tokenize=True,
            add_generation_prompt=True,
            return_dict=True,
            return_tensors="pt",
        ).to(model.device)
        inputs.pop("token_type_ids", None)
        generated_ids = model.generate(**inputs, max_new_tokens=args.max_new_tokens)
        generated = generated_ids[0][inputs["input_ids"].shape[1] :]
        markdown = _clean_generation(processor.decode(generated, skip_special_tokens=False))
        page_payloads.append({"page_path": str(page_path), "markdown": markdown})
        markdown_pages.append(markdown)

    joined_markdown = "\n\n<!-- pagebreak -->\n\n".join(markdown_pages)
    materialize_external_markdown(
        input_path,
        output_dir,
        joined_markdown,
        metadata={"engine": "glm-ocr", "model_id": args.model_id, "prompt": args.prompt},
        raw_payload={"pages": page_payloads},
    )
    return 0


def _clean_generation(text: str) -> str:
    value = text.strip()
    for token in ("<|assistant|>", "<|user|>", "<|endoftext|>", "<|im_end|>"):
        value = value.replace(token, "")
    return value.strip()


if __name__ == "__main__":
    raise SystemExit(main())