A multimodal vision-language model specialized in extracting structured metadata from scanned Arabic legal documents, including government regulations, official correspondence, and institutional records.
Arabic Legal Documents OCR Parser is a multimodal vision-language model fine-tuned from Google's Gemma-3-4B-IT for the task of structured metadata extraction from scanned Arabic legal documents. Given an image of a legal document page, the model outputs a comprehensive JSON object containing classified document metadata — including document type, issuing authority, physical properties, official seals/stamps, signatures, routing information, and quality assessments.
The model was fine-tuned using LoRA (Low-Rank Adaptation) via LLaMA-Factory. After training, the LoRA adapter was merged back into the base model — this repository contains the full-precision merged weights, ready for direct inference without any adapter loading or quantization.
Metadata extraction — Extracting seals, stamps, signatures, dates, reference numbers, and routing information from scanned pages.
Quality assessment — Evaluating scan quality, completeness, and legibility for downstream archival processes.
Downstream Use
The model can be integrated into larger systems:
Document management systems (DMS) — As a preprocessing component that auto-populates metadata fields.
Legal research platforms — To index and search across large corpora of Arabic legal documents.
Government digitization initiatives — For bulk processing of ministerial and regulatory archives.
RAG pipelines — As a structured extraction layer feeding into retrieval-augmented generation systems.
Out-of-Scope Use
This model is NOT intended for:
❌ Full-text OCR — The model extracts structured metadata, not the full body text of documents.
❌ Legal advice or interpretation — The model classifies and extracts metadata; it does not provide legal analysis.
❌ Non-Arabic documents — While the model understands English prompts, it is specialized for Arabic legal documents.
❌ Handwritten text recognition — The model is trained on printed/typed scanned documents; handwriting extraction is not supported.
❌ Real-time or safety-critical applications — The model may produce incorrect extractions and should not be used where errors have significant legal consequences without human review.
How to Get Started with the Model
Using Transformers
python
1from transformers import AutoProcessor, AutoModelForImageTextToText
2from PIL import Image
3import torch
4import json
56# Load the merged model directly — no adapter loading needed7model_id ="MohamedSamyAI/arabic-legal-documents-ocr-parser-1.0"8processor = AutoProcessor.from_pretrained(model_id)9model = AutoModelForImageTextToText.from_pretrained(10 model_id,11 torch_dtype=torch.float16,12 device_map="auto",13)1415# Load your document image16image = Image.open("path/to/legal_document.jpg")1718# Prepare the prompt19prompt ="""<image>You are a professional OCR Details Extractor.
20Your rule to extract the: document_classification, source, physical_properties, official_marks, signatures_authorization, routing_distribution, attachments_references, condition_notes and confidence_quality of the document.
21Extract the final output into a json format.
22Do not generate any introduction or conclusion."""2324messages =[25{"role":"user","content":[26{"type":"image"},27{"type":"text","text": prompt}28]}29]3031# Generate32inputs = processor.apply_chat_template(messages, return_tensors="pt").to(model.device)33outputs = model.generate(**inputs, max_new_tokens=2048)34result = processor.decode(outputs[0], skip_special_tokens=True)3536# Parse the JSON output37parsed = json.loads(result)38print(json.dumps(parsed, indent=2, ensure_ascii=False))
💡 Note: This is a fully merged model — you load it directly like any other model. No adapter paths or LoRA configuration required.
Annotations: Each document image is paired with a comprehensive JSON annotation covering 9 extraction categories.
Split
Description
Train
Primary training set with diverse legal document types
Validation
Held-out set for monitoring overfitting during training
Training Procedure
The model was fine-tuned using Supervised Fine-Tuning (SFT) with QLoRA (4-bit quantized LoRA). After training, the LoRA adapter was merged back into the base model to produce the full merged weights published in this repository.
Domain specificity: The model is trained exclusively on Arabic legal documents. Performance on other document types (medical, financial, academic) is not guaranteed.
Language bias: While the model understands English prompts, extracted content is predominantly Arabic. Multi-language documents may have reduced accuracy.
Scan quality sensitivity: Low-resolution, heavily damaged, or poorly scanned documents may produce incomplete or inaccurate extractions.
Hallucination risk: Like all generative models, this model may occasionally produce plausible-sounding but incorrect metadata fields.
Date/number extraction: Calendar conversions (Hijri ↔ Gregorian) and number extraction from degraded scans remain challenging.
Risks
Over-reliance: Users should not treat model outputs as ground truth for legal proceedings without human verification.
Privacy: The training data contains scanned government documents. Users should ensure compliance with applicable data protection regulations when deploying this model.
Recommendations
✅ Always include human-in-the-loop verification for critical metadata fields.
✅ Monitor the confidence_quality field in model outputs to flag uncertain extractions.
✅ Use the requires_manual_review flag to route low-confidence documents to human reviewers.
✅ Fine-tune further on your specific document corpus for best results.