Views
No views yet

| Model | ArXiv | Old Scans Math | Tables | Old Scans | Headers and Footers | Multi column | Long tiny text | Base | Overall |
|---|---|---|---|---|---|---|---|---|---|
| olmOCR pipeline v0.4.0 with olmOCR-2-7B-1025 | 82.9 | 82.1 | 84.3 | 48.3 | 95.7 | 84.3 | 81.4 | 99.7 | 82.3 ± 1.1 |
| olmOCR pipeline v0.4.0 with olmOCR-2-7B-1025-FP8 | 83.0 | 82.3 | 84.9 | 47.7 | 96.1 | 83.7 | 81.9 | 99.7 | 82.4 ± 1.1 |
pip install olmocr>=0.4.01import torch
2import base64
3import urllib.request
4
5from io import BytesIO
6from PIL import Image
7from transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration
8
9from olmocr.data.renderpdf import render_pdf_to_base64png
10from olmocr.prompts import build_no_anchoring_v4_yaml_prompt
11
12# Initialize the model
13model = Qwen2_5_VLForConditionalGeneration.from_pretrained("allenai/olmOCR-2-7B-1025-FP8", device_map="auto").eval()
14processor = AutoProcessor.from_pretrained("Qwen/Qwen2.5-VL-7B-Instruct")
15device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16model.to(device)
17
18# Grab a sample PDF
19urllib.request.urlretrieve("https://olmocr.allenai.org/papers/olmocr.pdf", "./paper.pdf")
20
21# Render page 1 to an image
22image_base64 = render_pdf_to_base64png("./paper.pdf", 1, target_longest_image_dim=1288)
23
24
25# Build the full prompt
26messages = [
27 {
28 "role": "user",
29 "content": [
30 {"type": "text", "text": build_no_anchoring_v4_yaml_prompt()},
31 {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{image_base64}"}},
32 ],
33 }
34 ]
35
36# Apply the chat template and processor
37text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
38main_image = Image.open(BytesIO(base64.b64decode(image_base64)))
39
40inputs = processor(
41 text=[text],
42 images=[main_image],
43 padding=True,
44 return_tensors="pt",
45)
46inputs = {key: value.to(device) for (key, value) in inputs.items()}
47
48
49# Generate the output
50output = model.generate(
51 **inputs,
52 temperature=0.1,
53 max_new_tokens=50,
54 num_return_sequences=1,
55 do_sample=True,
56 )
57
58# Decode the output
59prompt_length = inputs["input_ids"].shape[1]
60new_tokens = output[:, prompt_length:]
61text_output = processor.tokenizer.batch_decode(
62 new_tokens, skip_special_tokens=True
63)
64
65print(text_output)
66# ['---\nprimary_language: en\nis_rotation_valid: True\nrotation_correction: 0\nis_table: False\nis_diagram: False\n---\nolmOCR: Unlocking Trillions of Tokens in PDFs with Vision Language Models\n\nJake Poz']