Downloads ยท 30 days
57
8% of all-time downloads
sherif1313/3arabLM-4B-islamic-v2
3arabLM-4B-islamic-v2 is a text generation model from sherif1313. Use it when you need the model to write or continue text. The card lists the license as apache-2.0.
<p align="center" <img src="pic.jpg" width="400"/ <p
Downloads ยท 30 days
57
8% of all-time downloads
All-time downloads
730
Public
Parameters
4.2B
8.4 GB on disk
Likes
2
Public
Click a slice to open those files.
.safetensors8.4 GB ยท 100%
From the Hugging Face model README
A Specialized Arabic Language Model for Islamic Heritage
3arabLM-4B-Islamic-v2 is an ongoing research model dedicated to learning, preserving, recalling, and reconstructing the Arabic Islamic scholarly heritage from classical and authoritative sources. Unlike general-purpose conversational LLMs, the primary objective of 3arabLM is not to imitate everyday conversations or produce short modern summaries. Instead, the project investigates whether a language model can function as a compressed digital library of classical Islamic scholarship, with substantial scholarly knowledge encoded directly into its parameters.
Learn from the books. Preserve the language. Preserve the methodology. Preserve the diversity.
โ ๏ธ **The current model represents less than 5% of the planned continual pretraining schedule. The full project is expected to expand over multiple stages covering nearly the complete Al-Maktaba Al-Shamela ecosystem. .
3arabLM is a long-term research project focused on building a large-scale Arabic language model specialized in the Islamic scholarly heritage. The long-term objective is to gradually expand the model across a broad range of Islamic and Arabic sciences rather than limiting it to a single discipline.
The current release has been continued-pretrained on six scholarly domains from the Shamela corpus:
| Domain | Description |
|---|---|
| ๐ Fiqh | Islamic Jurisprudence |
| ๐ Tafsir | Quranic Exegesis |
| ๐ Hadith | Prophetic Traditions and Hadith Sciences |
| ๐ Aqeedah | Islamic Creed and Theology |
| โ๏ธ Nahw and Sarf | Arabic Grammar and Morphology |
| โ๏ธ Fatwas | Legal Opinions and Verdicts |
Future releases will progressively expand the corpus with additional collections, commentaries, manuscripts, scholarly editions, and specialized literature across Hadith, Tafsir, Fiqh, Arabic linguistics, history, biography, literature, and related fields.
The central philosophy of 3arabLM can be summarized as:
Large Language Models as Compressed Digital Libraries
Instead of treating a language model primarily as a text generator, this project investigates whether model parameters can encode substantial amounts of classical scholarly knowledge.
The project therefore explores a different paradigm:
Retrieval from Weights
rather than relying exclusively on:
Retrieval-Augmented Generation (RAG)
The objective is not to eliminate retrieval systems, but to investigate how much scholarly knowledge can be learned and reconstructed directly from the model's internal parameters.
3arabLM follows a source-oriented training philosophy.
Instead of maximizing general internet-scale diversity, the project focuses on carefully curated scholarly literature.
The model is trained to learn:
The objective is to preserve the diversity of Islamic scholarship rather than collapsing it into a single generalized representation.
A central objective of this project is to preserve differences between scholars, books, schools, and methodologies.
For example:
The model is intended to learn that different scholars may have:
Scholarly disagreement is therefore treated as part of the knowledge itself rather than as noise to be removed.
One of the main objectives of 3arabLM is to preserve the linguistic characteristics of classical Arabic scholarship.
The model is designed to learn:
The project investigates whether continual pretraining on large scholarly corpora can produce models capable of reconstructing texts in language close to their original scholarly style.
A major foundation of the project is Al-Maktaba Al-Shamela, together with other Arabic scholarly and heritage sources.
The current and developing corpus covers a broad range of Arabic and Islamic scholarship, including:
๐ Tafsir & Quranic Sciences ๐ Hadith Sciences & Hadith Literature โ๏ธ Fiqh & Usul al-Fiqh ๐ง Aqeedah & Islamic Theology ๐ Sirah & Prophetic Biography ๐ Islamic History & Civilization ๐ค Biography, Tabaqat & Rijal ๐ Arabic Language, Grammar & Morphology ๐ค Lexicography & Dictionaries ๐ Classical Literature & Poetry ๐ฏ Spiritual & Ethical Literature ๐ Scholarly Research, Bibliographies & Catalogs The corpus is continuously expanding to provide broader coverage of the classical Arabic scholarly tradition and its diverse textual genres. Official Library: goldenshamela
The corpus is transformed through a dedicated extraction and preprocessing pipeline.
Shamela
โ
โโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโ
โ โ
Books Indices
โ โ
โผ โผ
Text Parser Keyword Parser
โ โ
โโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโ
โผ
Unified Page Builder
โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โผ โผ โผ
Metadata TOC Keywords
โ
โผ
pages.parquet
โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โผ โผ โผ
CPT SFT RAG
โ
Book โ Chapter โ Section โ Paragraph โ Sentence
The preprocessing pipeline aims to preserve the hierarchical structure of scholarly books:
This structure provides contextual information beyond plain text.
Future training stages will increasingly incorporate structured metadata.
Planned metadata includes:
| Field | Description |
|---|---|
| Book ID | Unique book identifier |
| Author ID | Author identifier |
| Century | Historical period |
| Madhhab | Jurisprudential school |
| Book Hierarchy | Book โ Chapter โ Section |
| Discipline | Scholarly domain |
| Semantic Tags | Domain-specific information |
The objective is to strengthen the model's understanding of scholarly context and relationships between texts.
Initial experiments suggest that the model is developing distinguishable internal representations across Islamic scholarly domains.
| Metric | Layer 0 | Layer 31 |
|---|---|---|
| Linear Probe Accuracy | 53.80% | 82.78% |
| Macro-F1 | 0.537 | 0.824 |
| Average Domain CKA | 0.0542 | 0.0259 |
| Stable Rank | 10.59 | 5.76 |
These results indicate that:
Modern LLM research frequently relies on Retrieval-Augmented Generation (RAG).
A traditional RAG pipeline can be represented as:
User Question
โ
Retriever
โ
External Knowledge Base
โ
Relevant Documents
โ
Language Model
โ
Answer
3arabLM investigates a complementary direction:
User Question
โ
Language Model
โ
Internal Parameters
โ
Knowledge Recall
โ
Reconstruction
The central research question is:
Can a sufficiently trained language model become a compressed scholarly library?
This project does not claim that external retrieval is unnecessary. Instead, it investigates the capacity of model parameters to store and reconstruct specialized scholarly knowledge.
The long-term vision is based on a shared foundation model followed by specialized expert models.
3arabLM Foundation
โ
โโโโโโโโโโโฌโโโโโโโโผโโโโโโโโฌโโโโโโโโโโ
โผ โผ โผ โผ โผ
Fiqh Hadith Tafsir Aqeedah History
Expert Expert Expert Expert Expert
โ โ โ โ โ
โโโโโโโโโโโดโโโโโโโโผโโโโโโโโดโโโโโโโโโโ
โผ
Islamic Heritage
Expert Ecosystem
Potential future experts include:
Future research will investigate ways to increase model capacity while preserving previously learned knowledge.
Potential directions include:
The goal is to increase memorization capacity while maintaining previously acquired scholarly knowledge.
Model: sherif1313/3arabLM-4B-islamic-v2
This release represents a new stage in the 3arabLM research project. The previous development focused more narrowly on Islamic scholarly domains such as Fiqh and Tafsir. The new direction expands the training curriculum toward a broader Islamic Heritage Foundation Model covering multiple branches of Islamic and Arabic scholarship. The model should still be considered an early research milestone. A sustantial portion of the planned training curriculum remains to be completed.
These books have been preserved in the model weightsู
ุงููุญู ุงููุงูู
ุชู
ููุฏ ุงูููุงุนุฏ ุจุดุฑุญ ุชุณููู ุงูููุงุฆุฏ
ุดุฑุญ ุฃูููุฉ ุงุจู ู
ุงูู ููุญุงุฒู
ู
ุดุฑุญ ุฃูููุฉ ุงุจู ู
ุงูู ููุดุงุทุจู = ุงูู
ูุงุตุฏ ุงูุดุงููุฉ
ุดุฑุญ ุงูู
ูุตู ูุงุจู ูุนูุด
ุงูู
ูุณูุนุฉ ุงูููููุฉ ุงููููุชูุฉ
ู
ูุณูุนุฉ ุงูุฅุฌู
ุงุน ูู ุงูููู ุงูุฅุณูุงู
ู
ู
ูุณูุนุฉ ููู ุงูุนุจุงุฏุงุช
ูุชุงูู ุงูุดุจูุฉ ุงูุฅุณูุงู
ูุฉ
ู
ุฌู
ูุน ูุชุงูู ูุฑุณุงุฆู ุงูุนุซูู
ูู
ุงูุณูู ุงููุจุฑู ููุจูููู ุช ุงูุชุฑูู
ุงูู
ุญูุท ูู ุงูุงุญุงุฏูุซ ุงููุจููุฉ ูุงูุณูู ูุงูุงุซุงุฑ
ุฌุงู
ุน ุงูุฑููุงุช
ุญููุฉ ุงูุฃูููุงุก ูุทุจูุงุช ุงูุฃุตููุงุก
ุตุญูุญ ุงูุจุฎุงุฑู
ุงูุฌุงู
ุน ูุดุนุจ ุงูุฅูู
ุงู ููุจูููู
ุงูู
ูุณูุนุฉ ุงูุนูุฏูุฉ - ุงูุฏุฑุฑ ุงูุณููุฉ
ุงูู
ูุฐุจ ุงูููู ุงูุฌุงู
ุน ูุชูุณูุฑ ุงุจู ุฌุฑูุฑ ุงูุทุจุฑู
ุงูู
ูุณูุนุฉ ุงููุฑุขููุฉ
ุชูุณูุฑ ุงุจู ูุซูุฑ _
ุชูุณูุฑ ุงููุฑุทุจู
ุฑูุญ ุงูุจูุงู
When selecting other books, please modify the code.
do_sample=True,
repetition_penalty=1.08,
no_repeat_ngram_size=4,
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
# ============================================================
# ๐ง ุงูุฅุนุฏุงุฏุงุช
# ============================================================
MODEL_PATH = "sherif1313/3arabLM-4B-islamic-v2" # ุบููุฑู ุฅูู ู
ุณุงุฑ ูู
ูุฐุฌู
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
MAX_NEW_TOKENS = 384
# ============================================================
# ๐ฆ ุชุญู
ูู ุงููู
ูุฐุฌ ูุงูู
ุญูู ุงููุบูู
# ============================================================
print("โณ ุชุญู
ูู ุงููู
ูุฐุฌ...")
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH, trust_remote_code=True)
if tokenizer.pad_token_id is None:
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(
MODEL_PATH,
dtype=torch.bfloat16,
device_map="auto",
trust_remote_code=True
)
model.eval()
# ============================================================
# ๐ ุฏุงูุฉ ุชูุณูู ุงูู
ูุชุงุฏุงุชุง (ู
ุทุงุจูุฉ ุชู
ุงู
ุงู ููุชุฏุฑูุจ)
# ============================================================
def format_prefix(book=None, author=None, category=None, part=None, page=None, hierarchy=None):
"""
ุชูููุฏ ุงูู
ูุชุงุฏุงุชุง ุจููุณ ุชูุณูู ุงูุชุฏุฑูุจ:
- ุฌู
ูุน ุงูุญููู ูู ุณุทุฑ ูุงุญุฏุ ู
ูุตููุฉ ุจู
ุณุงูุงุช
- ุจุฏูู ููุทุชูู (:) ุจุนุฏ ุงุณู
ุงูุญูู
- part> ู page> ู title> (ู
ุน >)
- hierarchy ุชุคุฎุฐ ู
ููุง ุงูููู
ุฉ ุงูุฃุฎูุฑุฉ ููุท
"""
tags = []
if book:
tags.append(f"book {book}")
if author:
tags.append(f"author {author}")
if category:
tags.append(f"category {category}")
if part is not None and str(part).strip():
tags.append(f"part> {part}")
if page is not None and str(page).strip():
tags.append(f"page> {page}")
if hierarchy:
parts = hierarchy.split('|')
title = parts[-1] if parts else ''
if title:
tags.append(f"title> {title}")
return " ".join(tags)
# ============================================================
# ๐ค ุฏุงูุฉ ุชูููุฏ ุงูุฅุฌุงุจุฉ (ุงุณุชุฑุฌุงุน ุฃู
ูู)
# ============================================================
def generate_text(prompt, book=None, author=None, category=None, part=None, page=None, hierarchy=None):
# ุจูุงุก ุงูู prompt ุจุงูุดูู: "metadata\nุงููุต"
prefix = format_prefix(book, author, category, part, page, hierarchy)
full_prompt = f"{prefix}\n{prompt}" if prefix else prompt
print(f"\n๐ ุงูุณุคุงู/ุงููุต: {prompt}")
print("-" * 50)
inputs = tokenizer(full_prompt, return_tensors="pt").to(DEVICE)
with torch.no_grad():
outputs = model.generate(
**inputs,
do_sample=False, # ุงุณุชุฑุฌุงุน ุญุชู
ู (Greedy)
repetition_penalty=1.02,
no_repeat_ngram_size=2,
#repetition_penalty=None
#no_repeat_ngram_size=None
max_new_tokens=MAX_NEW_TOKENS,
eos_token_id=tokenizer.eos_token_id,
pad_token_id=tokenizer.pad_token_id,
)
answer = tokenizer.decode(outputs[0][inputs['input_ids'].shape[1]:], skip_special_tokens=True)
print(f"๐ค ุฅุฌุงุจุฉ ุงููู
ูุฐุฌ:\n{answer}")
print("=" * 80)
# ============================================================
# ๐ ุฃุณุฆูุฉ ุชุณุชูุฏ ุฅูู ุงููุชุจ ุงูู
ูุฌูุฏุฉ ูุนูุงู ูู ./1/2
# ============================================================
TEST_CASES = [
# ================================================
# ุงูููู
# ========================================================
{
"prompt": " ุญูู
ุงูู
ุณุญ ุนูู ุงูุฎูููุ",
"book": "ุงูู
ูุณูุนุฉ ุงูููููุฉ ุงููููุชูุฉ",
"author": "ูุฒุงุฑุฉ ุงูุฃููุงู ูุงูุดุคูู ุงูุฅุณูุงู
ูุฉ ุจุงููููุช",
"category": "ููู ุนุงู
"
},
{
"prompt": " ู
ุจุทูุงุช ุงููุถูุกุ",
"book": "ู
ูุณูุนุฉ ููู ุงูุนุจุงุฏุงุช",
"author": "ุนูู ุจู ูุงูู ุงูุดุญูุฏ",
"category": "ููู ุนุงู
"
},
{
"prompt": " ุญูู
ุงูุฌู
ุน ูุงููุตุฑ ูู ุงูุณูุฑุ",
"book": "ูุชุงูู ุงูุดุจูุฉ ุงูุฅุณูุงู
ูุฉ",
"author": "ูุฌูุฉ ุงููุชูู ุจุงูุดุจูุฉ ุงูุฅุณูุงู
ูุฉ",
"category": "ุงููุชุงูู"
},
# ========================================================
# ุงูุญุฏูุซ ูุงูุขุซุงุฑ
# ========================================================
{
"prompt": " ุฃูู ุญุฏูุซ ูู ุตุญูุญ ุงูุจุฎุงุฑูุ",
"book": "ุตุญูุญ ุงูุจุฎุงุฑู - ุท ุงูุดุนุจ",
"author": "ู
ุญู
ุฏ ุจู ุฅุณู
ุงุนูู ุงูุจุฎุงุฑู",
"category": "ู
ุชูู ุงูุญุฏูุซ"
},
{
"prompt": " ุญุฏูุซ ุฌุจุฑูู ูู ุงูุฅุณูุงู
ูุงูุฅูู
ุงู ูุงูุฅุญุณุงูุ",
"book": "ุตุญูุญ ุงูุจุฎุงุฑู - ุท ุงูุดุนุจ",
"author": "ู
ุญู
ุฏ ุจู ุฅุณู
ุงุนูู ุงูุจุฎุงุฑู",
"category": "ู
ุชูู ุงูุญุฏูุซ"
},
{
"prompt": " ูุถู ุทูุจ ุงูุนูู
ุ",
"book": "ุงูุฌุงู
ุน ูุดุนุจ ุงูุฅูู
ุงู ููุจูููู",
"author": "ุฃุญู
ุฏ ุจู ุงูุญุณูู ุงูุจูููู",
"category": "ู
ุชูู ุงูุญุฏูุซ"
},
{
"prompt": " ูุถู ุงูุตุฏูุ",
"book": "ุงูุณูู ุงููุจุฑู ููุจูููู ุช ุงูุชุฑูู",
"author": "ุฃุญู
ุฏ ุจู ุงูุญุณูู ุงูุจูููู",
"category": "ู
ุชูู ุงูุญุฏูุซ"
},
{
"prompt": " ูุถู ุฐูุฑ ุงููู ุชุนุงููุ",
"book": "ุงูู
ุญูุท ูู ุงูุงุญุงุฏูุซ ุงููุจููุฉ ูุงูุณูู ูุงูุงุซุงุฑ",
"author": "ุบูุฑ ู
ุญุฏุฏ",
"category": "ู
ุชูู ุงูุญุฏูุซ"
},
{
"prompt": " ูุถู ุงูุตูุงุฉ ุนูู ุงููุจู ุตูู ุงููู ุนููู ูุณูู
ุ",
"book": "ุฌุงู
ุน ุงูุฑููุงุช",
"author": "ุบูุฑ ู
ุญุฏุฏ",
"category": "ู
ุชูู ุงูุญุฏูุซ"
},
{
"prompt": " ู
ูุฒูุฉ ุงูุตุฏู ูู ุณูุฑุฉ ุงูุตุงูุญููุ",
"book": "ุญููุฉ ุงูุฃูููุงุก ูุทุจูุงุช ุงูุฃุตููุงุก",
"author": "ุฃุจู ูุนูู
ุงูุฃุตุจูุงูู",
"category": "ู
ุชูู ุงูุญุฏูุซ"
},
# ========================================================
# ุงูุนููุฏุฉ
# ========================================================
{
"prompt": " ู
ุนูู ุงูุฅูู
ุงู ุจุงูููุ",
"book": "ุงูู
ูุณูุนุฉ ุงูุนูุฏูุฉ - ุงูุฏุฑุฑ ุงูุณููุฉ",
"author": "ุงูู
ูุณูุนุฉ ุงูุนูุฏูุฉ",
"category": "ุงูุนููุฏุฉ"
},
# ========================================================
# ุงูุชูุณูุฑ
# ========================================================
{
"prompt": " ุชูุณูุฑ ุณูุฑุฉ ุงููุงุชุญุฉุ",
"book": "ุชูุณูุฑ ุงููุฑุทุจู",
"author": "ู
ุญู
ุฏ ุจู ุฃุญู
ุฏ ุงููุฑุทุจู",
"category": "ุงูุชูุงุณูุฑ"
},
{
"prompt": " ู
ุนูู ูููู ุชุนุงูู: ุงูุญู
ุฏ ููู ุฑุจ ุงูุนุงูู
ููุ",
"book": "ุชูุณูุฑ ุงูุฑุงุฒู = ู
ูุงุชูุญ ุงูุบูุจ ุฃู ุงูุชูุณูุฑ ุงููุจูุฑ",
"author": "ูุฎุฑ ุงูุฏูู ุงูุฑุงุฒู",
"category": "ุงูุชูุงุณูุฑ"
},
{
"prompt": " ุชูุณูุฑ ูููู ุชุนุงูู: ูู ูู ุงููู ุฃุญุฏุ",
"book": "ุฑูุญ ุงูุจูุงู",
"author": "ุฅุณู
ุงุนูู ุญูู",
"category": "ุงูุชูุงุณูุฑ"
},
{
"prompt": " ุชูุณูุฑ ูููู ุชุนุงูู: ูู ูู ุงููู ุฃุญุฏุ",
"book": "ุชูุณูุฑ ุงุจู ูุซูุฑ",
"author": "ุงุจู ูุซูุฑ",
"category": "ุงูุชูุงุณูุฑ"
},
{
"prompt": " ู
ุนูู ุงูุชููู ูู ุงููุฑุขู ุงููุฑูู
ุ",
"book": "ุงูู
ูุณูุนุฉ ุงููุฑุขููุฉ",
"author": "ุบูุฑ ู
ุญุฏุฏ",
"category": "ุงูุชูุงุณูุฑ"
},
]
# ============================================================
# ๐ ุชุดุบูู ุงูุงุฎุชุจุงุฑ
# ============================================================
print("\n๐ ุจุฏุก ุงุฎุชุจุงุฑ ุงููู
ูุฐุฌ ุนูู ุฃุณุฆูุฉ ู
ุชููุนุฉ...\n")
for test in TEST_CASES:
generate_text(
prompt=test["prompt"],
book=test.get("book"),
author=test.get("author"),
category=test.get("category"),
part=test.get("part"),
page=test.get("page"),
hierarchy=test.get("hierarchy")
)
print("\nโ
ุงูุชูู ุงูุงุฎุชุจุงุฑ.")
The current model has several limitations:
The project explores several research questions:
How much classical scholarly knowledge can be encoded directly into model parameters?
Can a language model reconstruct scholarly passages without relying entirely on external retrieval?
Can ูbook, chapter, section, and paragraph structures improve scholarly representations?
Can author, book, century, madhhab, and discipline metadata improve knowledge organization?
Can model capacity be increased without catastrophic forgetting?
Can specialized Islamic experts be efficiently developed from a shared foundation?
The central research hypothesis of 3arabLM is that language models may function not only as generators, but also as compressed representations of large scholarly corpora. The project therefore explores the possibility of building:
The ultimate objective is to study how much classical Arabic scholarly knowledge can be represented and reconstructed from model parameters.
The ultimate objective of 3arabLM is:
To preserve, recall, and reconstruct the Arabic Islamic scholarly heritage with the highest possible degree of fidelity while maintaining the language, terminology, methodologies, and diversity of the original scholarly tradition.
The project is not intended to replace scholars.
It is a research effort exploring how modern language-model technology can contribute to:
| Stage | Description | Status |
|---|---|---|
| Stage 1 | Foundation โ Continual pretraining on selected Islamic scholarly domains | โ Complete |
| Stage 2 | Scholarly Expansion โ Expansion into Hadith, Sirah, History, Aqeedah, Arabic Language, Literature, and Spiritual Literature | ๐ In Progress |
| Stage 3 | Metadata Integration โ Adding structured information about books, authors, periods, schools, and disciplines | โณ Planned |
| Stage 4 | Capacity Scaling โ Increasing model capacity while preserving previously learned knowledge | โณ Planned |
| Stage 5 | Expert Models โ Developing specialized experts for individual scholarly disciplines | โณ Planned |
| Stage 6 | Islamic Heritage Foundation โ Building a large-scale Arabic foundation model | โณ Long-Term |
The project welcomes researchers and contributors interested in:
The project is associated with the research work:
A Representation-Diagnostic Framework for Arabic Heritage Language Models: A Case Study on Continued Pretraining with the Shamela Corpus
The research investigates representation diagnostics and domain-specific adaptation of Arabic language models using classical Arabic heritage corpora.
3arabLM explores a simple but ambitious question:
Can a language model become a compressed digital library of the Arabic Islamic scholarly heritage?
The project seeks to preserve not only:
but also:
The long-term vision is a continually expanding Arabic foundation model that represents major branches of Islamic and Arabic scholarly heritage while preserving their linguistic, methodological, and historical diversity.
If you use this model in academic research, please cite the associated research work:
@article{3arabLM-4B-islamic-v2,
title={3arabLM-4B-islamic-v2},
author={Hassan, Sherif},
year={2026},
url={https://huggingface.co/sherif1313/3arabLM-4B-islamic-v2}
}