Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/cyberzhg/keras-gpt-2
Load GPT-2 checkpoint and generate texts
https://github.com/cyberzhg/keras-gpt-2
gpt-2 keras language-model nlp
Last synced: about 1 month ago
JSON representation
Load GPT-2 checkpoint and generate texts
- Host: GitHub
- URL: https://github.com/cyberzhg/keras-gpt-2
- Owner: CyberZHG
- License: mit
- Archived: true
- Created: 2019-02-18T06:33:45.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-01-22T11:44:25.000Z (almost 3 years ago)
- Last Synced: 2024-09-27T13:23:14.400Z (about 1 month ago)
- Topics: gpt-2, keras, language-model, nlp
- Language: Python
- Homepage: https://pypi.org/project/keras-gpt-2/
- Size: 94.7 KB
- Stars: 128
- Watchers: 7
- Forks: 32
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Keras GPT-2
[![Version](https://img.shields.io/pypi/v/keras-gpt-2.svg)](https://pypi.org/project/keras-gpt-2/)
![License](https://img.shields.io/pypi/l/keras-gpt-2.svg)\[[中文](https://github.com/CyberZHG/keras-gpt-2/blob/master/README.zh-CN.md)|[English](https://github.com/CyberZHG/keras-gpt-2/blob/master/README.md)\]
Load pretrained weights and predict with [GPT-2](https://d4mucfpksywv.cloudfront.net/better-language-models/language_models_are_unsupervised_multitask_learners.pdf).
## Install
```bash
pip install keras-gpt-2
```## Demo
```python
import os
from keras_gpt_2 import load_trained_model_from_checkpoint, get_bpe_from_files, generatemodel_folder = 'xxx/yyy/117M'
config_path = os.path.join(model_folder, 'hparams.json')
checkpoint_path = os.path.join(model_folder, 'model.ckpt')
encoder_path = os.path.join(model_folder, 'encoder.json')
vocab_path = os.path.join(model_folder, 'vocab.bpe')print('Load model from checkpoint...')
model = load_trained_model_from_checkpoint(config_path, checkpoint_path)
print('Load BPE from files...')
bpe = get_bpe_from_files(encoder_path, vocab_path)
print('Generate text...')
output = generate(model, bpe, ['From the day forth, my arm'], length=20, top_k=1)# If you are using the 117M model and top_k equals to 1, then the result will be:
# "From the day forth, my arm was broken, and I was in a state of pain. I was in a state of pain,"
print(output[0])
```