1use std::ffi::CString;
18use std::path::Path;
19use std::sync::{Arc, Mutex};
20
21use transcribe_cpp_sys as sys;
22
23use crate::backend::Device;
24use crate::error::{check, Result};
25use crate::result::owned_str;
26use crate::session::Session;
27use crate::types::{Backend, ExtSlot, Feature, TimestampKind};
28use crate::version;
29
30#[derive(Debug, Clone, PartialEq, Eq)]
32pub struct ModelOptions {
33 pub backend: Backend,
36 pub device: Option<crate::Device>,
39}
40
41impl Default for ModelOptions {
42 fn default() -> Self {
43 ModelOptions {
44 backend: Backend::Auto,
45 device: None,
46 }
47 }
48}
49
50#[derive(Debug, Clone, PartialEq)]
52pub struct Capabilities {
53 pub native_sample_rate: i32,
54 pub languages: Vec<String>,
56 pub translate_target_languages: Vec<String>,
58 pub max_timestamp_kind: TimestampKind,
60 pub supports_language_detect: bool,
61 pub supports_translate: bool,
62 pub supports_streaming: bool,
63 pub supports_spec_decode: bool,
64 pub max_audio_ms: i64,
66}
67
68pub(crate) struct ModelInner {
70 pub(crate) ptr: *mut sys::transcribe_model,
71 pub(crate) compute_lock: Mutex<bool>,
77}
78
79unsafe impl Send for ModelInner {}
83unsafe impl Sync for ModelInner {}
84
85impl Drop for ModelInner {
86 fn drop(&mut self) {
87 unsafe { sys::transcribe_model_free(self.ptr) };
89 }
90}
91
92#[derive(Clone)]
94pub struct Model {
95 pub(crate) inner: Arc<ModelInner>,
96}
97
98impl std::fmt::Debug for Model {
99 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
100 f.debug_struct("Model")
101 .field("arch", &self.arch())
102 .field("variant", &self.variant())
103 .field("backend", &self.backend())
104 .finish()
105 }
106}
107
108impl Model {
109 pub fn load(path: impl AsRef<Path>) -> Result<Model> {
111 Model::load_with(path, &ModelOptions::default())
112 }
113
114 pub fn load_with(path: impl AsRef<Path>, options: &ModelOptions) -> Result<Model> {
116 version::ensure_compatible()?;
118
119 let path = path.as_ref();
120 let c_path = CString::new(path_bytes(path)?)?;
121
122 let mut params: sys::transcribe_model_load_params = unsafe { std::mem::zeroed() };
123 unsafe { sys::transcribe_model_load_params_init(&mut params) };
124 params.backend = options.backend.to_raw();
125 params.device = options
126 .device
127 .as_ref()
128 .map_or(std::ptr::null_mut(), |device| device.handle);
129
130 let mut out: *mut sys::transcribe_model = std::ptr::null_mut();
131 let status = unsafe { sys::transcribe_model_load_file(c_path.as_ptr(), ¶ms, &mut out) };
132 check_model_load(status, &format!("load {}", path.display()))?;
133 debug_assert!(!out.is_null());
134
135 Ok(Model {
136 inner: Arc::new(ModelInner {
137 ptr: out,
138 compute_lock: Mutex::new(false),
139 }),
140 })
141 }
142
143 pub fn session(&self) -> Result<Session> {
145 self.session_with(&SessionOptions::default())
146 }
147
148 pub fn session_with(&self, options: &SessionOptions) -> Result<Session> {
150 Session::new(self, options)
151 }
152
153 pub fn capabilities(&self) -> Capabilities {
155 let mut caps: sys::transcribe_capabilities = unsafe { std::mem::zeroed() };
156 unsafe { sys::transcribe_capabilities_init(&mut caps) };
157 let _ = unsafe { sys::transcribe_model_get_capabilities(self.inner.ptr, &mut caps) };
160
161 let mut languages = Vec::new();
162 if !caps.languages.is_null() && caps.n_languages > 0 {
163 let slice =
164 unsafe { std::slice::from_raw_parts(caps.languages, caps.n_languages as usize) };
165 for &lang in slice {
166 languages.push(owned_str(lang));
167 }
168 }
169 let mut translate_target_languages = Vec::new();
170 if !caps.translate_target_languages.is_null() && caps.n_translate_target_languages > 0 {
171 let slice = unsafe {
172 std::slice::from_raw_parts(
173 caps.translate_target_languages,
174 caps.n_translate_target_languages as usize,
175 )
176 };
177 for &lang in slice {
178 translate_target_languages.push(owned_str(lang));
179 }
180 }
181
182 Capabilities {
183 native_sample_rate: caps.native_sample_rate,
184 languages,
185 translate_target_languages,
186 max_timestamp_kind: TimestampKind::from_raw(caps.max_timestamp_kind),
187 supports_language_detect: caps.supports_language_detect,
188 supports_translate: caps.supports_translate,
189 supports_streaming: caps.supports_streaming,
190 supports_spec_decode: caps.supports_spec_decode,
191 max_audio_ms: caps.max_audio_ms,
192 }
193 }
194
195 pub fn supports(&self, feature: Feature) -> bool {
197 unsafe { sys::transcribe_model_supports(self.inner.ptr, feature.to_raw()) }
198 }
199
200 pub fn accepts_ext(&self, slot: ExtSlot, kind: u32) -> bool {
202 unsafe { sys::transcribe_model_accepts_ext_kind(self.inner.ptr, slot.to_raw(), kind) }
203 }
204
205 pub fn arch(&self) -> String {
207 owned_str(unsafe { sys::transcribe_model_arch_string(self.inner.ptr) })
208 }
209
210 pub fn variant(&self) -> String {
212 owned_str(unsafe { sys::transcribe_model_variant_string(self.inner.ptr) })
213 }
214
215 pub fn backend(&self) -> String {
218 owned_str(unsafe { sys::transcribe_model_backend(self.inner.ptr) })
219 }
220
221 pub fn device(&self) -> Result<Device> {
226 let handle = unsafe { sys::transcribe_model_device(self.inner.ptr) };
227 if handle.is_null() {
228 return Err(crate::Error::Backend(
229 "model has no resolved device".to_string(),
230 ));
231 }
232 let mut raw: sys::transcribe_device_info = unsafe { std::mem::zeroed() };
233 unsafe { sys::transcribe_device_info_init(&mut raw) };
234 let status = unsafe { sys::transcribe_device_get_info(handle, &mut raw) };
235 check(status, "device_get_info")?;
236 Ok(Device::from_raw(&raw, handle, None))
237 }
238
239 pub fn tokenize(&self, text: &str) -> Result<Vec<i32>> {
243 let c_text = CString::new(text)?;
244 let mut buf = vec![0i32; text.len().max(16)];
246 loop {
247 let n = unsafe {
248 sys::transcribe_tokenize(
249 self.inner.ptr,
250 c_text.as_ptr(),
251 buf.as_mut_ptr(),
252 buf.len(),
253 )
254 };
255 if n == i32::MIN {
256 return Err(crate::error::Error::NotImplemented(
257 "model tokenizer has no encode path".to_string(),
258 ));
259 }
260 if n < 0 {
261 buf.resize((-n) as usize, 0);
262 continue;
263 }
264 buf.truncate(n as usize);
265 return Ok(buf);
266 }
267 }
268}
269
270fn check_model_load(status: sys::transcribe_status, context: &str) -> Result<()> {
271 #[cfg(feature = "dynamic-backends")]
272 {
273 if status == sys::transcribe_status::TRANSCRIBE_ERR_BACKEND
274 && crate::backend::device_count() == 0
275 {
276 let code = status.0 as i32;
277 let base = crate::error::status_string(code);
278 return Err(crate::error::Error::Backend(format!(
279 "{context}: {base} (status {code}); dynamic-backends builds \
280 require backend modules to be loaded before model load. Call \
281 transcribe_cpp::init_backends_default() when modules are \
282 bundled next to libtranscribe, or init_backends(dir) for a \
283 custom provider directory."
284 )));
285 }
286 }
287 check(status, context)
288}
289
290#[derive(Debug, Clone, PartialEq, Eq)]
292pub struct SessionOptions {
293 pub n_threads: i32,
295 pub kv_type: crate::types::KvType,
297 pub n_ctx: i32,
299}
300
301impl Default for SessionOptions {
302 fn default() -> Self {
303 SessionOptions {
304 n_threads: 0,
305 kv_type: crate::types::KvType::Auto,
306 n_ctx: 0,
307 }
308 }
309}
310
311#[derive(Debug, Clone, Copy, PartialEq, Eq)]
313pub struct SessionLimits {
314 pub effective_n_ctx: i32,
316 pub effective_max_audio_ms: i64,
318 pub max_kv_bytes: i64,
320}
321
322#[cfg(unix)]
328pub(crate) fn path_bytes(path: &Path) -> Result<Vec<u8>> {
329 use std::os::unix::ffi::OsStrExt;
330 Ok(path.as_os_str().as_bytes().to_vec())
331}
332
333#[cfg(not(unix))]
334pub(crate) fn path_bytes(path: &Path) -> Result<Vec<u8>> {
335 path.to_str().map(|s| s.as_bytes().to_vec()).ok_or_else(|| {
337 crate::error::Error::InvalidArgument(format!("non-UTF-8 path: {}", path.display()))
338 })
339}