Servers

OpenNMT provides simple translation servers to easily showcase your results remotely.

REST

Please use export THC_CACHING_ALLOCATOR=0 to save memory on server side.

You can use an easy REST syntax to simply send plain text. Sentences will be tokenized, translated and then detokenized using OpenNMT tools.

The server uses the restserver-xavante dependency, you need to install it by running:

luarocks install restserver-xavante

The translation server can be run using any of the arguments from tokenize.lua or translate.lua.

Single Model Rest server (first version, kept for backward compatiblity)

th tools/rest_translation_server.lua -model ../Recipes/baseline-1M-enfr/exp/model-baseline-1M-enfr_epoch13_3.44.t7 -gpuid 1 -host ... -port -case_feature -bpe_model ...

Note

The default host is set to 127.0.0.1 and default port to 7784.

You can test it with a curl command locally or from any other client:

curl -v -H "Content-Type: application/json" -X POST -d '[{ "src" : "Hello World" }]' http://IP_address:7784/translator/translate

Answer will be embedded in a JSON format, translated sentence in the tgt section. Additionally you can get the attention matrix with the -withAttn option in the server command line.

Multi Model Rest server

luarocks install yaml

This version supports multi models listed in a YAML config file.

Here is an example with two models:

-
  model: '/NMTModels/en-fr/model-enfr_epoch600_3.03.t7'
  replace_unk: true
  mode: aggressive
  joiner_annotate: true
  case_feature: true
  segment_case: true
  beam_size: 5

-
  model: '/NMTModels/en-it/model-enit_epoch600_4.17.t7'
  replace_unk: true
  mode: aggressive
  joiner_annotate: true
  case_feature: true
  segment_case: true
  beam_size: 5

By default, it uses the file in tools/rest_config.yml but you can modify with --mode_config the location.

th tools/rest_multi_models.lua -gpuid 1

Note

The default host is set to 127.0.0.1 which only allows local access. 如果您想要支持远程访问,请改用 0.0.0.0。 Default port is 7784. You can change the unload time with --unload_time Xsec

You can test it with a curl command locally or from any other client:

You need to select the model id in the order of the config file.

curl -v -H "Content-Type: application/json" -X POST -d '[{ "src" : "Hello World" , "id" : 1 }]' http://IP_address:7784/translator/translate

ZeroMQ

该服务器使用 0MQ 进行远程过程调用 (RPC). 您可以通过以下操作在 Ubuntu 上安装 0MQ 和 Lua 绑定:

sudo apt-get install libzmq-dev
luarocks install dkjson
luarocks install lua-zmq ZEROMQ_LIBDIR=/usr/lib/x86_64-linux-gnu/ ZEROMQ_INCDIR=/usr/include

可以使用 translate.lua 的引数来运作翻译服务器。

th tools/translation_server.lua -host ... -port ... -model ...

Note

The default host is set to 127.0.0.1 which only allows local access. 如果您想要支持远程访问,请改用 0.0.0.0

它以消息队列(message queue)的形式运行,接受JSON批处理的src语句。 例如,下面五行的 Python 编码可以用来发送一个要翻译的单句。

import zmq, sys, json
sock = zmq.Context().socket(zmq.REQ)
sock.connect("tcp://127.0.0.1:5556")
sock.send(json.dumps([{"src": " ".join(sys.argv[1:])}]))
print sock.recv()

如需更长的例子,请看我们正在继续开发的 Python/Flask server