Generative AI Code Example in Python: A Simple Beginner’s Guide

Generative AI Python workflow showing prompt, pretrained model, generated text, and human review

Generative AI can create new content such as text, images, audio, and code. One of the easiest ways to understand how it works is to try a small practical example.

In this guide, we will use Python and a pretrained text-generation model to generate text from a simple prompt. The example is designed for beginners and can be run in environments such as Google Colab or Visual Studio Code.

What We Will Build

The example will follow this simple process:

Prompt → Generative AI Model → Generated Text → Human Review

We will give the model a short prompt, and it will generate additional text based on patterns learned during training.

Step 1: Install the Required Libraries

Install the Transformers and PyTorch libraries:

pip install transformers torch

If you are using Google Colab, you can run:

!pip install transformers torch

Step 2: Load a Pretrained Generative AI Model

We can use the Hugging Face Transformers library to load a pretrained text-generation model.

from transformers import pipeline

generator = pipeline(
    "text-generation",
    model="distilgpt2"
)

The pipeline() function provides a simple way to use a pretrained model without building or training one from scratch.

Step 3: Create a Prompt

Now create a short prompt:

prompt = "Artificial intelligence can help businesses by"

result = generator(
    prompt,
    max_new_tokens=50,
    num_return_sequences=1
)

print(result[0]["generated_text"])

The model will continue the prompt and generate new text.

The exact result may be different each time because generative models can produce different outputs from the same or similar prompts.

How the Example Works

The process is simple:

  1. Prompt: We provide starting text.
  2. Tokenization: The text is converted into tokens the model can process.
  3. Model processing: The pretrained model predicts likely next tokens.
  4. Generation: New tokens are generated to extend the prompt.
  5. Human review: The result should be checked for accuracy and relevance.

This demonstrates an important idea behind many Generative AI systems: the model generates new content based on patterns learned from its training data.

Try Your Own Prompts

You can experiment by changing the prompt:

prompt = "Machine learning is useful because"

or:

prompt = "Generative AI can improve productivity by"

Comparing different prompts is a simple way to understand how the wording and context of an input can influence the generated response.

Important Limitations

Generated text should not automatically be treated as factual.

A Generative AI model may produce information that is incorrect, incomplete, outdated, or unrelated to the intended meaning. Human review remains important, particularly when AI-generated content is used for professional or high-impact tasks.

This small example is therefore useful for understanding the basic workflow, but real-world Generative AI applications normally require additional controls, testing, evaluation, and sometimes access to external or verified information.

Final Thoughts

Generative AI becomes easier to understand when you experiment with it directly.

With only a few lines of Python, we can provide a prompt to a pretrained model and generate new text. The same basic idea is used in much more advanced systems for writing, summarisation, coding, question answering, and other language-based tasks.

The next step is to learn how to set up a complete AI development environment and run machine learning and deep learning projects using tools such as Visual Studio Code and Google Colab.

Wise up to AI. Learn it. Use it. Grow with it.


Written by Yousef Ismail AbuAlShawareb


References and Further Reading

Hugging Face (n.d.) Transformers: Pipeline. Hugging Face Transformers Documentation. Available at: Hugging Face Transformers – Pipeline documentation (Accessed: 15 September 2026).

Hugging Face (n.d.) DistilGPT2. Hugging Face Model Hub. Available at: Hugging Face – DistilGPT2 model page (Accessed: 15 September 2026).

Radford, A., Wu, J., Child, R., Luan, D., Amodei, D. and Sutskever, I. (2019) Language Models are Unsupervised Multitask Learners. OpenAI. Available at: OpenAI – Language Models are Unsupervised Multitask Learners (Accessed: 15 September 2026).

Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A.N., Kaiser, Ł. and Polosukhin, I. (2017) ‘Attention Is All You Need’, Advances in Neural Information Processing Systems, 30. Available at: NeurIPS – Attention Is All You Need (Accessed: 15 September 2026).

Comments