• Stars
    star
    367
  • Rank 116,257 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

✍🏻 gpt2-client: Easy-to-use TensorFlow Wrapper for GPT-2 117M, 345M, 774M, and 1.5B Transformer Models πŸ€– πŸ“

gpt2-client (Archived)

Easy-to-use Wrapper for GPT-2 124M, 345M, 774M, and 1.5B Transformer Models

Pypi package GitHub license GitHub license

Buy Me A Coffee

What is it β€’ Installation β€’ Getting Started

Made by Rishabh Anand β€’ https://rish-16.github.io

What is it

GPT-2 is a Natural Language Processing model developed by OpenAI for 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 the Attention Is All You Need paper in 2017. The model has 4 versions - 124M, 345M, 774M, and 1558M - 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-client is a wrapper around the original gpt-2 repository that features the same functionality but with more accessiblity, comprehensibility, and utilty. You can play around with all four 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.*

Installation

Install client via pip. Ideally, gpt2-client is well supported for Python >= 3.5 and TensorFlow >= 1.X. Some libraries may need to be reinstalled or upgraded using the --upgrade flag via pip if Python 2.X is used.

pip install gpt2-client

Note: gpt2-client is not compatible with TensorFlow 2.0 , try TensorFlow 1.14.0

Getting started

1. Download the model weights and checkpoints

from gpt2_client import GPT2Client

gpt2 = GPT2Client('124M') # This could also be `355M`, `774M`, or `1558M`. Rename `save_dir` to anything.
gpt2.load_model(force_download=False) # Use cached versions if available.

This creates a directory called models in the current working directory and downloads the weights, checkpoints, model JSON, and hyper-parameters required by the model. Once you have called the load_model() function, you need not call it again assuming that the files have finished downloading in the models directory.

Note: Set force_download=True to overwrite the existing cached model weights and checkpoints

2. Start generating text!

from gpt2_client import GPT2Client

gpt2 = GPT2Client('124M') # This could also be `355M`, `774M`, or `1558M`
gpt2.load_model()

gpt2.generate(interactive=True) # Asks user for prompt
gpt2.generate(n_samples=4) # Generates 4 pieces of text
text = gpt2.generate(return_text=True) # Generates text and returns it in an array
gpt2.generate(interactive=True, n_samples=3) # A different prompt each time

You 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 prompts

from gpt2_client import GPT2Client

gpt2 = GPT2Client('124M') # This could also be `355M`, `774M`, or `1558M`
gpt2.load_model()

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 text

4. Fine-tuning GPT-2 to custom datasets

from gpt2_client import GPT2Client

gpt2 = GPT2Client('124M') # This could also be `355M`, `774M`, or `1558M`
gpt2.load_model()

my_corpus = './data/shakespeare.txt' # path to corpus
custom_text = gpt2.finetune(my_corpus, return_text=True) # Load your custom dataset

In order to fine-tune GPT-2 to your custom corpus or dataset, it's ideal to have a GPU or TPU at hand. Google Colab is one such tool you can make use of to re-train/fine-tune your custom model.

5. Encoding and decoding text sequences

from gpt2_client import GPT2Client

gpt2 = GPT2Client('124M') # This could also be `355M`, `774M`, or `1558M`
gpt2.load_model()

# encoding a sentence
encs = gpt2.encode_seq("Hello world, this is a sentence")
# [15496, 995, 11, 428, 318, 257, 6827]

# decoding an encoded sequence
decs = gpt2.decode_seq(encs)
# Hello world, this is a sentence

Contributing

Suggestions, 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!

Donations

Open-source is really fun. Your donations motivate me to bring fresh ideas to life. If interested in supporting my open-source endeavours, please do donate – it means a lot to me!

Buy Me A Coffee

Licence

MIT

More Repositories

1

aft-pytorch

Unofficial PyTorch implementation of Attention Free Transformer (AFT) layers by Apple Inc.
Python
226
star
2

sight

πŸ‘ Sightseer: TensorFlow library for state-of-the-art Computer Vision and Object Detection models
Python
204
star
3

grafog

Graph Data Augmentation Library for PyTorch Geometric
Python
127
star
4

Angelfire

Add custom context menus to any DOM element πŸ‘ΌπŸ» πŸ”₯ (NUSHackers OpenHack 2020 Winner)
JavaScript
102
star
5

tokenlearner-pytorch

Unofficial PyTorch implementation of TokenLearner by Google AI
Python
63
star
6

CycleGANsformer

Unpaired Image-to-Image Translation with Transformer-based GANs in PyTorch [WIP]
Python
56
star
7

involution_pytorch

Unofficial PyTorch implementation of the Involution layer from CVPR 2021
Python
45
star
8

Network-Optimisation

Neural Network Hyper-parameter Tuning with Genetic Algorithms πŸ€– 🧬
Python
34
star
9

mlp-mixer-tf

