File size: 2,053 Bytes
056d53a
 
 
 
 
 
e854a70
056d53a
e854a70
4bc83e6
ed5c33f
056d53a
 
 
 
 
 
 
 
 
 
 
 
990f234
 
e854a70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
---
language:
- id
pipeline_tag: conversational
---

# How to Use : 

## Load the 🦙 Alpaca-LoRA model

```python
import torch
import bitsandbytes as bnb
from transformers import LlamaTokenizer, LlamaForCausalLM, GenerationConfig
from peft import PeftModel, PeftConfig, prepare_model_for_int8_training, LoraConfig, get_peft_model

peft_model_id = "firqaaa/indo-Alpaca-LoRA-7b"

tokenizer = LlamaTokenizer.from_pretrained("decapoda-research/llama-7b-hf")
model = LlamaForCausalLM.from_pretrained("decapoda-research/llama-7b-hf",
                                         load_in_8bit=True,
                                         device_map="auto")
# Load the LoRA model
model = PeftModel.from_pretrained(model, peft_model_id)
```
## Create Prompt Template

```python
def generate_prompt(instruction, input=None):
    if input:
        return f"""Berikut ini adalah petunjuk yang menjelaskan tugas, serta masukan yang menyediakan konteks tambahan. Tulis balasan yang melengkapi permintaan dengan tepat.

Petunjuk:
{instruction}

Masukan:
{input}

Output:"""

    else:
        return f"""Berikut ini terdapat panduan yang menjelaskan tugas. Mohon tuliskan balasan yang melengkapi permintaan dengan tepat.

Panduan:
{instruction}

Output:"""
```

## Evaluation
You are free to change parameters inside `GenerationConfig` to get better result.

```python
generation_config = GenerationConfig(
    temperature=0.2,
    top_p=0.75,
    num_beams=8
)

def evaluate(instruction, input=None):
    prompt = generate_prompt(instruction, input)
    inputs = tokenizer(prompt, return_tensors="pt")
    input_ids = inputs["input_ids"].cuda()
    generation_output = model.generate(
        input_ids=input_ids,
        generation_config=generation_config,
        return_dict_in_generate=True,
        output_scores=True,
        max_new_tokens=256
    )
    for s in generation_output.sequences:
        output = tokenizer.decode(s)
        print("Output:", output.split("Output:")[1].strip())

# input your question/instruction
evaluate(input("Petunjuk: "))
```