Views
No views yet
sapkotapraful/FullyOCR-2-mergedpip install torch transformers pillow1import torch
2from PIL import Image
3from transformers import AutoTokenizer, AutoProcessor, AutoModelForImageTextToText
4
5MODEL_ID = "sapkotapraful/FullyOCR-2-merged"
6
7device = "cuda" if torch.cuda.is_available() else "cpu"
8dtype = torch.bfloat16 if device == "cuda" and torch.cuda.is_bf16_supported() else (
9 torch.float16 if device == "cuda" else torch.float32
10)
11
12# Load model
13model = AutoModelForImageTextToText.from_pretrained(
14 MODEL_ID,
15 torch_dtype=dtype,
16 device_map="auto" if device == "cuda" else None,
17 trust_remote_code=True,
18)
19model.eval()
20
21# Load tokenizer + processor
22tokenizer = AutoTokenizer.from_pretrained(MODEL_ID, trust_remote_code=True)
23processor = AutoProcessor.from_pretrained(MODEL_ID, trust_remote_code=True)
24
25# Load image
26image = Image.open("document.png").convert("RGB")
27
28instruction = "<|MD|>"
29
30messages = [
31 {
32 "role": "user",
33 "content": [
34 {"type": "image"},
35 {"type": "text", "text": instruction},
36 ],
37 }
38]
39
40input_text = processor.apply_chat_template(
41 messages,
42 tokenize=False,
43 add_generation_prompt=True,
44)
45
46inputs = processor(
47 images=image,
48 text=input_text,
49 return_tensors="pt",
50)
51
52inputs = {
53 k: v.to(model.device) if hasattr(v, "to") else v
54 for k, v in inputs.items()
55}
56
57with torch.no_grad():
58 output_ids = model.generate(
59 **inputs,
60 max_new_tokens=1024,
61 do_sample=False,
62 num_beams=1,
63 use_cache=True,
64 pad_token_id=tokenizer.pad_token_id,
65 )
66
67decoded = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]
68extracted = decoded.split(instruction)[-1].strip()
69
70print(extracted)1@model{fullyocr2,
2 title = {FullyOCR-2: Multimodal Document OCR Model},
3 author = {Praful Sapkota},
4 year = {2026},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/sapkotapraful/FullyOCR-2-merged}
7}