Update README.md
Browse files
README.md
CHANGED
|
@@ -31,9 +31,75 @@ inference: true
|
|
| 31 |
> باستخدام مكتبة `transformers`:
|
| 32 |
|
| 33 |
```python
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
model_id = "<username>/<repo-name>" # هذا المستودع
|
| 37 |
-
tok = AutoTokenizer.from_pretrained(model_id, use_fast=True)
|
| 38 |
-
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
|
| 39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
> باستخدام مكتبة `transformers`:
|
| 32 |
|
| 33 |
```python
|
| 34 |
+
!pip install --upgrade bitsandbytes
|
| 35 |
+
!pip install -q datasets
|
| 36 |
+
!pip install -q trl
|
| 37 |
+
!pip install git+https://github.com/huggingface/peft.git
|
| 38 |
+
!pip install -q -U accelerate
|
| 39 |
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
from huggingface_hub import login
|
| 42 |
+
login()
|
| 43 |
+
|
| 44 |
+
import torch
|
| 45 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
|
| 46 |
+
from peft import LoraConfig, get_peft_model, prepare_model_for_kbit_training, PeftModel, PeftConfig
|
| 47 |
+
from datasets import load_dataset
|
| 48 |
+
from transformers import TrainingArguments, pipeline
|
| 49 |
+
from trl import SFTTrainer
|
| 50 |
+
|
| 51 |
+
bnb_cfg = BitsAndBytesConfig(
|
| 52 |
+
load_in_4bit=True,
|
| 53 |
+
bnb_4bit_quant_type="nf4",
|
| 54 |
+
bnb_4bit_use_double_quant=True,
|
| 55 |
+
bnb_4bit_compute_dtype="bfloat16",
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
# -*- coding: utf-8 -*-
|
| 59 |
+
|
| 60 |
+
import torch
|
| 61 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 62 |
+
model_path = "3okasha/jais-finetuned-v1"
|
| 63 |
+
|
| 64 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 65 |
+
|
| 66 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 67 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 68 |
+
model_path,
|
| 69 |
+
quantization_config=bnb_cfg,
|
| 70 |
+
device_map="auto",
|
| 71 |
+
trust_remote_code=True
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
def user_prompt(human_prompt):
|
| 75 |
+
prompt_template=f"### HUMAN:\n{human_prompt}\n\n### RESPONSE:\n"
|
| 76 |
+
return prompt_template
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
model.config.use_cache = False
|
| 80 |
+
if hasattr(model, "generation_config"): model.generation_config.use_cache = False
|
| 81 |
+
|
| 82 |
+
def get_response(text,tokenizer=tokenizer,model=model):
|
| 83 |
+
input_ids = tokenizer(text, return_tensors="pt").input_ids
|
| 84 |
+
inputs = input_ids.to(device)
|
| 85 |
+
input_len = inputs.shape[-1]
|
| 86 |
+
generate_ids = model.generate(
|
| 87 |
+
inputs,
|
| 88 |
+
top_p=0.9,
|
| 89 |
+
temperature=0.3,
|
| 90 |
+
max_length=50-input_len,
|
| 91 |
+
min_length=input_len + 4,
|
| 92 |
+
repetition_penalty=1.2,
|
| 93 |
+
do_sample=True,
|
| 94 |
+
)
|
| 95 |
+
response = tokenizer.batch_decode(
|
| 96 |
+
generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=True
|
| 97 |
+
)[0]
|
| 98 |
+
return response
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
text= user_prompt("كيف الحال")
|
| 102 |
+
print(get_response(text))
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
```
|