Your Shakespeare Play

Generating Shakespeare with GPT-2

Welcome!

TL;DR: Click ABC and get started!

The server (API endpoint) (see Github link in the About section for more info) is hosted on Azure Web App Service with CPU only, so it will take a long time to generate output. Be patient!

Results are stored in localStorage, so if you don't clear them, they will stay even if you close your browser.

Intro

This is an app that uses Artificial Intelligence to generate Shakespeare passages. Specifically, it uses the GPT-2 model, published by OpenAI in 2019.

You can provide a prompt, and the app will generate some Shakespeare passages that GPT-2 "thinks" should follow. Or you can just let it generate without providing any input, and see what it comes up with. Here's a sample I got while writing this page. It's based on Hamlet:

OPHELIA:
How stands your disposition to be married?

HAMLET:
It is an honor that is not seen; for 'tis not seen.

OPHELIA:
What is it, my lord?

HAMLET:
Make but about it. It is no more unhandsome
to confess your impotent affection than to note
your passion.

OPHELIA:
O, my lord, your fantasy takes delight in
't. We shall hear your conference, and, by my
faith, welcome to England.

HAMLET:
 [aside]  Still on my mother.

OPHELIA:
My lord, I come to whet your mind to
church.

HAMLET:
As heartlings do, then.

Some things to note:

  1. In the output box, the bolded part is the prompt, the rest is output.
  2. You don't have to provide a passage from the play. In fact, this is the whole point. The prompt used for Ophelia here is actually taken from Romeo and Juliet.
  3. AI isn't perfect. Expect things that are grammatically correct but have no meaning.

There two defined "modes":

  1. "ABC" (or, Standard mode): choose a play and enter a prompt.
  2. Advanced mode: you can customize many other options, and prompt is not furthur processed. See later section "Advanced mode".

Framing a prompt

There are many ways to write a prompt. You can click Example to get some inspiration. You can "talk" to a character, let a character "talk" to a character, or let an unspecified character provoke a response from a character, or you might get a monologue without any character speaking it. You might also get stage directions.

When specifying a character, use this format:

[CHARACTER IN CAPS]:
[Words spoken by the character]

Namely, put the character name in all caps, add a colon after it, and start his/her words on a new line. You can also just specify the character name and no content, in which case the model will try to fill in what the character would say.

Output

The output box puts your prompt and the output together. Sometimes, the model will continue on the same line as your prompt, instead of always starting a new line.

All outputs are saved in browser's localStorage, so they will be there even if you close your browser. However, having multiple tabs of this app open at the same time may cause weird concurrency issues. The app has not been tested in that way, so it's best to avoid doing that.

If you want to export your generated output, currently the best way is copy and paste.

Advanced mode

In advanced mode, the prompt is not furthur processed. This means that the play's ID (in the context of this app) is prefixed to every line. This was done for model training, which probably accounts for good results in containing the play output to a given play. This method is inspired by this.

Previously when prefixing was not done and when the model was trained with the the tinyshakespeare text, I ran into "issues" where Horatio would be speaking as a woman and characters across plays would be talking to each other. Which might be what you want, depending on circumstances.

But prefixing also gives you the flexibility to deliberately mix and match plays and give a hint of which play you want. I did not try this, but you're free to explore!

For a complete list of plays supported in this app and their ids, see About.

You can also specify other parameters: top-k, top-p, temperature, minimum ouput length, and maximux output length. Look at this article for what those inputs mean, or simply ask Google.

More AI and Shakespeare

Check out a much more serious poetry generation with GPT-2, and now with GPT-3 (which produces works with much higher quality!).