Skip to content
Afa' Afa'

Text Generation with GPT2

Generate French text with a small GPT-2 model and Transformers.

1 min read Updated

Generate French text with a small GPT-2 model from Hugging Face Transformers, sampling with top-k and top-p for variety. The script is below.

text_generation.py

python
import torch
from transformers import GPT2Tokenizer, GPT2LMHeadModel

model_name = 'asi/gpt-fr-cased-small'
tokenizer = GPT2Tokenizer.from_pretrained(model_name)
model = GPT2LMHeadModel.from_pretrained(model_name)

input_sentence = "Il était une fois"
input_ids = tokenizer.encode(input_sentence, return_tensors='pt')
output = model.generate(
  input_ids, 
  max_length=100,
  do_sample=True,   
  top_k=50, 
  top_p=0.95, 
  num_return_sequences=1
)

print (tokenizer.decode(output[0], skip_special_tokens=True))
Something wrong or want to discuss this article? Get in touch

Search articles and projects

Type to filter articles and projects. Use the arrow keys to move through results and Enter to open one. Press Escape to close.