Unofficial Implementation of MLP-Mixer in TensorFlow
Python
26
star
10

rna-backbone-design

Source code for RNA-FrameFlow: SE(3) Flow Matching for 3D RNA Backbone Design
Python
25
star
11

CS3244-Tutorial-Material

All supplementary material used by me while TA-ing CS3244: Machine Learning
Python
21
star
12

Metroplex

Using Simulated Annealing, Hill Climbing, and Genetic Algorithms to recreate images using shapes πŸ€– 🧬
Python
16
star
13

nam-pytorch

Unofficial PyTorch implementation of Neural Additive Models (NAM) by Agarwal, et al.
Python
13
star
14

TransGAN-PyTorch

[WIP] PyTorch implementation of the TransGAN paper
Python
12
star
15

gpt3-pytorch

Unofficial PyTorch Implementation of OpenAI's GPT-3
Python
11
star
16

dalle2-pytorch

Unofficial PyTorch implementation of DALL-E 2 by OpenAI
Python
10
star
17

CatGAN

Generative Adversarial Network (GAN) that generates cat faces using Google's Quick, Draw! dataset πŸ€– 😺
Python
10
star
18

Audily

πŸ“‘πŸ€– Convert PDF scans to Audiobooks with a click!
Python
8
star
19

Regolith

πŸŒ— An elegant Pseudocode-like programming language built in Python
Python
7
star
20

vit-tensorflow

[WIP] TensorFlow wrapper of Vision Transformer for SOTA image classification
Python
7
star
21

Hringhorni

A Python library for Cellular Automaton simulations
Python
6
star
22

gym-maze

gym environment for randomly-generated mazes
Jupyter Notebook
5
star
23

Neural-Machine-Translation

Neural Machine Translation model to translate English to German and vice versa.
Jupyter Notebook
5
star
24

Strelitzia

Summaries of Hacker News shared links right in your inbox
Python
4
star
25

audio-tf-pytorch

[WIP] Unofficial PyTorch implementation of the "Audio Transformer" by Verma & Berger (2021)
Python
4
star
26

Reverse-Image-Search

Small-scale reverse image search
Python
4
star
27

FGSM-Attacks

TensorFlow implementation of Fast Gradient Sign Method Attack, a White Box Adversarial Attack
Python
4
star
28

cs4248-project

Code for CS4248 Group Project (AY22/23 Sem 2)
Jupyter Notebook
4
star
29

Sheet-to-Bot

Quickly build chatbots using only Google Sheets
Python
3
star
30

open-vector

A computation and visualisation library for vector operations
Python
3
star
31

NachoDB

πŸ§€ A simple sqlite-like database written in C
C
3
star
32

CommandF

Darkflow integration to CommandF
Python
3
star
33

Golang

Learning experiments with Golang
Go
3
star
34

model-boosting

Pure NumPy implementations of popular Boosting techniques
Python
3
star
35

andromeda

Chrome extension that renders LaTeX in Substack articles
JavaScript
3
star
36

HackNRoll-2021

TypeScript
3
star
37

Hybrid-Recommendation-System

A collaborative and content-based movie recommendation system created using Python.
Python
3
star
38

Quinque

Fun image manipulation experiments with Python
Python
3
star
39

Stock-Prediction

Python code to predict Google (Alphabet) stock using QUANDL datasets. Accuracy may vary between 90% to 100%.
Python
3
star
40

convnext_pytorch

Unofficial PyTorch implementation of ConvNeXt by Facebook AI Research (FAIR)
3
star
41

cs4243-project

Fast Image Vector Search Tool built in PyTorch
Jupyter Notebook
3
star
42

ChatWithPerson

Generated personas you can chat with πŸ‘ΎπŸ‘‹πŸ»
Python
3
star
43

Simulated-Annealing-Attack

Attacking Neural Networks using Stochastic Optimisation
Python
2
star
44

mlblocks-beta

Landing page for the MLBlocks beta platform
CSS
2
star
45

Network-Estimation

Mapping the internal representation of a neural network using a k-nearest neighbours model
Python
2
star
46

Back-to-C

Picking up the C syntax after 8 years
C
2
star
47

Chirp.me

Convert Twitter Threads to Blog Posts
JavaScript
2
star
48

Keras

Keras experiments playground
Python
2
star
49

Cinimax

Minimax Implementation in C
C
2
star
50

shrubtest

🌳πŸ§ͺIn-editor API testing and experimentation extension for VS Code
TypeScript
2
star
51

GraphConv-Lightning

⚑️ Graph Convolutional Network built entirely using PyTorch Lightning
Python
2
star
52

gin-attn-conv-pytorch

Unofficial implementation of Graph Isomorphism Network in Python
Python
2
star
53

gist2blog

Converting a collection of GitHub Gists into a blog
JavaScript
2
star
54

Mario-Source

Implementation of Mario using runes from Source v1
JavaScript
2
star
55

