Downloads · 30 days
40
0% of all-time downloads
syntheticbot/ocr-qwen
ocr-qwen is a machine learning model from syntheticbot. Use it for the machine learning task on the model card, and read the license before you ship it in a product. The card lists the license as apache-2.0.
syntheticbot/ocr-qwen is a fine-tuned model for Optical Character Recognition (OCR) tasks, derived from the base model Qwen/Qwen2.5-VL-7B-Instruct. This model is engineered for high accuracy in extracting text from im…
Downloads · 30 days
40
0% of all-time downloads
All-time downloads
10.6K
Public
Parameters
8.3B
16.6 GB on disk
Likes
12
Public
Click a slice to open those files.
.safetensors16.6 GB · 100%
From the Hugging Face model README
syntheticbot/ocr-qwen is a fine-tuned model for Optical Character Recognition (OCR) tasks, derived from the base model Qwen/Qwen2.5-VL-7B-Instruct. This model is engineered for high accuracy in extracting text from images, including documents and scenes containing text.
This repository provides the instruction-tuned and OCR-optimized 7B Qwen-VL-7B-ocr model. For comprehensive details about the foundational model architecture, please refer to the Qwen/Qwen2.5-VL-7B-Instruct repository, as well as the Blog and GitHub pages for Qwen2.5-VL.
For optimal performance and access to OCR-specific features, it is recommended to build from source:
pip install git+https://github.com/huggingface/transformers accelerate
The following examples illustrate the use of syntheticbot/ocr-qwen with 🤗 Transformers and qwen_vl_utils for OCR applications.
pip install git+https://github.com/huggingface/transformers accelerate
Install the toolkit for streamlined visual input processing:
pip install qwen-vl-utils[decord]==0.0.8
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
from qwen_vl_utils import process_vision_info
import torch
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
"syntheticbot/ocr-qwen",
torch_dtype="auto",
device_map="auto"
)
processor = AutoProcessor.from_pretrained("syntheticbot/ocr-qwen")
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "path/to/your/document_image.jpg",
},
{"type": "text", "text": "Extract the text from this image."},
],
}
]
text = processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
text=[text],
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors="pt",
)
inputs = inputs.to("cuda" if torch.cuda.is_available() else "cpu")
generated_ids = model.generate(**inputs, max_new_tokens=512)
generated_ids_trimmed = [
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print("Extracted Text:", output_text[0])
<details>
<summary>Example for Structured Output (JSON for Table Extraction)</summary>
from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
from qwen_vl_utils import process_vision_info
import torch
import json
model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
"syntheticbot/ocr-qwen",
torch_dtype="auto",
device_map="auto"
)
processor = AutoProcessor.from_pretrained("syntheticbot/ocr-qwen")
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "path/to/your/table_image.jpg",
},
{"type": "text", "text": "Extract the table from this image and output it as JSON."},
],
}
]
text = processor.apply_chat_template(
messages, tokenize=False, add_generation_prompt=True
)
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
text=[text],
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors="pt",
)
inputs = inputs.to("cuda" if torch.cuda.is_available() else "cpu")
generated_ids = model.generate(**inputs, max_new_tokens=1024)
generated_ids_trimmed = [
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_text = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print("Extracted Table (JSON):\n", output_text[0])
try:
json_output = json.loads(output_text[0])
print("\nParsed JSON Output:\n", json.dumps(json_output, indent=2))
except json.JSONDecodeError:
print("\nCould not parse output as JSON. Output is plain text.")
</details>
<details>
<summary>Batch inference for OCR</summary>
messages1 = [
{
"role": "user",
"content": [
{"type": "image", "image": "path/to/image1.jpg"},
{"type": "text", "text": "Extract text from this image."},
],
}
]
messages2 = [
{
"role": "user",
"content": [
{"type": "image", "image": "path/to/image2.jpg"},
{"type": "text", "text": "Read the text in this document."},
],
}
]
messages = [messages1, messages2]
texts = [
processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
for msg in messages
]
image_inputs, video_inputs = process_vision_info(messages)
inputs = processor(
text=texts,
images=image_inputs,
videos=video_inputs,
padding=True,
return_tensors="pt",
)
inputs = inputs.to("cuda" if torch.cuda.is_available() else "cpu")
generated_ids = model.generate(**inputs, max_new_tokens=512)
generated_ids_trimmed = [
out_ids[len(in_ids) :] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
]
output_texts = processor.batch_decode(
generated_ids_trimmed, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print("Extracted Texts (Batch):\n", output_texts)
</details>
For users in mainland China, ModelScope is recommended. Use snapshot_download for checkpoint management. Adapt model names to syntheticbot/ocr-qwen in ModelScope implementations.
Input images support local files, URLs, and base64 encoding.
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "http://path/to/your/document_image.jpg"
},
{
"type": "text",
"text": "Extract the text from this image URL."
},
],
}
]
Higher resolution images typically improve OCR accuracy, especially for small text. Adjust resolution using min_pixels, max_pixels, resized_height, and resized_width parameters with the processor.
min_pixels = 512 * 28 * 28
max_pixels = 2048 * 28 * 28
processor = AutoProcessor.from_pretrained(
"syntheticbot/ocr-qwen",
min_pixels=min_pixels, max_pixels=max_pixels
)
Control resizing dimensions directly:
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "file:///path/to/your/document_image.jpg",
"resized_height": 600,
"resized_width": 800,
},
{"type": "text", "text": "Extract the text."},
],
}
]
@misc{qwen2.5-VL,
title = {Qwen2.5-VL},
url = {https://qwenlm.github.io/blog/qwen2.5-vl/},
author = {Qwen Team},
month = {January},
year = {2025}
}
@article{Qwen2VL,
title={Qwen2-VL: Enhancing Vision-Language Model's Perception of the World at Any Resolution},
author={Wang, Peng and Bai, Shuai and Tan, Sinan and Wang, Shijie and Fan, Zhihao and Bai, Jinze and Chen, Keqin and Liu, Xuejing and Wang, Jialin and Ge, Wenbin and Fan, Yang and Dang, Kai and Du, Mengfei and Ren, Xuancheng and Men, Rui and Liu, Dayiheng and Zhou, Chang and Zhou, Jingren and Lin, Junyang},
journal={arXiv preprint arXiv:2409.12191},
year={2024}
}
@article{Qwen-VL,
title={Qwen-VL: A Versatile Vision-Language Model for Understanding, Localization, Text Reading, and Beyond},
author={Bai, Jinze and Bai, Shuai and Yang, Shusheng and Wang, Shijie and Tan, Sinan and Wang, Peng and Lin, Junyang and Zhou, Chang and Zhou, Jingren},
journal={arXiv preprint arXiv:2308.12966},
year={2023}
}