Instructions to use tencent/HunyuanOCR with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use tencent/HunyuanOCR with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="tencent/HunyuanOCR") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoModelForSeq2SeqLM model = AutoModelForSeq2SeqLM.from_pretrained("tencent/HunyuanOCR", dtype="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps
- vLLM
How to use tencent/HunyuanOCR with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "tencent/HunyuanOCR" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tencent/HunyuanOCR", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/tencent/HunyuanOCR
- SGLang
How to use tencent/HunyuanOCR with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "tencent/HunyuanOCR" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tencent/HunyuanOCR", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "tencent/HunyuanOCR" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "tencent/HunyuanOCR", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use tencent/HunyuanOCR with Docker Model Runner:
docker model run hf.co/tencent/HunyuanOCR
Questions related to inputs and outputs
When we print text after the processor.apply_chat_template() it gives something like:
['<|hy_begin▁of▁sentence|><|hy_place▁holder▁no▁3|><|hy_place▁holder▁no▁100|><|hy_place▁holder▁no▁102|><|hy_place▁holder▁no▁101|>Detect and recognize text in the image, and output the text coordinates in a formatted manner.<|hy_User|>']
What is the purpose of "<|hy_place▁holder▁no▁3|>" ?
it is not documented anywhere.
Also we would expect "<|hy_User|>" before the prompt (eg just after BOS), so why is it at the end of the prompt where we would expect the assistant token?
Now for the output, you use skip_special_tokens=False, which eliminates a bunch of special tokens:
<|hy_place▁holder▁no▁110|>
<|hy_place▁holder▁no▁111|>
<|hy_place▁holder▁no▁112|>
<|hy_place▁holder▁no▁113|>
I don't see anything in the doc or code to explain their meaning.
Thanks for the feedback.
Thanks for the feedback. You may find it in the chat template that "<|hy_place▁holder▁no▁3|>" serves as the separator between system prompt and user prompt. In this sepcific model we put system prompt as blank. And "100", "101", "102" serve as image start, end, image itself repectively.
For the "meaningless" special tokens, we are considering putting the actual meaning in the tokenizer in the next update. They might seem useless in HunyuanOCR, but useful in other Hunyuan Series models.
br
ALSO:
is it on purpose that in the chattemplate (in tokenizer_config.json):
"<|hy_User|>" comes AFTER the content of the user
eos_token is "<|hy_Assistant|>"
Pad token is supposed to be "<|hy_▁pad▁|>" = 120002
but there is also 120817 (-1)
finally I am seeing nowhere im_newline_id , is it actually used ?
Obviously, looking at tokenizer_config.json, this "<|hy_Assistant|>" token seems to be used as eos_token as well. If this is the case this needs to be part of the config.json so that th emodel understand both the regular eos_token and this one as eos_tokens.