# GLM-OCR on Tenstorrent p150: Python API ```python from tt_glmocr import config as C from tt_glmocr.api import GlmOcr with GlmOcr.from_config(C.resolve_config()) as m: # opens the p150, loads the weights, captures the traces r = m.ocr("page.png") # task "text": path, bytes, PIL image, uint8 array, data URL print(r.text, r.finish_reason, r.usage, r.image, r.timings) r = m.ocr("table.png", task="table", output_format="markdown") # HTML r = m.ocr("formula.png", task="formula") # $$ LaTeX $$ r = m.ocr("invoice.png", task="extract", schema={"invoice_number": "", "total": ""}, output_format="json") print(r.json) # {"invoice_number": "...", "total": "..."} r = m.ocr("page.png", max_image_tokens=1024, max_new_tokens=2048) # a smaller image budget: faster s = m.ocr_stream("page.png") # text deltas as tokens are decoded for delta in s: print(delta, end="", flush=True) print(s.result.usage) ``` `ocr(image, task=None, prompt=None, schema=None, *, max_new_tokens=None, temperature=None, top_k=None, top_p=None, repetition_penalty=None, seed=None, max_pixels=None, min_pixels=None, max_image_tokens=None, output_format=None, on_delta=None, cancel=None)` returns an `OcrResult(text, raw_text, tokens, finish_reason, task, output_format, usage, timings, image, json, json_error, sampling)`; `ocr_stream(...)` is the same as an iterator of text deltas (the result is `stream.result` afterwards). The parameters are those of `POST /ocr` (the model card's table): - `task`: `text` (default; prompt `Text Recognition:`), `table` (`Table Recognition:`), `formula` (`Formula Recognition:`), `extract` (needs `schema`: a dict / JSON string of the fields to fill; the prompt is the model card's preface `请按下列JSON格式输出图中信息:` + the schema with 4-space indentation). `prompt` replaces the task's prompt (the model is trained on the card's prompts). - `output_format`: `raw` (default), `markdown` (the official SDK's clean-up of one region), `json` (the output parsed; code fences tolerated; `json_error` on failure). With `task="extract"` the parsed JSON is in `r.json` whatever the format. - `max_pixels` / `max_image_tokens` / `min_pixels`: the official processor's units (2 x height x width; `max_image_tokens = max_pixels / 1568`); defaults 9,633,792 (6144 tokens, about 4.8 MP) and 12,544. - `max_new_tokens`: default `GLMOCR_DEFAULT_MAX_NEW_TOKENS` (4096), at most `GLMOCR_MAX_NEW_TOKENS`, clipped to the KV capacity left after the prompt; `finish_reason` is `stop` (the model's stop token), `length` or `cancelled` (`cancel`, a `threading.Event`, was set). - `temperature` 0 (default) = greedy, the official decoding; `top_k` <= `GLMOCR_K_MAX` (64); `repetition_penalty` 1 .. 2 (the official SDK uses 1.1); `seed` makes sampling reproducible. The two halves of a request can be called separately: `prepare(image, ...)` (validation, image decoding, resizing, patches, prompt: CPU only, thread-safe) and `run(prepared, on_delta=None, cancel=None)` (the device; one at a time). The server prepares the next request while the device serves the current one. The compile-time configuration is `config.ServeConfig` (the `GLMOCR_*` environment variables of the model card; `C.resolve_config({...})` takes a dict). `GLMOCR_BACKEND=cpu` runs the clean-room CPU reference instead of the device (float32, bit-exact with the official model; slow: development and checks), `GLMOCR_BACKEND=fake` a deterministic stand-in without weights (tests). `GlmOcr.from_config(cfg, device=dev)` uses an already opened p150 (the caller keeps it); without `device` the p150 is opened with ETH dispatch and closed by `close()`. Requirements: tt-metal at `44d66500520fda9f2c7060c0f6b41ec48f7ab37e` with the two patches in `patches/` (ETH dispatch: the 12 x 10 grid), and `numpy<2 safetensors huggingface_hub tokenizers pillow` (+ `fastapi uvicorn` for the server). The weights are fetched from [zai-org/GLM-OCR](https://huggingface.co/zai-org/GLM-OCR) at the pinned commit (`GLMOCR_MODEL_DIR` points at a local copy instead). Package layout (`tt_glmocr/`): `frontend.py` (the official preprocessing, prompt, 3D positions, tokenizer), `api.py` (this API), `backends.py` (device / CPU / fake), `postprocess.py` (output formats), `config.py` (configuration), `tt/` (the device engines: `ttnn_vision.py` vision encoder, one trace per patch bucket; `ttnn_decoder.py` prefill, one trace per row bucket, and decode; `model.py` the pipeline), `reference/` (the CPU reference), `server/` (HTTP server, stdlib client, smoke test), `samples/`, `licenses/`.