
pip install git+https://github.com/huggingface/transformers@82a06db03535c49aa987719ed0746a76093b1ec4Note: We will merge it into the Transformers main branch later.
1from transformers import AutoProcessor
2from transformers import HunYuanVLForConditionalGeneration
3from PIL import Image
4import torch
5
6def clean_repeated_substrings(text):
7 """Clean repeated substrings in text"""
8 n = len(text)
9 if n<8000:
10 return text
11 for length in range(2, n // 10 + 1):
12 candidate = text[-length:]
13 count = 0
14 i = n - length
15
16 while i >= 0 and text[i:i + length] == candidate:
17 count += 1
18 i -= length
19
20 if count >= 10:
21 return text[:n - length * (count - 1)]
22
23 return text
24
25model_name_or_path = "tencent/HunyuanOCR"
26processor = AutoProcessor.from_pretrained(model_name_or_path, use_fast=False)
27img_path = "path/to/your/image.jpg"
28image_inputs = Image.open(img_path)
29messages1 = [
30 {"role": "system", "content": ""},
31 {
32 "role": "user",
33 "content": [
34 {"type": "image", "image": img_path},
35 {"type": "text", "text": (
36 "检测并识别图片中的文字,将文本坐标格式化输出。"
37 )},
38 ],
39 }
40]
41messages = [messages1]
42texts = [
43 processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
44 for msg in messages
45]
46inputs = processor(
47 text=texts,
48 images=image_inputs,
49 padding=True,
50 return_tensors="pt",
51)
52model = HunYuanVLForConditionalGeneration.from_pretrained(
53 model_name_or_path,
54 attn_implementation="eager",
55 dtype=torch.bfloat16,
56 device_map="auto"
57)
58with torch.no_grad():
59 device = next(model.parameters()).device
60 inputs = inputs.to(device)
61 generated_ids = model.generate(**inputs, max_new_tokens=16384, do_sample=False)
62if "input_ids" in inputs:
63 input_ids = inputs.input_ids
64else:
65 print("inputs: # fallback", inputs)
66 input_ids = inputs.inputs
67generated_ids_trimmed = [
68 out_ids[len(in_ids):] for in_ids, out_ids in zip(input_ids, generated_ids)
69]
70output_texts = clean_repeated_substrings(processor.batch_decode(
71 generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
72))
73print(output_texts)1uv venv hunyuanocr
2source hunyuanocr/bin/activate
3
4uv pip install -U vllm --pre --extra-index-url https://wheels.vllm.ai/nightly1sudo dpkg -i cuda-compat-12-9_575.57.08-0ubuntu1_amd64.deb
2echo 'export LD_LIBRARY_PATH=/usr/local/cuda-12.9/compat:$LD_LIBRARY_PATH' >> ~/.bashrc
3source ~/.bashrc
4# verify cuda-compat-12-9
5ls /usr/local/cuda-12.9/compat1vllm serve tencent/HunyuanOCR \
2 --no-enable-prefix-caching \
3 --mm-processor-cache-gb 0 \
4 --gpu-memory-utilization 0.21from vllm import LLM, SamplingParams
2from PIL import Image
3from transformers import AutoProcessor
4
5def clean_repeated_substrings(text):
6 """Clean repeated substrings in text"""
7 n = len(text)
8 if n<8000:
9 return text
10 for length in range(2, n // 10 + 1):
11 candidate = text[-length:]
12 count = 0
13 i = n - length
14
15 while i >= 0 and text[i:i + length] == candidate:
16 count += 1
17 i -= length
18
19 if count >= 10:
20 return text[:n - length * (count - 1)]
21
22 return text
23
24model_path = "tencent/HunyuanOCR"
25llm = LLM(model=model_path, trust_remote_code=True)
26processor = AutoProcessor.from_pretrained(model_path)
27sampling_params = SamplingParams(temperature=0, max_tokens=16384)
28
29img_path = "/path/to/image.jpg"
30img = Image.open(img_path)
31messages = [
32 {"role": "system", "content": ""},
33 {"role": "user", "content": [
34 {"type": "image", "image": img_path},
35 {"type": "text", "text": "检测并识别图片中的文字,将文本坐标格式化输出。"}
36 ]}
37]
38prompt = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
39inputs = {"prompt": prompt, "multi_modal_data": {"image": [img]}}
40output = llm.generate([inputs], sampling_params)[0]
41print(clean_repeated_substrings(output.outputs[0].text))| Task | Prompt |
|---|---|
| Spotting | 检测并识别图片中的文字,将文本坐标格式化输出。 |
| Document Parsing | • 识别图片中的公式,用LaTeX格式表示。 • 把图中的表格解析为HTML。 • 解析图中的图表,对于流程图使用Mermaid格式表示,其他图表使用Markdown格式表示。 • 提取文档图片中正文的所有信息用markdown格式表示,其中页眉、页脚部分忽略,表格用html格式表达,文档中公式用latex格式表示,按照阅读顺序组织进行解析。 |
| General Parsing | • 提取图中的文字。 |
| Information Extraction | • 输出Key的值。 • 提取图片中的: ['key1','key2', ...] 的字段内容,并按照JSON格式返回。 • 提取图中的字幕 |
| Translation | 先提取文字,再将文字内容翻译为英文。若是文档,则其中页眉、页脚忽略。公式用latex格式表示,表格用html格式表示。 |
| Wechat Discussion Group | Discord Group |
|---|---|
![]() | Join HunyuanOCR Discord |
@misc{hunyuanvisionteam2025hunyuanocrtechnicalreport,
title={HunyuanOCR Technical Report},
author={Hunyuan Vision Team and Pengyuan Lyu and Xingyu Wan and Gengluo Li and Shangpin Peng and Weinong Wang and Liang Wu and Huawen Shen and Yu Zhou and Canhui Tang and Qi Yang and Qiming Peng and Bin Luo and Hower Yang and Xinsong Zhang and Jinnian Zhang and Houwen Peng and Hongming Yang and Senhao Xie and Longsha Zhou and Ge Pei and Binghong Wu and Kan Wu and Jieneng Yang and Bochao Wang and Kai Liu and Jianchen Zhu and Jie Jiang and Linus and Han Hu and Chengquan Zhang},
year={2025},
journal={arXiv preprint arXiv:2511.19575},
url={https://arxiv.org/abs/2511.19575},
}