-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
49 lines (38 loc) · 1.45 KB
/
test.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
import os
import sys
from typing import NoReturn
from config import CHECKPOINT_FILE_NAME, BATCH_SIZE, CLASSES
from models import create_model
from preprocessing import tokenize, encode_inputs
def main() -> NoReturn:
with open(sys.argv[1], 'r') as f:
text = f.read()
inputs, _ = tokenize(text)
encoded_inputs = encode_inputs(inputs, remove_too_long=False)
model = create_model()
checkpoint_file_path = os.environ.get('CHECKPOINT_FILE',
os.path.join(os.environ.get('CHECKPOINTS_DIR', '.'),
CHECKPOINT_FILE_NAME))
model.load_weights(checkpoint_file_path)
print(f'Weights is loaded from: {checkpoint_file_path}')
classes = model.predict_classes(encoded_inputs,
batch_size=BATCH_SIZE,
verbose=1)
for i in range(len(inputs)):
for j in range(len(inputs[i])):
token = inputs[i][j]
if j < len(classes[i]) - 1:
cl = classes[i][j + 1]
else:
cl = 0
if token.startswith('##'):
print(token[2:], end='')
else:
if j > 0:
print(' ', end='')
if cl > 0:
print(f'[{CLASSES[cl]}] ', end='')
print(token, end='')
print('\n', end='')
if __name__ == '__main__':
main()