-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnew_data_interactive.py
47 lines (42 loc) · 1.32 KB
/
new_data_interactive.py
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
"""
python interactive.py
"""
import torch
from transformers import AutoModelWithLMHead, AutoTokenizer
model_name = "skt/kogpt2-base-v2"
ckpt_name = "model_save/skt-kogpt2-base-v2.pt"
model = AutoModelWithLMHead.from_pretrained(model_name)
tokenizer = AutoTokenizer.from_pretrained(model_name)
SPECIAL_TOKENS = {
"bos_token": "<bos>",
"eos_token": "<eos>",
"pad_token": "<pad>",
"sep_token": "<seq>"
}
SPECIAL_TOKENS_VALUES = ["<bos>", "<eos>", "<pad>", "<seq>"]
tokenizer.add_special_tokens(SPECIAL_TOKENS)
model.resize_token_embeddings(len(tokenizer))
model.load_state_dict(torch.load(ckpt_name, map_location="cpu"))
model.cuda()
with torch.no_grad():
while True:
t = input("\nUser: ")
tokens = tokenizer(
t,
return_tensors="pt",
truncation=True,
padding=True,
max_length=50
)
input_ids = tokens.input_ids.cuda()
attention_mask = tokens.attention_mask.cuda()
sample_output = model.generate(
input_ids,
do_sample=True,
max_length=50,
# max_new_tokens=50,
# top_k=50,
# return_dict_in_generate=True
)
gen = sample_output[0]
print("System: " + tokenizer.decode(gen[len(input_ids[0]):-1], skip_special_tokens=True))