#[cfg(not(feature="no-ml"))]
pub fn encode_text(text:&Vec<String>)->Option<Vec<(String,Option<Vec<f32>>)>>{
use rayon::prelude::*;
use rust_bert::pipelines::sentence_embeddings::{
SentenceEmbeddingsBuilder,
SentenceEmbeddingsModel, SentenceEmbeddingsModelType
};
if text.len() == 0{
println!("No text found");
return None
}
let mut embedded_vector:Vec<Vec<(String,Option<Vec<f32>>)>> = vec![vec![],vec![],vec![],vec![]];
text.par_chunks(4).zip(&mut embedded_vector).for_each(|(sentences, encoding_vec)| {
let model:SentenceEmbeddingsModel;
match SentenceEmbeddingsBuilder::remote(SentenceEmbeddingsModelType::AllMiniLmL6V2)
.create_model(){
Ok(sentence_embedding_model)=> model = sentence_embedding_model,
Err(err)=>panic!("Error Generating Model: {:?}",err)
}
for sentence in sentences{
match model.encode(&[sentence]){
Ok(encodings)=>{
encoding_vec.push((sentence.to_owned(), Some(encodings[0].to_owned())));
},
Err(err)=>{
encoding_vec.push((sentence.to_owned(), None));
panic!("Error creating embeddings for text: {:?}",err)
}
}
}
});
let mut output_vec:Vec<(String,Option<Vec<f32>>)> = vec![];
for mut slice in embedded_vector{
output_vec.append(&mut slice);
}
Some(output_vec)
}
#[cfg(not(feature="no-ml"))]
pub fn encode_text_with_model_from_path(model_path:&String, text:&Vec<String>)->Option<Vec<(String,Option<Vec<f32>>)>>{
use rayon::prelude::*;
use rust_bert::pipelines::sentence_embeddings::{
SentenceEmbeddingsBuilder,
SentenceEmbeddingsModel, SentenceEmbeddingsModelType
};
if text.len() == 0{
println!("No text found");
return None
}
let mut embedded_vector:Vec<Vec<(String,Option<Vec<f32>>)>> = vec![vec![],vec![],vec![],vec![]];
text.par_chunks(4).zip(&mut embedded_vector).for_each(|(sentences, encoding_vec)| {
let model:SentenceEmbeddingsModel;
match SentenceEmbeddingsBuilder::local(model_path.clone()).with_device(tch::Device::cuda_if_available()).create_model(){
Ok(sentence_embedding_model)=> model = sentence_embedding_model,
Err(err)=>panic!("Error Generating Model: {:?}",err)
}
for sentence in sentences{
match model.encode(&[sentence]){
Ok(encodings)=>{
encoding_vec.push((sentence.to_owned(), Some(encodings[0].to_owned())));
},
Err(err)=>{
encoding_vec.push((sentence.to_owned(), None));
panic!("Error creating embeddings for text: {:?}",err)
}
}
}
});
let mut output_vec:Vec<(String,Option<Vec<f32>>)> = vec![];
for mut slice in embedded_vector{
output_vec.append(&mut slice);
}
Some(output_vec)
}