How to generate text with KerasHub

Pretrained language-model presets in KerasHub let a Keras 3 project turn a prompt string into generated text without training a model first. A short generation run checks the package install, backend selection, preset download, tokenizer/preprocessor path, and model call before the code moves into a notebook, API, or batch job.

The GPT2CausalLM task exposes generate() for causal language modeling. Loading from gpt2_base_en attaches the matching preprocessor, and a shorter preprocessor sequence_length keeps a CPU smoke test bounded while still exercising the preset assets.

The script uses the TensorFlow backend because keras-hub installs TensorFlow for preprocessing and validates without extra backend wheels. Generated wording can vary when sampling is enabled; compiling with the greedy sampler keeps the smoke-test output tied to one completion path.

Steps to generate text with KerasHub:

  1. Install KerasHub and the TensorFlow backend in the project environment.
    $ python -m pip install --upgrade keras-hub tensorflow

    Use an isolated virtual environment, notebook kernel, or container for model dependencies. The first preset load also downloads model assets into the Keras/Kaggle cache for that environment.

  2. Create generate_kerashub_text.py with a backend choice, GPT-2 preset, greedy sampler, and prompt.
    import os
     
    os.environ.setdefault("KERAS_BACKEND", "tensorflow")
     
    import keras
    import keras_hub
     
     
    PRESET = "gpt2_base_en"
    PROMPT = "KerasHub lets developers"
     
     
    preprocessor = keras_hub.models.GPT2CausalLMPreprocessor.from_preset(
        PRESET,
        sequence_length=64,
    )
    model = keras_hub.models.GPT2CausalLM.from_preset(
        PRESET,
        preprocessor=preprocessor,
    )
    model.compile(sampler="greedy")
     
    completion = model.generate(PROMPT, max_length=40, strip_prompt=True)
     
    print(f"backend: {keras.config.backend()}")
    print(f"preset: {PRESET}")
    print(f"prompt: {PROMPT}")
    print(f"completion: {completion.strip()}")

    Set KERAS_BACKEND before importing keras, keras_hub, or any project module that imports Keras. A backend change after import does not move the running process to another framework.

  3. Run the script.
    $ python generate_kerashub_text.py
    backend: tensorflow
    preset: gpt2_base_en
    prompt: KerasHub lets developers
    completion: create and share their own content with the community.
    
    KerasHub is a free, open source, open source, and open source software platform for developers

    The first run downloads gpt2_base_en weights and tokenizer files. Use a larger preset or a GPU-backed environment only when the project needs higher-quality generation than a local smoke test.

  4. Confirm the printed fields match the selected backend, preset, prompt, and completion.

    strip_prompt=True prints only the generated continuation. Remove it when the downstream code needs the prompt and completion in one returned string.