🚀 olmOCR-7B-0225-preview
這是 olmOCR 模型的預覽版本,該模型基於 olmOCR-mix-0225 數據集對 Qwen2-VL-7B-Instruct 進行微調得到。
快速鏈接:
使用此模型的最佳方式是通過 olmOCR 工具包。該工具包通過 sglang 提供了高效的推理設置,能夠大規模處理數百萬個文檔。
🚀 快速開始
此模型期望輸入為單個文檔圖像,且圖像最長邊需渲染為 1024 像素。
提示信息必須包含文檔的額外元數據,生成這些元數據最簡單的方法是使用 olmOCR 工具包 提供的方法。
✨ 主要特性
- 基於 Qwen2-VL-7B-Instruct 微調,結合特定數據集提升性能。
- 藉助 olmOCR 工具包可高效處理大規模文檔。
📦 安裝指南
若要手動使用該模型,需先安裝 olmOCR:
pip install olmocr
💻 使用示例
基礎用法
若不使用 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 許可證。該模型僅供研究和教育使用。更多信息請參閱我們的 負責任使用指南。