Update README.md
Browse files
README.md
CHANGED
@@ -4,3 +4,77 @@ language:
|
|
4 |
- uk
|
5 |
---
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
- uk
|
5 |
---
|
6 |
|
7 |
+
|
8 |
+
# UAlpaca: Ukrainian instruction-tuned LLaMA
|
9 |
+
|
10 |
+
## Usage
|
11 |
+
|
12 |
+
Check the Github repo with code: https://github.com/robinhad/kruk
|
13 |
+
|
14 |
+
Generation Example: [Open In Colab](https://colab.research.google.com/github/robinhad/kruk/blob/main/notebooks/ualpaca-lora.ipynb)
|
15 |
+
|
16 |
+
# Requirements
|
17 |
+
```bash
|
18 |
+
pip install bitsandbytes
|
19 |
+
pip install -q datasets loralib sentencepiece
|
20 |
+
pip install -q git+https://github.com/zphang/transformers@c3dc391 # this model uses a fork of transformers that provides LLaMA tokenizer; this wasn't merged yet
|
21 |
+
pip install -q git+https://github.com/huggingface/peft.git
|
22 |
+
```
|
23 |
+
```python
|
24 |
+
from peft import PeftModel
|
25 |
+
from transformers import LLaMATokenizer, LLaMAForCausalLM, GenerationConfig
|
26 |
+
|
27 |
+
tokenizer = LLaMATokenizer.from_pretrained("decapoda-research/llama-7b-hf")
|
28 |
+
model = LLaMAForCausalLM.from_pretrained(
|
29 |
+
"decapoda-research/llama-7b-hf",
|
30 |
+
load_in_8bit=True,
|
31 |
+
device_map="auto",
|
32 |
+
)
|
33 |
+
model = PeftModel.from_pretrained(model, "robinhad/ualpaca-7b-llama")
|
34 |
+
|
35 |
+
# convert input to correct prompt
|
36 |
+
def generate_prompt(instruction, input=None):
|
37 |
+
if input:
|
38 |
+
return f"""Унизу надається інструкція, яка описує завдання разом із вхідними даними, які надають додатковий контекст. Напиши відповідь, яка правильно доповнює запит.
|
39 |
+
|
40 |
+
### Інструкція:
|
41 |
+
{instruction}
|
42 |
+
|
43 |
+
### Вхідні дані:
|
44 |
+
{input}
|
45 |
+
|
46 |
+
### Відповідь:"""
|
47 |
+
else:
|
48 |
+
return f"""Унизу надається інструкція, яка описує завдання. Напиши відповідь, яка правильно доповнює запит.
|
49 |
+
|
50 |
+
### Інструкція:
|
51 |
+
{instruction}
|
52 |
+
|
53 |
+
### Відповідь:"""
|
54 |
+
|
55 |
+
# config and inference
|
56 |
+
generation_config = GenerationConfig(
|
57 |
+
temperature=0.2,
|
58 |
+
top_p=0.75,
|
59 |
+
num_beams=4,
|
60 |
+
)
|
61 |
+
|
62 |
+
def evaluate(instruction, input=None):
|
63 |
+
prompt = generate_prompt(instruction, input)
|
64 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
65 |
+
input_ids = inputs["input_ids"].cuda()
|
66 |
+
generation_output = model.generate(
|
67 |
+
input_ids=input_ids,
|
68 |
+
generation_config=generation_config,
|
69 |
+
return_dict_in_generate=True,
|
70 |
+
output_scores=True,
|
71 |
+
max_new_tokens=256
|
72 |
+
)
|
73 |
+
for s in generation_output.sequences:
|
74 |
+
output = tokenizer.decode(s)
|
75 |
+
print("Відповідь:", output.split("### Відповідь:")[1].strip())
|
76 |
+
|
77 |
+
input_data = "Як звали батька Тараса Григоровича Шевченка?"
|
78 |
+
print("Інструкція:", input_data)
|
79 |
+
evaluate("Інструкція: " + input_data)
|
80 |
+
```
|