1from unsloth import FastVisionModel
2from PIL import Image
3import torch
4
5# Load model
6model, tokenizer = FastVisionModel.from_pretrained(
7 "AhmedZaky1/DIMI-Arabic-OCR-v2",
8 load_in_4bit=True,
9 device_map="auto"
10)
11FastVisionModel.for_inference(model)
12
13# Load image
14image = Image.open("arabic_document.jpg")
15
16# Prepare prompt
17instruction = "استخرج النص العربي والأرقام الموجودة في هذه الصورة بدقة عالية."
18
19messages = [
20 {
21 "role": "user",
22 "content": [
23 {"type": "image", "image": image},
24 {"type": "text", "text": instruction},
25 ],
26 }
27]
28
29# Apply chat template
30text = tokenizer.apply_chat_template(
31 messages, tokenize=False, add_generation_prompt=True
32)
33
34# Tokenize
35inputs = tokenizer(
36 text=[text],
37 images=[image],
38 padding=True,
39 return_tensors="pt",
40 truncation=False
41).to("cuda")
42
43# Generate
44with torch.inference_mode():
45 outputs = model.generate(
46 **inputs,
47 max_new_tokens=2048,
48 do_sample=False
49 )
50
51# Decode
52generated_ids = [
53 out[len(inp):] for inp, out in zip(inputs.input_ids, outputs)
54]
55prediction = tokenizer.batch_decode(
56 generated_ids,
57 skip_special_tokens=True
58)[0]
59
60print(prediction)
The dataset covers modern standard Arabic with and without diacritics.
1@misc{dimi-arabic-ocr-2025,
2 author = {Ahmed Zaky},
3 title = {DIMI-Arabic-OCR: Fine-tuned Qwen2.5-VL for Arabic Text Recognition},
4 year = {2025},
5 publisher = {Hugging Face},
6 howpublished = {\url{https://huggingface.co/AhmedZaky1/DIMI-Arabic-OCR}}
7}