# 🏆 sherif1313/Arabic-GLM-OCR-v2
A powerful Arabic OCR model (proficient learner)
📌 Overview
This model is an advanced Arabic OCR system designed to combine deep linguistic understanding with high accuracy in visual text extraction.
The model was trained using a unique strategy focused on:
Reducing the model's active capacity during training
Maintaining the stability of visual features
Promoting genuine language understanding rather than rote memorization
🚀 Key Features
🔹 Model size: Approximately 2 GB
🔹 Performance: Outperforms much larger models in most tasks
🔹 Type: Robust learning model (requires fine-tuning for inference)
✅ Deep understanding of Arabic language context
✅ Intelligent spelling correction
✅ High visual accuracy in text extraction
✅ Noise reduction
✅ Highly stable training behavior
✅ Strong generalization on non-visual data
🧪 Evaluation Results
Metric
Value
Evaluation loss 0.1041
Training-evaluation gap 0% - 2.5%
Excellent stability
📌 This indicates near-perfect training equilibrium with minimal overshoot.
🧠 Training Philosophy
- Reduce Training Capacity
The model was trained using only half its capacity in order to:
Preserve visual representations
Prevent image deterioration
Improve overall stability
2. From "Memorizing Shapes" to "Learning Rules"
Instead of:
Memorizing word shapes
The model now learns:
Grammar rules and image-text relationships
- Controlling Inference
The training included:
Reducing excessive inference
Limiting the linking of complex ideas
Reverting processed information to its original size before output
🎯 Objective:
Forcing the model to accurately copy text instead of paraphrasing it
- Multilevel Reasoning Capability
The model was given internal inference capabilities during:
Reading the page
Analyzing the text
Generating output
This leads to:
Better understanding of invisible data
Stronger real-world performance
⚙️ Inference Settings (Very Important)
⚠️ This is a powerful learner ← Requires precise control during inference
🎯 Use Cases
📄 OCR for Arabic books
📰 Text extraction from images
📚 Manuscript digitization
🧾 Document processing
🔍 Text enhancement after OCR
⚠️ Important Notes
The model may attempt autocorrect if not properly constrained.
To accurately copy text, use directives such as:
Extract the text exactly as it is, without correction or paraphrasing.
📦 Why is the model small?
Despite its small size (approximately 2 GB), its outstanding performance is due to:
Effective training methodology
Minimized cognitive noise
Focus on patterns Significant
Highly Efficient Representation Learning
🏁 Conclusion
This model achieves a rare balance between:
Visual Accuracy 👁️
Language Comprehension 🧠
Training Stability ⚖️
💡 It can be considered a sophisticated model for Arabic OCR, competing with larger systems.
| License | Model Size | Python |
|---|
| Apache-2.0 | 2.2GB | 3.12 |
⚠️ Important Notes
In some cases, the model may attempt to correct the text if it is not properly configured.
For exact copying:
Use a clear prompt such as:
"Extract the text as is, without modification"
❌ Do not use high temperature settings → will cause hallucinations.
✅ Use "Restricted" settings for optimal accuracy.
✅ Best suited for OCR tasks, not creative writing.
Send feedback
Press tab for actions
Recommended Settings It includes:
with torch.no_grad():
generated_ids = model.generate(
**inputs,
max_new_tokens=512, # Keep repeating the loop
do_sample=True,
temperature=0.4,
top_p=0.9,
repetition_penalty=1.1
🖼️ Visualizations
🛠️ How to use it
git clone
https://github.com/zai-org/glm-ocr.git
cd glm-ocr
uv venv --python 3.12 --seed && source .venv/bin/activate
uv pip install -e .
1from transformers import AutoProcessor, AutoModelForImageTextToText
2import torch
3
4MODEL_PATH = "sherif1313/Arabic-GLM-OCR-v2"
5messages = [
6 {
7 "role": "user",
8 "content": [
9 {
10 "type": "image",
11 "url": "test_image.png"
12 },
13 {
14 "type": "text",
15 "text": "Text Recognition:"
16 }
17 ],
18 }
19]
20processor = AutoProcessor.from_pretrained(MODEL_PATH)
21model = AutoModelForImageTextToText.from_pretrained(
22 pretrained_model_name_or_path=MODEL_PATH,
23 torch_dtype="auto",
24 device_map="auto",
25)
26inputs = processor.apply_chat_template(
27 messages,
28 tokenize=True,
29 add_generation_prompt=True,
30 return_dict=True,
31 return_tensors="pt"
32).to(model.device)
33inputs.pop("token_type_ids", None)
34generated_ids = model.generate(**inputs, max_new_tokens=2018)
35output_text = processor.decode(generated_ids[0][inputs["input_ids"].shape[1]:], skip_special_tokens=False)
36print(output_text)
🛠️ How to use it web
1
2import gradio as gr
3from transformers import AutoProcessor, AutoModelForImageTextToText
4import torch
5from PIL import Image
6import re
7
8# --- KONFIGURASI MODEL ---
9MODEL_PATH = "sherif1313/Arabic-GLM-OCR-v2"
10
11# Deteksi perangkat secara otomatis
12device = "cuda" if torch.cuda.is_available() else "cpu"
13dtype = torch.float16 if device == "cuda" else torch.float32
14print(f"🚀 Mesin OCR dimulai: Device={device} | Dtype={dtype}")
15
16# --- INISIALISASI MODEL (dengan pengecekan error) ---
17try:
18 print("⏳ Memuat processor...")
19 processor = AutoProcessor.from_pretrained(MODEL_PATH, trust_remote_code=True)
20
21 print("⏳ Memuat model (mungkin butuh waktu beberapa menit)...")
22 model = AutoModelForImageTextToText.from_pretrained(
23 MODEL_PATH,
24 dtype=dtype,
25 trust_remote_code=True,
26 low_cpu_mem_usage=True,
27 device_map="auto"
28 )
29 model.eval()
30 print("✅ Model siap digunakan!")
31except Exception as e:
32 print(f"❌ Gagal memuat model: {e}")
33 raise # Hentikan eksekusi jika model gagal dimuat
34
35# --- DAFTAR GAMBAR CONTOH (pastikan file-file ini ada di folder yang sama dengan skrip) ---
36EXAMPLE_IMAGES = [
37
38]
39
40# --- FUNGSI OCR ---
41import re # تأكد من وجود هذا في أعلى الملف
42
43def proses_intelijen(image):
44 if image is None:
45 return "⚠️ Silakan unggah gambar terlebih dahulu."
46
47 messages = [
48 {
49 "role": "user",
50 "content": [
51 {"type": "image", "image": image},
52 {"type": "text", "text": "Text Recognition:"}
53 ],
54 }
55 ]
56
57 try:
58 # --- معالجة الصورة وتوليد النص (كما هو في كودك الأصلي) ---
59 inputs = processor.apply_chat_template(
60 messages,
61 add_generation_prompt=True,
62 tokenize=True,
63 return_dict=True,
64 return_tensors="pt"
65 ).to(model.device)
66
67 with torch.no_grad():
68 generated_ids = model.generate(
69 **inputs,
70 max_new_tokens=512,
71 do_sample=False
72 )
73
74 hasil = generated_ids[0][len(inputs["input_ids"][0]):]
75 teks_final = processor.decode(hasil, skip_special_tokens=True)
76
77 # ----------------------------------------------------------------
78 # --- منطق التنظيف المتقدم (إزالة التكرار و HTML والنقاط) ---
79 # ----------------------------------------------------------------
80
81 # 1. حذف وسوم HTML القبيحة (مثل <html>, <td>, etc.)
82 teks_final = re.sub(r'<[^>]+>', '', teks_final)
83
84 # 2. حذف التكرار المتتالي للجمل (مهم جداً في حالتك)
85 # هذا السطر يبحث عن أي جملة أو مجموعة كلمات تظهر مرتين أو أكثر متتاليتين
86 # ويستبدلها بمظهر واحد فقط.
87 # (.{10,}?) يعني: التقط نصاً طوله 10 أحرف فأكثر (لتجنب تكرار حروف قصيرة)
88 # (\s+\1)+ يعني: متبوعاً بمسافات ونفس النص السابق مكرراً
89 teks_final = re.sub(r'(\b.{10,}?)(\s+\1)+', r'\1', teks_final)
90
91
92
93 # ----------------------------------------------------------------
94
95 return teks_final
96
97 except Exception as e:
98 return f"🚨 Terjadi kesalahan: {str(e)}"
99
100# --- ANTARMUKA GRADIO ---
101css_custom = """
102.container { max-width: 1200px; margin: auto; padding-top: 20px; }
103h1 { text-align: center; color: #3b82f6; }
104"""
105
106with gr.Blocks(css=css_custom, title="Arabic GLM-OCR") as app:
107 with gr.Column(elem_classes="container"):
108 gr.Markdown("# Arabic GLM-OCR")
109 gr.Markdown("Arabic OCR powered by GLM-OCR.")
110
111 with gr.Row():
112 with gr.Column(scale=1):
113 input_img = gr.Image(type="pil", label="Upload Gambar", height=450)
114 scan_btn = gr.Button("🚀 MULAI SCAN", variant="primary", size="lg")
115
116 with gr.Column(scale=1):
117 output_txt = gr.Textbox(label="Hasil Teks", lines=24)
118
119 # Tambahkan contoh gambar yang bisa diklik
120 gr.Examples(
121 examples=EXAMPLE_IMAGES,
122 inputs=input_img,
123 outputs=output_txt,
124 fn=proses_intelijen,
125 cache_examples=False, # Set ke True jika ingin mempercepat (butuh disk space)
126 label="Contoh Gambar (klik untuk memuat)"
127 )
128
129 # Hubungkan tombol dengan fungsi
130 scan_btn.click(fn=proses_intelijen, inputs=input_img, outputs=output_txt)
131
132if __name__ == "__main__":
133 app.launch() demo.queue().launch(theme=gr.themes.Soft(), allowed_paths=["examples"])