OpenNMT Forum

How to input sentences on the fly from terminal, instead from file?


I’m new to using OpenNMT, but have used PyTorch and TF. I trained a text-to-text Transformer model using OpenNMT-py, but always had to provide a file for input and a destination file to get the outputs. I checked, where still the input is fed through *.pt. Is there a way I can write something like the following in OpenNMT ? Thanks in advance!

model = my_model
while True:
sent = input("Enter your input: ") #input sentence in English
translated_sent = my_model.translate(sent) #translated sentence

Hi @raviteja_anantha,

Take a look at Ctranslate2

import ctranslate2
translator = ctranslate2.Translator("ende_ctranslate2/")
translator.translate_batch([["▁H", "ello", "▁world", "!"]]) 

It is even faster

Thanks @anderleich. I looked into ctranslate2, but still wanted to know if we can do this in python with openNMT-py.

You can have a look at the REST server: Simple OpenNMT-py REST server
Not exactly what you want but it might help.
Else, if you really want to use the onmt code as library inside your python code it’s doable, but it’ll require a bit more work. (This might help, or the REST server code for instance.)

Thanks @francoishernandez, I’ll do some exploration using the pointers you shared.