Update README.md
Browse files
README.md
CHANGED
@@ -11,4 +11,67 @@ tags:
|
|
11 |
- Instruct Alpaca
|
12 |
- PEFT
|
13 |
- LoRA
|
14 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
- Instruct Alpaca
|
12 |
- PEFT
|
13 |
- LoRA
|
14 |
+
---
|
15 |
+
|
16 |
+
## How to use
|
17 |
+
```py
|
18 |
+
import torch
|
19 |
+
import bitsandbytes as bnb
|
20 |
+
from peft import PeftModel, PeftConfig, prepare_model_for_int8_training, LoraConfig, get_peft_model
|
21 |
+
from transformers import LlamaTokenizer, LlamaForCausalLM, GenerationConfig
|
22 |
+
|
23 |
+
peft_model_id = "Yasbok/Alpaca_instruction_fine_tune_Arabic"
|
24 |
+
# config = PeftConfig.from_pretrained(peft_model_id)
|
25 |
+
|
26 |
+
tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
|
27 |
+
model = LlamaForCausalLM.from_pretrained("decapoda-research/llama-7b-hf",
|
28 |
+
load_in_8bit=True,
|
29 |
+
device_map="auto",)
|
30 |
+
# Load the Lora model
|
31 |
+
model = PeftModel.from_pretrained(model, peft_model_id)
|
32 |
+
|
33 |
+
# Based on the inference code by `tloen/alpaca-lora`
|
34 |
+
def generate_prompt(instruction, input=None):
|
35 |
+
if input:
|
36 |
+
return f"""يوجد أدناه تعليمات تصف مهمة ، إلى جانب إدخال يوفر المزيد من السياق. اكتب ردًا يكمل الطلب بشكل مناسب.
|
37 |
+
|
38 |
+
|
39 |
+
### تعليمات:
|
40 |
+
{instruction}
|
41 |
+
|
42 |
+
### مدخل:
|
43 |
+
{input}
|
44 |
+
|
45 |
+
### انتاج:"""
|
46 |
+
else:
|
47 |
+
return f"""يوجد أدناه إرشادات تصف مهمة. يُرجى كتابة رد يكمل الطلب بشكل مناسب.
|
48 |
+
|
49 |
+
|
50 |
+
|
51 |
+
### تعليمات:
|
52 |
+
{instruction}
|
53 |
+
|
54 |
+
generation_config = GenerationConfig(
|
55 |
+
temperature=0.2,
|
56 |
+
top_p=0.75,
|
57 |
+
num_beams=4,
|
58 |
+
)
|
59 |
+
|
60 |
+
def evaluate(instruction, input=None):
|
61 |
+
prompt = generate_prompt(instruction, input)
|
62 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
63 |
+
input_ids = inputs["input_ids"].cuda()
|
64 |
+
generation_output = model.generate(
|
65 |
+
input_ids=input_ids,
|
66 |
+
generation_config=generation_config,
|
67 |
+
return_dict_in_generate=True,
|
68 |
+
output_scores=True,
|
69 |
+
max_new_tokens=256
|
70 |
+
)
|
71 |
+
for s in generation_output.sequences:
|
72 |
+
output = tokenizer.decode(s)
|
73 |
+
print("انتاج:", output.split("### انتاج:")[1].strip())
|
74 |
+
instruction = "Tell me about alpacas"
|
75 |
+
|
76 |
+
evaluate(input("تعليمات: "))
|
77 |
+
```
|