Views
No views yet
1import os
2import torch
3import pandas as pd
4from datasets import Dataset
5from trl import SFTTrainer
6from huggingface_hub import login
7import re
8from peft import LoraConfig, get_peft_model
9import numpy as np
10from transformers import (
11 AutoTokenizer,
12 Llama4ForConditionalGeneration,
13 BitsAndBytesConfig,
14 TrainingArguments,
15 DataCollatorForLanguageModeling,
16 AutoModelForCausalLM
17)
18
19#should install transformers 4.51.3
20
21hf_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxe"
22login(hf_token)
23
24model_id = "NYUAD-ComNets/NYUAD_Llama4_Inheritance_Solver2"
25
26# Load tokenizer and model
27tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
28model = Llama4ForConditionalGeneration.from_pretrained(
29 model_id,
30 device_map="auto",
31 torch_dtype=torch.bfloat16,
32 trust_remote_code=True
33)
34
35# Template for inference prompt
36
37inference_prompt_template = """
38أنت خبير في علم المواريث في الشريعة الإسلامية. استخدم التفكير خطوة بخطوة لتحديد أنصبة الورثة. ابدأ دائماً بذكر الورثة، وتحديد نوعهم (مثل: زوج، ابن، أخ)، ثم تحقق من وجود فرع وارث أو أصل وارث. بعد ذلك، طبّق الفرائض المقدّرة ثم قواعد التعصيب إذا وُجد فائض في التركة.
39اتبع الخطوات التالية:
40اذكر الورثة.
41حدد الفروض المقدّرة لكل وارث.
42افحص وجود الحجب والتقديم.
43وزّع الباقي إن وجد بالتعصيب.
44تحقق من أن مجموع الأنصبة يساوي كامل التركة.
45Then output your final answer using a single word only from this list A, B, C, D, E, F.
46### Context:
47{}
48
49### Response:
50{}"""
51
52def generate_answer(context):
53 prompt = inference_prompt_template.format(context, "")
54 inputs = tokenizer(prompt + tokenizer.eos_token, return_tensors="pt").to("cuda")
55
56 with torch.no_grad():
57 outputs = model.generate(
58 input_ids=inputs["input_ids"],
59 attention_mask=inputs["attention_mask"],
60 max_new_tokens=10,
61 eos_token_id=tokenizer.eos_token_id,
62 use_cache=True,
63 temperature =0.1,
64 top_p=1
65 )
66 response = tokenizer.batch_decode(outputs, skip_special_tokens=True)
67 print(response)
68
69 response = response[0].split("### Response:")[1][-1]
70
71df=pd.read_csv('/path_to/islamic_inheritance_problem.csv.csv')
72for k,o1,o2,o3,o4,o5,o6 in zip(df.question.values
73 ,df.option1.values,df.option2.values
74 ,df.option3.values,df.option4.values
75 ,df.option5.values,df.option6.values):
76
77
78 example = k+' '+o1+' '+o2+' '+o3+' '+o4+' '+o5+' '+o6
79
80 predicted_label = generate_answer(example)
81 print("Predicted:", predicted_label)
@inproceedings{aldahoul2025nyuad,
title={Nyuad at qias shared task: Benchmarking the legal reasoning of llms in arabic islamic inheritance cases},
author={AlDahoul, Nouar and Zaki, Yasir},
booktitle={Proceedings of The Third Arabic Natural Language Processing Conference: Shared Tasks},
pages={861--866},
year={2025}
}
@article{aldahoul2025benchmarking,
title={Benchmarking the Legal Reasoning of LLMs in Arabic Islamic Inheritance Cases},
author={AlDahoul, Nouar and Zaki, Yasir},
journal={arXiv preprint arXiv:2508.15796},
year={2025}
}