Skip to main content

ct2rs/
lib.rs

1// lib.rs
2//
3// Copyright (c) 2023-2025 Junpei Kawamoto
4//
5// This software is released under the MIT License.
6//
7// http://opensource.org/licenses/mit-license.php
8
9//! This crate provides Rust bindings for [OpenNMT/CTranslate2](https://github.com/OpenNMT/CTranslate2).
10//!
11//! This crate provides the following:
12//!
13//! * Rust bindings for
14//!   [ctranslate2::Translator](https://opennmt.net/CTranslate2/python/ctranslate2.Translator.html),
15//!   [ctranslate2::Generator](https://opennmt.net/CTranslate2/python/ctranslate2.Generator.html),
16//!   and
17//!   [ctranslate2::Whisper](https://opennmt.net/CTranslate2/python/ctranslate2.models.Whisper.html)
18//!   provided by CTranslate2, specifically [`sys::Translator`], [`sys::Generator`], and
19//!   [`sys::Whisper`].
20//! * More user-friendly versions of these, [`Translator`], [`Generator`],
21//!   and [`Whisper`] (`whisper` feature is required),
22//!   which incorporate tokenizers for easier handling.
23//!
24//! # Basic Usage
25//! The following example translates two strings using default settings and outputs each to
26//! the standard output.
27//!
28//! ```no_run
29//! # use anyhow::Result;
30//! #
31//! use ct2rs::{Config, Translator, TranslationOptions};
32//!
33//! # fn main() -> Result<()> {
34//! let sources = vec![
35//!     "Hallo World!",
36//!     "This crate provides Rust bindings for CTranslate2."
37//! ];
38//! let translator = Translator::new("/path/to/model", &Default::default())?;
39//! let results = translator.translate_batch(&sources, &Default::default(), None)?;
40//! for (r, _) in results{
41//!     println!("{}", r);
42//! }
43//! # Ok(())
44//! # }
45//! ```
46//!
47//! # Supported Models
48//! The `ct2rs` crate has been tested and confirmed to work with the following models:
49//!
50//! - BART
51//! - BLOOM
52//! - FALCON
53//! - Marian-MT
54//! - MPT
55//! - NLLB
56//! - GPT-2
57//! - GPT-J
58//! - OPT
59//! - T5
60//! - Whisper
61//!
62//! Please see the respective
63//! [examples](https://github.com/jkawamoto/ctranslate2-rs/tree/main/examples) for each model.
64//!
65//! # Stream API
66//! This crate also offers a streaming API that utilizes callback closures.
67//! Please refer to
68//! [the example code](https://github.com/jkawamoto/ctranslate2-rs/blob/main/examples/stream.rs)
69//! for more information.
70//!
71//!
72
73#![cfg_attr(docsrs, feature(doc_cfg))]
74
75#[cfg(feature = "mkl")]
76extern crate intel_onemkl_prebuild;
77
78#[cfg(feature = "openblas")]
79extern crate openblas_src;
80
81#[cfg(feature = "dnnl")]
82extern crate onednn_src;
83
84pub use generator::{GenerationOptions, Generator};
85#[cfg(feature = "hub")]
86#[cfg_attr(docsrs, doc(cfg(feature = "hub")))]
87pub use hub::download_model;
88pub use result::GenerationStepResult;
89pub use sys::{
90    set_log_level, set_random_seed, BatchType, ComputeType, Config, Device, LogLevel,
91    ScoringOptions, ScoringResult,
92};
93pub use tokenizer::Tokenizer;
94pub use translator::{TranslationOptions, Translator};
95#[cfg(feature = "whisper")]
96#[cfg_attr(docsrs, doc(cfg(feature = "whisper")))]
97pub use whisper::{Whisper, WhisperOptions};
98
99mod generator;
100mod result;
101pub mod sys;
102mod tokenizer;
103pub mod tokenizers;
104mod translator;
105
106#[cfg(feature = "whisper")]
107#[cfg_attr(docsrs, doc(cfg(feature = "whisper")))]
108mod whisper;
109
110#[cfg(feature = "hub")]
111#[cfg_attr(docsrs, doc(cfg(feature = "hub")))]
112mod hub;