Translator

Struct Translator 

Source
pub struct Translator<T: Tokenizer> { /* private fields */ }
Expand description

A text translator with a tokenizer.

§Example

The following example translates two strings using default settings and outputs each to the standard output.

use ct2rs::{Config, Translator, TranslationOptions, GenerationStepResult};

let sources = vec![
    "Hallo World!",
    "This crate provides Rust bindings for CTranslate2."
];
let translator = Translator::new("/path/to/model", &Default::default())?;
let results = translator.translate_batch(&sources, &Default::default(), None)?;
for (r, _) in results{
    println!("{}", r);
}

The following example translates a single string and uses a callback closure for streaming the output to standard output.

 use std::io::{stdout, Write};
 use anyhow::Result;

 use ct2rs::{Config, Translator, TranslationOptions, GenerationStepResult};

 let sources = vec![
     "Hallo World! This crate provides Rust bindings for CTranslate2."
 ];
 let options = TranslationOptions {
     // beam_size must be 1 to use the stream API.
     beam_size: 1,
     ..Default::default()
 };
 let mut callback = |step_result: GenerationStepResult| -> Result<()> {
     print!("{:?}", step_result.text);
     stdout().flush()?;
     Ok(())
 };
 let translator = Translator::new("/path/to/model", &Config::default())?;
 let results = translator.translate_batch(&sources, &options, Some(&mut callback))?;

Implementations§

Source§

impl Translator<Tokenizer>

Source

pub fn new<U: AsRef<Path>>(path: U, config: &Config) -> Result<Self>

Initializes the translator with crate::tokenizers::auto::Tokenizer.

§Arguments
  • path - A path to the directory containing the language model to be loaded.
  • config - A reference to a Config structure that specifies various settings and configurations for the Translator.
§Returns

Returns a Result that, if successful, contains the initialized Translator. If an error occurs during initialization, the function will return an error wrapped in the Result.

Source§

impl<T: Tokenizer> Translator<T>

Source

pub fn with_tokenizer<U: AsRef<Path>>( path: U, tokenizer: T, config: &Config, ) -> Result<Self>

Initializes the translator with the given tokenizer.

§Arguments
  • path - The path to the directory containing the language model.
  • tokenizer - An instance of a tokenizer.
  • config - A reference to a Config structure specifying the settings for the Translator.
§Returns

Returns a Result containing the initialized Translator, or an error if initialization fails.

§Example

This example demonstrates creating a Translator instance with a Sentencepiece tokenizer.

use ct2rs::{Config, TranslationOptions, Translator};
use ct2rs::tokenizers::sentencepiece::Tokenizer;

let translator = Translator::with_tokenizer(
    "/path/to/model",
    Tokenizer::from_file("/path/to/source.spm", "/path/to/target.spm")?,
    &Config::default()
)?;
Source

pub fn translate_batch<U, V, W>( &self, sources: &[U], options: &TranslationOptions<V, W>, callback: Option<&mut dyn FnMut(GenerationStepResult) -> Result<()>>, ) -> Result<Vec<(String, Option<f32>)>>
where U: AsRef<str>, V: AsRef<str>, W: AsRef<str>,

Translates multiple lists of strings in a batch processing manner.

This function takes a vector of strings and performs batch translation according to the specified settings in options. The results of the batch translation are returned as a vector. An optional callback closure can be provided which is invoked for each new token generated during the translation process. This allows for step-by-step reception of the batch translation results. If the callback returns Err, it will stop the translation for that batch. Note that if a callback is provided, options.beam_size must be set to 1.

§Arguments
  • source - A vector of strings to be translated.
  • options - Settings applied to the batch translation process.
  • callback - An optional mutable reference to a closure that is called for each token generation step. The closure takes a GenerationStepResult and returns a anyhow::Result<()>. If it returns Err, the translation process for the current batch will stop.
§Returns

Returns a Result containing a vector of TranslationResult if successful, or an error if the translation fails.

Source

pub fn translate_batch_with_target_prefix<U, V, W, E>( &self, sources: &[U], target_prefixes: &Vec<Vec<V>>, options: &TranslationOptions<W, E>, callback: Option<&mut dyn FnMut(GenerationStepResult) -> Result<()>>, ) -> Result<Vec<(String, Option<f32>)>>
where U: AsRef<str>, V: AsRef<str>, W: AsRef<str>, E: AsRef<str>,

Translates multiple lists of strings with target prefixes in a batch processing manner.

This function takes a vector of strings and corresponding target prefixes, performing batch translation according to the specified settings in options. An optional callback closure can be provided which is invoked for each new token generated during the translation process.

This function is similar to translate_batch, with the addition of handling target prefixes that guide the translation process. For more detailed parameter and option descriptions, refer to the documentation for Translator::translate_batch.

§Arguments
  • sources - A vector of strings translated.
  • target_prefix - A vector of token lists, each list representing a sequence of target prefix tokens that provide a starting point for the translation output.
  • options - Settings applied to the batch translation process.
  • callback - An optional mutable reference to a closure that is called for each token generation step. The closure takes a GenerationStepResult and returns a anyhow::Result<()>. If it returns Err, the translation process for the current batch will stop.
§Returns

Returns a Result containing a vector of TranslationResult if successful, or an error if the translation fails.

Source

pub fn num_queued_batches(&self) -> Result<usize>

Number of batches in the work queue.

Source

pub fn num_active_batches(&self) -> Result<usize>

Number of batches in the work queue or currently processed by a worker.

Source

pub fn num_replicas(&self) -> Result<usize>

Number of parallel replicas.

Trait Implementations§

Source§

impl<T: Tokenizer> Debug for Translator<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Translator<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Translator<T>
where T: RefUnwindSafe,

§

impl<T> Send for Translator<T>
where T: Send,

§

impl<T> Sync for Translator<T>
where T: Sync,

§

impl<T> Unpin for Translator<T>
where T: Unpin,

§

impl<T> UnwindSafe for Translator<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,