Crate ct2rs

source ·
Expand description

This crate provides Rust bindings for OpenNMT/CTranslate2.

§Examples

The following example translates English to German and Japanese.

use ct2rs::config::{Config, Device};
use ct2rs::{TranslationOptions, Translator};

let t = Translator::new("/path/to/model", Device::CPU, Config::default())?;
let res = t.translate_batch(
    vec![
        "Hello world!",
        "This library provides Rust bindings for CTranslate2.",
    ],
    vec![vec!["deu_Latn"], vec!["jpn_Jpan"]],
    &TranslationOptions {
        return_scores: true,
        ..Default::default()
    },
)?;
for r in res {
    println!("{}, (score: {:?})", r.0, r.1);
}

The following example generates text.

use ct2rs::config::{Config, Device};
use ct2rs::{Generator, GenerationOptions};

let g = Generator::new("/path/to/model", Device::CPU, Config::default())?;
let res = g.generate_batch(
    vec!["prompt"],
    &GenerationOptions::default(),
)?;
for r in res {
    println!("{:?}", r.0);
}

Please also see the other sample code available in the examples directory.

Re-exports§

Modules§

  • Configs and associated enums.
  • This module provides raw Rust bindings to the ctranslate2::Generator.
  • This module provides raw Rust bindings to the ctranslate2::Translator.

Structs§