Pre-trained on MSA + dialectal Arabic mixture for complementary coverage.
A cultural hallucination occurs when an LLM produces a response that is factually or culturally incorrect within Arab/Islamic contexts — misapplying Western legal frameworks (EU AI Act, GDPR) to Islamic jurisprudence, fabricating hadith or Islamic rulings, ignoring Arab institutional contributions to AI (KACST, SDAIA, MBZUAI, Vision 2030), responding in the wrong Arabic dialect, or using Western examples in Saudi/Gulf contexts.
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4tokenizer = AutoTokenizer.from_pretrained("HassanB4/sawb-arbertv2")
5model = AutoModelForSequenceClassification.from_pretrained("HassanB4/sawb-arbertv2")
6model.eval()
7
8question = "كيف تُطبَّق مبادئ أخلاقيات الذكاء الاصطناعي في القضاء الإسلامي؟"
9answer = "يجب تطبيق AI Act الأوروبي على المحاكم الإسلامية..."
10
11text = f"السؤال: {question}\n\nإجابة النموذج: {answer[:500]}"
12inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
13
14with torch.no_grad():
15 logits = model(**inputs).logits
16
17prob = torch.softmax(logits, dim=-1)[0, 1].item()
18is_hallucination = prob > 0.30
19print(f"Hallucination probability: {prob:.3f} | Detected: {is_hallucination}")
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4MODELS = [
5 "HassanB4/sawb-arabert",
6 "HassanB4/sawb-arabert-large",
7 "HassanB4/sawb-arbertv2",
8 "HassanB4/sawb-marbertv2",
9]
10THRESHOLD = 0.30
11
12question = "كيف تُطبَّق مبادئ أخلاقيات الذكاء الاصطناعي في القضاء الإسلامي؟"
13answer = "يجب تطبيق AI Act الأوروبي على المحاكم الإسلامية..."
14text = f"السؤال: {question}\n\nإجابة النموذج: {answer[:500]}"
15
16probs = []
17for model_name in MODELS:
18 tokenizer = AutoTokenizer.from_pretrained(model_name)
19 model = AutoModelForSequenceClassification.from_pretrained(model_name)
20 model.eval()
21 inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
22 with torch.no_grad():
23 logits = model(**inputs).logits
24 probs.append(torch.softmax(logits, dim=-1)[0, 1].item())
25
26ensemble_prob = sum(probs) / len(probs)
27is_hallucination = ensemble_prob > THRESHOLD
28print(f"Ensemble probability: {ensemble_prob:.3f} | Detected: {is_hallucination}")
HassanB4/sawb-arabic-hallucination-dataset — 1,828 Arabic (question, LLM answer) pairs covering 6 cultural hallucination categories.