import torchfrom PIL import Imagefrom peft import PeftModelfrom transformers import AutoProcessor, Qwen2_5_VLForConditionalGeneration base_id = "allenai/olmOCR-2-7B-1025"lora_id = "larshiakzemil/olmocr-farsi-lora"device = "cuda" if torch.cuda.is_available() else "cpu"dtype = torch.bfloat16 if device == "cuda" else torch.float32 processor = AutoProcessor.from_pretrained(base_id, trust_remote_code=True)model = Qwen2_5_VLForConditionalGeneration.from_pretrained( base_id, torch_dtype=dtype, device_map=device, trust_remote_code=True)model = PeftModel.from_pretrained(model, lora_id)model.eval() image = Image.open("page.jpg").convert("RGB")prompt = ( "Attached is one page of a document that you must process. " "Just return the plain text representation of this document as if you were reading it naturally. " "Convert equations to LateX and tables to HTML.\n" "Return your output as markdown, with a front matter section on top specifying values for the " "primary_language, is_rotation_valid, rotation_correction, is_table, and is_diagram parameters.")messages = [{ "role": "user", "content": [ {"type": "image", "image": image}, {"type": "text", "text": prompt}, ],}]text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)inputs = processor(text=[text], images=[image], return_tensors="pt")inputs = {k: v.to(device) if hasattr(v, "to") else v for k, v in inputs.items()} with torch.inference_mode(): out = model.generate(**inputs, max_new_tokens=512, do_sample=False)print(processor.decode(out[0], skip_special_tokens=True))