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)
>> ์ค๋ฅด๋ฝ๋ด๋ฆฌ๋ฝ
์ฐ ๊ผญ๋๊ธฐ๋ฅผ ์ฌ๋ ค๋ค๋ณด๋
์๋ํ ๋ฉ๊ณ ์๋ํ
๋๋ญ๊ฐ์ง์ ๋งค๋ฌ๋ฆฐ
์์ ์ฐ์ ํ ๋ง๋ฆฌ
์ด๋ฆ ๋ชจ๋ฅผ ํ ํํฌ๊ธฐ ์๊ณ
์ด๋๋ก ๊ฐ ํ์ฉ ๋ ๋๊ฐ ๋ฒ๋ ธ๋ค
``` |