Instructions to use ahmedewaidar22/ar-route with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ahmedewaidar22/ar-route with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="ahmedewaidar22/ar-route")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("ahmedewaidar22/ar-route", device_map="auto") - Notebooks
- Google Colab
- Kaggle
AR-Route: Dialect-Aware, Confidence-Gated Retrieval for Arabic RAG
Models and retrieval corpus for the paper "Dialect-Aware, Confidence-Gated Retrieval for Arabic RAG: When Learned Complexity Routing Fails to Transfer" (Ahmed Dewaidar, Mohamed Shaheen, Eman AbdelAziz; Arab Academy for Science, Technology and Maritime Transport).
- Code: https://github.com/ahmedewaidar22/Dialect-Aware-Confidence-Gated-Retrieval-for-Arabic-RAG
- Evaluation data: https://huggingface.co/datasets/ahmedewaidar22/ar-route-eval (DOI: 10.57967/hf/11866)
The proposed model
- A dialect head (this repo) classifies the question as Egyptian Arabic or Modern Standard Arabic (MSA).
- Egyptian questions are rewritten into MSA by an LLM (gpt-4.1-mini); MSA questions are left unchanged.
- Hybrid retrieval (BM25 + BGE-M3 dense search, reciprocal rank fusion) over the corpus (this repo) returns candidates, which are reranked with bge-reranker-v2-m3 down to the top two passages.
- The generator answers from those two passages. Only if it answers "I don't know" does the system escalate to iterative multi-step retrieval.
On a frozen confirmatory test of 1,027 Egyptian questions it reached 76.8% accuracy against 74.3% for standard RAG (+2.5 points, McNemar p = 0.012) at 7% lower generator cost, and stayed within a 5-point non-inferiority margin of always-iterative retrieval at 41% lower generator cost.
Contents
| Folder | What it is | Used by the proposed model? |
|---|---|---|
dialect_head/ |
AraBERT v0.2 base fine-tuned to classify Egyptian Arabic vs MSA (model.safetensors, tokenizer, meta.json, evaluation report and test-set scores). |
Yes |
corpus/ |
Retrieval corpus of 25,147 Arabic passages (passages.jsonl), precomputed BGE-M3 embeddings (emb_bgem3.npy, emb_ids.json) and corpus_stats.txt. |
Yes |
final_router/ |
Router v1, the learned three-stage complexity router studied in the paper (text head, two reader heads, stage B/C classifiers and policy.json thresholds). |
No. Included for reproducibility: it beat random mixing of strategies on training-like data but did not beat standard RAG at equal cost on held-out Egyptian questions. |
Corpus sources (passages): ArabicaQA 14,051; HotpotQA contexts in Arabic 9,389; MAFQA 1,162; ArDQA 345; pilot set 200.
Usage: dialect head
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from huggingface_hub import snapshot_download
path = snapshot_download("ahmedewaidar22/ar-route", allow_patterns=["dialect_head/*"])
tok = AutoTokenizer.from_pretrained(f"{path}/dialect_head")
model = AutoModelForSequenceClassification.from_pretrained(f"{path}/dialect_head").eval()
q = "امتى اتبنى السد العالي؟"
with torch.no_grad():
probs = model(**tok(q, return_tensors="pt", truncation=True)).logits.softmax(-1)[0]
print({model.config.id2label[i]: round(p.item(), 3) for i, p in enumerate(probs)})
In the paper, questions with an Egyptian probability above 0.8 are rewritten into MSA, questions below 0.2 are searched as written, and questions in between are searched both ways with the two rankings fused. The full pipeline, including retrieval and the fallback, is in the code repository.
Limitations
The dialect head distinguishes only Egyptian Arabic from MSA; other dialects are out of scope. Results were measured with gpt-4.1-mini (and gpt-6.1-sol as a second generator) on one corpus; other generators, retrievers and dialects may behave differently. Rewriting adds about 0.8 s per Egyptian question.
License
Apache-2.0 for the models and code in this repository. Corpus passages come from public datasets (ArabicaQA, HotpotQA, MAFQA, ArDQA) and remain under their original terms; please cite those datasets when you use the corpus.
Citation
@misc{dewaidar2026arroute,
title = {Dialect-Aware, Confidence-Gated Retrieval for Arabic RAG: When Learned Complexity Routing Fails to Transfer},
author = {Ahmed Dewaidar , Mohamed Shaheen and Eman AbdelAziz},
year = {2026},
note = {Under review}
}
Model tree for ahmedewaidar22/ar-route
Base model
aubmindlab/bert-base-arabertv02