# IMPORTANT: Import unsloth first!import unslothfrom unsloth import FastVisionModelfrom PIL import Imageimport torch # Load the modelmodel, tokenizer = FastVisionModel.from_pretrained( "AhmedZaky1/DIMI-Arabic-OCR", load_in_4bit=True, use_gradient_checkpointing="unsloth",) FastVisionModel.for_inference(model) # Prepare your imageimage = Image.open("/content/2.jpg") # Arabic instructioninstruction = "استخرج النص العربي والأرقام الموجودة في هذه الصورة بدقة عالية جدًا، مع الحفاظ الكامل على الترتيب الأصلي والتنسيق." # Prepare messagesmessages = [ {"role": "user", "content": [ {"type": "image", "image": image}, # Include image here {"type": "text", "text": instruction} ]}] # Apply chat templateinput_text = tokenizer.apply_chat_template( messages, add_generation_prompt=True,) # Tokenize with proper parameters to avoid truncationinputs = tokenizer( text=input_text, images=image, return_tensors="pt", padding=True, truncation=False, max_length=None, ).to("cuda") # Generatewith torch.no_grad(): outputs = model.generate( **inputs, max_new_tokens=2048, do_sample=False, temperature=None, top_p=None, pad_token_id=tokenizer.pad_token_id, eos_token_id=tokenizer.eos_token_id, ) # Decode the predictiongenerated_ids = outputs[0][inputs['input_ids'].shape[1]:]prediction = tokenizer.decode(generated_ids, skip_special_tokens=True).strip() print("Extracted Arabic Text:")print(prediction)