OpenNMT Forum

CTranslate2 in Python

Hi,

In the CTranslate2 documentation there’s a snippet of code indicating you could use the library in python:

import ctranslate2
translator = ctranslate2.Translator("ende_ctranslate2/", device="cpu")

input_tokens = ["▁H", "ello", "▁world", "!"]
result = translator.translate_batch([input_tokens])

print(result[0][0])

However, I don’t know how or where to get the ctranslate2 module (my IDE is unable to find it)

Could you give me any orientation to do it?

Currently, the easiest way to use the library is from the Docker images. For example if you followed the README to generate “ende_ctranslate2” you could try:

$ docker run -it --rm -v $PWD:/data --entrypoint python opennmt/ctranslate2:latest-ubuntu18
Python 2.7.15+ (default, Oct  7 2019, 17:39:04) 
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctranslate2
>>> translator = ctranslate2.Translator("/data/ende_ctranslate2/", device="cpu")
>>> translator.translate_batch([["▁H", "ello", "▁world", "!"]])
[[{'tokens': ['\xe2\x96\x81Hallo', '\xe2\x96\x81Welt', '!'], 'score': -1.0516233444213867}]]

Would that work you?

Hi Guillaume,

Many thanks for your prompt reply.
Well, actually mi intention was to use it as a library inside an already working python project outside docker.
So I guess I would need to be able to deploy all that code as a Python package in the same way it was done inside the docker machine.
I actually tried, but I don’t know how to do it.

You could define an new Docker images starting with FROM opennmt/ctranslate2:latest-ubuntu18 which adds your projects and dependencies.

Or if you have a Linux distribution that matches one of the CTranslate2 Docker images, you could extract the Python package and install it on the host, for example:

$ docker run -it --rm -u $UID:$UID -v $PWD:/data --entrypoint cp \
    opennmt/ctranslate2:latest-ubuntu16 -r /opt/ctranslate2 /data/ctranslate2
$ pip install ctranslate2/ctranslate2-*.whl
$ export LD_LIBRARY_PATH=$PWD/ctranslate2/lib