fn generate(
        &self,
        prompt_texts: Option<Vec<&str>>,
        attention_mask: Option<Tensor>,
    ) -> Vec<String> {
        let eos_token_ids = PrivateLanguageGenerator::get_eos_ids(self).clone();

        let config = PrivateLanguageGenerator::get_config(self);
        let max_length = config.max_length;
        let encoding_max_len = if self.is_encoder_decoder() {
            1024u64
        } else {
            max_length
        };
        let pad_token_id = match self.get_pad_id() {
            Some(value) => Some(*value),
            None => match &eos_token_ids {
                Some(eos_ids) => Some(eos_ids[0]),
                None => None,
            },
        };

        let input_ids = match prompt_texts {
            Some(text) => self.encode_prompt_text(text, encoding_max_len, pad_token_id),
            None => match self.get_bos_id() {
                Some(bos_id) => {
                    Tensor::ones(&[1, 1], (Int64, self.get_var_store().device())) * *bos_id
                }
                None => panic!(
                    "A model with a BOS token must be used to start generation with an empty input"
                ),
            },
        };
        let generated = self.generate_from_ids_and_past(input_ids, attention_mask);
        let mut output = Vec::with_capacity(generated.len());
        for generated_sequence in generated {
            output.push(self.get_tokenizer().decode(generated_sequence, true, true));
        }
        output
    }
/// Generate text based on a vector of promp texts.
///
/// # Arguments
///
/// * `prompt_texts` - `Option<Vec<&str>>` Optional vector of text prompts. An empty prompt to the model may be passed if the model implement a `bos_id`.
/// * `attention_mask` - `Option<Tensor>` Optional attention mask to hide portions of the prompt.
///
/// # Returns
/// * `Vec<String>` Vector of generated strings based on the prompts of length *number_of_prompts* x *num_return_sequences*.
///
/// # Example
///
/// ```no_run
/// # use std::path::PathBuf;
/// # use tch::Device;
/// # fn main() -> anyhow::Result<()> {
/// use rust_bert::pipelines::generation::{GPT2Generator, GenerateConfig, LanguageGenerator};
/// # let mut home: PathBuf = dirs::home_dir().unwrap();
/// # home.push("rustbert");
/// # home.push("gpt2");
/// # let config_path = &home.as_path().join("config.json");
/// # let vocab_path = &home.as_path().join("vocab.txt");
/// # let merges_path = &home.as_path().join("merges.txt");
/// # let weights_path = &home.as_path().join("model.ot");
/// let device = Device::cuda_if_available();
/// let generate_config = GenerateConfig {
///     max_length: 30,
///     do_sample: true,
///     num_beams: 5,
///     temperature: 1.1,
///     num_return_sequences: 3,
///     ..Default::default()
/// };
/// let mut gpt2_generator = GPT2Generator::new(generate_config)?;
/// let input_context = "The dog";
/// let second_input_context = "The cat was";
/// let output = gpt2_generator.generate(Some(vec![input_context, second_input_context]), None);
/// # Ok(())
/// # }
/// ```
/// Example output: \
/// ```no_run
/// # let output =
/// [
///     "The dog's owners, however, did not want to be named. According to the lawsuit, the animal's owner, a 29-year",
///     "The dog has always been part of the family. \"He was always going to be my dog and he was always looking out for me",
///     "The dog has been able to stay in the home for more than three months now. \"It's a very good dog. She's",
///     "The cat was discovered earlier this month in the home of a relative of the deceased. The cat\'s owner, who wished to remain anonymous,",
///     "The cat was pulled from the street by two-year-old Jazmine.\"I didn't know what to do,\" she said",
///     "The cat was attacked by two stray dogs and was taken to a hospital. Two other cats were also injured in the attack and are being treated."
/// ]
/// # ;
/// ```
Generate text based on a vector of promp texts.
Generate text based on a vector of promp texts .
fn generate(
        &self,
        prompt_texts: Option<Vec<&str>>,
        attention_mask: Option<Tensor>,
    ) -> Vec<String> {
        let eos_token_ids = PrivateLanguageGenerator::get_eos_ids(self).clone();
        let config = PrivateLanguageGenerator::get_config(self);
        let max_length = config.max_length;
        let encoding_max_len = if self.is_encoder_decoder() {
            1024u64
        } else {
            max_length
        };
        let pad_token_id = match self.get_pad_id() {
            Some(value) => Some(*value),
            None => match &eos_token_ids {
                Some(eos_ids) => Some(eos_ids[0]),
                None => None,
            },
        };
        let input_ids = match prompt_texts {
            Some(text) => self.encode_prompt_text(text, encoding_max_len, pad_token_id),
            None => match self.get_bos_id() {
                Some(bos_id) => {
                    Tensor::ones(&[1, 1], (Int64, self.get_var_store().device())) * *bos_id
                }
                None => panic!(
                    "A model with a BOS token must be used to start generation with an empty input"
                ),
            },
        };
        let generated = self.generate_from_ids_and_past(input_ids, attention_mask);
        let mut output = Vec::with_capacity(generated.len());
        for generated_sequence in generated {
            output.push(self.get_tokenizer().decode(generated_sequence, true, true));
        }
        output
    }
