Try it
Use Python 3.10 or newer. A GPU is recommended for faster recognition; CPU inference is also supported.
Download the model, install its dependencies, and recognize the included example image:
python -m pip install huggingface_hubhf download Yesianrohn/YesOCR-0.8B --local-dir ./YesOCR-0.8Bcd YesOCR-0.8Bpython -m pip install -r requirements.txt python inference.py --model . --image example.png --output result.txt

The recognized text is printed in the terminal and saved to result.txt:
YesOCR demoOrder: A1024Total: 128.50
Recognize your own images
Replace your-image.png with the path to your image:
python inference.py --model . --image your-image.png --output result.txt
The same command works for Chinese, English, and mixed-language images. There is no need to select
a language. The result is a transcription of the original text.
The script automatically uses a GPU when one is available. Add --device cpu to run on CPU.
Once the model and dependencies are downloaded, --model . lets you use the local files offline.
You can also load Yesianrohn/YesOCR-0.8B directly in Python.
The example below uses a CUDA GPU with BF16 support. Replace example.png with your image path;
keep the provided OCR prompt for both Chinese and English images.
import torchfrom PIL import Image, ImageOps, PngImagePluginfrom transformers import AutoModelForImageTextToText, AutoProcessor MODEL_ID = "Yesianrohn/YesOCR-0.8B" # Allow PNGs with large compressed color profiles.PngImagePlugin.MAX_TEXT_CHUNK = 64 * 1024 * 1024PngImagePlugin.MAX_TEXT_MEMORY = 256 * 1024 * 1024 processor = AutoProcessor.from_pretrained(MODEL_ID)model = AutoModelForImageTextToText.from_pretrained( MODEL_ID, dtype=torch.bfloat16, attn_implementation="sdpa",).to("cuda").eval() with Image.open("example.png") as source: image = ImageOps.exif_transpose(source).convert("RGB") prompt = ( "请按阅读顺序转录图片中的全部可辨认文字,保留原文和换行,文本块之间空一行。" "仅输出转录结果。")messages = [{ "role": "user", "content": [ {"type": "image", "image": image}, {"type": "text", "text": "\n" + prompt}, ],}] text = processor.apply_chat_template( messages, tokenize=False, add_generation_prompt=True, enable_thinking=False,)inputs = processor(text=[text], images=[image], return_tensors="pt").to(model.device) with torch.inference_mode(): output_ids = model.generate( **inputs, max_new_tokens=4096, do_sample=False, num_beams=1, use_cache=True, ) # Decode only the newly generated tokens.new_tokens = output_ids[:, inputs["input_ids"].shape[1]:]result = processor.batch_decode(new_tokens, skip_special_tokens=True)[0].strip()image.close()print(result)
For CPU inference, replace dtype=torch.bfloat16 with dtype=torch.float32
and .to("cuda") with .to("cpu").
Tips
- Use clear images with readable text. For dense or long pages, try cropping the page into sections in reading order.
- If a long transcription stops early, add
--max-new-tokens 8192.
- If GPU memory is insufficient, try
--max-pixels 1000000.
- Output is plain text. Complex tables, columns, and page layouts may need manual formatting.
License and credits
Published by Yesianrohn under the Apache License 2.0.
Thanks to the Qwen team for the base model. See NOTICE for attribution.