Jehadoumer commited on
Commit
ff7ab30
·
verified ·
1 Parent(s): 1a1970b

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +259 -259
README.md CHANGED
@@ -1,260 +1,260 @@
1
- # Automatic Hadith Isnad Parsing Models
2
-
3
- Fine-tuned transformer models for **automatic isnad parsing**: extracting narrator mentions and narration relations (who narrated to whom) from hadith isnad text. Use the output to build graph representations of transmission chains for authenticity analysis, visualization, and retrieval.
4
-
5
- **Contents:** [What is isnad parsing?](#what-is-isnad-parsing) · [Example: model input and output](#example-model-input-and-output) · [Model architectures](#model-architectures) · [Hugging Face models](#hugging-face-models) · [Data](#data-used-for-training-and-evaluation) · [Results](#results-summary-test-set) · [Installation](#installation) · [How to use](#how-to-use) · [Reference](#reference) · [License](#license)
6
-
7
- ---
8
-
9
- ## What is isnad parsing?
10
-
11
- The **isnad** is the chain of narrators at the start of a hadith; it records who transmitted the report across generations. **Isnad parsing** means:
12
-
13
- 1. **Narrator recognition** — identifying narrator mentions (and optionally narration words) in the isnad text.
14
- 2. **Relation extraction** — extracting direct narration links as **(head, tail)** pairs: the head is the receiver of the hadith, the tail is the transmitter.
15
-
16
- When a hadith has **multiple chains** (branching or parallel paths), we can also predict a **chain identifier** per link, giving **(head, tail, chain_number)** triplets so that each transmission path stays distinct in the graph.
17
-
18
- - **Chain-blind**: output is duplets **(head, tail)** only; no chain IDs. Suitable for a single merged graph of all links (no validation on paths).
19
- - **Chain-aware**: output is triplets **(head, tail, chain_number)**; suitable for reconstructing separate paths and building property graphs with chain IDs on edges.
20
-
21
- **Example:** The following isnad encodes **three distinct transmission chains** that share some narrators but branch into different paths. Preserving a chain identifier per relation is necessary to avoid invalid narration paths when building the graph.
22
-
23
- **Isnad (Arabic and translation):**
24
-
25
- > حدثنا مسدد ، حدثنا يحيى ، عن سفيان قال: حدثني منصور وسليمان ، عن أبي وائل ، عن أبي ميسرة ، عن عبد الله . قال: وحدثني واصل ، عن أبي وائل ، عن عبد الله - رضي الله عنه...
26
- >
27
- > *Musaddad narrated to us, Yahya narrated to us, from Sufyan, who said: Mansur and Sulaiman narrated to me, from Abu Wa'il, from Abu Maysarah, from Abdullah. He said: Wasil also narrated to me, from Abu Wa'il, from Abdullah – may Allah be pleased with him...*
28
-
29
- This hadith isnad has **three narration chains**. They share the same opening (Musaddad ← Yahya ← Sufyan) but then branch:
30
-
31
- - **Chain 1** goes through **Wasil**: Sufyan ← Wasil ← Abu Wa'il ← Abdullah.
32
- - **Chain 2** goes through **Sulaiman**: Sufyan ← Sulaiman ← Abu Wa'il ← Abu Maysarah ← Abdullah.
33
- - **Chain 3** goes through **Mansur**: Sufyan ← Mansur ← Abu Wa'il ← Abu Maysarah ← Abdullah.
34
-
35
- Without chain numbers, merging all (head, tail) links into one graph would mix these paths and create invalid connections. The figures below show each chain separately, then the merged graph with chain numbers on the edges.
36
-
37
- **Chain 1** (Wasil path):
38
-
39
- ![Graph representation of chain 1](figures/chain-1-example.png)
40
-
41
- **Chain 2** (Sulaiman path):
42
-
43
- ![Graph representation of chain 2](figures/chain2-example.png)
44
-
45
- **Chain 3** (Mansur path):
46
-
47
- ![Graph representation of chain 3](figures/chain-3-example.png)
48
-
49
- **Merged graph** (all three chains, with chain numbers on edges):
50
-
51
- ![Complete hadith isnad as narration graph](figures/all-chains-example.png)
52
-
53
- ---
54
-
55
- ## Example: model input and output
56
-
57
- The models take raw isnad text and generate a linearized sequence of relation tuples. Below: one input and its chain-blind and chain-aware outputs.
58
-
59
- **Input** (Arabic isnad as in source text). English translation for demonstration:
60
-
61
- > حدثنا يوسف القاضي ، ثنا عبد الواحد بن غياث ، ثنا حماد بن سلمة ، عن أيوب وعبيد الله بن عمر ، عن نافع ، عن ابن عمر قال : قال زيد بن ثابت...
62
- >
63
- > *Yusuf Al-Qadi narrated to us, saying: Abdul Wahid bin Ghayath narrated to us, saying: Hammad bin Salamah narrated to us, from Ayyub and Ubaidullah bin Umar, from Nafi', from Ibn Umar, who said: Zaid bin Thabit said...*
64
-
65
- **Chain-blind output** (duplets only; no chain IDs). Each relation is **`<head>`** *receiver* **`<tail>`** *transmitter*; the sequence ends with **`#hadith`**.:
66
- ```
67
- <head> يوسف القاضي <tail> عبد الواحد بن غياث
68
- <head> عبد الواحد بن غياث <tail> حماد بن سلمة
69
- <head> حماد بن سلمة <tail> عبيد الله بن عمر
70
- <head> حماد بن سلمة <tail> أيوب
71
- <head> عبيد الله بن عمر <tail> نافع
72
- <head> أيوب <tail> نافع
73
- <head> نافع <tail> ابن ��مر
74
- <head> ابن عمر <tail> زيد بن ثابت #hadith
75
- ```
76
-
77
- **Chain-aware output** (triplets with chain identifier). Each relation adds **`<chain_number>`** *k* so that the two transmission paths stay distinct.:
78
- ```
79
- <head> يوسف القاضي <tail> عبد الواحد بن غياث <chain_number> 1
80
- <head> عبد الواحد بن غياث <tail> حماد بن سلمة <chain_number> 1
81
- <head> حماد بن سلمة <tail> عبيد الله بن عمر <chain_number> 1
82
- <head> عبيد الله بن عمر <tail> نافع <chain_number> 1
83
- <head> نافع <tail> ابن عمر <chain_number> 1
84
- <head> ابن عمر <tail> زيد بن ثابت <chain_number> 1
85
- <head> يوسف القاضي <tail> عبد الواحد بن غياث <chain_number> 2
86
- <head> عبد الواحد بن غياث <tail> حماد بن سلمة <chain_number> 2
87
- <head> حماد بن سلمة <tail> أيوب <chain_number> 2
88
- <head> أيوب <tail> نافع <chain_number> 2
89
- <head> نافع <tail> ابن عمر <chain_number> 2
90
- <head> ابن عمر <tail> زيد بن ثابت <chain_number> 2
91
- #hadith
92
- ```
93
-
94
- ---
95
-
96
- ## Model architectures
97
-
98
- | Base model | Type | Description |
99
- |------------|------|-------------|
100
- | **CAMeLBERT-CA** | Encoder (BERT) | Pre-trained on Classical Arabic. Joint NER + relation classification (biaffine). Outputs **chain-blind duplets** only. Best for high-precision direct links. |
101
- | **AraT5** | Encoder–decoder (T5) | T5 for Arabic; text-to-text. Can be fine-tuned for **chain-blind** or **chain-aware** output. Best for chain-aware triplets and full graph reconstruction. |
102
- | **AraBART** | Encoder–decoder (BART) | BART for Arabic; denoising pretraining. Can be fine-tuned for **chain-blind** or **chain-aware** output. May show more spelling variation; useful for tasks like narrator normalization. |
103
-
104
- **With-prefix vs without-prefix**: Some models were trained with a task prefix (e.g. `[isnad]`) in the input; others without. Use the variant that matches your intended input format.
105
-
106
- ---
107
-
108
- ## Hugging Face models
109
-
110
- All models are hosted on the Hugging Face Hub. Choose by **task** (chain-blind vs chain-aware) and **base architecture** (CAMeLBERT-CA, AraT5, AraBART).
111
-
112
- ### Chain-blind (duplets only)
113
-
114
- | Model | Hugging Face |
115
- |-------|--------------|
116
- | CAMeLBERT-CA (chain-blind) | [Jehadoumer/ChainBlind-HadithIsnadParser-CAMeLBERT](https://huggingface.co/Jehadoumer/ChainBlind-HadithIsnadParser-CAMeLBERT) |
117
- | AraT5 (chain-blind, with prefix) | [Jehadoumer/ChainBlind-HadithIsnadParser-withPrefix-AraT5](https://huggingface.co/Jehadoumer/ChainBlind-HadithIsnadParser-withPrefix-AraT5) |
118
- | AraT5 (chain-blind, no prefix) | [Jehadoumer/ChainBlind-HadithIsnadParser-AraT5](https://huggingface.co/Jehadoumer/ChainBlind-HadithIsnadParser-AraT5) |
119
- | AraBART (chain-blind, with prefix) | [Jehadoumer/ChainBlind-HadithIsnadParser-withPrefix-AraBART](https://huggingface.co/Jehadoumer/ChainBlind-HadithIsnadParser-withPrefix-AraBART) |
120
- | AraBART (chain-blind, no prefix) | [Jehadoumer/ChainBlind-HadithIsnadParser-AraBART](https://huggingface.co/Jehadoumer/ChainBlind-HadithIsnadParser-AraBART) |
121
-
122
- ### Chain-aware (triplets with chain number)
123
-
124
- | Model | Hugging Face |
125
- |-------|--------------|
126
- | AraT5 (chain-aware, with prefix) | [Jehadoumer/ChainAware-HadithIsnadParser-withPrefix-AraT5](https://huggingface.co/Jehadoumer/ChainAware-HadithIsnadParser-withPrefix-AraT5) |
127
- | AraT5 (chain-aware, no prefix) | [Jehadoumer/ChainAware-HadithIsnadParser-AraT5](https://huggingface.co/Jehadoumer/ChainAware-HadithIsnadParser-AraT5) |
128
- | AraBART (chain-aware, with prefix) | [Jehadoumer/ChainAware-HadithIsnadParser-withPrefix-AraBART](https://huggingface.co/Jehadoumer/ChainAware-HadithIsnadParser-withPrefix-AraBART) |
129
- | AraBART (chain-aware, no prefix) | [Jehadoumer/ChainAware-HadithIsnadParser-AraBART](https://huggingface.co/Jehadoumer/ChainAware-HadithIsnadParser-AraBART) |
130
-
131
- ---
132
-
133
- ## Data used for training and evaluation
134
-
135
- **Source.** Training and evaluation data come from the [Ifta' Sunnah Platform](https://sunna.alifta.gov.sa/) (King Abdullah bin Abdul Aziz Program for the Prophetic Sunnah), and curated for the experiment at [Ifta-Sunnah–Hadith-and-Narrators-Dataset](https://github.com/JehadOumer/Ifta-Sunnah-Hadith-and-Narrators-Dataset). The platform provides **33 foundational hadith books** (Kutub al-Matn), plus 57 additional books with supportive and interpretative content; the hadith dataset used here was extracted from the **33 books**, which are organized mainly by hadith content (thematic). In total, **276,347 hadiths** were extracted from the platform; after preprocessing (isnad isolation, entity tagging, relation tuples, and filtering), **223,478 hadith instances** were used for automatic isnad parsing.
136
-
137
- **Splits.** The dataset was split **70% training, 15% validation, 15% testing**. Summary:
138
-
139
- | | Training | Validation | Testing |
140
- |---|----------|------------|---------|
141
- | **Hadiths** | 156,434 | 33,521 | 33,523 |
142
- | **Duplets** (chain-blind relations) | 756,714 | 162,782 | 162,010 |
143
- | **Triplets** (chain-aware relations) | 877,016 | 190,049 | 188,755 |
144
-
145
- ---
146
-
147
- ## Results summary (test set)
148
-
149
- Models are evaluated on the **held-out test set** (33,523 hadith isnad texts). Metrics: exact match for relations; narrator set comparison for narrator recognition. Tables below include **with-prefix** and **no-prefix** variants for the encoder–decoder models.
150
-
151
- ### Narrator recognition (predicted vs gold set of narrators)
152
-
153
- | Model | Precision | Recall | F1 |
154
- |-------|-----------|--------|-----|
155
- | CAMeLBERT-CA | 0.98 | 0.99 | 0.98 |
156
- | AraT5 | 0.97 | 0.97 | 0.97 |
157
- | AraT5-Prefix | 0.97 | 0.97 | 0.97 |
158
- | AraBART | 0.78 | 0.94 | 0.84 |
159
- | AraBART-Prefix | 0.78 | 0.94 | 0.84 |
160
-
161
- ### Chain-blind relation extraction (duplets)
162
-
163
- | Model | Precision | Recall | F1 |
164
- |-------|-----------|--------|-----|
165
- | CAMeLBERT-CA | 0.96 | 0.93 | 0.95 |
166
- | AraT5 | 0.96 | 0.95 | 0.95 |
167
- | AraT5-Prefix | 0.96 | 0.95 | 0.95 |
168
- | AraBART | 0.66 | 0.64 | 0.65 |
169
- | AraBART-Prefix | 0.66 | 0.65 | 0.65 |
170
-
171
- ### Chain-aware relation extraction (triplets)
172
-
173
- Only the encoder–decoder models produce chain-aware output.
174
-
175
- | Model | Precision | Recall | F1 |
176
- |-------|-----------|--------|-----|
177
- | AraT5 | 0.94 | 0.93 | 0.93 |
178
- | AraT5-Prefix | 0.94 | 0.93 | 0.93 |
179
- | AraBART | 0.76 | 0.37 | 0.48 |
180
- | AraBART-Prefix | 0.76 | 0.37 | 0.48 |
181
-
182
- ---
183
-
184
- ## Installation
185
-
186
- ```bash
187
- pip install torch transformers huggingface_hub safetensors
188
- ```
189
-
190
- The [CAMeLBERT-CA Hub repo](https://huggingface.co/Jehadoumer/ChainBlind-HadithIsnadParser-CAMeLBERT) provides `model.safetensors`; the inference script loads that when using `--model-id`.
191
-
192
- ---
193
-
194
- ## How to use
195
-
196
- ### CAMeLBERT-CA (chain-blind): inference script in this repo
197
-
198
- The CAMeLBERT-CA model does **not** output duplets directly from raw text. It needs a pipeline that: (1) runs NER to get narrator spans, (2) builds **candidate head–tail duplets**, and (3) runs the **biaffine classifier** on each candidate. This repo provides that pipeline in `camelbert_duplets_biaffine_inference.py`.
199
-
200
- **Run inference** (loads the model from Hugging Face by default):
201
-
202
- ```bash
203
- # Built-in example isnad
204
- python camelbert_duplets_biaffine_inference.py --no-cuda
205
-
206
- # Your own isnad text (space-separated words)
207
- python camelbert_duplets_biaffine_inference.py --input "حدثنا أبو معاوية حدثنا الأعمش عن مسلم" --no-cuda
208
-
209
- # From a JSONL file (one JSON object per line with "isnad_sequence" or "isnad_text")
210
- python camelbert_duplets_biaffine_inference.py --input-file examples.jsonl --no-cuda
211
- ```
212
-
213
- **Output:** One line per input in chain-blind form: `<head> A <tail> B <head> B <tail> C ... #hadith`
214
-
215
- To use a **local checkpoint** (same format as training output): `--checkpoint path/to/best_model.pth`
216
-
217
- **Test on random examples** from the predictions JSON (install `ijson` for large files):
218
-
219
- ```bash
220
- pip install ijson
221
- python test_camelbert_inference_from_predictions.py --predictions-json "path/to/predictions_camelbert_duplets_biaffine.json" --num-examples 2 --no-cuda
222
- ```
223
-
224
- ### AraT5 / AraBART (chain-blind or chain-aware)
225
-
226
- These models **generate** the linearized output directly from raw isnad text:
227
-
228
- ```python
229
- from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
230
-
231
- model_name = "Jehadoumer/ChainBlind-HadithIsnadParser-AraT5" # or any other model ID above
232
- tokenizer = AutoTokenizer.from_pretrained(model_name)
233
- model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
234
-
235
- # Input: raw isnad text (Arabic)
236
- isnad_text = "..." # e.g. "حدثنا فلان، حدثنا فلان، عن فلان..."
237
- inputs = tokenizer(isnad_text, return_tensors="pt", truncation=True, max_length=512)
238
- outputs = model.generate(**inputs, max_new_tokens=512)
239
-
240
- # Decode and parse <head> / <tail> (and optionally <chain_number>) from the output
241
- decoded = tokenizer.decode(outputs[0], skip_special_tokens=False)
242
- # Then extract (head, tail) or (head, tail, chain_number) from decoded.
243
- ```
244
-
245
- Output format: **chain-blind** — sequence of `<head> A <tail> B` segments ending with `#hadith`. **Chain-aware** — each segment also includes `<chain_number> k`. Parse with regex or the same logic as in the evaluation scripts.
246
-
247
- ---
248
-
249
- ## Reference
250
-
251
- - **Thesis:** *Relation Extraction for Graph‐Based Representation of Hadith Isnad*. Describes the task, data, model architectures, training, and evaluation in detail.
252
- - **Dataset:** [Ifta-Sunnah-Hadith-and-Narrators-Dataset](https://github.com/JehadOumer/Ifta-Sunnah-Hadith-and-Narrators-Dataset) — hadiths and narrator profiles with structured chains of narration. **Original source:** [Ifta' Sunnah Platform](https://sunna.alifta.gov.sa/) (King Abdullah bin Abdul Aziz Program for the Prophetic Sunnah).
253
-
254
- If you use these models or the dataset in your work, please cite the thesis and the original data source as appropriate.
255
-
256
- ---
257
-
258
- ## License
259
-
260
  This project is released under an **open-source license** that allows use, modification, and distribution with **attribution to the creators**. The base models (CAMeLBERT-CA, AraT5, AraBART) follow their respective upstream licenses; the fine-tuned weights are released under the same open-source, attribution-based terms.
 
1
+ # Automatic Hadith Isnad Parsing Models
2
+
3
+ Fine-tuned transformer models for **automatic isnad parsing**: extracting narrator mentions and narration relations (who narrated to whom) from hadith isnad text. Use the output to build graph representations of transmission chains for authenticity analysis, visualization, and retrieval.
4
+
5
+ **Contents:** [What is isnad parsing?](#what-is-isnad-parsing) · [Example: model input and output](#example-model-input-and-output) · [Model architectures](#model-architectures) · [Hugging Face models](#hugging-face-models) · [Data](#data-used-for-training-and-evaluation) · [Results](#results-summary-test-set) · [Installation](#installation) · [How to use](#how-to-use) · [Reference](#reference) · [License](#license)
6
+
7
+ ---
8
+
9
+ ## What is isnad parsing?
10
+
11
+ The **isnad** is the chain of narrators at the start of a hadith; it records who transmitted the report across generations. **Isnad parsing** means:
12
+
13
+ 1. **Narrator recognition** — identifying narrator mentions (and optionally narration words) in the isnad text.
14
+ 2. **Relation extraction** — extracting direct narration links as **(head, tail)** pairs: the head is the receiver of the hadith, the tail is the transmitter.
15
+
16
+ When a hadith has **multiple chains** (branching or parallel paths), we can also predict a **chain identifier** per link, giving **(head, tail, chain_number)** triplets so that each transmission path stays distinct in the graph.
17
+
18
+ - **Chain-blind**: output is duplets **(head, tail)** only; no chain IDs. Suitable for a single merged graph of all links (no validation on paths).
19
+ - **Chain-aware**: output is triplets **(head, tail, chain_number)**; suitable for reconstructing separate paths and building property graphs with chain IDs on edges.
20
+
21
+ **Example:** The following isnad encodes **three distinct transmission chains** that share some narrators but branch into different paths. Preserving a chain identifier per relation is necessary to avoid invalid narration paths when building the graph.
22
+
23
+ **Isnad (Arabic and translation):**
24
+
25
+ > حدثنا مسدد ، حدثنا يحيى ، عن سفيان قال: حدثني منصور وسليمان ، عن أبي وائل ، عن أبي ميسرة ، عن عبد الله . قال: وحدثني واصل ، عن أبي وائل ، عن عبد الله - رضي الله عنه...
26
+ >
27
+ > *Musaddad narrated to us, Yahya narrated to us, from Sufyan, who said: Mansur and Sulaiman narrated to me, from Abu Wa'il, from Abu Maysarah, from Abdullah. He said: Wasil also narrated to me, from Abu Wa'il, from Abdullah – may Allah be pleased with him...*
28
+
29
+ This hadith isnad has **three narration chains**. They share the same opening (Musaddad ← Yahya ← Sufyan) but then branch:
30
+
31
+ - **Chain 1** goes through **Wasil**: Sufyan ← Wasil ← Abu Wa'il ← Abdullah.
32
+ - **Chain 2** goes through **Sulaiman**: Sufyan ← Sulaiman ← Abu Wa'il ← Abu Maysarah ← Abdullah.
33
+ - **Chain 3** goes through **Mansur**: Sufyan ← Mansur ← Abu Wa'il ← Abu Maysarah ← Abdullah.
34
+
35
+ Without chain numbers, merging all (head, tail) links into one graph would mix these paths and create invalid connections. The figures below show each chain separately, then the merged graph with chain numbers on the edges.
36
+
37
+ **Chain 1** (Wasil path):
38
+
39
+ ![Graph representation of chain 1](figures/chain-1-example.png)
40
+
41
+ **Chain 2** (Sulaiman path):
42
+
43
+ ![Graph representation of chain 2](figures/chain2-example.png)
44
+
45
+ **Chain 3** (Mansur path):
46
+
47
+ ![Graph representation of chain 3](figures/chain-3-example.png)
48
+
49
+ **Merged graph** (all three chains, with chain numbers on edges):
50
+
51
+ ![Complete hadith isnad as narration graph](figures/all-chains-example.png)
52
+
53
+ ---
54
+
55
+ ## Example: model input and output
56
+
57
+ The models take raw isnad text and generate a linearized sequence of relation tuples. Below: one input and its chain-blind and chain-aware outputs.
58
+
59
+ **Input** (Arabic isnad as in source text). English translation for demonstration:
60
+
61
+ > حدثنا يوسف القاضي ، ثنا عبد الواحد بن غياث ، ثنا حماد بن سلمة ، عن أيوب وعبيد الله بن عمر ، عن نافع ، عن ابن عمر قال : قال زيد بن ثابت...
62
+ >
63
+ > *Yusuf Al-Qadi narrated to us, saying: Abdul Wahid bin Ghayath narrated to us, saying: Hammad bin Salamah narrated to us, from Ayyub and Ubaidullah bin Umar, from Nafi', from Ibn Umar, who said: Zaid bin Thabit said...*
64
+
65
+ **Chain-blind output** (duplets only; no chain IDs). Each relation is **`<head>`** *receiver* **`<tail>`** *transmitter*; the sequence ends with **`#hadith`**.:
66
+ ```
67
+ <head> يوسف القاضي <tail> عبد الواحد بن غياث
68
+ <head> عبد الواحد بن غياث <tail> حماد بن سلمة
69
+ <head> حماد بن سلمة <tail> عبيد الله بن عمر
70
+ <head> حماد بن سلمة <tail> أيوب
71
+ <head> عبيد الله بن عمر <tail> نافع
72
+ <head> أيوب <tail> نافع
73
+ <head> نافع <tail> ابن عمر
74
+ <head> ابن عمر <tail> زيد بن ثابت #hadith
75
+ ```
76
+
77
+ **Chain-aware output** (triplets with chain identifier). Each relation adds **`<chain_number>`** *k* so that the two transmission paths stay distinct.:
78
+ ```
79
+ <head> يوسف القاضي <tail> عبد الواحد بن غياث <chain_number> 1
80
+ <head> عبد الواحد بن غياث <tail> حماد بن سلمة <chain_number> 1
81
+ <head> حماد بن سلمة <tail> عبيد الله بن عمر <chain_number> 1
82
+ <head> عبيد الله بن عمر <tail> نافع <chain_number> 1
83
+ <head> نافع <tail> ابن عمر <chain_number> 1
84
+ <head> ابن عمر <tail> زيد بن ثابت <chain_number> 1
85
+ <head> يوسف القاضي <tail> عبد الواحد بن غياث <chain_number> 2
86
+ <head> عبد الواحد بن غياث <tail> حماد بن سلمة <chain_number> 2
87
+ <head> حماد بن سلمة <tail> أيوب <chain_number> 2
88
+ <head> أيوب <tail> نافع <chain_number> 2
89
+ <head> نافع <tail> ابن عمر <chain_number> 2
90
+ <head> ابن عمر <tail> زيد بن ثابت <chain_number> 2
91
+ #hadith
92
+ ```
93
+
94
+ ---
95
+
96
+ ## Model architectures
97
+
98
+ | Base model | Type | Description |
99
+ |------------|------|-------------|
100
+ | **CAMeLBERT-CA** | Encoder (BERT) | Pre-trained on Classical Arabic. Joint NER + relation classification (biaffine). Outputs **chain-blind duplets** only. Best for high-precision direct links. |
101
+ | **AraT5** | Encoder–decoder (T5) | T5 for Arabic; text-to-text. Can be fine-tuned for **chain-blind** or **chain-aware** output. Best for chain-aware triplets and full graph reconstruction. |
102
+ | **AraBART** | Encoder–decoder (BART) | BART for Arabic; denoising pretraining. Can be fine-tuned for **chain-blind** or **chain-aware** output. May show more spelling variation; useful for tasks like narrator normalization. |
103
+
104
+ **With-prefix vs without-prefix**: Some models were trained with a task prefix (e.g. `[isnad]`) in the input; others without. Use the variant that matches your intended input format.
105
+
106
+ ---
107
+
108
+ ## Hugging Face models
109
+
110
+ All models are hosted on the Hugging Face Hub. Choose by **task** (chain-blind vs chain-aware) and **base architecture** (CAMeLBERT-CA, AraT5, AraBART).
111
+
112
+ ### Chain-blind (duplets only)
113
+
114
+ | Model | Hugging Face |
115
+ |-------|--------------|
116
+ | CAMeLBERT-CA (chain-blind) | [Jehadoumer/ChainBlind-HadithIsnadParser-CAMeLBERT](https://huggingface.co/Jehadoumer/ChainBlind-HadithIsnadParser-CAMeLBERT) |
117
+ | AraT5 (chain-blind, with prefix) | [Jehadoumer/ChainBlind-HadithIsnadParser-withPrefix-AraT5](https://huggingface.co/Jehadoumer/ChainBlind-HadithIsnadParser-withPrefix-AraT5) |
118
+ | AraT5 (chain-blind, no prefix) | [Jehadoumer/ChainBlind-HadithIsnadParser-AraT5](https://huggingface.co/Jehadoumer/ChainBlind-HadithIsnadParser-AraT5) |
119
+ | AraBART (chain-blind, with prefix) | [Jehadoumer/ChainBlind-HadithIsnadParser-withPrefix-AraBART](https://huggingface.co/Jehadoumer/ChainBlind-HadithIsnadParser-withPrefix-AraBART) |
120
+ | AraBART (chain-blind, no prefix) | [Jehadoumer/ChainBlind-HadithIsnadParser-AraBART](https://huggingface.co/Jehadoumer/ChainBlind-HadithIsnadParser-AraBART) |
121
+
122
+ ### Chain-aware (triplets with chain number)
123
+
124
+ | Model | Hugging Face |
125
+ |-------|--------------|
126
+ | AraT5 (chain-aware, with prefix) | [Jehadoumer/ChainAware-HadithIsnadParser-withPrefix-AraT5](https://huggingface.co/Jehadoumer/ChainAware-HadithIsnadParser-withPrefix-AraT5) |
127
+ | AraT5 (chain-aware, no prefix) | [Jehadoumer/ChainAware-HadithIsnadParser-AraT5](https://huggingface.co/Jehadoumer/ChainAware-HadithIsnadParser-AraT5) |
128
+ | AraBART (chain-aware, with prefix) | [Jehadoumer/ChainAware-HadithIsnadParser-withPrefix-AraBART](https://huggingface.co/Jehadoumer/ChainAware-HadithIsnadParser-withPrefix-AraBART) |
129
+ | AraBART (chain-aware, no prefix) | [Jehadoumer/ChainAware-HadithIsnadParser-AraBART](https://huggingface.co/Jehadoumer/ChainAware-HadithIsnadParser-AraBART) |
130
+
131
+ ---
132
+
133
+ ## Data used for training and evaluation
134
+
135
+ **Source.** Training and evaluation data come from the [Ifta' Sunnah Platform](https://sunna.alifta.gov.sa/) (King Abdullah bin Abdul Aziz Program for the Prophetic Sunnah), and curated for the experiment at [Ifta-Sunnah–Hadith-and-Narrators-Dataset](https://github.com/JehadOumer/Ifta-Sunnah-Hadith-and-Narrators-Dataset). The platform provides **33 foundational hadith books** (Kutub al-Matn), plus 57 additional books with supportive and interpretative content; the hadith dataset used here was extracted from the **33 books**, which are organized mainly by hadith content (thematic). In total, **276,347 hadiths** were extracted from the platform; after preprocessing (isnad isolation, entity tagging, relation tuples, and filtering), **223,478 hadith instances** were used for automatic isnad parsing.
136
+
137
+ **Splits.** The dataset was split **70% training, 15% validation, 15% testing**. Summary:
138
+
139
+ | | Training | Validation | Testing |
140
+ |---|----------|------------|---------|
141
+ | **Hadiths** | 156,434 | 33,521 | 33,523 |
142
+ | **Duplets** (chain-blind relations) | 756,714 | 162,782 | 162,010 |
143
+ | **Triplets** (chain-aware relations) | 877,016 | 190,049 | 188,755 |
144
+
145
+ ---
146
+
147
+ ## Results summary (test set)
148
+
149
+ Models are evaluated on the **held-out test set** (33,523 hadith isnad texts). Metrics: exact match for relations; narrator set comparison for narrator recognition. Tables below include **with-prefix** and **no-prefix** variants for the encoder–decoder models.
150
+
151
+ ### Narrator recognition (predicted vs gold set of narrators)
152
+
153
+ | Model | Precision | Recall | F1 |
154
+ |-------|-----------|--------|-----|
155
+ | CAMeLBERT-CA | 0.98 | 0.99 | 0.98 |
156
+ | AraT5 | 0.97 | 0.97 | 0.97 |
157
+ | AraT5-Prefix | 0.97 | 0.97 | 0.97 |
158
+ | AraBART | 0.78 | 0.94 | 0.84 |
159
+ | AraBART-Prefix | 0.78 | 0.94 | 0.84 |
160
+
161
+ ### Chain-blind relation extraction (duplets)
162
+
163
+ | Model | Precision | Recall | F1 |
164
+ |-------|-----------|--------|-----|
165
+ | CAMeLBERT-CA | 0.96 | 0.93 | 0.95 |
166
+ | AraT5 | 0.96 | 0.95 | 0.95 |
167
+ | AraT5-Prefix | 0.96 | 0.95 | 0.95 |
168
+ | AraBART | 0.66 | 0.64 | 0.65 |
169
+ | AraBART-Prefix | 0.66 | 0.65 | 0.65 |
170
+
171
+ ### Chain-aware relation extraction (triplets)
172
+
173
+ Only the encoder–decoder models produce chain-aware output.
174
+
175
+ | Model | Precision | Recall | F1 |
176
+ |-------|-----------|--------|-----|
177
+ | AraT5 | 0.94 | 0.93 | 0.93 |
178
+ | AraT5-Prefix | 0.94 | 0.93 | 0.93 |
179
+ | AraBART | 0.76 | 0.37 | 0.48 |
180
+ | AraBART-Prefix | 0.76 | 0.37 | 0.48 |
181
+
182
+ ---
183
+
184
+ ## Installation
185
+
186
+ ```bash
187
+ pip install torch transformers huggingface_hub safetensors
188
+ ```
189
+
190
+ The [CAMeLBERT-CA Hub repo](https://huggingface.co/Jehadoumer/ChainBlind-HadithIsnadParser-CAMeLBERT) provides `model.safetensors`; the inference script loads that when using `--model-id`.
191
+
192
+ ---
193
+
194
+ ## How to use
195
+
196
+ ### CAMeLBERT-CA (chain-blind): inference script in this repo
197
+
198
+ The CAMeLBERT-CA model does **not** output duplets directly from raw text. It needs a pipeline that: (1) runs NER to get narrator spans, (2) builds **candidate head–tail duplets**, and (3) runs the **biaffine classifier** on each candidate. This repo provides that pipeline in `camelbert_duplets_biaffine_inference.py`.
199
+
200
+ **Run inference** (loads the model from Hugging Face by default):
201
+
202
+ ```bash
203
+ # Built-in example isnad
204
+ python camelbert_duplets_biaffine_inference.py --no-cuda
205
+
206
+ # Your own isnad text (space-separated words)
207
+ python camelbert_duplets_biaffine_inference.py --input "حدثنا أبو معاوية حدثنا الأعمش عن مسلم" --no-cuda
208
+
209
+ # From a JSONL file (one JSON object per line with "isnad_sequence" or "isnad_text")
210
+ python camelbert_duplets_biaffine_inference.py --input-file examples.jsonl --no-cuda
211
+ ```
212
+
213
+ **Output:** One line per input in chain-blind form: `<head> A <tail> B <head> B <tail> C ... #hadith`
214
+
215
+ To use a **local checkpoint** (same format as training output): `--checkpoint path/to/best_model.pth`
216
+
217
+ **Test on random examples** from the predictions JSON (install `ijson` for large files):
218
+
219
+ ```bash
220
+ pip install ijson
221
+ python test_camelbert_inference_from_predictions.py --predictions-json "path/to/predictions_camelbert_duplets_biaffine.json" --num-examples 2 --no-cuda
222
+ ```
223
+
224
+ ### AraT5 / AraBART (chain-blind or chain-aware)
225
+
226
+ These models **generate** the linearized output directly from raw isnad text:
227
+
228
+ ```python
229
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
230
+
231
+ model_name = "Jehadoumer/ChainBlind-HadithIsnadParser-AraT5" # or any other model ID above
232
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
233
+ model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
234
+
235
+ # Input: raw isnad text (Arabic)
236
+ isnad_text = "..." # e.g. "حدثنا فلان، حدثنا فلان، عن فلان..."
237
+ inputs = tokenizer(isnad_text, return_tensors="pt", truncation=True, max_length=512)
238
+ outputs = model.generate(**inputs, max_new_tokens=512)
239
+
240
+ # Decode and parse <head> / <tail> (and optionally <chain_number>) from the output
241
+ decoded = tokenizer.decode(outputs[0], skip_special_tokens=False)
242
+ # Then extract (head, tail) or (head, tail, chain_number) from decoded.
243
+ ```
244
+
245
+ Output format: **chain-blind** — sequence of `<head> A <tail> B` segments ending with `#hadith`. **Chain-aware** — each segment also includes `<chain_number> k`. Parse with regex or the same logic as in the evaluation scripts.
246
+
247
+ ---
248
+
249
+ ## Reference
250
+
251
+ - **Thesis:** [*Relation Extraction for Graph‐Based Representation of Hadith Isnad*](https://www.um.edu.mt/library/oar/handle/123456789/137629) (OAR@UM, University of Malta, 2025). Describes the task, data, model architectures, training, and evaluation in detail.
252
+ - **Dataset:** [Ifta-Sunnah-Hadith-and-Narrators-Dataset](https://github.com/JehadOumer/Ifta-Sunnah-Hadith-and-Narrators-Dataset) — hadiths and narrator profiles with structured chains of narration. **Original source:** [Ifta' Sunnah Platform](https://sunna.alifta.gov.sa/) (King Abdullah bin Abdul Aziz Program for the Prophetic Sunnah).
253
+
254
+ If you use these models or the dataset in your work, please cite the thesis and the original data source as appropriate.
255
+
256
+ ---
257
+
258
+ ## License
259
+
260
  This project is released under an **open-source license** that allows use, modification, and distribution with **attribution to the creators**. The base models (CAMeLBERT-CA, AraT5, AraBART) follow their respective upstream licenses; the fine-tuned weights are released under the same open-source, attribution-based terms.