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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<div align="center">
|
| 2 |
+
|
| 3 |
+
# HunyuanOCR
|
| 4 |
+
|
| 5 |
+
</div>
|
| 6 |
+
|
| 7 |
+
<p align="center">
|
| 8 |
+
<img src="[./assets/hyocr-head-img.png](https://github.com/Tencent-Hunyuan/HunyuanOCR/blob/main/assets/hyocr-head-img.png)" width="80%"/> <br>
|
| 9 |
+
</p>
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
<p align="center">
|
| 13 |
+
<a href="https://huggingface.co/spaces/tencent/HunyuanOCR"><b>🐙 Github</b></a> |
|
| 14 |
+
<a href="https://huggingface.co/spaces/tencent/HunyuanOCR"><b>🎯 Demo</b></a> |
|
| 15 |
+
<a href="https://huggingface.co/tencent/HunyuanOCR"><b>📥 Model Download</b></a> |
|
| 16 |
+
<a href="./Hunyuan_OCR_Technical_Report.pdf"><b>📄 Technical Report</b></a>
|
| 17 |
+
</p>
|
| 18 |
+
|
| 19 |
+
## 🔥 News
|
| 20 |
+
- **[2025/11/25]** 📝 Inference code and model weights publicly available.
|
| 21 |
+
|
| 22 |
+
## 📖 Introduction
|
| 23 |
+
**HunyuanOCR** stands as a leading end-to-end OCR expert VLM powered by Hunyuan's native multimodal architecture. With a remarkably lightweight 1B parameter design, it has achieved multiple state-of-the-art benchmarks across the industry. The model demonstrates mastery in **complex multilingual document parsing** while excelling in practical applications including **text spotting, open-field information extraction, video subtitle extraction, and photo translation**.
|
| 24 |
+
|
| 25 |
+
Built on Tencent's Hunyuan technology, this versatile model delivers exceptional performance through end-to-end architecture design and single-pass inference. It significantly simplifies deployment while maintaining competitive performance against both established cascade systems and commercial APIs.
|
| 26 |
+
|
| 27 |
+
## ✨ Key Features
|
| 28 |
+
|
| 29 |
+
- 💪 **Efficient Lightweight Architecture**: Built on Hunyuan's native multimodal architecture and training strategy, achieving SOTA performance with only 1B parameters, significantly reducing deployment costs.
|
| 30 |
+
|
| 31 |
+
- 📑 **Comprehensive OCR Capabilities**: A single model covering classic OCR tasks including text detection and recognition, complex document parsing, open-field information extraction and video subtitle extraction, while supporting end-to-end photo translation and document QA.
|
| 32 |
+
|
| 33 |
+
- 🚀 **Ultimate Usability**: Deeply embraces the "end-to-end" philosophy of large models - achieving SOTA results with single instruction and single inference, offering greater efficiency and convenience compared to industry cascade solutions.
|
| 34 |
+
|
| 35 |
+
- 🌏 **Extensive Language Support**: Robust support for over 100 languages, excelling in both single-language and mixed-language scenarios across various document types.
|
| 36 |
+
|
| 37 |
+
<div align="left">
|
| 38 |
+
<img src="./assets/hyocr-pipeline.png" alt="HunyuanOCR framework" width="80%">
|
| 39 |
+
</div>
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
## 🛠️ Dependencies and Installation
|
| 45 |
+
|
| 46 |
+
### System Requirements
|
| 47 |
+
- 🖥️ Operating System: Linux
|
| 48 |
+
- 🐍 Python: 3.12+ (recommended and tested)
|
| 49 |
+
- ⚡ CUDA: 12.8
|
| 50 |
+
- 🔥 PyTorch: 2.7.1
|
| 51 |
+
- 🎮 GPU: NVIDIA GPU with CUDA support
|
| 52 |
+
- 🧠 GPU Memory: 80GB
|
| 53 |
+
- 💾 Disk Space: 6GB
|
| 54 |
+
|
| 55 |
+
## 🚀 Quick Start with vLLM
|
| 56 |
+
### Installation
|
| 57 |
+
```bash
|
| 58 |
+
pip install vllm --extra-index-url https://wheels.vllm.ai/nightly
|
| 59 |
+
```
|
| 60 |
+
|
| 61 |
+
### Model Inference
|
| 62 |
+
|
| 63 |
+
```python
|
| 64 |
+
from vllm import LLM, SamplingParams
|
| 65 |
+
from PIL import Image
|
| 66 |
+
from transformers import AutoProcessor
|
| 67 |
+
|
| 68 |
+
model_path = "tencent/HunyuanOCR"
|
| 69 |
+
llm = LLM(model=model_path, trust_remote_code=True)
|
| 70 |
+
processor = AutoProcessor.from_pretrained(model_path)
|
| 71 |
+
sampling_params = SamplingParams(temperature=0, max_tokens=16384)
|
| 72 |
+
|
| 73 |
+
img_path = "/path/to/image.jpg"
|
| 74 |
+
img = Image.open(img_path)
|
| 75 |
+
messages = [
|
| 76 |
+
{"role": "user", "content": [
|
| 77 |
+
{"type": "image", "image": img_path},
|
| 78 |
+
{"type": "text", "text": "Detect and recognize text in the image, and output the text coordinates in a formatted manner."}
|
| 79 |
+
]}
|
| 80 |
+
]
|
| 81 |
+
prompt = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 82 |
+
inputs = {"prompt": prompt, "multi_modal_data": {"image": [img]}}
|
| 83 |
+
output = llm.generate([inputs], sampling_params)[0]
|
| 84 |
+
print(output.outputs[0].text)
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
## 🚀 Quick Start with Transformers
|
| 88 |
+
|
| 89 |
+
### Installation
|
| 90 |
+
```bash
|
| 91 |
+
pip install git+https://github.com/huggingface/transformers@82a06db03535c49aa987719ed0746a76093b1ec4
|
| 92 |
+
```
|
| 93 |
+
> **Note**: We will merge it into the Transformers main branch later.
|
| 94 |
+
|
| 95 |
+
### Model Inference
|
| 96 |
+
|
| 97 |
+
```python
|
| 98 |
+
from transformers import AutoProcessor
|
| 99 |
+
from transformers import HunYuanVLForConditionalGeneration
|
| 100 |
+
from PIL import Image
|
| 101 |
+
import torch
|
| 102 |
+
|
| 103 |
+
model_name_or_path = "tencent/HunyuanOCR"
|
| 104 |
+
processor = AutoProcessor.from_pretrained(model_name_or_path, use_fast=False)
|
| 105 |
+
img_path = "path/to/your/image.jpg"
|
| 106 |
+
image_inputs = Image.open(img_path)
|
| 107 |
+
messages1 = [
|
| 108 |
+
{
|
| 109 |
+
"role": "user",
|
| 110 |
+
"content": [
|
| 111 |
+
{"type": "image", "image": img_path},
|
| 112 |
+
{"type": "text", "text": (
|
| 113 |
+
"Extract all information from the main body of the document image "
|
| 114 |
+
"and represent it in markdown format, ignoring headers and footers. "
|
| 115 |
+
"Tables should be expressed in HTML format, formulas in the document "
|
| 116 |
+
"should be represented using LaTeX format, and the parsing should be "
|
| 117 |
+
"organized according to the reading order."
|
| 118 |
+
)},
|
| 119 |
+
],
|
| 120 |
+
}
|
| 121 |
+
]
|
| 122 |
+
messages = [messages1]
|
| 123 |
+
texts = [
|
| 124 |
+
processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
|
| 125 |
+
for msg in messages
|
| 126 |
+
]
|
| 127 |
+
inputs = processor(
|
| 128 |
+
text=texts,
|
| 129 |
+
images=image_inputs,
|
| 130 |
+
padding=True,
|
| 131 |
+
return_tensors="pt",
|
| 132 |
+
)
|
| 133 |
+
model = HunYuanVLForConditionalGeneration.from_pretrained(
|
| 134 |
+
model_name_or_path,
|
| 135 |
+
attn_implementation="eager",
|
| 136 |
+
dtype=torch.bfloat16,
|
| 137 |
+
device_map="auto"
|
| 138 |
+
)
|
| 139 |
+
with torch.no_grad():
|
| 140 |
+
device = next(model.parameters()).device
|
| 141 |
+
inputs = inputs.to(device)
|
| 142 |
+
generated_ids = model.generate(**inputs, max_new_tokens=1024, do_sample=False)
|
| 143 |
+
if "input_ids" in inputs:
|
| 144 |
+
input_ids = inputs.input_ids
|
| 145 |
+
else:
|
| 146 |
+
print("inputs: # fallback", inputs)
|
| 147 |
+
input_ids = inputs.inputs
|
| 148 |
+
generated_ids_trimmed = [
|
| 149 |
+
out_ids[len(in_ids):] for in_ids, out_ids in zip(input_ids, generated_ids)
|
| 150 |
+
]
|
| 151 |
+
output_texts = processor.batch_decode(
|
| 152 |
+
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
|
| 153 |
+
)
|
| 154 |
+
print(output_texts)
|
| 155 |
+
```
|
| 156 |
+
|
| 157 |
+
## 💬 Application-oriented Prompts
|
| 158 |
+
|
| 159 |
+
| Task | English | Chinese |
|
| 160 |
+
|------|---------|---------|
|
| 161 |
+
| **Spotting** | Detect and recognize text in the image, and output the text coordinates in a formatted manner. | 检测并识别图片中的文字,将文本坐标格式化输出。 |
|
| 162 |
+
| **Parsing** | • Identify the formula in the image and represent it using LaTeX format.<br><br>• Parse the table in the image into HTML.<br><br>• Parse the chart in the image; use Mermaid format for flowcharts and Markdown for other charts.<br><br>• Extract all information from the main body of the document image and represent it in markdown format, ignoring headers and footers. Tables should be expressed in HTML format, formulas in the document should be represented using LaTeX format, and the parsing should be organized according to the reading order. | • 识别图片中的公式,用 LaTeX 格式表示。<br><br>• 把图中的表格解析为 HTML。<br><br>• 解析图中的图表,对于流程图使用 Mermaid 格式表示,其他图表使用 Markdown 格式表示。<br><br>• 提取文档图片中正文的所有信息用 markdown 格式表示,其中页眉、页脚部分忽略,表格用 html 格式表达,文档中公式用 latex 格式表示,按照阅读顺序组织进行解析。 |
|
| 163 |
+
| **Information Extraction** | • Output the value of Key.<br><br>• Extract the content of the fields: ['key1','key2', ...] from the image and return it in JSON format.<br><br>• Extract the subtitles from the image. | • 输出 Key 的值。<br><br>• 提取图片中的: ['key1','key2', ...] 的字段内容,并按照 JSON 格式返回。<br><br>• 提取图片中的字幕。 |
|
| 164 |
+
| **Translation** | First extract the text, then translate the text content into English. If it is a document, ignore the header and footer. Formulas should be represented in LaTeX format, and tables should be represented in HTML format. | 先提取文字,再将文字内容翻译为英文。若是文档,则其中页眉、页脚忽略。公式用latex格式表示,表格用html格式表示。 |
|
| 165 |
+
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
## 📚 Citation
|
| 170 |
+
@misc{hunyuanocr2025,
|
| 171 |
+
title={HunyuanOCR Technical Report},
|
| 172 |
+
author={Tencent Hunyuan Vision Team},
|
| 173 |
+
year={2025},
|
| 174 |
+
publisher={GitHub},
|
| 175 |
+
journal={GitHub repository},
|
| 176 |
+
howpublished={\url{https://github.com/Tencent-Hunyuan/HunyuanOCR}}
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
## 🙏 Acknowledgements
|
| 180 |
+
Thanks to all contributors who helped build HunyuanOCR
|
| 181 |
+
Special thanks to the Tencent Hunyuan Team
|
| 182 |
+
We appreciate the support from the open-source community
|