Text Generation

Hugging Face 🤗 Pipeline

Jan Kirenz

Python setup

from transformers import pipeline

Intuition

Use Cases

  • Stories Generation

  • Code Generation: can help programmers in their repetitive coding tasks.

Task Variants

  • Completion Generation Models
    • Given an incomplete sentence, complete it.
    • Continue a story given the first sentences.
    • Provided a code description, generate the code.
  • Text-to-Text Generation Models
    • Translation
    • Summarization
    • Text classification
  • Inference
    • takes an incomplete text and returns multiple outputs with which the text can be completed.

Pipeline example

Create pipeline with GPT-2

generator = pipeline('text-generation', model='gpt2')

Provide text

my_text = "Hello, I study online media management"

Make inference

generator(my_text, max_length=30, num_return_sequences=3)

Output

[{'generated_text': 'Hello, I study online media management, and while most of my courses focus on the business, I specialize in online development.\n\nIf you know'},
 {'generated_text': 'Hello, I study online media management at a computer-focused high school. One of my favorite exercises is watching and reading my students write things.\n'},
 {'generated_text': 'Hello, I study online media management, so I know that it\'s not the best idea to be a part of the digital environment yourself."\n\n'}]