use super::punctuation_error;
use crate::{session::driver::FinalTextProcessor, AsrError, PunctConfig};
use sherpa_onnx::{
OfflinePunctuation, OfflinePunctuationConfig, OnlinePunctuation, OnlinePunctuationConfig,
};
use std::sync::Arc;
enum Punct {
CtTransformer(OfflinePunctuation),
CnnBilstm(OnlinePunctuation),
}
fn lowercase_ascii(text: &str) -> String {
text.chars().map(|c| c.to_ascii_lowercase()).collect()
}
impl Punct {
fn add(&self, text: &str) -> Option<String> {
match self {
Punct::CtTransformer(p) => p.add_punctuation(text),
Punct::CnnBilstm(p) => p.add_punctuation(&lowercase_ascii(text)),
}
}
}
pub(crate) fn create(c: &PunctConfig) -> Result<FinalTextProcessor, AsrError> {
let files = super::model_layout::find_punct_model_files(&c.model).map_err(punctuation_error)?;
let model = files.model.to_string_lossy().into_owned();
let punct = match files.vocab {
None => {
let mut config = OfflinePunctuationConfig::default();
config.model.ct_transformer = Some(model);
config.model.num_threads = 2;
config.model.provider = Some("cpu".into());
Punct::CtTransformer(OfflinePunctuation::create(&config).ok_or_else(|| {
AsrError::backend("failed to initialize CT-Transformer punctuation model")
})?)
}
Some(vocab) => {
let mut config = OnlinePunctuationConfig::default();
config.model.cnn_bilstm = Some(model);
config.model.bpe_vocab = Some(vocab.to_string_lossy().into_owned());
config.model.num_threads = 2;
config.model.provider = Some("cpu".into());
Punct::CnnBilstm(OnlinePunctuation::create(&config).ok_or_else(|| {
AsrError::backend("failed to initialize CNN-BiLSTM punctuation model")
})?)
}
};
Ok(Arc::new(move |text: &str| {
punct.add(text).unwrap_or_else(|| text.to_string())
}))
}