First, you need to install Python or use Google Colab online by signing in to it. Most of the generative AI libraries, like Hugging Face's transformers, work seamlessly with Python. We will work on Google Colab as it is easy to use and work on it.
Go to Google Colab. Create a new notebook. Run the following to install libraries in Colab(in the cell):
pip install transformers torch tensorflow numpy matplotlib
Pre-trained models allow you to skip the resource-intensive process of training your own model and instead use existing models to solve tasks such as text generation, summarization, and more. One of the most popular libraries for accessing pre-trained models is Hugging Face's transformers library.
For example, you can use the GPT-2 model to generate text based on a given prompt.
Code Example: Generate Text with GPT-2:
Here’s how you can use the GPT-2 model to generate text based on a prompt.
click on new cell and paste the below code.
from transformers import GPT2LMHeadModel, GPT2Tokenizer
# Load the pre-trained GPT-2 model and tokenizer
model = GPT2LMHeadModel.from_pretrained("gpt2")
tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
# Define the input text prompt
input_text = "Once upon a time"
# Tokenize the input text (convert it into IDs that the model can understand)
input_ids = tokenizer.encode(input_text, return_tensors="pt")
# Generate text based on the input
output = model.generate(input_ids, max_length=100)
# Decode the output (convert the generated token IDs back to readable text)
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
# Print the generated text
print(generated_text)
Explanation:
GPT2LMHeadModel.from_pretrained("gpt2"): This loads the GPT-2 model from Hugging Face’s model hub.
GPT2Tokenizer.from_pretrained("gpt2"): This loads the tokenizer for GPT-2, which converts text into tokens that the model can understand.
model.generate(input_ids, max_length=100): This tells the model to generate up to 100 tokens of text starting from the input prompt.
tokenizer.decode(output[0], skip_special_tokens=True): This converts the generated tokens back into human-readable text.
Now that you have set up your environment and learned how to generate text with GPT-2, it’s time to explore and experiment further.
Experiment with different prompts:
Try modifying the input text to see how the model responds to different prompts. For example:
input_text = "In the future, technology will"
input_text = "A curious scientist discovers"
Adjust the max_length parameter to generate longer or shorter text. For example:
output = model.generate(input_ids, max_length=200)
Control randomness with temperature and top_k:
You can modify the temperature parameter to control the randomness of the output. Higher values (e.g., 1.0) make the text more random, while lower values (e.g., 0.5) make it more deterministic.
output = model.generate(input_ids, max_length=100, temperature=0.7, top_k=50)
temperature: Controls the randomness of predictions. Lower values make the text more repetitive, while higher values introduce more creativity.
top_k: Limits the number of possible next words to the top k choices, which can help with more coherent text generation.
By the end of this section You have learned how to use pre-trained models like GPT-2 for text generation without needing to train a model from scratch. Experimented with simple changes to modify the input text and fine-tune the generated output. This sets the foundation for you to start experimenting more with generative models in the next stages, whether it's generating creative text or exploring more advanced tools for working with different types of data. Happy Coding!