-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate_beam.py
69 lines (61 loc) · 2.12 KB
/
translate_beam.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
import torch
import numpy as np
import sentencepiece as sp
import curses
import argparse
from models.whisper import Whisper
from models.transformer import Transformer
UNK_IDX = 0
BOS_IDX = 1
EOS_IDX = 2
PAD_IDX = 3
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model', type=str, required=True, help='Model type to use', choices=['whisper', 'transformer'])
parser.add_argument('--model-path', type=str, required=True, help='Path to model checkpoint')
parser.add_argument('--tokenizer-path', type=str, required=True, help='Path to tokenizer model')
return parser.parse_args()
def main():
# parse args
args = parse_args()
# load model
print('Loading model...')
if args.model == 'whisper':
model = Whisper.load_from_checkpoint(args.model_path).cuda()
elif args.model == 'transformer':
model = Transformer.load_from_checkpoint(args.model_path).cuda()
# load tokenizer
print('Loading tokenizer...')
tokenizer = sp.SentencePieceProcessor(model_file=args.tokenizer_path)
# translate
print(f'Ready to {"translate" if args.model == "transformer" else "transcribe"}!')
def stream_translate(stdscr, src: torch.Tensor):
curses.noecho()
curses.cbreak()
beams = None
lengths = [0] * 16
for beam_bits in model.translate_with_beams(src, BOS_IDX, EOS_IDX, beam_width=8, max_new_tokens=20):
if beams is None:
beams = beam_bits
else:
beams = np.concatenate((beams, beam_bits), axis=-1)
# print the beam_bits
for i, bit in enumerate(beam_bits):
text = tokenizer.IdToPiece(int(bit)).replace('▁', ' ')
stdscr.addstr(i * 2, lengths[i], text)
lengths[i] += len(text)
stdscr.refresh()
stdscr.getkey()
while True:
if args.model == 'whisper':
# input src mel
mel = 'data/atis/mel_normalised/test/x11037ss.npy'
#mel = input('Enter a path to a mel: ').strip()
src = torch.tensor(np.load(mel).T, dtype=torch.float).cuda()
elif args.model == 'transformer':
# input src text
text = input('Enter a sentence to translate: ').strip()
src = torch.tensor(tokenizer.encode(text), dtype=torch.long)
curses.wrapper(stream_translate, src)
if __name__ == '__main__':
main()