AI That Writes


You’ve used AI that looks and AI that reads. Today: AI that writes. You give it the beginning of a sentence, and it makes up what comes next — the same basic idea behind the chatbots you’ve heard about.

💡 In Colab. The model downloads the first time you run it.

Load a writer

!pip install -q transformers
from transformers import pipeline

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

distilgpt2 is a small, fast writing model — perfect for experimenting. (Bigger models write better but are slower to download.)

Give it a starting line

result = writer("Once upon a time, a brave little robot", max_length=40)
print(result[0]["generated_text"])

The AI takes your opening and continues it, inventing the rest. Run it a few times — you’ll get a different story each time, because there’s some randomness in how it picks words.

💡 The result is a list with a dictionary (like last lesson). result[0]["generated_text"] pulls out the text.

Try it 🎯

  1. Change the starting line to your own — a story opener, a joke setup, a recipe.
  2. Change max_length=40 to 60 for a longer piece.
  3. Run the same prompt three times. Notice it’s different each time.

How does it actually work?

Here’s the surprising truth: the AI doesn’t understand your story or plan an ending. It learned, from enormous amounts of text, which word tends to come next. So it just keeps predicting the next likely word, one at a time. That’s it. It’s a very, very good guesser of “what word probably follows” — not a thinker.

That’s why AI writing can sound smooth but go off the rails, repeat itself, or confidently make things up. It’s predicting, not knowing.

Think about it 🔮

Why do you get a different story each time you run the same prompt? (Because the AI adds a bit of randomness when choosing each next word — so it doesn’t always pick the single most likely one. Same start, different paths.)

Fix the bug 🐞

This tries to print the AI’s story but shows a messy list-and-dictionary instead of clean text:

result = writer("In the year 3000,", max_length=40)
print(result)

(Reach in for the text: print(result[0]["generated_text"]). result is a list; result[0] is a dictionary with the key "generated_text".)

Your mission 🚀

Make a story starter tool: ask the user for an opening line with input(), feed it to the writer, and print the AI’s continuation. Run it with a few different openings and pick your favorite story.

start = input("Start a story: ")
result = writer(start, max_length=50)
print(result[0]["generated_text"])

What you learned today

  • pipeline("text-generation") makes an AI that continues your text.
  • The result is a list with a dictionary: result[0]["generated_text"].
  • The AI predicts the next likely word over and over — it doesn’t understand or plan.
  • That’s why it’s creative but also repeats, wanders, or makes things up.

Next time you’ll put all three AIs together in a project — and start thinking hard about where AI gets things wrong. 🔎