<jupyter_start><jupyter_text>Generate

This is a notebook for generating text with the pretrained HansardGPT2 model. This should be ran with a GPU in Google Colab.

To ensure you are running with a GPU go to Runtime -> Change Runtime Type, and ensure GPU is selected as Hardware accelerator

Downloads required module (HuggingFace transformers)<jupyter_code>!pip install transformers<jupyter_output><empty_output><jupyter_text>Downloads model and tokenizer<jupyter_code>from transformers import GPT2LMHeadModel, GPT2Tokenizer
import torch

model = GPT2LMHeadModel.from_pretrained("CallumRai/HansardGPT2")
tokenizer = GPT2Tokenizer.from_pretrained("CallumRai/HansardGPT2")

device = torch.device("cuda")
model = model.to(device)<jupyter_output><empty_output><jupyter_text>By adding starting text to ```prompt += ""```, will print three utterances generated by the model.<jupyter_code>prompt = "<|startoftext|> "
prompt += ""

generated = torch.tensor(tokenizer.encode(prompt)).unsqueeze(0)
generated = generated.to(device)

sample_outputs = model.generate(
                                generated,  
                                do_sample=True,
                                top_k=50,
                                max_length = 300,
                                top_p=0.95,
                                num_return_sequences=3
                                )

for i, sample_output in enumerate(sample_outputs):
  print("{}: {}\n\n".format(i, tokenizer.decode(sample_output, skip_special_tokens=True)))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08604206500956023, 'fraction_numerical': 0.010834926704907584, 'mean_word_length': 2.915211970074813, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '416451', 'license': 'MIT', 'n_tokens_mistral': 477, 'n_tokens_neox': 450, 'n_words': 106, 'path': 'hansard/scripts/generate.ipynb', 'repo_name': 'CallumRai/Hansard', 'size': '1569'}