typhoon-ocr2 0.1.0__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.
@@ -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 2025 SCB 10X Co., Ltd.
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.
@@ -0,0 +1,7 @@
1
+ typhoon-ocr2
2
+ Copyright 2026 Surapon Nonesung
3
+
4
+ This project is derived from typhoon-ocr (https://github.com/scb-10x/typhoon-ocr),
5
+ Copyright the Typhoon OCR Contributors, SCB 10X, licensed under the Apache License 2.0.
6
+ It is maintained as a separate package with the Typhoon team's permission.
7
+ "Typhoon" is used to refer to the Typhoon models; this package is not an official SCB 10X product.
@@ -0,0 +1,233 @@
1
+ Metadata-Version: 2.4
2
+ Name: typhoon-ocr2
3
+ Version: 0.1.0
4
+ Summary: Parse Thai and English documents (PDFs and images) into layout elements and Markdown with Typhoon OCR V2
5
+ Author: Surapon Nonesung
6
+ License: Apache-2.0
7
+ Project-URL: Homepage, https://github.com/srn-source/typhoon-ocr2
8
+ Project-URL: Model (2B), https://huggingface.co/typhoon-ai/typhoon-ocr2-2b
9
+ Project-URL: Model (0.8B), https://huggingface.co/typhoon-ai/typhoon-ocr2-0.8b
10
+ Project-URL: Playground, https://playground.opentyphoon.ai/ocr
11
+ Keywords: ocr,document-parsing,layout-analysis,thai,vlm,typhoon
12
+ Classifier: Programming Language :: Python :: 3
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Topic :: Scientific/Engineering :: Image Recognition
16
+ Classifier: Natural Language :: Thai
17
+ Classifier: Natural Language :: English
18
+ Requires-Python: >=3.10
19
+ Description-Content-Type: text/markdown
20
+ License-File: LICENSE
21
+ License-File: NOTICE
22
+ Requires-Dist: httpx>=0.24
23
+ Requires-Dist: pillow>=9.0
24
+ Requires-Dist: pypdfium2>=4.0
25
+ Provides-Extra: dev
26
+ Requires-Dist: pytest>=7; extra == "dev"
27
+ Dynamic: license-file
28
+
29
+ # typhoon-ocr2
30
+
31
+ Read Thai and English documents (PDFs and images) with **Typhoon OCR**. For every page you get:
32
+
33
+ - the page as **Markdown** (tables as HTML, formulas as LaTeX), and
34
+ - with Typhoon OCR V2, the **layout elements** in reading order: each one has a category (title, text, table, …), a bounding box and its text.
35
+
36
+ Links: [Typhoon OCR V2 2B](https://huggingface.co/typhoon-ai/typhoon-ocr2-2b) · [Typhoon OCR V2 0.8B](https://huggingface.co/typhoon-ai/typhoon-ocr2-0.8b) · [Playground](https://playground.opentyphoon.ai/ocr) · [Typhoon API docs](https://docs.opentyphoon.ai/)
37
+
38
+ This package continues [scb-10x/typhoon-ocr](https://github.com/scb-10x/typhoon-ocr) (`pip install typhoon-ocr`) with the Typhoon team's permission. It has a new name, so code that uses `typhoon-ocr` keeps working.
39
+
40
+ ## Install
41
+
42
+ ```bash
43
+ pip install typhoon-ocr2
44
+ ```
45
+
46
+ PDF support is included; you do not need to install Poppler.
47
+
48
+ ## Quick start: Typhoon API (no GPU needed)
49
+
50
+ ```python
51
+ import os
52
+ from typhoon_ocr2 import TyphoonOCR
53
+
54
+ os.environ["TYPHOON_API_KEY"] = "YOUR_KEY" # your Typhoon API key (or pass api_key="YOUR_KEY" below)
55
+
56
+ ocr = TyphoonOCR(
57
+ base_url="https://api.opentyphoon.ai/v1", # the Typhoon API
58
+ version="v2", # Typhoon OCR V2 ("v1.5" for Typhoon OCR 1.5)
59
+ upscale=True, # enlarge images under 1,600 px (recommended; see Settings)
60
+ )
61
+
62
+ pages = ocr.parse("report.pdf", pages="1-3") # a PDF or an image; pages: "1-3", 2, [1, 4] or None for all
63
+ for page in pages:
64
+ print(f"--- page {page.page}")
65
+ print(page.markdown)
66
+ ```
67
+
68
+ ## Typhoon OCR V2 or 1.5
69
+
70
+ Switch with `version`; the rest of the code stays the same.
71
+
72
+ ```python
73
+ ocr_v15 = TyphoonOCR(
74
+ base_url="https://api.opentyphoon.ai/v1",
75
+ version="v1.5",
76
+ )
77
+ print(ocr_v15.parse("page.jpg")[0].markdown)
78
+ ```
79
+
80
+ | | `version="v2"` | `version="v1.5"` |
81
+ |---|---|---|
82
+ | model used on the Typhoon API | `typhoon-ocr-v2` | `typhoon-ocr-v1.5` |
83
+ | `page.markdown` | yes | yes |
84
+ | `page.elements` (category, box, text) | yes | no (empty list) |
85
+ | `ask()` / `extract()` | yes | not tested |
86
+
87
+ **`version` or `model`?** `version` tells the package which prompt to send and how to read the answer. `model` is the model's name on the server. On the Typhoon API you only need `version`: the package picks the matching model for you. You need `model` when you run the model yourself (next section), because there the name depends on how you started the server.
88
+
89
+ ## Run the model yourself
90
+
91
+ Start a server, then point `base_url` and `model` at it:
92
+
93
+ ```bash
94
+ # vLLM 0.33.0 or newer (GPU)
95
+ vllm serve typhoon-ai/typhoon-ocr2-2b --max-model-len 40960 \
96
+ --limit-mm-per-prompt '{"image": 1}' \
97
+ --mm-processor-kwargs '{"min_pixels": 2048, "max_pixels": 16777216}'
98
+
99
+ # Ollama 0.40.15 or newer
100
+ ollama pull SuraponOCR/typhoon-ocr2-2b
101
+
102
+ # llama.cpp (GGUF)
103
+ llama-server -hf typhoon-ai/typhoon-ocr2-2b-GGUF:Q8_0 --jinja --reasoning off -c 40960 --image-max-tokens 16384
104
+ ```
105
+
106
+ | Server | `base_url` | `model` |
107
+ |---|---|---|
108
+ | vLLM | `"http://localhost:8000/v1"` | `"typhoon-ai/typhoon-ocr2-2b"` |
109
+ | Ollama | `"http://localhost:11434"` | `"SuraponOCR/typhoon-ocr2-2b"` |
110
+ | llama.cpp | `"http://localhost:8080/v1"` | `"typhoon-ocr2"` (llama-server serves one model; any name works) |
111
+
112
+ ```python
113
+ ocr = TyphoonOCR(
114
+ base_url="http://localhost:8000/v1",
115
+ model="typhoon-ai/typhoon-ocr2-2b",
116
+ version="v2",
117
+ upscale=True,
118
+ )
119
+ ```
120
+
121
+ For the smaller page parser, use `typhoon-ai/typhoon-ocr2-0.8b` (Ollama: `SuraponOCR/typhoon-ocr2-0.8b`). On vLLM older than 0.33.0, add `--enforce-eager` to `vllm serve`: in the default compiled mode of vLLM 0.27.1, accuracy dropped by about 9 points on MDPBench. Other OpenAI-compatible servers work the same way.
122
+
123
+ ## What you get back
124
+
125
+ `ocr.parse()` returns one `PageResult` per page:
126
+
127
+ | Field | Meaning |
128
+ |---|---|
129
+ | `page.page` | page number (1-based) |
130
+ | `page.markdown` | the page as Markdown; by default without headers, footers and page footnotes (see below) |
131
+ | `page.elements` | V2 only: **every** element the model returned, as `{"bbox": [x1, y1, x2, y2], "category": "...", "text": "..."}`, in reading order |
132
+ | `page.image_size` | `(width, height)` of the page image the boxes refer to |
133
+ | `page.truncated` | True if the output stopped at the token limit (usually a repetition loop); `elements` keeps the complete ones |
134
+ | `page.raw` | the model's raw output |
135
+
136
+ ### Headers, footers and other categories
137
+
138
+ The model always returns header, footer and page_footnote elements, and they are always in `page.elements`. Only `page.markdown` leaves them out by default, because that is how the model was evaluated (and running page numbers and headers usually get in the way of reading). You choose what the Markdown keeps:
139
+
140
+ ```python
141
+ pages = ocr.parse("report.pdf") # default: no header, footer, page_footnote
142
+ pages = ocr.parse("report.pdf", keep_header_footer=True) # keep every element
143
+ pages = ocr.parse("report.pdf", exclude=["footer"]) # keep headers, drop footers only
144
+ pages = ocr.parse("report.pdf", exclude=["header", "footer", "page_footnote", "figure_caption"]) # your own list
145
+
146
+ footers = [e["text"] for e in pages[0].elements if e["category"] == "footer"] # the elements themselves
147
+ ```
148
+
149
+ On the command line: `--keep-header-footer`, or `--exclude footer` (several names allowed; `--exclude` with no names keeps everything).
150
+
151
+ The 13 categories are header, title, text, figure, table, formula, figure_caption, table_caption, formula_caption, figure_footnote, table_footnote, page_footnote and footer. Boxes use a 0–1000 grid relative to the page, so they do not depend on the image size:
152
+
153
+ ```python
154
+ from typhoon_ocr2 import draw_layout, load_pages, to_pixels
155
+
156
+ image = load_pages("report.pdf", pages=[1])[0]
157
+ first = pages[0].elements[0]
158
+ print(first["category"], to_pixels(first["bbox"], *image.size)) # box in pixels
159
+ draw_layout(image, pages[0].elements).save("page1_layout.png") # boxes numbered in reading order
160
+ ```
161
+
162
+ ## Questions and key information (Typhoon OCR V2 2B)
163
+
164
+ ```python
165
+ answer = ocr.ask("receipt.jpg", "What is the total amount?") # plain text
166
+ fields = ocr.extract("receipt.jpg", ["Date", "Total", "VAT 7%"]) # dict with these keys
167
+ everything = ocr.extract("receipt.jpg") # every field; the model picks the keys
168
+ ```
169
+
170
+ The 0.8B model is a page parser only; it is much weaker at questions and extraction. Check extracted values before you use them: key-information extraction is the 2B model's weakest task.
171
+
172
+ ## Command line
173
+
174
+ ```bash
175
+ export TYPHOON_API_KEY=YOUR_KEY
176
+ API=https://api.opentyphoon.ai/v1
177
+
178
+ typhoon-ocr2 report.pdf --base-url $API # Markdown of every page (V2)
179
+ typhoon-ocr2 report.pdf --base-url $API --pages 2-4 --format json # layout elements
180
+ typhoon-ocr2 receipt.jpg --base-url $API --ask "What is the total amount?"
181
+ typhoon-ocr2 receipt.jpg --base-url $API --extract Date Total
182
+ typhoon-ocr2 page.jpg --base-url $API --version v1.5 # Typhoon OCR 1.5
183
+ typhoon-ocr2 page.jpg --base-url http://localhost:11434 --model SuraponOCR/typhoon-ocr2-2b # Ollama
184
+ ```
185
+
186
+ To avoid repeating `--base-url`, set `TYPHOON_OCR_BASE_URL`. Add `--no-upscale` to turn upscaling off.
187
+
188
+ ## Settings
189
+
190
+ All settings of `TyphoonOCR(...)`. The defaults are the settings the models were evaluated with.
191
+
192
+ | Setting | Default | What it does |
193
+ |---|---|---|
194
+ | `base_url` | `$TYPHOON_OCR_BASE_URL`, else `"http://localhost:8000/v1"` | server address |
195
+ | `model` | on the Typhoon API, chosen from `version`; on Ollama, `"SuraponOCR/typhoon-ocr2-2b"`; on other servers, the first model they serve | model name on the server; needed only for your own server |
196
+ | `api_key` | `$TYPHOON_API_KEY` | key for the Typhoon API |
197
+ | `version` | `"v2"` (or follows `model`, if you set one) | `"v2"` or `"v1.5"`: the prompt and output format |
198
+ | `upscale` | `True` | enlarge images whose long side is under 1,600 px. On ThaiOCRBench this raised Typhoon OCR V2 2B from 59.9 to 62.9 and 0.8B from 37.3 to 41.3; large pages are not changed. `False` turns it off; a number sets another size |
199
+ | `max_tokens` | `16384` | output limit per page |
200
+ | `pdf_long_side` | `2048` | PDF pages are rendered with this long side, in pixels |
201
+ | `max_workers` | `4` | PDF pages sent in parallel |
202
+ | `timeout` | `600` | seconds to wait for one page |
203
+ | `backend` | `"auto"` | `"openai"` or `"ollama"`; auto picks Ollama for `http://localhost:11434` |
204
+ | `figure_language` | `"Thai"` | v1.5 only: language of figure descriptions |
205
+
206
+ The prompts are the ones the models were trained with (`typhoon_ocr2.PARSE_PROMPT` for V2), and decoding is greedy with thinking turned off.
207
+
208
+ ## Coming from `typhoon-ocr`
209
+
210
+ | `typhoon-ocr` | `typhoon-ocr2` |
211
+ |---|---|
212
+ | `from typhoon_ocr import ocr_document` | `from typhoon_ocr2 import ocr_document` (same idea), or `TyphoonOCR` for more |
213
+ | `ocr_document(path, page_num=1, base_url=..., model=...)` | `ocr_document(path, pages=1, base_url=..., model=...)` |
214
+ | returns a Markdown string | `TyphoonOCR.parse()` also returns layout elements with boxes |
215
+ | one page per call | `pages="1-3,5"` or all pages, sent in parallel |
216
+ | needs Poppler for PDFs | no system dependency |
217
+
218
+ ## Development
219
+
220
+ ```bash
221
+ pip install -e ".[dev]"
222
+ pytest # unit tests, no GPU needed
223
+ TYPHOON_OCR2_TEST_URL=http://localhost:8000/v1 pytest -m integration # against a running server
224
+ ```
225
+
226
+ ## Releasing
227
+
228
+ 1. On pypi.org, add a Trusted Publisher for project `typhoon-ocr2`: this GitHub repository, workflow `publish.yml`, environment `pypi`.
229
+ 2. Bump `__version__` in `src/typhoon_ocr2/__init__.py`, commit, then tag and push: `git tag v0.1.0 && git push origin v0.1.0`.
230
+
231
+ ## License
232
+
233
+ Apache-2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE). Use of the Typhoon models is subject to the [OpenTyphoon Terms and Conditions](https://opentyphoon.ai/tac).
@@ -0,0 +1,205 @@
1
+ # typhoon-ocr2
2
+
3
+ Read Thai and English documents (PDFs and images) with **Typhoon OCR**. For every page you get:
4
+
5
+ - the page as **Markdown** (tables as HTML, formulas as LaTeX), and
6
+ - with Typhoon OCR V2, the **layout elements** in reading order: each one has a category (title, text, table, …), a bounding box and its text.
7
+
8
+ Links: [Typhoon OCR V2 2B](https://huggingface.co/typhoon-ai/typhoon-ocr2-2b) · [Typhoon OCR V2 0.8B](https://huggingface.co/typhoon-ai/typhoon-ocr2-0.8b) · [Playground](https://playground.opentyphoon.ai/ocr) · [Typhoon API docs](https://docs.opentyphoon.ai/)
9
+
10
+ This package continues [scb-10x/typhoon-ocr](https://github.com/scb-10x/typhoon-ocr) (`pip install typhoon-ocr`) with the Typhoon team's permission. It has a new name, so code that uses `typhoon-ocr` keeps working.
11
+
12
+ ## Install
13
+
14
+ ```bash
15
+ pip install typhoon-ocr2
16
+ ```
17
+
18
+ PDF support is included; you do not need to install Poppler.
19
+
20
+ ## Quick start: Typhoon API (no GPU needed)
21
+
22
+ ```python
23
+ import os
24
+ from typhoon_ocr2 import TyphoonOCR
25
+
26
+ os.environ["TYPHOON_API_KEY"] = "YOUR_KEY" # your Typhoon API key (or pass api_key="YOUR_KEY" below)
27
+
28
+ ocr = TyphoonOCR(
29
+ base_url="https://api.opentyphoon.ai/v1", # the Typhoon API
30
+ version="v2", # Typhoon OCR V2 ("v1.5" for Typhoon OCR 1.5)
31
+ upscale=True, # enlarge images under 1,600 px (recommended; see Settings)
32
+ )
33
+
34
+ pages = ocr.parse("report.pdf", pages="1-3") # a PDF or an image; pages: "1-3", 2, [1, 4] or None for all
35
+ for page in pages:
36
+ print(f"--- page {page.page}")
37
+ print(page.markdown)
38
+ ```
39
+
40
+ ## Typhoon OCR V2 or 1.5
41
+
42
+ Switch with `version`; the rest of the code stays the same.
43
+
44
+ ```python
45
+ ocr_v15 = TyphoonOCR(
46
+ base_url="https://api.opentyphoon.ai/v1",
47
+ version="v1.5",
48
+ )
49
+ print(ocr_v15.parse("page.jpg")[0].markdown)
50
+ ```
51
+
52
+ | | `version="v2"` | `version="v1.5"` |
53
+ |---|---|---|
54
+ | model used on the Typhoon API | `typhoon-ocr-v2` | `typhoon-ocr-v1.5` |
55
+ | `page.markdown` | yes | yes |
56
+ | `page.elements` (category, box, text) | yes | no (empty list) |
57
+ | `ask()` / `extract()` | yes | not tested |
58
+
59
+ **`version` or `model`?** `version` tells the package which prompt to send and how to read the answer. `model` is the model's name on the server. On the Typhoon API you only need `version`: the package picks the matching model for you. You need `model` when you run the model yourself (next section), because there the name depends on how you started the server.
60
+
61
+ ## Run the model yourself
62
+
63
+ Start a server, then point `base_url` and `model` at it:
64
+
65
+ ```bash
66
+ # vLLM 0.33.0 or newer (GPU)
67
+ vllm serve typhoon-ai/typhoon-ocr2-2b --max-model-len 40960 \
68
+ --limit-mm-per-prompt '{"image": 1}' \
69
+ --mm-processor-kwargs '{"min_pixels": 2048, "max_pixels": 16777216}'
70
+
71
+ # Ollama 0.40.15 or newer
72
+ ollama pull SuraponOCR/typhoon-ocr2-2b
73
+
74
+ # llama.cpp (GGUF)
75
+ llama-server -hf typhoon-ai/typhoon-ocr2-2b-GGUF:Q8_0 --jinja --reasoning off -c 40960 --image-max-tokens 16384
76
+ ```
77
+
78
+ | Server | `base_url` | `model` |
79
+ |---|---|---|
80
+ | vLLM | `"http://localhost:8000/v1"` | `"typhoon-ai/typhoon-ocr2-2b"` |
81
+ | Ollama | `"http://localhost:11434"` | `"SuraponOCR/typhoon-ocr2-2b"` |
82
+ | llama.cpp | `"http://localhost:8080/v1"` | `"typhoon-ocr2"` (llama-server serves one model; any name works) |
83
+
84
+ ```python
85
+ ocr = TyphoonOCR(
86
+ base_url="http://localhost:8000/v1",
87
+ model="typhoon-ai/typhoon-ocr2-2b",
88
+ version="v2",
89
+ upscale=True,
90
+ )
91
+ ```
92
+
93
+ For the smaller page parser, use `typhoon-ai/typhoon-ocr2-0.8b` (Ollama: `SuraponOCR/typhoon-ocr2-0.8b`). On vLLM older than 0.33.0, add `--enforce-eager` to `vllm serve`: in the default compiled mode of vLLM 0.27.1, accuracy dropped by about 9 points on MDPBench. Other OpenAI-compatible servers work the same way.
94
+
95
+ ## What you get back
96
+
97
+ `ocr.parse()` returns one `PageResult` per page:
98
+
99
+ | Field | Meaning |
100
+ |---|---|
101
+ | `page.page` | page number (1-based) |
102
+ | `page.markdown` | the page as Markdown; by default without headers, footers and page footnotes (see below) |
103
+ | `page.elements` | V2 only: **every** element the model returned, as `{"bbox": [x1, y1, x2, y2], "category": "...", "text": "..."}`, in reading order |
104
+ | `page.image_size` | `(width, height)` of the page image the boxes refer to |
105
+ | `page.truncated` | True if the output stopped at the token limit (usually a repetition loop); `elements` keeps the complete ones |
106
+ | `page.raw` | the model's raw output |
107
+
108
+ ### Headers, footers and other categories
109
+
110
+ The model always returns header, footer and page_footnote elements, and they are always in `page.elements`. Only `page.markdown` leaves them out by default, because that is how the model was evaluated (and running page numbers and headers usually get in the way of reading). You choose what the Markdown keeps:
111
+
112
+ ```python
113
+ pages = ocr.parse("report.pdf") # default: no header, footer, page_footnote
114
+ pages = ocr.parse("report.pdf", keep_header_footer=True) # keep every element
115
+ pages = ocr.parse("report.pdf", exclude=["footer"]) # keep headers, drop footers only
116
+ pages = ocr.parse("report.pdf", exclude=["header", "footer", "page_footnote", "figure_caption"]) # your own list
117
+
118
+ footers = [e["text"] for e in pages[0].elements if e["category"] == "footer"] # the elements themselves
119
+ ```
120
+
121
+ On the command line: `--keep-header-footer`, or `--exclude footer` (several names allowed; `--exclude` with no names keeps everything).
122
+
123
+ The 13 categories are header, title, text, figure, table, formula, figure_caption, table_caption, formula_caption, figure_footnote, table_footnote, page_footnote and footer. Boxes use a 0–1000 grid relative to the page, so they do not depend on the image size:
124
+
125
+ ```python
126
+ from typhoon_ocr2 import draw_layout, load_pages, to_pixels
127
+
128
+ image = load_pages("report.pdf", pages=[1])[0]
129
+ first = pages[0].elements[0]
130
+ print(first["category"], to_pixels(first["bbox"], *image.size)) # box in pixels
131
+ draw_layout(image, pages[0].elements).save("page1_layout.png") # boxes numbered in reading order
132
+ ```
133
+
134
+ ## Questions and key information (Typhoon OCR V2 2B)
135
+
136
+ ```python
137
+ answer = ocr.ask("receipt.jpg", "What is the total amount?") # plain text
138
+ fields = ocr.extract("receipt.jpg", ["Date", "Total", "VAT 7%"]) # dict with these keys
139
+ everything = ocr.extract("receipt.jpg") # every field; the model picks the keys
140
+ ```
141
+
142
+ The 0.8B model is a page parser only; it is much weaker at questions and extraction. Check extracted values before you use them: key-information extraction is the 2B model's weakest task.
143
+
144
+ ## Command line
145
+
146
+ ```bash
147
+ export TYPHOON_API_KEY=YOUR_KEY
148
+ API=https://api.opentyphoon.ai/v1
149
+
150
+ typhoon-ocr2 report.pdf --base-url $API # Markdown of every page (V2)
151
+ typhoon-ocr2 report.pdf --base-url $API --pages 2-4 --format json # layout elements
152
+ typhoon-ocr2 receipt.jpg --base-url $API --ask "What is the total amount?"
153
+ typhoon-ocr2 receipt.jpg --base-url $API --extract Date Total
154
+ typhoon-ocr2 page.jpg --base-url $API --version v1.5 # Typhoon OCR 1.5
155
+ typhoon-ocr2 page.jpg --base-url http://localhost:11434 --model SuraponOCR/typhoon-ocr2-2b # Ollama
156
+ ```
157
+
158
+ To avoid repeating `--base-url`, set `TYPHOON_OCR_BASE_URL`. Add `--no-upscale` to turn upscaling off.
159
+
160
+ ## Settings
161
+
162
+ All settings of `TyphoonOCR(...)`. The defaults are the settings the models were evaluated with.
163
+
164
+ | Setting | Default | What it does |
165
+ |---|---|---|
166
+ | `base_url` | `$TYPHOON_OCR_BASE_URL`, else `"http://localhost:8000/v1"` | server address |
167
+ | `model` | on the Typhoon API, chosen from `version`; on Ollama, `"SuraponOCR/typhoon-ocr2-2b"`; on other servers, the first model they serve | model name on the server; needed only for your own server |
168
+ | `api_key` | `$TYPHOON_API_KEY` | key for the Typhoon API |
169
+ | `version` | `"v2"` (or follows `model`, if you set one) | `"v2"` or `"v1.5"`: the prompt and output format |
170
+ | `upscale` | `True` | enlarge images whose long side is under 1,600 px. On ThaiOCRBench this raised Typhoon OCR V2 2B from 59.9 to 62.9 and 0.8B from 37.3 to 41.3; large pages are not changed. `False` turns it off; a number sets another size |
171
+ | `max_tokens` | `16384` | output limit per page |
172
+ | `pdf_long_side` | `2048` | PDF pages are rendered with this long side, in pixels |
173
+ | `max_workers` | `4` | PDF pages sent in parallel |
174
+ | `timeout` | `600` | seconds to wait for one page |
175
+ | `backend` | `"auto"` | `"openai"` or `"ollama"`; auto picks Ollama for `http://localhost:11434` |
176
+ | `figure_language` | `"Thai"` | v1.5 only: language of figure descriptions |
177
+
178
+ The prompts are the ones the models were trained with (`typhoon_ocr2.PARSE_PROMPT` for V2), and decoding is greedy with thinking turned off.
179
+
180
+ ## Coming from `typhoon-ocr`
181
+
182
+ | `typhoon-ocr` | `typhoon-ocr2` |
183
+ |---|---|
184
+ | `from typhoon_ocr import ocr_document` | `from typhoon_ocr2 import ocr_document` (same idea), or `TyphoonOCR` for more |
185
+ | `ocr_document(path, page_num=1, base_url=..., model=...)` | `ocr_document(path, pages=1, base_url=..., model=...)` |
186
+ | returns a Markdown string | `TyphoonOCR.parse()` also returns layout elements with boxes |
187
+ | one page per call | `pages="1-3,5"` or all pages, sent in parallel |
188
+ | needs Poppler for PDFs | no system dependency |
189
+
190
+ ## Development
191
+
192
+ ```bash
193
+ pip install -e ".[dev]"
194
+ pytest # unit tests, no GPU needed
195
+ TYPHOON_OCR2_TEST_URL=http://localhost:8000/v1 pytest -m integration # against a running server
196
+ ```
197
+
198
+ ## Releasing
199
+
200
+ 1. On pypi.org, add a Trusted Publisher for project `typhoon-ocr2`: this GitHub repository, workflow `publish.yml`, environment `pypi`.
201
+ 2. Bump `__version__` in `src/typhoon_ocr2/__init__.py`, commit, then tag and push: `git tag v0.1.0 && git push origin v0.1.0`.
202
+
203
+ ## License
204
+
205
+ Apache-2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE). Use of the Typhoon models is subject to the [OpenTyphoon Terms and Conditions](https://opentyphoon.ai/tac).