Struct TranslationModelBuilder

Source
pub struct TranslationModelBuilder { /* private fields */ }
Expand description

§Translation Model Builder

Allows the user to provide a variable set of inputs and identifies the best translation model that fulfills the constraints provided by the user. Options that can provided by the user include:

  • Target device (CPU/CUDA)
  • Model size (medium, large or extra large)
  • source languages to support (as an array of Language)
  • target languages to support (as an array of Language)
  • model type (ModelType, supported models include Marian, T5, MBart50 or M2M100)

The logic for selecting the most appropriate model is as follows:

  • If not specified, the model will be executed on a CUDA device if available, otherwise on the CPU
  • If the model type is specified (e.g. Marian), a model with this architecture will be created. The compatibility of the model with the source and target languages will be verified, and the builder will error if the settings provided are not supported.
  • If the model size is specified, a model of the corresponding size class (computational budget) will be created. The compatibility of the model with the source and target languages will be verified, and the builder will error if the settings provided are not supported.
  • If no source or target languages are provided, a multilingual M2M100 model will be returned
  • If no model type is provided, an average sized-model (Marian) will be returned if a pretrained model exists that covers the requested source/target languages provided. Otherwise a M2M100 multi-lingual model will be returned.

The options for the builder are provided with dedicated “builder function”, the call to create_model() creates a model from the builder.

§Example

use rust_bert::pipelines::translation::{Language, TranslationModelBuilder};
fn main() -> anyhow::Result<()> {
    let model = TranslationModelBuilder::new()
        .with_source_languages(vec![Language::English])
        .with_target_languages(vec![Language::Spanish, Language::French, Language::Italian])
        .create_model()?;

    let input_context_1 = "This is a sentence to be translated";
    let input_context_2 = "The dog did not wake up.";

    let output =
        model.translate(&[input_context_1, input_context_2], None, Language::Spanish)?;

    for sentence in output {
        println!("{}", sentence);
    }
    Ok(())
}

Implementations§

Source§

impl TranslationModelBuilder

Source

pub fn new() -> TranslationModelBuilder

Build a new TranslationModelBuilder

§Returns
  • TranslationModelBuilder Translation model builder
§Example
use rust_bert::pipelines::translation::TranslationModelBuilder;
fn main() -> anyhow::Result<()> {
    let model = TranslationModelBuilder::new().create_model();
    Ok(())
}
Source

pub fn with_device(&mut self, device: Device) -> &mut Self

Specify the device for the translation model

§Arguments
§Returns
  • TranslationModelBuilder Translation model builder
§Example
use rust_bert::pipelines::translation::TranslationModelBuilder;
use tch::Device;
fn main() -> anyhow::Result<()> {
    let model = TranslationModelBuilder::new()
        .with_device(Device::Cuda(0))
        .create_model();
    Ok(())
}
Source

pub fn with_model_type(&mut self, model_type: ModelType) -> &mut Self

Specify the model type for the translation model

§Arguments
  • model_type - ModelType type of translation model to load.
§Returns
  • TranslationModelBuilder Translation model builder
§Example
use rust_bert::pipelines::common::ModelType;
use rust_bert::pipelines::translation::TranslationModelBuilder;
fn main() -> anyhow::Result<()> {
    let model = TranslationModelBuilder::new()
        .with_model_type(ModelType::M2M100)
        .create_model();
    Ok(())
}
Source

pub fn with_medium_model(&mut self) -> &mut Self

Use a medium-sized translation model (Marian-based)

§Returns
  • TranslationModelBuilder Translation model builder
§Example
use rust_bert::pipelines::translation::TranslationModelBuilder;
fn main() -> anyhow::Result<()> {
    let model = TranslationModelBuilder::new()
        .with_medium_model()
        .create_model();
    Ok(())
}
Source

pub fn with_large_model(&mut self) -> &mut Self

Use a large translation model (M2M100, 418M parameters-based)

§Returns
  • TranslationModelBuilder Translation model builder
§Example
use rust_bert::pipelines::translation::TranslationModelBuilder;
fn main() -> anyhow::Result<()> {
    let model = TranslationModelBuilder::new()
        .with_large_model()
        .create_model();
    Ok(())
}
Source

pub fn with_xlarge_model(&mut self) -> &mut Self

Use a very large translation model (M2M100, 1.2B parameters-based)

§Returns
  • TranslationModelBuilder Translation model builder
§Example
use rust_bert::pipelines::translation::TranslationModelBuilder;
fn main() -> anyhow::Result<()> {
    let model = TranslationModelBuilder::new()
        .with_xlarge_model()
        .create_model();
    Ok(())
}
Source

pub fn with_source_languages<S>(&mut self, source_languages: S) -> &mut Self
where S: AsRef<[Language]> + Debug,

Specify the source languages the model should support

§Arguments
  • source_languages - Array-like of Language the model should be able to translate from
§Returns
  • TranslationModelBuilder Translation model builder
§Example
use rust_bert::pipelines::translation::Language;
use rust_bert::pipelines::translation::TranslationModelBuilder;
fn main() -> anyhow::Result<()> {
    let model = TranslationModelBuilder::new()
        .with_source_languages([Language::French, Language::Italian])
        .create_model();
    Ok(())
}
Source

pub fn with_target_languages<T>(&mut self, target_languages: T) -> &mut Self
where T: AsRef<[Language]> + Debug,

Specify the target languages the model should support

§Arguments
  • target_languages - Array-like of Language the model should be able to translate to
§Returns
  • TranslationModelBuilder Translation model builder
§Example
use rust_bert::pipelines::translation::Language;
use rust_bert::pipelines::translation::TranslationModelBuilder;
fn main() -> anyhow::Result<()> {
    let model = TranslationModelBuilder::new()
        .with_target_languages([
            Language::Japanese,
            Language::Korean,
            Language::ChineseMandarin,
        ])
        .create_model();
    Ok(())
}
Source

pub fn create_model(&self) -> Result<TranslationModel, RustBertError>

Creates the translation model based on the specifications provided

§Returns
  • TranslationModel Generated translation model
§Example
use rust_bert::pipelines::translation::Language;
use rust_bert::pipelines::translation::TranslationModelBuilder;
fn main() -> anyhow::Result<()> {
    let model = TranslationModelBuilder::new()
        .with_target_languages([
            Language::Japanese,
            Language::Korean,
            Language::ChineseMandarin,
        ])
        .create_model();
    Ok(())
}

Trait Implementations§

Source§

impl Default for TranslationModelBuilder

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> Same for T

Source§

type Output = T

Should always be Self
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,

Source§

impl<T> MaybeSendSync for T