Expand description
This crate provides Rust bindings for OpenNMT/CTranslate2.
§Examples
The following example translates English to German and Japanese using the tokenizer provided by
the Hugging Face’s tokenizers
crate.
use ct2rs::{TranslationOptions, Translator};
use ct2rs::config::Config;
use ct2rs::tokenizers::Tokenizer;
let path = "/path/to/model";
let t = Translator::new(&path, Tokenizer::new(&path)?, &Config::default())?;
let res = t.translate_batch_with_target_prefix(
&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 using the tokenizer provided by Sentencepiece crate.
use ct2rs::config::{Config, Device};
use ct2rs::{Generator, GenerationOptions};
use ct2rs::sentencepiece::Tokenizer;
let path = "/path/to/model";
let g = Generator::new(&path, Tokenizer::new(&path)?, &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§
pub use crate::generator::GenerationOptions;
pub use crate::translator::TranslationOptions;
Modules§
- Configs and associated enums.
- This module provides raw Rust bindings to the
ctranslate2::Generator
. - A module for utilizing the tokenizer based on Sentencepiece crate.
- A module for utilizing the tokenizer provided by the Hugging Face’s
tokenizers
crate. - This module provides raw Rust bindings to the
ctranslate2::Translator
.
Structs§
- A text generator with a tokenizer.
- A text translator with a tokenizer.
Traits§
- Defines the necessary functions for a tokenizer.