fn generate ( & self , prompt_texts : Option < Vec < & str > > , attention_mask : Option < Tensor > , ) -> Vec < String > { let eos_token_ids = PrivateLanguageGenerator :: get_eos_ids ( self ) . clone ( ) ; let config = PrivateLanguageGenerator :: get_config ( self ) ; let max_length = config . max_length ; let encoding_max_len = if self . is_encoder_decoder ( ) { 1024u64 } else { max_length } ; let pad_token_id = match self . get_pad_id ( ) { Some ( value ) => Some ( * value ) , None => match & eos_token_ids { Some ( eos_ids ) => Some ( eos_ids [ 0 ] ) , None => None , } , } ; let input_ids = match prompt_texts { Some ( text ) => self . encode_prompt_text ( text , encoding_max_len , pad_token_id ) , None => match self . get_bos_id ( ) { Some ( bos_id ) => { Tensor :: ones ( & [ 1 , 1 ] , ( Int64 , self . get_var_store ( ) . device ( ) ) ) * * bos_id } None => panic ! ( " " ) , } , } ; let generated = self . generate_from_ids_and_past ( input_ids , attention_mask ) ; let mut output = Vec :: with_capacity ( generated . len ( ) ) ; for generated_sequence in generated { output . push ( self . get_tokenizer ( ) . decode ( generated_sequence , true , true ) ) ; } output }
Generate text based on a vector of promp texts.
Generate text based on a vector of promp texts .
&self
prompt_texts
Option<Vec<&str>>
attention_mask
Option<Tensor>
`Vec` Vector of generated strings based on the prompts of length *number_of_prompts* x *num_return_sequences*.
` Vec ` Vector of generated strings based on the prompts of length * number_of_prompts * x * num_return_sequences * .
&self
prompt_texts
Option<Vec<&str>>
attention_mask
Option<Tensor>
example
None
use std::path::pathbuf;
None
use tch::device;
None
fn main() -> anyhow::result<()> {
use rust_bert::pipelines::generation::{GPT2Generator, GenerateConfig, LanguageGenerator}.
use rust_bert :: pipelines :: generation :: { GPT2Generator GenerateConfig LanguageGenerator } .
let mut home: pathbuf = dirs::home_dir().unwrap();
None
home.push("rustbert");
None
home.push("gpt2");
None
let config_path = &home.as_path().join("config.json");
None
let vocab_path = &home.as_path().join("vocab.txt");
None
let merges_path = &home.as_path().join("merges.txt");
None
let weights_path = &home.as_path().join("model.ot");
let device = Device::cuda_if_available();
let generate_config = GenerateConfig {
max_length: 30,
do_sample: true,
num_beams: 5,
temperature: 1.1,
num_return_sequences: 3,
Default::default()
};
let mut gpt2_generator = GPT2Generator::new(generate_config)?;
let input_context = "The dog";
let second_input_context = "The cat was";
let output = gpt2_generator.generate(Some(vec![input_context, second_input_context]), None).
let device = Device :: cuda_if_available () ; let generate_config = GenerateConfig { max_length : 30 do_sample : true num_beams : 5 temperature : 1 . 1 num_return_sequences : 3 Default :: default () } ; let mut gpt2_generator = GPT2Generator :: new ( generate_config ) ? ; let input_context = " The dog " ; let second_input_context = " The cat was " ; let output = gpt2_generator . generate ( Some ( vec! [ input_context second_input_context ] ) None ) .
ok(())
None
}
```
Example output: \
```no_run
` ` ` Example output : \ ` ` ` no_run
let output 
[
"The dog's owners, however, did not want to be named. According to the lawsuit, the animal's owner, a 29-year",
"The dog has always been part of the family. \"He was always going to be my dog and he was always looking out for me",
"The dog has been able to stay in the home for more than three months now. \"It's a very good dog. She's",
"The cat was discovered earlier this month in the home of a relative of the deceased. The cat\'s owner, who wished to remain anonymous,",
"The cat was pulled from the street by two-year-old Jazmine.\"I didn't know what to do,\" she said",
"The cat was attacked by two stray dogs and was taken to a hospital. Two other cats were also injured in the attack and are being treated."
]
[ " The dog ' s owners however did not want to be named . According to the lawsuit the animal ' s owner a 29 - year " " The dog has always been part of the family . \ " He was always going to be my dog and he was always looking out for me " " The dog has been able to stay in the home for more than three months now . \ " It ' s a very good dog . She ' s " " The cat was discovered earlier this month in the home of a relative of the deceased . The cat \ ' s owner who wished to remain anonymous " " The cat was pulled from the street by two - year - old Jazmine . \ " I didn ' t know what to do \ " she said " " The cat was attacked by two stray dogs and was taken to a hospital . Two other cats were also injured in the attack and are being treated . " ]
;
None