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>
impl Translator<Tokenizer>
Sourcepub fn new<U: AsRef<Path>>(path: U, config: &Config) -> Result<Self>
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 aConfigstructure that specifies various settings and configurations for theTranslator.
§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>
impl<T: Tokenizer> Translator<T>
Sourcepub fn with_tokenizer<U: AsRef<Path>>(
path: U,
tokenizer: T,
config: &Config,
) -> Result<Self>
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 aConfigstructure specifying the settings for theTranslator.
§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()
)?;Sourcepub 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>)>>
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>)>>
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 aGenerationStepResultand returns aanyhow::Result<()>. If it returnsErr, 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.
Sourcepub 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>)>>
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>)>>
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 aGenerationStepResultand returns aanyhow::Result<()>. If it returnsErr, 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.
Sourcepub fn num_queued_batches(&self) -> Result<usize>
pub fn num_queued_batches(&self) -> Result<usize>
Number of batches in the work queue.
Sourcepub fn num_active_batches(&self) -> Result<usize>
pub fn num_active_batches(&self) -> Result<usize>
Number of batches in the work queue or currently processed by a worker.
Sourcepub fn num_replicas(&self) -> Result<usize>
pub fn num_replicas(&self) -> Result<usize>
Number of parallel replicas.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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