语言:
- 英文
许可证: apache-2.0
数据集:
- allenai/olmOCR-mix-0225
基础模型:
- Qwen/Qwen2-VL-7B-Instruct
库名称: transformers

olmOCR-7B-0225预览版
本模型是基于Qwen2-VL-7B-Instruct微调的olmOCR预览版本,训练数据来自olmOCR-mix-0225数据集。
快速链接:
建议通过olmOCR工具包使用本模型。该工具包集成了基于sglang的高效推理框架,可支持海量文档处理。
使用说明
输入要求为单页文档图像,最长边需调整为1024像素。提示词需包含文档元数据,推荐使用olmOCR工具包自动生成。
手动提示构建
若需手动构建提示词而非使用工具包,请参考以下代码示例:
pip install olmocr
import torch
import base64
import urllib.request
from io import BytesIO
from PIL import Image
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
from olmocr.data.renderpdf import render_pdf_to_base64png
from olmocr.prompts import build_finetuning_prompt
from olmocr.prompts.anchor import get_anchor_text
model = Qwen2VLForConditionalGeneration.from_pretrained("allenai/olmOCR-7B-0225-preview", torch_dtype=torch.bfloat16).eval()
processor = AutoProcessor.from_pretrained("Qwen/Qwen2-VL-7B-Instruct")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
urllib.request.urlretrieve("https://molmo.allenai.org/paper.pdf", "./paper.pdf")
image_base64 = render_pdf_to_base64png("./paper.pdf", 1, target_longest_image_dim=1024)
anchor_text = get_anchor_text("./paper.pdf", 1, pdf_engine="pdfreport", target_length=4000)
prompt = build_finetuning_prompt(anchor_text)
messages = [{
"role": "user",
"content": [
{"type": "text", "text": prompt},
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_base64}"}},
],
}]
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
main_image = Image.open(BytesIO(base64.b64decode(image_base64)))
inputs = processor(
text=[text],
images=[main_image],
padding=True,
return_tensors="pt",
)
inputs = {key: value.to(device) for (key, value) in inputs.items()}
output = model.generate(
**inputs,
temperature=0.8,
max_new_tokens=50,
num_return_sequences=1,
do_sample=True,
)
prompt_length = inputs["input_ids"].shape[1]
new_tokens = output[:, prompt_length:]
text_output = processor.tokenizer.batch_decode(new_tokens, skip_special_tokens=True)
print(text_output)
许可与用途
olmOCR采用Apache 2.0许可证,仅限研究与教育用途。更多信息请参阅负责任使用指南。