Views
No views yet
<|MD|>bitsandbytes to run on GPUs with limited VRAM (e.g., 8GB - 12GB).<|MD|> token to trigger the OCR extraction flow.1pip install unsloth torch pillow
21from unsloth import FastVisionModel
2import torch
3from PIL import Image
4
5# 1. Load model + tokenizer
6model, tokenizer = FastVisionModel.from_pretrained(
7 "sapkotapraful/FullyOCR-2",
8 load_in_4bit=True,
9)
10
11# 2. Setup Device
12image = Image.open('document.jpg')
13model.eval()
14device = "cuda" if torch.cuda.is_available() else "cpu"
15if device == "cuda":
16 model = model.to(device)
17
18# 3. Prepare Prompt
19instruction = "<|MD|>"
20messages = [
21 {"role": "user", "content": [
22 {"type": "image"},
23 {"type": "text", "text": instruction}
24 ]}
25]
26
27input_text = tokenizer.apply_chat_template(messages, add_generation_prompt=True)
28
29# 4. Generate
30inputs = tokenizer(
31 image,
32 input_text,
33 add_special_tokens=False,
34 return_tensors="pt",
35).to(device)
36
37with torch.no_grad(), torch.amp.autocast(device_type="cuda", enabled=(device=="cuda")):
38 output_ids = model.generate(
39 **inputs,
40 max_new_tokens=1024,
41 use_cache=True,
42 num_beams=1,
43 do_sample=False,
44 pad_token_id=tokenizer.pad_token_id,
45 )
46
47# 5. Extract Markdown
48decoded = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]
49extracted = decoded.split(instruction)[-1].strip()
50print(extracted)
51