Text Generation
Transformers
Japanese
English
Inference Endpoints
File size: 2,578 Bytes
28ee93f
 
7b68403
 
 
 
 
 
 
 
28ee93f
7b68403
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
---
license: cc-by-sa-3.0
datasets:
- databricks/databricks-dolly-15k
- kunishou/databricks-dolly-69k-ja-en-translation
language:
- ja
- en
library_name: transformers
pipeline_tag: text-generation
---
[cyberagent/open-calm-7b](https://huggingface.co/cyberagent/open-calm-7b)に対して[kunishou/databricks-dolly-69k-ja-en-translation](https://huggingface.co/datasets/kunishou/databricks-dolly-69k-ja-en-translation)をpeftを用いて(というより[tloen/alpaca-lora](https://github.com/tloen/alpaca-lora)を改変して)チューニングしたものの差分です。  
lora-alpacaから学習時のパラメータは特に変えていません。 
```
import torch
from peft import PeftModel
from transformers import AutoModelForCausalLM, AutoTokenizer

LOAD_8BIT = False
BASE_MODEL = "cyberagent/open-calm-7b"
LORA_WEIGHTS = "nakayama/lora-db-dolly-69k-ja-en-translation-for-open-calm-7b"

tokenizer = AutoTokenizer.from_pretrained(BASE_MODEL)

model = AutoModelForCausalLM.from_pretrained(
    BASE_MODEL,
    load_in_8bit=LOAD_8BIT,
    torch_dtype=torch.float16,
    device_map="auto",
)
model = PeftModel.from_pretrained(
    model,
    LORA_WEIGHTS,
    torch_dtype=torch.float16,
    adapter_name=LORA_WEIGHTS
)

def generate_prompt(instruction, input=None):
    if input:
        return f"""以下は、タスクを説明する命令と、さらなるコンテキストを提供する入力の組み合わせです。要求を適切に満たすような応答を書きなさい。

### Instruction:
{instruction}

### Input:
{input}

### Response:"""
    else:
        return f"""以下は、ある作業を記述した指示です。依頼を適切に完了させる回答を書きなさい。

### Instruction:
{instruction}

### Response:"""

if not LOAD_8BIT:
    model.half()

instruction="次に示す日本語の文章を英語に翻訳しなさい。"

input="富士山はとても高い山で、その高さは日本一と言われています。"

prompt = generate_prompt(instruction, input)
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

with torch.no_grad():
    generation_output = model.generate(
        **inputs,
        do_sample=True,
        temperature=0.1,
        top_p=0.75,
        top_k=20,
        return_dict_in_generate=True,
        output_scores=True,
        max_new_tokens=128,
        repetition_penalty=1.5,
        no_repeat_ngram_size=5,
        pad_token_id=tokenizer.pad_token_id,
    )
s = generation_output.sequences[0]
output = tokenizer.decode(s)
print(output.split("### Response:")[1].strip())
```