Files
piperust/src/onnx.rs
T
2026-10-16 11:36:02 +01:00

26 lines
907 B
Rust

//! Process-wide ONNX Runtime environment.
//!
//! ONNX Runtime allows a single environment per process (`ort::init().build()`
//! fails on the second call), yet every [`Session`](ort::session::Session) must
//! be built from it. This module lazily creates that environment once and hands
//! out cheap `Arc` clones, so the STT and VAD pipelines share it.
use anyhow::{Context, Result};
use ort::environment::Environment;
use parking_lot::Mutex;
static ENVIRONMENT: Mutex<Option<Environment>> = Mutex::new(None);
/// Return the shared ONNX Runtime environment, creating it on first use.
pub fn environment() -> Result<Environment> {
let mut guard = ENVIRONMENT.lock();
if let Some(env) = guard.as_ref() {
return Ok(env.clone());
}
let env = ort::init()
.build()
.context("failed to create ONNX Runtime environment")?;
*guard = Some(env.clone());
Ok(env)
}