gpt2_poem / README.md
ddobokki's picture
add: README.md
6ee6039
|
raw
history blame
1.01 kB
```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)
>> ์˜ค๋ฅด๋ฝ๋‚ด๋ฆฌ๋ฝ
์‚ฐ ๊ผญ๋Œ€๊ธฐ๋ฅผ ์˜ฌ๋ ค๋‹ค๋ณด๋‹ˆ
์•„๋“ํžˆ ๋ฉ€๊ณ  ์•„๋“ํ•œ
๋‚˜๋ญ‡๊ฐ€์ง€์— ๋งค๋‹ฌ๋ฆฐ
์ž‘์€ ์‚ฐ์ƒˆ ํ•œ ๋งˆ๋ฆฌ
์ด๋ฆ„ ๋ชจ๋ฅผ ํ’€ ํ•œํฌ๊ธฐ ์•ˆ๊ณ 
์–ด๋””๋ก ๊ฐ€ ํ›Œ์ฉ ๋– ๋‚˜๊ฐ€ ๋ฒ„๋ ธ๋‹ค
```