pub struct TextInput {
pub texts: Vec<String>,
pub labels: Labels,
}Expand description
Input text and labels
Fields§
§texts: Vec<String>§labels: LabelsImplementations§
Source§impl TextInput
impl TextInput
Sourcepub fn new(texts: Vec<String>, labels: Vec<String>) -> Self
pub fn new(texts: Vec<String>, labels: Vec<String>) -> Self
Creates an input with the same set of labels for every text
Sourcepub fn from_str(texts: &[&str], labels: &[&str]) -> Self
pub fn from_str(texts: &[&str], labels: &[&str]) -> Self
Creates an input with the same set of labels for every text
Examples found in repository?
examples/sample1.rs (lines 13-16)
7fn main() -> gliclass::util::result::Result<()> {
8 const TOKENIZER_PATH: &str = "models/gliclass-small-v1.0/tokenizer.json";
9 const MODEL_PATH: &str = "models/gliclass-small-v1.0/onnx/model.onnx";
10
11 let gliclass = GLiClass::new(TOKENIZER_PATH, MODEL_PATH, Parameters::default())?;
12
13 let input = TextInput::from_str(
14 &["One day I will see the world!"],
15 &["travel", "dreams", "sport", "science", "politics"],
16 );
17
18 let classes = gliclass.inference(input)?;
19 println!("Scores: {:?}", classes.scores);
20
21 // check the results against the results obtained with the original implementation
22 assert!(gliclass::util::test::is_close_to_a(
23 &classes.scores.slice(ndarray::s![0, ..]),
24 &[0.9999985694885254, 0.9999986886978149, 0.9999117851257324, 0.9996119141578674, 0.8172177672386169],
25 0.00001,
26 ));
27
28 Ok(())
29}More examples
examples/sample2.rs (lines 13-20)
5fn main() -> gliclass::util::result::Result<()> {
6 const TOKENIZER_PATH: &str = "models/gliclass-small-v1.0/tokenizer.json";
7 const MODEL_PATH: &str = "models/gliclass-small-v1.0/onnx/model.onnx";
8
9 let params = gliclass::params::Parameters::default();
10 let pipeline = gliclass::pipeline::ClassificationPipeline::new(TOKENIZER_PATH, ¶ms)?;
11 let model = orp::model::Model::new(MODEL_PATH, orp::params::RuntimeParameters::default())?;
12
13 let inputs = gliclass::input::text::TextInput::from_str(
14 &[
15 "Rust is a systems programming language focused on safety, speed, and concurrency, with a strong ownership model that prevents memory errors without needing a garbage collector.",
16 "Traveling is the perfect way to explore new cultures through their food, from savoring street tacos in Mexico to indulging in fresh sushi in Japan.",
17 "Traveling for science allows researchers to explore new environments, gather crucial data, and collaborate with experts worldwide to expand our understanding of the universe.",
18 ],
19 &["computing", "science", "programming", "travel", "food", "politics"]
20 );
21
22 let classes = model.inference(inputs, &pipeline, ¶ms)?;
23
24 for i in 0..classes.len() {
25 println!("Text {i}:\n\t=> {}\n\t=> {:?}",
26 classes.best_label(i, None).unwrap(),
27 classes.ordered_scores(i, None).unwrap().iter().rev().collect::<Vec<_>>(),
28 );
29 }
30
31 Ok(())
32}Sourcepub fn new_per_text(texts: Vec<String>, labels: Vec<Vec<String>>) -> Self
pub fn new_per_text(texts: Vec<String>, labels: Vec<Vec<String>>) -> Self
Creates an input with a different set of labels for each text. The texts and labels arrays must have the same length, otherwise an error will be raised at some point.
Sourcepub fn from_str_per_text(texts: &[&str], labels: &[&[&str]]) -> Self
pub fn from_str_per_text(texts: &[&str], labels: &[&[&str]]) -> Self
Creates an input with a different set of labels for each text. The texts and labels arrays must have the same length, otherwise an error will be raised at some point.
Examples found in repository?
examples/sample3.rs (lines 13-24)
5fn main() -> gliclass::util::result::Result<()> {
6 const TOKENIZER_PATH: &str = "models/gliclass-small-v1.0/tokenizer.json";
7 const MODEL_PATH: &str = "models/gliclass-small-v1.0/onnx/model.onnx";
8
9 let params = gliclass::params::Parameters::default();
10 let pipeline = gliclass::pipeline::ClassificationPipeline::new(TOKENIZER_PATH, ¶ms)?;
11 let model = orp::model::Model::new(MODEL_PATH, orp::params::RuntimeParameters::default())?;
12
13 let inputs = gliclass::input::text::TextInput::from_str_per_text(
14 &[
15 "Rust is a systems programming language focused on safety, speed, and concurrency, with a strong ownership model that prevents memory errors without needing a garbage collector.",
16 "Traveling is the perfect way to explore new cultures through their food, from savoring street tacos in Mexico to indulging in fresh sushi in Japan.",
17 "Traveling for science allows researchers to explore new environments, gather crucial data, and collaborate with experts worldwide to expand our understanding of the universe.",
18 ],
19 &[
20 &["performance", "user interface"], // expecting 'performance'
21 &["gastronomy", "plane"], // expecting 'gastronomy'
22 &["conferencing", "teaching"], // expecting 'conferencing'
23 ]
24 );
25
26 let classes = model.inference(inputs, &pipeline, ¶ms)?;
27
28 for i in 0..classes.len() {
29 println!("Text {i} => {}", classes.best_label(i, None).unwrap());
30 }
31
32 Ok(())
33}More examples
examples/sample4.rs (lines 12-23)
4fn main() -> gliclass::util::result::Result<()> {
5 const TOKENIZER_PATH: &str = "models/gliclass-modern-base-v2.0-init/tokenizer.json";
6 const MODEL_PATH: &str = "models/gliclass-modern-base-v2.0-init/onnx/model.onnx";
7
8 let params = gliclass::params::Parameters::default().with_prompt_first(true);
9 let pipeline = gliclass::pipeline::ClassificationPipeline::new(TOKENIZER_PATH, ¶ms)?;
10 let model = orp::model::Model::new(MODEL_PATH, orp::params::RuntimeParameters::default())?;
11
12 let inputs = gliclass::input::text::TextInput::from_str_per_text(
13 &[
14 "Rust is a systems programming language focused on safety, speed, and concurrency, with a strong ownership model that prevents memory errors without needing a garbage collector.",
15 "Traveling is the perfect way to explore new cultures through their food, from savoring street tacos in Mexico to indulging in fresh sushi in Japan.",
16 "Traveling for science allows researchers to explore new environments, gather crucial data, and collaborate with experts worldwide to expand our understanding of the universe.",
17 ],
18 &[
19 &["performance", "user interface"], // expecting 'performance'
20 &["gastronomy", "plane"], // expecting 'gastronomy'
21 &["conferencing", "teaching"], // expecting 'conferencing'
22 ]
23 );
24
25 let classes = model.inference(inputs, &pipeline, ¶ms)?;
26
27 for i in 0..classes.len() {
28 println!("Text {i}:\n\t=> {}\n\t=> {:?}",
29 classes.best_label(i, None).unwrap(),
30 classes.ordered_scores(i, None).unwrap().iter().rev().collect::<Vec<_>>(),
31 );
32 }
33
34 Ok(())
35}Trait Implementations§
Source§impl Composable<TextInput, PromptInput> for InputToPrompt
Transformation from input text to prompts
impl Composable<TextInput, PromptInput> for InputToPrompt
Transformation from input text to prompts
fn apply(&self, input: TextInput) -> Result<PromptInput>
fn compose<T, P>(self, other: T) -> impl Composable<I, P>where
Self: Sized,
T: Composable<O, P>,
Auto Trait Implementations§
impl Freeze for TextInput
impl RefUnwindSafe for TextInput
impl Send for TextInput
impl Sync for TextInput
impl Unpin for TextInput
impl UnwindSafe for TextInput
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more