Karam Abu MokhEmail me

Case study Live

GPT-2, Game of Thrones Q&A

A GPT-2 I trained on the five A Song of Ice and Fire books, so it answers in their voice. Before it answers, it searches the books and reads the best passages, and the page lists them under the answer. It is running now: ask it something below.

Query matrix card
My TransformerBlock and GPTModel classes

Ask it something

The model runs on a public Hugging Face Space. Ask about Westeros: it searches the books, reads the best passages, then answers, and the passages are listed under the answer.

Checking the model…

Open the full demo

My role
Solo, end to end
When
July to August 2025, retrained and given a book search October 2026
Built with
  • Python
  • PyTorch
  • tiktoken
  • OpenAI API
  • Google Colab
  • Kaggle
  • NumPy
  • BM25
  • bge-small embeddings
  • Reciprocal rank fusion
  • MiniLM cross-encoder
  • ONNX Runtime
  • FastAPI
  • Docker
  • Hugging Face Spaces
  • pytest

GPT-2, in one line

GPT-2 is the language model OpenAI released in 2019: a much smaller, older relative of the models behind ChatGPT. Its weights are public, so you can train it further yourself.

What I built

The whole path: books to text, text to pieces, practice questions written by a second AI model, the GPT-2 network classes, written in PyTorch while following a build-an-LLM course and loaded with OpenAI's released weights, fine-tuning in Colab and on Kaggle, a search over the five books (26,112 short passages, found by their words and by their meaning, then re-ranked), a second round of training so the model uses what the search finds, a FastAPI service on a free Hugging Face Space and a question box, the one on this page.

How it works

  1. The five books are read into plain text, cleaned of copyright pages and contents lists, and cut into pieces.
  2. A second AI model, gpt-4o-mini, writes one fact question and one full-sentence answer per piece.
  3. GPT-2 medium, in network code I wrote myself, is fine-tuned on book passages and question-answer pairs mixed in the same training steps.
  4. The books are cut again into 26,112 passages of about 128 tokens, each tagged with its book and chapter, and indexed twice: by the words in it and by its meaning. The index is private, because the books are copyrighted.
  5. When you ask, the service searches both indexes, a small ranking model re-reads the ten best passages, and the best three go in front of your question.
  6. Pasting passages in front of the model alone did not help: it ignored them. So I trained it a second time, on free Kaggle GPU time, on 19,289 questions that come with their passage.
  7. A small FastAPI service on a free Hugging Face Space answers in about five to twenty seconds. The answer comes back with its sources: book, chapter and a snippet of at most 25 words. The first question after a quiet spell wakes it, which takes about a minute.

5books, all of A Song of Ice and Fire so far

34,001cleaned question-answer pairs, from 61,650 first written

26,112book passages in the search, each with its book and chapter

MediumGPT-2 size, from OpenAI's released weights

Retraining changed the voice

Measure2025 modelFirst retrainSecond retrain
Loss on 1,233 test questions2.7342.1462.119
Loss on one book passage3.0722.6592.615
Ends its own answer (10 questions, 3 tries)0 of 3030 of 3030 of 30
Held-out test from one part of A Feast for Crows, Oct 2026. The two 2026 runs never trained on it; the 2025 model had read the passage, but not the questions. Lower loss is better. The second retrain mixed the book text and the question-answer pairs in the same training steps.

The book search changed the facts

MeasureNo searchSearch, same modelSearch, retrained (live)
Fact questions right (of 30)121017
New fact questions right (of 42)2813
Four old mistakes fixed (of 4)114
Answers with an invented name36%28%12%
Same questions, same settings, same computer. A fact counts as right when 2 of 3 tries had the accepted words and none of the rejected ones; the 42 new questions were never used to train or tune. An invented name is a person named in the answer who is in neither the question nor the passages (196 held-out questions). The search puts the right passage in its top 5 for 78 percent of those questions. Asked live on the Space, one try per question, the 30 facts gave 19 right in Oct 2026.

The same network, two more ways

I re-wired the same GPT-2 network into a spam classifier for text messages: 48.75% accuracy before training, 84.00% after, on 300 messages it had never seen. My build notebooks go from the tokenizer to fine-tuning, one step at a time.

Try the spam classifier

Paste a text message, or tap an example. The model answers with one word.

Checking the model…