Package: gpt2-client
gpt2-clientEasy-to-use Wrapper for GPT-2 117M, 345M, 774M, and 1.5B Transformer ModelsWhat is it•Installation•Getting StartedMade by Rishabh Anand •https://rish-16.github.ioWhat is itGPT-2 is a Natural Language Processing modeldeveloped by OpenAIfor text generation. It is the successor to the GPT (Generative Pre-trained Transformer) model trained on 40GB of text from the internet. It features a Transformer model that was brought to light by theAttention Is All You Needpaper in 2017. The model has 4 versions -117M,345M,774M, and1558M- that differ in terms of the amount of training data fed to it and the number of parameters they contain.The 1.5B model is currently the largest available model released by OpenAI.Finally,gpt2-clientis a wrapper around the originalgpt-2repositorythat features the same functionality but with more accessiblity, comprehensibility, and utilty. You can play around with all three GPT-2 models in less than five lines of code.Note: This client wrapper is in no way liable to any damage caused directly or indirectly. Any names, places, and objects referenced by the model are fictional and seek no resemblance to real life entities or organisations. Samples are unfiltered and may contain offensive content. User discretion advised.InstallationInstall client viapip. Ideally,gpt2-clientis well supported forPython >= 3.5andTensorFlow >= 1.X. Some libraries may need to be reinstalled or upgraded using the--upgradeflag viapipifPython 2.Xis used.pipinstallgpt2-clientNote:gpt2-clientisnotcompatible with TensorFlow 2.0Getting started1. Download the model weights and checkpointsfromgpt2_clientimportGPT2Clientgpt2=GPT2Client('117M')# This could also be `345M`, `774M`, or `1558M`. Rename `save_dir` to anything.gpt2.load_model(force_download=False)# Use cached versions if available.This creates a directory calledmodelsin the current working directory and downloads the weights, checkpoints, model JSON, and hyper-parameters required by the model. Once you have called theload_model()function, you need not call it again assuming that the files have finished downloading in themodelsdirectory.Note:Setforce_download=Trueto overwrite the existing cached model weights and checkpoints2. Start generating text!fromgpt2_clientimportGPT2Clientgpt2=GPT2Client('117M')# This could also be `345M`, `774M`, or `1558M`gpt2.generate(interactive=True)# Asks user for promptgpt2.generate(n_samples=4)# Generates 4 pieces of texttext=gpt2.generate(return_text=True)# Generates text and returns it in an arraygpt2.generate(interactive=True,n_samples=3)# A different prompt each timeYou can see from the aforementioned sample that the generation options are highly flexible. You can mix and match based on what kind of text you need generated, be it multiple chunks or one at a time with prompts.3. Generating text from batch of promptsfromgpt2_clientimportGPT2Clientgpt2=GPT2Client('117M')# This could also be `345M`, `774M`, or `1558M`prompts=["This is a prompt 1","This is a prompt 2","This is a prompt 3","This is a prompt 4"]text=gpt2.generate_batch_from_prompts(prompts)# returns an array of generated text4. Fine-tuning GPT-2 to custom datasetsfromgpt2_clientimportGPT2Clientgpt2=GPT2Client('117M')# This could also be `345M`, `774M`, or `1558M`my_corpus='./data/shakespeare.txt'# path to corpuscustom_text=gpt2.finetune(my_corpus,return_text=True)# Load your custom datasetIn order to fine-tune GPT-2 to your custom corpus or dataset, it's ideal to have a GPU or TPU at hand.Google Colabis one such tool you can make use of to re-train/fine-tune your custom model.5. Encoding and decoding text sequencesfromgpt2_clientimportGPT2Clientgpt2=GPT2Client('117M')# This could also be `345M`, `774M`, or `1558M`gpt2.load_model()# encoding a sentenceencs=gpt2.encode_seq("Hello world, this is a sentence")# [15496, 995, 11, 428, 318, 257, 6827]# decoding an encoded sequencedecs=gpt2.decode_seq(encs)# Hello world, this is a sentenceContributingSuggestions, improvements, and enhancements are always welcome! If you have any issues, please do raise one in the Issues section. If you have an improvement, do file an issue to discuss the suggestion before creating a PR.All ideas – no matter how outrageous – welcome!LicenceMIT