translator-gl-zh / trans_ct2.py
gamallo's picture
new update
a84f6c2
import ctranslate2
import sys
model = sys.argv[1]
file_name = sys.argv[2]
file = open(file_name, 'r')
translator = ctranslate2.Translator(model, device="cuda")
#translator = ctranslate2.Translator(model, device="cpu")
for line in file:
line = line.strip()
#line = line.split()
# r = translator.translate_batch([line], beam_size=5, batch_type='examples')
r = translator.translate_batch(
# [line.split()], replace_unknowns=True, beam_size=5, batch_type='tokens'
[line.split()], beam_size=5, replace_unknowns=True, disable_unk=True, batch_type='tokens'
)
results =' '.join(r[0].hypotheses[0])
#results =' '.join(r[0][0]['tokens'])
# print(results[0].hypotheses[0])
print(results)