Class Service

Class Documentation

class Service

Service offers methods create an asynchronous translation service that translates a plain (without any markups and emojis) UTF-8 encoded text.

This implementation supports translation from 1 source language to 1 target language.

This is intended to be similar to the ones provided for training or decoding in ML pipelines with the following additional capabilities:

  1. Provision of a request -> response based translation flow unlike the usual a line based translation or decoding provided in most ML frameworks.

  2. Internal handling of normalization etc which changes source text to provide to client translation meta-information like alignments consistent with the unnormalized input text.

  3. The API splits each text entry into sentences internally, which are then translated independent of each other. The translated sentences are then joined back together and returned in Response.

Service exposes methods to instantiate the service from a string configuration (which can cover most translators) and to translate an incoming blob of text.

An example use of this API looks as follows:

 options = ...;
 service = Service(options);
 std::string input_text = "Hello World";
 std::future<Response>
     responseFuture = service.translate(std::move(input_text));
 responseFuture.wait(); // Wait until translation has completed.
 Response response(std::move(response.get());

// Do things with response.

Optionally Service can be initialized by also passing bytearray memories for purposes of efficiency (which defaults to empty and then reads from file supplied through config).

Public Functions

Service(Ptr<Options> options, MemoryBundle memoryBundle = {})

Construct Service from Marian options.

If memoryBundle is empty, Service is initialized from file-based loading. Otherwise, Service is initialized from the given bytearray memories.

Parameters
  • options: Marian options object

  • memoryBundle: holds all byte-array memories. Can be a set/subset of model, shortlist, vocabs and ssplitPrefixFile bytes. Optional.

Service(const std::string &config, MemoryBundle memoryBundle = {})

Construct Service from a string configuration.

If memoryBundle is empty, Service is initialized from file-based loading. Otherwise, Service is initialized from the given bytearray memories.

Parameters
  • [in] config: string parsable as YAML expected to adhere with marian config

  • [in] memoryBundle: holds all byte-array memories. Can be a set/subset of model, shortlist, vocabs and ssplitPrefixFile bytes. Optional.

~Service()

Explicit destructor to clean up after any threads initialized in asynchronous operation mode.

std::future<Response> translate(std::string &&source, ResponseOptions options = ResponseOptions())

Translate an input, providing Options to construct Response.

This is useful when one has to set/unset alignments or quality in the Response to save compute spent in constructing these objects.

Parameters
  • [in] source: rvalue reference of the string to be translated

  • [in] responseOptions: Options indicating whether or not to include some member in the Response, also specify any additional configurable parameters.

std::vector<Response> translateMultiple(std::vector<std::string> &&source, ResponseOptions responseOptions)

Translate multiple text-blobs in a single blocking API call, providing ResponseOptions which applies across all text-blobs dictating how to construct Response.

ResponseOptions can be used to enable/disable additional information like quality-scores, alignments etc.

All texts are combined to efficiently construct batches together providing speedups compared to calling translate() indepdently on individual text-blob. Note that there will be minor differences in output when text-blobs are individually translated due to approximations but similar quality nonetheless. If you have async/multithread capabilities, it is recommended to work with futures and translate() API.

Parameters
  • [in] source: rvalue reference of the string to be translated

  • [in] translationRequest: ResponseOptions indicating whether or not to include some member in the Response, also specify any additional configurable parameters.

bool isAlignmentSupported() const

Returns if model is alignment capable or not.