
In our previous Hugging Face with gpt-2 project, we used the text-generation pipeline. This is great, but it hides all the powerful options. In this post, we’ll explore Hugging Face Manual Text Generation to unlock more flexibility and control.
To get full control, you must load the model and tokenizer manually. This lets you control things like “creativity” (temperature) and “quality” (top_k).
Step 1: Installation
pip install transformers torch
Step 2: Load the Model and Tokenizer
We will load GPT-2 and its tokenizer.
from transformers import AutoTokenizer, AutoModelForCausalLM import torch model_name = "gpt2" # (a classic, smaller model) tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForCausalLM.from_pretrained(model_name)
Step 3: Tokenize, Generate, Decode
This is the same 3-step process as our manual translation project.
- Tokenize: Convert our English prompt into numbers.
- Generate: Feed those numbers into the model, which outputs new numbers (the generated text).
- Decode: Convert those new numbers back into human-readable text.
prompt = "The future of Python in 2026 will be"
# 1. Tokenize (convert text to numbers)
inputs = tokenizer(prompt, return_tensors="pt")
# 2. Generate (the AI "thinks")
# This is where we can add powerful options!
# - 'max_length': Total length of the output
# - 'temperature': Controls randomness. Higher = more "creative".
# - 'top_k': Picks from the 'k' most likely next words.
output_tokens = model.generate(
**inputs,
max_length=50,
temperature=0.7,
top_k=50
)
# 3. Decode (convert numbers back to text)
generated_text = tokenizer.batch_decode(output_tokens, skip_special_tokens=True)[0]
print("--- AI Generated Text ---")
print(generated_text)Output:
--- AI Generated Text --- The future of Python in 2026 will be a little different from what it is today. It will be a language that is more mature, more stable, and more powerful.
You now have full, granular control over your text generation, allowing you to fine-tune the “creativity” of your AI.
Key Takeaways
- This article explores Hugging Face Manual Text Generation for enhanced flexibility and control over text generation.
- To gain full control, manually load the model and tokenizer, allowing adjustments for creativity and quality.
- The process involves three steps: tokenization, generation, and decoding of text for better output.
- Users can fine-tune the ‘creativity’ of AI-generated text by manipulating parameters during the process.





