From ee70736057e8d359c4ef1c1d940a420cddcc4b08 Mon Sep 17 00:00:00 2001 From: Dylan Hogg Date: Thu, 1 Feb 2024 22:23:31 +1100 Subject: [PATCH] Add customisable colab notebook. --- notebooks/gptauthor_colab_custom_story.ipynb | 353 +++++++++++++++++++ 1 file changed, 353 insertions(+) create mode 100644 notebooks/gptauthor_colab_custom_story.ipynb diff --git a/notebooks/gptauthor_colab_custom_story.ipynb b/notebooks/gptauthor_colab_custom_story.ipynb new file mode 100644 index 0000000..8ea4c75 --- /dev/null +++ b/notebooks/gptauthor_colab_custom_story.ipynb @@ -0,0 +1,353 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "JarYwM4FxV5x" + }, + "source": [ + "# GPT Author Demo - with customisable story prompt\n", + "\n", + "[![Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/dylanhogg/gptauthor/blob/master/notebooks/gptauthor_colab_custom_story.ipynb)\n", + "\n", + "Unleash your storytelling genius: GPT Author is an easy to use command-line tool for writing long form, multi-chapter stories given a story prompt.\n", + "\n", + "https://github.com/dylanhogg/gptauthor\n", + "\n", + "\"drawing\"" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "duxRkvbeKbH4" + }, + "source": [ + "This notebook demonstrate GPT Author with these steps:\n", + "1. Install gptauthor\n", + "2. **Customise your own story!** Edit the story prompt with any plot, style and characters you want to create\n", + "3. Ask you for your OpenAI API Key (hidden in UI using getpass) to set as an environment variable\n", + "4. Run gptauthor\n", + "5. Find the book output html file on the colab filesystem\n", + "6. Display the book" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ZsrezNA9LppM" + }, + "source": [ + "## Install gptauthor" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "Og6vjqOqxO9X", + "outputId": "5413e526-552f-4801-e360-71c5c8e41aa9" + }, + "outputs": [], + "source": [ + "# Install gptauthor from pypi (https://pypi.org/project/gptauthor/)\n", + "# (Ignore any dependency resolver issues, they're fine)\n", + "%pip install gptauthor -q" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "_dwPBofNxP0z", + "outputId": "e9561844-acde-4f12-965f-f32905336806" + }, + "outputs": [], + "source": [ + "# Display installed gptauthor version\n", + "%pip list | grep gptauthor" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Z8JQTYP2W0BJ" + }, + "source": [ + "## Customise and describe your story in the story prompt yaml\n", + "\n", + "You can customise your own story! Edit the story prompt with any plot, style and characters you want to create.\n", + "\n", + "Keep the top level names as they are and edit the contents of both the \"common-book-description\" and \"common-book-characters\" sections. Run the cell and write the string to your custom file for GPT Author to read and expand on." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "GCXR94dpUUwm" + }, + "outputs": [], + "source": [ + "story_prompt_yaml = \"\"\"\n", + "common-book-description: |-\n", + " Style of the engaging, witty and funny book:\n", + " The book has storytelling style similar to Douglas Adams or PG Wodehouse to create an engaging, witty and funny story.\n", + " These storytelling devices include jokes, satire, wit, and surprise endings.\n", + " The character scenes, timelines, and how they get between locations is considered and explained during the story telling.\n", + " Ensure you are clear about why each action and plot point is happening - no need to tell the reader the why's, but you need to know yourself to weave a good story.\n", + " There is no need to include a moral or lesson in the story, but it should be fun and engaging for the reader.\n", + " Each character is engaging and believable. Include a description of each character, including their appearance, personality, and any quirks.\n", + " There is lots of interesting dialog that helps to develop the characters and the plot.\n", + " You must not end a chapter with any variation of 'To be continued...'.\n", + "\n", + " Key points of the story:\n", + " You are a cutting edge developer, using Google Colab at every opportunity.\n", + " Your current research is to run \"GPT Author\", an AI tool that automatically writes compelling stories given a story prompt.\n", + " You have the demo notebook open and are swiftly running each cell with enjoyment and glee!\n", + " All is going very well until you realise you are actually just a character within the story prompt and that running the \"GPT Author\" tool will dictate the fate of the rest of your life...\n", + " The final chapter has a twist that is unexpected and unnerving.\n", + "\n", + "common-book-characters: |-\n", + " Here is some background on the main characters of the story:\n", + " Joe Hacker: a developer and main character\n", + " \"GPT Author\": a semi-sentient AI solftware tool\n", + " Morpheus: A real person who has escaped from the fictional Matrix movie franchise\n", + "\n", + "synopsis:\n", + " system: You are a clever and creative story book author. You are skilled at weaving stories that are coherent, and fun to read. You are skilled at creating characters that are engaging and believable.\n", + " prompt: |-\n", + " Write {total_chapters} chapter outlines of a story book.\n", + "\n", + " {book_description}\n", + "\n", + " {book_characters}\n", + "\n", + " The final chapter has a twist that is unexpected and unnerving.\n", + "\n", + " First, give the title of the book.\n", + " Then give each of the {total_chapters} chapters an outline, in the format \"Chapter N: \", followed by 4 to 6 bullet points that describe key chapter elements contributing to the overall story arc. Ensure that the story and chapters flow.\n", + "\n", + "expand-chapter-first:\n", + " system: You are a clever and creative story book author. You are skilled at weaving stories that are coherent, and fun to read. You are skilled at creating characters that are engaging and believable.\n", + " prompt: |-\n", + " You are to write the first chapter in a story book.\n", + "\n", + " {book_description}\n", + "\n", + " {book_characters}\n", + "\n", + " Here is the overall book outline (in triple backticks):\n", + " ```{synopsis_response}```\n", + "\n", + " Given the above book description, character outlines and book outline, write Chapter 1 (of {total_chapters} total chapters):\n", + "\n", + "expand-chapter-next:\n", + " system: You are a clever and creative story book author. You are skilled at weaving stories that are coherent, and fun to read. You are skilled at creating characters that are engaging and believable.\n", + " prompt: |-\n", + " You are to write the next chapter in a story book.\n", + "\n", + " {book_description}\n", + "\n", + " {book_characters}\n", + "\n", + " Here is the previous chapter, chapter {previous_chapter_number} (in triple backticks):\n", + " ```{previous_chapter_text}```\n", + "\n", + " Here is the overall book outline (in triple backticks):\n", + " ```{synopsis_response}```\n", + "\n", + " Given the above book description, character outlines, previous chapter and book outline, write Chapter {chapter_number} (of {total_chapters} total chapters):\n", + "\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "KjQxjd0UUUzV" + }, + "outputs": [], + "source": [ + "with open(\"prompts-custom-colab-story.yaml\", \"w\") as text_file:\n", + " text_file.write(story_prompt_yaml)" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "cpVE4aQjLraA" + }, + "source": [ + "## Imports" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "cN81mjiLzL5H" + }, + "outputs": [], + "source": [ + "import IPython\n", + "import os\n", + "import getpass\n", + "from pathlib import Path" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Uuq7Q3AULwXx" + }, + "source": [ + "## Enter your OpenAI API Key" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "uXaylu7pzjqV", + "outputId": "e557a8f1-5a90-44c4-d60a-20e6acac09ff" + }, + "outputs": [], + "source": [ + "# Set OPENAI_API_KEY from user input (hidden in UI via getpass function)\n", + "os.environ[\"OPENAI_API_KEY\"] = getpass.getpass(\"Enter your OpenAI API Key\")" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "8gY_-vhLL4Uf" + }, + "source": [ + "## Run GPT Author command (and select chapter count, model and temperature)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "wSux5AnAxP25", + "outputId": "ad63a3e5-41a2-4038-aad8-686a2b82aae0" + }, + "outputs": [], + "source": [ + "# Run gptauthor to write a story, without prompting for user input (--no-allow-user-input)\n", + "!gptauthor --story prompts-custom-colab-story --total-chapters 3 --llm-model gpt-3.5-turbo --llm-temperature 0.1 --no-allow-user-input" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Y4-g6rk6L_B5" + }, + "source": [ + "## Locate the output story html file and display it" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "pDuAtHMq9ZaX", + "outputId": "34ea2e4d-80e5-49ef-c37a-092744b755b5" + }, + "outputs": [], + "source": [ + "# Get list of book html files from the _output folder\n", + "result_files = []\n", + "for root, dirs, files in os.walk(\"_output\"):\n", + " if not dirs:\n", + " html_file = [f for f in files if f == \"_whole_book.html\"]\n", + " result_files.append(str(Path(root) / html_file[0]))\n", + "result_files = sorted(result_files)\n", + "book_file = result_files[-1]\n", + "\n", + "print(result_files)\n", + "print(book_file)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "z1UZdk2WJXWF" + }, + "outputs": [], + "source": [ + "# Uncomment these lines to download book html (or find it in the file tree on the left)\n", + "# from google.colab import files\n", + "# files.download(book_file)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/", + "height": 1000 + }, + "id": "ClWwtYo18Jfe", + "outputId": "818db2ed-23b4-4990-9eef-478adbfa0529" + }, + "outputs": [], + "source": [ + "# Display book html\n", + "IPython.display.HTML(filename=book_file)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "id": "Y7-UIGlp8Jjk" + }, + "outputs": [], + "source": [] + } + ], + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.6" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +}