glmocr 0.1.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. glmocr-0.1.1/LICENSE +201 -0
  2. glmocr-0.1.1/PKG-INFO +423 -0
  3. glmocr-0.1.1/README.md +353 -0
  4. glmocr-0.1.1/glmocr/__init__.py +61 -0
  5. glmocr-0.1.1/glmocr/__main__.py +6 -0
  6. glmocr-0.1.1/glmocr/api.py +666 -0
  7. glmocr-0.1.1/glmocr/cli.py +210 -0
  8. glmocr-0.1.1/glmocr/config.py +393 -0
  9. glmocr-0.1.1/glmocr/config.yaml +305 -0
  10. glmocr-0.1.1/glmocr/dataloader/__init__.py +5 -0
  11. glmocr-0.1.1/glmocr/dataloader/page_loader.py +404 -0
  12. glmocr-0.1.1/glmocr/layout/__init__.py +41 -0
  13. glmocr-0.1.1/glmocr/layout/base.py +51 -0
  14. glmocr-0.1.1/glmocr/layout/layout_detector.py +372 -0
  15. glmocr-0.1.1/glmocr/maas_client.py +538 -0
  16. glmocr-0.1.1/glmocr/ocr_client.py +485 -0
  17. glmocr-0.1.1/glmocr/parser_result/__init__.py +6 -0
  18. glmocr-0.1.1/glmocr/parser_result/base.py +140 -0
  19. glmocr-0.1.1/glmocr/parser_result/pipeline_result.py +115 -0
  20. glmocr-0.1.1/glmocr/pipeline/__init__.py +5 -0
  21. glmocr-0.1.1/glmocr/pipeline/pipeline.py +736 -0
  22. glmocr-0.1.1/glmocr/postprocess/__init__.py +6 -0
  23. glmocr-0.1.1/glmocr/postprocess/base_post_processor.py +43 -0
  24. glmocr-0.1.1/glmocr/postprocess/result_formatter.py +534 -0
  25. glmocr-0.1.1/glmocr/server.py +205 -0
  26. glmocr-0.1.1/glmocr/tests/__init__.py +1 -0
  27. glmocr-0.1.1/glmocr/tests/conftest.py +87 -0
  28. glmocr-0.1.1/glmocr/tests/test_integration.py +210 -0
  29. glmocr-0.1.1/glmocr/tests/test_unit.py +1600 -0
  30. glmocr-0.1.1/glmocr/utils/__init__.py +43 -0
  31. glmocr-0.1.1/glmocr/utils/image_utils.py +392 -0
  32. glmocr-0.1.1/glmocr/utils/layout_postprocess_utils.py +407 -0
  33. glmocr-0.1.1/glmocr/utils/lock_utils.py +86 -0
  34. glmocr-0.1.1/glmocr/utils/logging.py +216 -0
  35. glmocr-0.1.1/glmocr/utils/markdown_utils.py +142 -0
  36. glmocr-0.1.1/glmocr/utils/result_postprocess_utils.py +115 -0
  37. glmocr-0.1.1/glmocr/utils/visualization_utils.py +389 -0
  38. glmocr-0.1.1/glmocr.egg-info/PKG-INFO +423 -0
  39. glmocr-0.1.1/glmocr.egg-info/SOURCES.txt +44 -0
  40. glmocr-0.1.1/glmocr.egg-info/dependency_links.txt +1 -0
  41. glmocr-0.1.1/glmocr.egg-info/entry_points.txt +2 -0
  42. glmocr-0.1.1/glmocr.egg-info/requires.txt +46 -0
  43. glmocr-0.1.1/glmocr.egg-info/top_level.txt +1 -0
  44. glmocr-0.1.1/pyproject.toml +107 -0
  45. glmocr-0.1.1/setup.cfg +4 -0
  46. glmocr-0.1.1/setup.py +66 -0
glmocr-0.1.1/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2026 Zhipu AI
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
glmocr-0.1.1/PKG-INFO ADDED
@@ -0,0 +1,423 @@
1
+ Metadata-Version: 2.4
2
+ Name: glmocr
3
+ Version: 0.1.1
4
+ Summary: GLM OCR - Optical Character Recognition powered by GLM
5
+ Home-page: https://github.com/ZHIPUAI/glm-ocr
6
+ Author: ZHIPUAI
7
+ Author-email: zai-org <wenmeng.yu@aminer.cn>
8
+ License: Apache-2.0
9
+ Project-URL: Homepage, https://github.com/zai-org/glm-ocr
10
+ Project-URL: Repository, https://github.com/zai-org/glm-ocr
11
+ Project-URL: Documentation, https://github.com/zai-org/glm-ocr#readme
12
+ Project-URL: Issues, https://github.com/zai-org/glm-ocr/issues
13
+ Keywords: ocr,glm,ai,vision
14
+ Classifier: Development Status :: 3 - Alpha
15
+ Classifier: Intended Audience :: Developers
16
+ Classifier: License :: OSI Approved :: Apache Software License
17
+ Classifier: Programming Language :: Python :: 3
18
+ Classifier: Programming Language :: Python :: 3.10
19
+ Classifier: Programming Language :: Python :: 3.11
20
+ Classifier: Programming Language :: Python :: 3.12
21
+ Classifier: Programming Language :: Python :: 3.13
22
+ Requires-Python: >=3.8
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: pillow>=12.1.0
26
+ Requires-Dist: requests>=2.32.0
27
+ Requires-Dist: pydantic>=2.12.0
28
+ Requires-Dist: wordfreq>=3.1.0
29
+ Requires-Dist: PyYAML>=6.0.0
30
+ Requires-Dist: portalocker>=3.2.0
31
+ Requires-Dist: python-dotenv>=1.2.0
32
+ Requires-Dist: torch>=2.10.0
33
+ Requires-Dist: torchvision>=0.25.0
34
+ Requires-Dist: transformers>=5.3.0
35
+ Requires-Dist: sentencepiece>=0.2.0
36
+ Requires-Dist: accelerate>=1.13.0
37
+ Requires-Dist: opencv-python>=4.13.0
38
+ Requires-Dist: pypdfium2>=5.6.0
39
+ Requires-Dist: flask>=3.1.0
40
+ Provides-Extra: layout
41
+ Requires-Dist: torch>=2.10.0; extra == "layout"
42
+ Requires-Dist: torchvision>=0.25.0; extra == "layout"
43
+ Requires-Dist: transformers>=5.3.0; extra == "layout"
44
+ Requires-Dist: sentencepiece>=0.2.0; extra == "layout"
45
+ Requires-Dist: accelerate>=1.13.0; extra == "layout"
46
+ Requires-Dist: opencv-python>=4.13.0; extra == "layout"
47
+ Provides-Extra: pdf
48
+ Requires-Dist: pypdfium2>=5.6.0; extra == "pdf"
49
+ Provides-Extra: server
50
+ Requires-Dist: flask>=3.1.0; extra == "server"
51
+ Provides-Extra: all
52
+ Requires-Dist: torch>=2.10.0; extra == "all"
53
+ Requires-Dist: torchvision>=0.25.0; extra == "all"
54
+ Requires-Dist: transformers>=5.3.0; extra == "all"
55
+ Requires-Dist: sentencepiece>=0.2.0; extra == "all"
56
+ Requires-Dist: accelerate>=1.13.0; extra == "all"
57
+ Requires-Dist: opencv-python>=4.13.0; extra == "all"
58
+ Requires-Dist: pdf2image>=1.16.0; extra == "all"
59
+ Requires-Dist: flask>=3.1.0; extra == "all"
60
+ Provides-Extra: dev
61
+ Requires-Dist: pytest>=9.0.0; extra == "dev"
62
+ Requires-Dist: black>=26.3.0; extra == "dev"
63
+ Requires-Dist: flake8>=7.0.0; extra == "dev"
64
+ Requires-Dist: mypy>=1.19.0; extra == "dev"
65
+ Requires-Dist: pre-commit>=4.5.0; extra == "dev"
66
+ Dynamic: author
67
+ Dynamic: home-page
68
+ Dynamic: license-file
69
+ Dynamic: requires-python
70
+
71
+ ## GLM-OCR
72
+
73
+ <div align="center">
74
+ <img src=resources/logo.svg width="40%"/>
75
+ </div>
76
+ <p align="center">
77
+ 👋 Join our <a href="resources/WECHAT.md" target="_blank">WeChat</a> and <a href="https://discord.gg/QR7SARHRxK" target="_blank">Discord</a> community
78
+ <br>
79
+ 📍 Use GLM-OCR's <a href="https://docs.z.ai/guides/vlm/glm-ocr" target="_blank">API</a>
80
+ </p>
81
+
82
+ <div align="center">
83
+ <a href="README_zh.md">简体中文</a> | English
84
+ </div>
85
+
86
+ ### Model Introduction
87
+
88
+ GLM-OCR is a multimodal OCR model for complex document understanding, built on the GLM-V encoder–decoder architecture. It introduces Multi-Token Prediction (MTP) loss and stable full-task reinforcement learning to improve training efficiency, recognition accuracy, and generalization. The model integrates the CogViT visual encoder pre-trained on large-scale image–text data, a lightweight cross-modal connector with efficient token downsampling, and a GLM-0.5B language decoder. Combined with a two-stage pipeline of layout analysis and parallel recognition based on PP-DocLayout-V3, GLM-OCR delivers robust and high-quality OCR performance across diverse document layouts.
89
+
90
+ **Key Features**
91
+
92
+ - **State-of-the-Art Performance**: Achieves a score of 94.62 on OmniDocBench V1.5, ranking #1 overall, and delivers state-of-the-art results across major document understanding benchmarks, including formula recognition, table recognition, and information extraction.
93
+
94
+ - **Optimized for Real-World Scenarios**: Designed and optimized for practical business use cases, maintaining robust performance on complex tables, code-heavy documents, seals, and other challenging real-world layouts.
95
+
96
+ - **Efficient Inference**: With only 0.9B parameters, GLM-OCR supports deployment via vLLM, SGLang, and Ollama, significantly reducing inference latency and compute cost, making it ideal for high-concurrency services and edge deployments.
97
+
98
+ - **Easy to Use**: Fully open-sourced and equipped with a comprehensive [SDK](https://github.com/zai-org/GLM-OCR) and inference toolchain, offering simple installation, one-line invocation, and smooth integration into existing production pipelines.
99
+
100
+ ### News & Updates
101
+
102
+ - **[2026.3.12]** GLM-OCR Technical Report is now available. See: [GLM-OCR Technical Report](https://arxiv.org/abs/2603.10910)
103
+ - **[2026.2.12]** Fine-tuning tutorial based on LLaMA-Factory is now available. See: [GLM-OCR Fine-tuning Guide](examples/finetune/README.md)
104
+
105
+ ### Download Model
106
+
107
+ | Model | Download Links | Precision |
108
+ | ------- | --------------------------------------------------------------------------------------------------------------------------- | --------- |
109
+ | GLM-OCR | [🤗 Hugging Face](https://huggingface.co/zai-org/GLM-OCR)<br> [🤖 ModelScope](https://modelscope.cn/models/ZhipuAI/GLM-OCR) | BF16 |
110
+
111
+ ## GLM-OCR SDK
112
+
113
+ We provide an SDK for using GLM-OCR more efficiently and conveniently.
114
+
115
+ ### Install SDK
116
+
117
+ > [UV Installation](https://docs.astral.sh/uv/getting-started/installation/)
118
+
119
+ ```bash
120
+ # Install from source
121
+ git clone https://github.com/zai-org/glm-ocr.git
122
+ cd glm-ocr
123
+ uv venv --python 3.12 --seed && source .venv/bin/activate
124
+ uv pip install -e .
125
+ ```
126
+
127
+ ### Model Deployment
128
+
129
+ Two ways to use GLM-OCR:
130
+
131
+ #### Option 1: Zhipu MaaS API (Recommended for Quick Start)
132
+
133
+ Use the hosted cloud API – no GPU needed. The cloud service runs the complete GLM-OCR pipeline internally, so the SDK simply forwards your request and returns the result.
134
+
135
+ 1. Get an API key from https://open.bigmodel.cn
136
+ 2. Configure `config.yaml`:
137
+
138
+ ```yaml
139
+ pipeline:
140
+ maas:
141
+ enabled: true # Enable MaaS mode
142
+ api_key: your-api-key # Required
143
+ ```
144
+
145
+ That's it! When `maas.enabled=true`, the SDK acts as a thin wrapper that:
146
+
147
+ - Forwards your documents to the Zhipu cloud API
148
+ - Returns the results directly (Markdown + JSON layout details)
149
+ - No local processing, no GPU required
150
+
151
+ Input note (MaaS): the upstream API accepts `file` as a URL or a `data:<mime>;base64,...` data URI.
152
+ If you have raw base64 without the `data:` prefix, wrap it as a data URI (recommended). The SDK will
153
+ auto-wrap local file paths / bytes / raw base64 into a data URI when calling MaaS.
154
+
155
+ API documentation: https://docs.bigmodel.cn/cn/guide/models/vlm/glm-ocr
156
+
157
+ #### Option 2: Self-host with vLLM / SGLang
158
+
159
+ Deploy the GLM-OCR model locally for full control. The SDK provides the complete pipeline: layout detection, parallel region OCR, and result formatting.
160
+
161
+ ##### Using vLLM
162
+
163
+ Install vLLM:
164
+
165
+ ```bash
166
+ uv pip install -U vllm --torch-backend=auto --extra-index-url https://wheels.vllm.ai/nightly
167
+ # Or use Docker
168
+ docker pull vllm/vllm-openai:nightly
169
+ ```
170
+
171
+ Launch the service:
172
+
173
+ ```bash
174
+ # In docker container, uv may not be need for transformers install
175
+ uv pip install git+https://github.com/huggingface/transformers.git
176
+
177
+ # Run with MTP for better performance
178
+ vllm serve zai-org/GLM-OCR --allowed-local-media-path / --port 8080 --speculative-config '{"method": "mtp", "num_speculative_tokens": 1}' --served-model-name glm-ocr
179
+ ```
180
+
181
+ ##### Using SGLang
182
+
183
+ Install SGLang:
184
+
185
+ ```bash
186
+ docker pull lmsysorg/sglang:dev
187
+ # Or build from source
188
+ uv pip install git+https://github.com/sgl-project/sglang.git#subdirectory=python
189
+ ```
190
+
191
+ Launch the service:
192
+
193
+ ```bash
194
+ # In docker container, uv may not be need for transformers install
195
+ uv pip install git+https://github.com/huggingface/transformers.git
196
+
197
+ # Run with MTP for better performance
198
+ python -m sglang.launch_server --model zai-org/GLM-OCR --port 8080 --speculative-algorithm NEXTN --speculative-num-steps 3 --speculative-eagle-topk 1 --speculative-num-draft-tokens 4 --served-model-name glm-ocr
199
+ # Modify the speculative config base on your device
200
+ ```
201
+
202
+ ##### Update Configuration
203
+
204
+ After launching the service, configure `config.yaml`:
205
+
206
+ ```yaml
207
+ pipeline:
208
+ maas:
209
+ enabled: false # Disable MaaS mode (default)
210
+ ocr_api:
211
+ api_host: localhost # or your vLLM/SGLang server address
212
+ api_port: 8080
213
+ ```
214
+
215
+ #### Option 3: Ollama/MLX
216
+
217
+ For specialized deployment scenarios, see the detailed guides:
218
+
219
+ - **[Apple Silicon with mlx-vlm](examples/mlx-deploy/README.md)** - Optimized for Apple Silicon Macs
220
+ - **[Ollama Deployment](examples/ollama-deploy/README.md)** - Simple local deployment with Ollama
221
+
222
+ ### SDK Usage Guide
223
+
224
+ #### CLI
225
+
226
+ ```bash
227
+ # Parse a single image
228
+ glmocr parse examples/source/code.png
229
+
230
+ # Parse a directory
231
+ glmocr parse examples/source/
232
+
233
+ # Set output directory
234
+ glmocr parse examples/source/code.png --output ./results/
235
+
236
+ # Use a custom config
237
+ glmocr parse examples/source/code.png --config my_config.yaml
238
+
239
+ # Enable debug logging with profiling
240
+ glmocr parse examples/source/code.png --log-level DEBUG
241
+ ```
242
+
243
+ #### Python API
244
+
245
+ ```python
246
+ from glmocr import GlmOcr, parse
247
+
248
+ # Simple function
249
+ result = parse("image.png")
250
+ result = parse(["img1.png", "img2.jpg"])
251
+ result = parse("https://example.com/image.png")
252
+ result.save(output_dir="./results")
253
+
254
+ # Note: a list is treated as pages of a single document.
255
+
256
+ # Class-based API
257
+ with GlmOcr() as parser:
258
+ result = parser.parse("image.png")
259
+ print(result.json_result)
260
+ result.save()
261
+ ```
262
+
263
+ #### Flask Service
264
+
265
+ ```bash
266
+ # Start service
267
+ python -m glmocr.server
268
+
269
+ # With debug logging
270
+ python -m glmocr.server --log-level DEBUG
271
+
272
+ # Call API
273
+ curl -X POST http://localhost:5002/glmocr/parse \
274
+ -H "Content-Type: application/json" \
275
+ -d '{"images": ["./example/source/code.png"]}'
276
+ ```
277
+
278
+ Semantics:
279
+
280
+ - `images` can be a string or a list.
281
+ - A list is treated as pages of a single document.
282
+ - For multiple independent documents, call the endpoint multiple times (one document per request).
283
+
284
+ ### Configuration
285
+
286
+ Full configuration in `glmocr/config.yaml`:
287
+
288
+ ```yaml
289
+ # Server (for glmocr.server)
290
+ server:
291
+ host: "0.0.0.0"
292
+ port: 5002
293
+ debug: false
294
+
295
+ # Logging
296
+ logging:
297
+ level: INFO # DEBUG enables profiling
298
+
299
+ # Pipeline
300
+ pipeline:
301
+ # OCR API connection
302
+ ocr_api:
303
+ api_host: localhost
304
+ api_port: 8080
305
+ api_key: null # or set API_KEY env var
306
+ connect_timeout: 300
307
+ request_timeout: 300
308
+
309
+ # Page loader settings
310
+ page_loader:
311
+ max_tokens: 16384
312
+ temperature: 0.01
313
+ image_format: JPEG
314
+ min_pixels: 12544
315
+ max_pixels: 71372800
316
+
317
+ # Result formatting
318
+ result_formatter:
319
+ output_format: both # json, markdown, or both
320
+
321
+ # Layout detection (optional)
322
+ enable_layout: false
323
+ ```
324
+
325
+ See [config.yaml](glmocr/config.yaml) for all options.
326
+
327
+ ### Output Formats
328
+
329
+ Here are two examples of output formats:
330
+
331
+ - JSON
332
+
333
+ ```json
334
+ [[{ "index": 0, "label": "text", "content": "...", "bbox_2d": null }]]
335
+ ```
336
+
337
+ - Markdown
338
+
339
+ ```markdown
340
+ # Document Title
341
+
342
+ Body...
343
+
344
+ | Table | Content |
345
+ | ----- | ------- |
346
+ | ... | ... |
347
+ ```
348
+
349
+ ### Example of full pipeline
350
+
351
+ you can run example code like:
352
+
353
+ ```bash
354
+ python examples/example.py
355
+ ```
356
+
357
+ Output structure (one folder per input):
358
+
359
+ - `result.json` – structured OCR result
360
+ - `result.md` – Markdown result
361
+ - `imgs/` – cropped image regions (when layout mode is enabled)
362
+
363
+ ### Modular Architecture
364
+
365
+ GLM-OCR uses composable modules for easy customization:
366
+
367
+ | Component | Description |
368
+ | --------------------- | -------------------------------------- |
369
+ | `PageLoader` | Preprocessing and image encoding |
370
+ | `OCRClient` | Calls the GLM-OCR model service |
371
+ | `PPDocLayoutDetector` | PP-DocLayout layout detection |
372
+ | `ResultFormatter` | Post-processing, outputs JSON/Markdown |
373
+
374
+ You can extend the behavior by creating custom pipelines:
375
+
376
+ ```python
377
+ from glmocr.dataloader import PageLoader
378
+ from glmocr.ocr_client import OCRClient
379
+ from glmocr.postprocess import ResultFormatter
380
+
381
+
382
+ class MyPipeline:
383
+ def __init__(self, config):
384
+ self.page_loader = PageLoader(config)
385
+ self.ocr_client = OCRClient(config)
386
+ self.formatter = ResultFormatter(config)
387
+
388
+ def process(self, request_data):
389
+ # Implement your own processing logic
390
+ pass
391
+ ```
392
+
393
+ ## Acknowledgement
394
+
395
+ This project is inspired by the excellent work of the following projects and communities:
396
+
397
+ - [PP-DocLayout-V3](https://huggingface.co/PaddlePaddle/PP-DocLayoutV3)
398
+ - [PaddleOCR](https://github.com/PaddlePaddle/PaddleOCR)
399
+ - [MinerU](https://github.com/opendatalab/MinerU)
400
+
401
+ ## License
402
+
403
+ The Code of this repo is under Apache License 2.0.
404
+
405
+ The GLM-OCR model is released under the MIT License.
406
+
407
+ The complete OCR pipeline integrates [PP-DocLayoutV3](https://huggingface.co/PaddlePaddle/PP-DocLayoutV3) for document layout analysis, which is licensed under the Apache License 2.0. Users should comply with both licenses when using this project.
408
+
409
+ ## Citation
410
+
411
+ If you find GLM-OCR useful in your research, please cite our technical report:
412
+
413
+ ```bibtex
414
+ @misc{duan2026glmocrtechnicalreport,
415
+ title={GLM-OCR Technical Report},
416
+ author={Shuaiqi Duan and Yadong Xue and Weihan Wang and Zhe Su and Huan Liu and Sheng Yang and Guobing Gan and Guo Wang and Zihan Wang and Shengdong Yan and Dexin Jin and Yuxuan Zhang and Guohong Wen and Yanfeng Wang and Yutao Zhang and Xiaohan Zhang and Wenyi Hong and Yukuo Cen and Da Yin and Bin Chen and Wenmeng Yu and Xiaotao Gu and Jie Tang},
417
+ year={2026},
418
+ eprint={2603.10910},
419
+ archivePrefix={arXiv},
420
+ primaryClass={cs.CL},
421
+ url={https://arxiv.org/abs/2603.10910},
422
+ }
423
+ ```