File size: 1,005 Bytes
6ee6039
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
```python
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM

# device setting
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# load model and tokenizer
model_name_or_path = "ddobokki/gpt2_poem"

tokenizer = AutoTokenizer.from_pretrained(model_name_or_path)
model = AutoModelForCausalLM.from_pretrained(model_name_or_path)
model.to(device)

keyword_start_token = "<k>"
keyword_end_token = "</k>"
text = "์‚ฐ ๊ผญ๋Œ€๊ธฐ๊ฐ€ ๋ณด์ด๋Š” ๊ฒฝ์น˜"
input_text = keyword_start_token + text + keyword_end_token

input_ids = tokenizer.encode(input_text, return_tensors="pt").to(device)
gen_ids = model.generate(
    input_ids, max_length=64, num_beams=100, no_repeat_ngram_size=2
)
generated = tokenizer.decode(gen_ids[0, :].tolist(), skip_special_tokens=True)
>> ์˜ค๋ฅด๋ฝ๋‚ด๋ฆฌ๋ฝ
์‚ฐ ๊ผญ๋Œ€๊ธฐ๋ฅผ ์˜ฌ๋ ค๋‹ค๋ณด๋‹ˆ
์•„๋“ํžˆ ๋ฉ€๊ณ  ์•„๋“ํ•œ
๋‚˜๋ญ‡๊ฐ€์ง€์— ๋งค๋‹ฌ๋ฆฐ
์ž‘์€ ์‚ฐ์ƒˆ ํ•œ ๋งˆ๋ฆฌ
์ด๋ฆ„ ๋ชจ๋ฅผ ํ’€ ํ•œํฌ๊ธฐ ์•ˆ๊ณ 
์–ด๋””๋ก ๊ฐ€ ํ›Œ์ฉ ๋– ๋‚˜๊ฐ€ ๋ฒ„๋ ธ๋‹ค
```