2025-11-09 15:40:29 +08:00
|
|
|
use anyhow::{Ok, Result, anyhow};
|
2025-10-15 21:03:49 +08:00
|
|
|
use candle_core::{Device, Tensor};
|
|
|
|
|
use tokenizers::Tokenizer;
|
|
|
|
|
|
|
|
|
|
pub struct TokenizerModel {
|
|
|
|
|
tokenizer: Tokenizer,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TokenizerModel {
|
|
|
|
|
pub fn init(path: &str) -> Result<Self> {
|
|
|
|
|
let path = path.to_string();
|
|
|
|
|
assert!(
|
|
|
|
|
std::path::Path::new(&path).exists(),
|
|
|
|
|
"model path file not exists"
|
|
|
|
|
);
|
|
|
|
|
let tokenizer_file = path.clone() + "/tokenizer.json";
|
|
|
|
|
assert!(
|
|
|
|
|
std::path::Path::new(&tokenizer_file).exists(),
|
|
|
|
|
"tokenizer.json not exists in model path"
|
|
|
|
|
);
|
|
|
|
|
let tokenizer = Tokenizer::from_file(tokenizer_file)
|
|
|
|
|
.map_err(|e| anyhow!(format!("tokenizer from file error{}", e)))?;
|
|
|
|
|
Ok(Self { tokenizer })
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-09 15:40:29 +08:00
|
|
|
pub fn text_encode_vec(&self, text: String, add_special_token: bool) -> Result<Vec<u32>> {
|
2025-10-15 21:03:49 +08:00
|
|
|
let token_id = self
|
|
|
|
|
.tokenizer
|
2025-11-09 15:40:29 +08:00
|
|
|
.encode(text, add_special_token)
|
2025-10-15 21:03:49 +08:00
|
|
|
.map_err(|e| anyhow!(format!("tokenizer encode error: {}", e)))?
|
|
|
|
|
.get_ids()
|
|
|
|
|
.to_vec();
|
2025-11-09 15:40:29 +08:00
|
|
|
Ok(token_id)
|
|
|
|
|
}
|
|
|
|
|
pub fn text_encode(&self, text: String, device: &Device) -> Result<Tensor> {
|
|
|
|
|
// let token_id = self
|
|
|
|
|
// .tokenizer
|
|
|
|
|
// .encode(text, true)
|
|
|
|
|
// .map_err(|e| anyhow!(format!("tokenizer encode error: {}", e)))?
|
|
|
|
|
// .get_ids()
|
|
|
|
|
// .to_vec();
|
|
|
|
|
let token_id = self.text_encode_vec(text, true)?;
|
2025-10-15 21:03:49 +08:00
|
|
|
let token_tensor = Tensor::from_slice(&token_id, (1, token_id.len()), device)?;
|
|
|
|
|
Ok(token_tensor)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn token_decode(&self, tokens: Vec<u32>) -> Result<String> {
|
|
|
|
|
let decode = self
|
|
|
|
|
.tokenizer
|
|
|
|
|
.decode(&tokens, true)
|
|
|
|
|
.map_err(|e| anyhow!(format!("tokenizer encode error{}", e)))?;
|
|
|
|
|
Ok(decode)
|
|
|
|
|
}
|
|
|
|
|
}
|