Update README.md
Browse files
README.md
CHANGED
|
@@ -9,8 +9,104 @@ tags: []
|
|
| 9 |
|
| 10 |
``` python
|
| 11 |
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
```
|
| 16 |
|
|
|
|
| 9 |
|
| 10 |
``` python
|
| 11 |
|
| 12 |
+
import os
|
| 13 |
+
import torch
|
| 14 |
+
import pandas as pd
|
| 15 |
+
from datasets import Dataset
|
| 16 |
+
from trl import SFTTrainer
|
| 17 |
+
from huggingface_hub import login
|
| 18 |
+
import re
|
| 19 |
+
from peft import LoraConfig, get_peft_model
|
| 20 |
+
import numpy as np
|
| 21 |
+
from transformers import (
|
| 22 |
+
AutoTokenizer,
|
| 23 |
+
Llama4ForConditionalGeneration,
|
| 24 |
+
BitsAndBytesConfig,
|
| 25 |
+
TrainingArguments,
|
| 26 |
+
DataCollatorForLanguageModeling,
|
| 27 |
+
AutoModelForCausalLM
|
| 28 |
+
)
|
| 29 |
|
| 30 |
+
#should install transformers 4.51.3
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
hf_token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxe"
|
| 34 |
+
login(hf_token)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
model_id = "NYUAD-ComNets/NYUAD_Llama4_Inheritance_Solver"
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Load tokenizer and model
|
| 42 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
|
| 43 |
+
model = Llama4ForConditionalGeneration.from_pretrained(
|
| 44 |
+
model_id,
|
| 45 |
+
device_map="auto",
|
| 46 |
+
torch_dtype=torch.bfloat16,
|
| 47 |
+
trust_remote_code=True
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
# Template for inference prompt
|
| 51 |
+
|
| 52 |
+
inference_prompt_template = """
|
| 53 |
+
أنت خبير في علم المواريث في الشريعة الإسلامية. استخدم التفكير خطوة بخطوة لتحديد أنصبة الورثة. ابدأ دائماً بذكر الورثة، وتحديد نوعهم (مثل: زوج، ابن، أخ)، ثم تحقق من وجود فرع وارث أو أصل وارث. بعد ذلك، طبّق الفرائض المقدّرة ثم قواعد التعصيب إذا وُجد فائض في التركة.
|
| 54 |
+
|
| 55 |
+
اتبع الخطوات التالية:
|
| 56 |
+
|
| 57 |
+
اذكر الورثة.
|
| 58 |
+
|
| 59 |
+
حدد الفروض المقدّرة لكل وارث.
|
| 60 |
+
|
| 61 |
+
افحص وجود الحجب والتقديم.
|
| 62 |
+
|
| 63 |
+
وزّع الباقي إن وجد بالتعصيب.
|
| 64 |
+
|
| 65 |
+
تحقق من أن مجموع الأنصبة يساوي كامل التركة.
|
| 66 |
+
|
| 67 |
+
Then output your final answer using a single word only from this list A, B, C, D, E, F.
|
| 68 |
+
### Context:
|
| 69 |
+
{}
|
| 70 |
+
|
| 71 |
+
### Response:
|
| 72 |
+
{}"""
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def generate_answer(context):
|
| 77 |
+
prompt = inference_prompt_template.format(context, "")
|
| 78 |
+
inputs = tokenizer(prompt + tokenizer.eos_token, return_tensors="pt").to("cuda")
|
| 79 |
+
|
| 80 |
+
with torch.no_grad():
|
| 81 |
+
outputs = model.generate(
|
| 82 |
+
input_ids=inputs["input_ids"],
|
| 83 |
+
attention_mask=inputs["attention_mask"],
|
| 84 |
+
max_new_tokens=10,
|
| 85 |
+
eos_token_id=tokenizer.eos_token_id,
|
| 86 |
+
use_cache=True,
|
| 87 |
+
temperature =0.1,
|
| 88 |
+
top_p=1
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
response = tokenizer.batch_decode(outputs, skip_special_tokens=True)
|
| 92 |
+
print(response)
|
| 93 |
+
|
| 94 |
+
response = response[0].split("### Response:")[1][-1]
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
df=pd.read_csv('/path_to/islamic_inheritance_problem.csv.csv')
|
| 99 |
+
for k,o1,o2,o3,o4,o5,o6 in zip(df.question.values
|
| 100 |
+
,df.option1.values,df.option2.values
|
| 101 |
+
,df.option3.values,df.option4.values
|
| 102 |
+
,df.option5.values,df.option6.values):
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
example = k+' '+o1+' '+o2+' '+o3+' '+o4+' '+o5+' '+o6
|
| 106 |
+
|
| 107 |
+
predicted_label = generate_answer(example)
|
| 108 |
+
print("Predicted:", predicted_label)
|
| 109 |
+
|
| 110 |
|
| 111 |
```
|
| 112 |
|