Actor-Critic-Model

TensorFlow implementation of the Actor-Critic model
Python
2
star
56

locoprop-pytorch

Unofficial PyTorch implementation of LocoProp, an enhanced BackProp alternative
Python
2
star
57

BBCS-ML-Winpetition

Datasets and resources for the BuildingBloCS 2019 Conference
2
star
58

Leetcode-Experiments

A repo where I store some basic Leetcode experiments
Python
2
star
59

Vocq-Neural-Summariser

GitHub issues summariser based on Neural Network applications
Python
2
star
60

Long-Short-Term-Memories

A podcast hosted by 2 DialoGPT Conversational Agents
Python
2
star
61

Team-Unicorn-CXA-2019

A platform for SMEs to connect with local producers and sellers in the area and keep track of their inventories and sales in a seamless manner
JavaScript
2
star
62

COVID-Viz

JavaScript
2
star
63

CS2109S-Teaching-Material

Resources used when teaching CS2109S in AY21/22 Sem 2 at NUS Computing
2
star
64

3D-Environment-Navigation

Python
2
star
65

gaia-vscode

The dark Earth VS Code theme to cybernetically enhance your programming
2
star
66

Cheery

Cheery website frontend
CSS
2
star
67

Neural-Additive-Models

TensorFlow 2.0 implementation of Neural Additive Models
Python
2
star
68

Proximal-Policy-Optimization

PPO algorithm on David Ha's Slime Volley environment
2
star
69

PCADA_Demo_1

Source code for SMU Academy PCADA Module 3: Web Scraping and Natural Language Processing
HTML
2
star
70

KL-Divergence

A few experiments with KL Divergence for different probability distributions
Python
2
star
71

SimCLR-pytorch

Unofficial PyTorch implementation of SimCLR by Google Brain
Python
2
star
72

Lightning-Transformer

Transformer and Attention Mechanism written in PyTorch Lightning ⚑️
Python
2
star
73

Minimax-Heuristic-Search-T3

Using Minimax to generate Tic Tac Toe rollouts
Python
2
star
74

Singer2Singer

[WIP] Inter-singer audio/song conversion using Deep Learning
Python
2
star
75

Sobel-Filters

A few experiments with the Solber-Feldman Operator for traditional Edge Detection
Python
2
star
76

NodeJS-MongoDB-Blog

A complete API for blog posts using a MongoDB AWS backend πŸ“‘
HTML
2
star
77

Self-driving-car

A lane detection, vehicle detection and path finding algorithm
Python
1
star
78

Coolor

Syntax highlighting Chrome Extension for code blocks in Medium articles
JavaScript
1
star
79

Stylus.js

CSS substitute fully written in JavaScript to supercharge the process of styling elements.
JavaScript
1
star
80

MLBlocks-Phase-2

Frontend for MLBlocks Phase 2
JavaScript
1
star
81

Natural-Language-Processing

NLP experiments playground
Python
1
star
82

hello-github-actions

1
star
83

Colour-Pop

A web-app to perform the "Colour Pop" effect on images
JavaScript
1
star
84

OpenAI-Problems

OpenAI Reinforcement Learning and Algorithms Playground
Jupyter Notebook
1
star
85

pytorch-perceiver

PyTorch implementation of the Perceiver model by DeepMind
Python
1
star
86

Lingua

An open-source embeddable component for code snippets with multi-lingual support
CSS
1
star
87

heimdall

Heimdall: access the power of GPT-3 straight from Siri on any apple product :D
Python
1
star
88

turismo

Lightweight PyTorch Geometric library to build graph transformers quickly [WIP]
1
star
89

pytorch-graphdl

PyTorch Implementation of popular Graph Neural Networks
Python
1
star
90

gym-navmaze

Custom gym maze environment
Python
1
star
91

H2-Computing-Practicals

Python
1
star
92

PyTorch

Jupyter Notebook
1
star
93

gnn-training-tricks

Analysing the impact of training tricks of GNN optimisation
Python
1
star
94

Neural-Suppression

Soucre code for Neural Suppression, a regularisation technique inspired by Dropout
Python
1
star
95

Opt-In-Opt-Out

What happens when you use an optimiser to optimise an optimiser?
Python
1
star
96

Python-Decision-Trees

Pure Python implementation of Decision Trees trained on the Iris Dataset
1
star
97

cs3244-project

Source code for CS3244 AY22/23 Sem 1 Group Project
1
star
98

HackerRank

HackerRank algorithms playground
Python
1
star
99

ANN-with-swapped-training-sets

Implementation of a Deep Neural Network with swapped training and tests sets. Instead of training on pixel values to predict class labels, this implementation trains on the class labels to predict pixel values for each class.
Python
1
star
100

Tumour-Analysis

A python script to predict likelihood of Malignant or Benign Tumours in a patient using 30 different features.
Python
1
star