-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
141 lines (118 loc) · 3.91 KB
/
inference.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import json
import tensorflow as tf
import sentencepiece as spm
from typing import Dict
from tqdm import tqdm
from hydra import initialize, compose
from utils.data import token_to_sentence
from model.chatbot import *
from model.model import DialogBERT, Generator
def inference(config_name: str,
config_path: str,
save_path: Dict,
vocab_path: str,
data_path: str):
"""inference
Args:
config_name (str): config file name
config_path (str): config path
save_path ({'dialog': str, 'gen': str}): dialogBERT and Generator weights path
vocab_path (str): vocabulary path
data_path (str): sample data path
"""
initialize(config_path)
cfg = compose(config_name)
# MODEL
dialog = DialogBERT(
cfg.model.vocab_size,
cfg.processing.uttr_len,
cfg.processing.cntxt_len,
cfg.model.d_h,
cfg.model.head,
cfg.model.d_ff,
cfg.model.uttr_layer,
cfg.model.cntxt_layer,
cfg.processing.p,
)
gen = Generator(
cfg.model.vocab_size,
cfg.processing.uttr_len,
cfg.model.d_h,
cfg.model.head,
cfg.model.d_ff,
cfg.model.uttr_layer,
cfg.processing.p,
)
dialog.load_weights(save_path['dialog'])
gen.load_weights(save_path['gen'])
type = input('MODEL TYPE\n' +
' 1. GreedyChatbot\n' +
' 2. BeamChatbot\n' +
' 3. CheatAllChatbot\n' +
' 4. CheatFirstChatbot\n:')
if type == '1':
chatbot = GreedyChatbot(dialog, gen)
elif type == '2':
chatbot = BeamChatbot(dialog, gen, 5, cfg.model.vocab_size)
elif type == '3':
chatbot = CheatAllChatbot(dialog, gen)
elif type == '4':
chatbot = CheatFirstChatbot(dialog, gen)
else:
return
# TOKENIZER
sp = spm.SentencePieceProcessor()
sp.Load(vocab_path)
# DATA
inst_f = open(data_path, "r", encoding="utf-8")
insts = json.load(inst_f)
cntxts, resps, idxs = [], [], []
for i, inst in tqdm(enumerate(insts), desc="Loading instances..."):
cntxts.append(inst['cntxt'])
resps.append(inst['resp'])
idx = []
for uttr in inst['cntxt']:
idx.append(sp.PieceToId(uttr))
if type == '3' or type == '4':
idx.append(sp.PieceToId(inst['resp']))
idxs.append(idx)
inputs = tf.convert_to_tensor(idxs, dtype=tf.float32)
# INFERENCE
while True:
i = input('num (exit == -1): ')
i = int(i)
if i == -1: exit()
resp = tf.cast(chatbot(inputs[i]), dtype=tf.int32)
print('context: ')
for idx, uttr in enumerate(cntxts[i]):
uttr = token_to_sentence(uttr)
print(f'{idx}: {uttr}')
if type == '1' or type == '3' or type == '4':
print('predict response: ')
predict = sp.IdToPiece(resp.numpy().tolist())
predict = token_to_sentence(predict)
print(predict)
elif type == '2':
print('predict response: ')
predict = sp.IdToPiece(resp[0].numpy().tolist())
predict = token_to_sentence(predict)
print(predict)
print('candidate response: ')
for idx, cand in enumerate(resp[1:]):
predict = sp.IdToPiece(cand.numpy().tolist())
predict = token_to_sentence(predict)
print(f"cand{idx}: {predict}")
print('true response: ')
true = token_to_sentence(resps[i])
print(true)
if __name__=="__main__":
version = "base_2_3"
config_name = f"{version}.yaml"
config_path = "./configs"
save_path = {
"dialog": f"./save/dialog_{version}",
"gen": f"./save/gen_{version}"
}
vocab_path = "./data/emotion/spm.model"
data_path = "./data/emotion/token_val.json"
inference(config_name, config_path, save_path, vocab_path, data_path)