This model is fine-tuned for paraphrasing Arabic sentences while preserving the original meaning of the sentence. It was trained using the Supervised Fine-Tuning (SFT) method on the Jais-13B model using the TRL library (SFTTrainer) and the PEFT/LoRA library.
1# -*- coding: utf-8 -*-
2
3!pip install --upgrade bitsandbytes
4!pip install -q datasets
5!pip install -q trl
6!pip install git+https://github.com/huggingface/peft.git
7!pip install -q -U accelerate
8
9
10from huggingface_hub import login
11login()
12
13import torch
14from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
15from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training, PeftModel, PeftConfig
16from datasets import load_dataset
17from transformers import TrainingArguments, pipeline
18from trl import SFTTrainer
19
20bnb_cfg = BitsAndBytesConfig(
21 load_in_8bit=True,
22# bnb_4bit_quant_type="nf4",
23# bnb_4bit_use_double_quant=True,
24 bnb_4bit_compute_dtype="bfloat16",
25)
26
27
28
29import torch
30from transformers import AutoTokenizer, AutoModelForCausalLM
31model_path = "3okasha/jais-finetuned-v1"
32
33device = "cuda" if torch.cuda.is_available() else "cpu"
34
35tokenizer = AutoTokenizer.from_pretrained(model_path)
36model = AutoModelForCausalLM.from_pretrained(
37 model_path,
38 quantization_config=bnb_cfg,
39 device_map="auto",
40 trust_remote_code=True
41 )
42
43def user_prompt(human_prompt):
44 prompt_template=f"input:\n{human_prompt}\n\nparaphrize:\n"
45 return prompt_template
46
47
48model.config.use_cache = False
49if hasattr(model, "generation_config"): model.generation_config.use_cache = False
50
51def get_response(text,tokenizer=tokenizer,model=model):
52 input_ids = tokenizer(text, return_tensors="pt").input_ids
53 inputs = input_ids.to(device)
54 input_len = inputs.shape[-1]
55 generate_ids = model.generate(
56 inputs,
57 top_p=0.9,
58 temperature=0.3,
59 max_length=50-input_len,
60 min_length=input_len + 4,
61 repetition_penalty=1.2,
62 do_sample=True,
63 )
64 response = tokenizer.batch_decode(
65 generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
66 )[0]
67 return response
68
69
70text= user_prompt("أعتقد يمكننا أن نبدأ")
71print(get_response(text))
72## أعتقد أنه يمكننا البدأ
73## أعتقد أننا يمكن أن نبدأ
74