Hollings.io

Twitter | GitHub
Check out my twitter or my bot's twitter or my other bot's twitter

Home

I wrote a book

In 2019, while playing around with the brand new GPT-2, I started a project called This Recipe Does Not Exist. The idea was to train a model on a dataset of recipes and then generate new recipes. The results were... interesting. I ended up with realistic recipes, as well as recipes like Garage Rocking Rolls - where it asks you to simmer 2 liters of vegetable oil in your refrigerator - and Apple 2560 Mhz QRP Antenna Connection Kit for APS-C 1225 Dual Mode Antenna - which is just... banana cookies?

I had a lot of fun with the project, but I wanted to do something more. In hopes of getting a little more of an audience, I hacked together the Twitter bot, @DoesRecipe. It was pretty simple to create an API endpoint for a random recipe, and a quick AWS Lambda script grabs a recipe and generates an image with the recipe on it. The bot then just posts the image to Twitter. At one point I hooked up the GPT-3 API to the bot, letting users request their own custom recipes. It went mini-viral a handful of times, but I was worried that accepting input from random users would result in the classic "Racist AI Chatbot" scenario and decided to remove that feature.

I let the bot do its thing for a year or two, until a friend nerdsniped me hard with a simple comment:

With Twitter hinting that they want to eventually slowly kill the free API, I thought this would be the perfect project to cap off This Recipe Does Not Exist, and let the bot die.

So I started brainstorming how I would actually make this book. I ended up deciding on a couple of things:

Stable Diffusion

I've been playing around with Stable Diffusion since the day it was publicly released. I run a little personal-ish discord server running my own bot that generates images through the AUTOMATIC1111 webUI API. Creating the cover image was as simple as posting a message in my discord server:

After an hour or two of Photoshop, I had a full sized cover. I asked ChatGPT to generate a few paragraphs for the back cover, and added a photo of the random public domain chef painting that I used for the Twitter bot's profile picture:

Generating an image for each recipe was slightly harder, but still pretty simple. The repo I'm using to run Stable Diffusion recently added a simple API, so I was able to loop over every recipe and run it overnight:

# paraphrased a bit
for recipe in recipes:
    data = {
        "prompt": recipe.title,
        "steps": 20,
        "height": 768,
        "width": 768,
    }
    r = requests.post("http://127.0.0.1:7860/sdapi/v1/txt2img", json=data)
    file = base64.b64decode(r.json()["images"][0])
    image_file_path = f"images/{prompt}.png"
    with open(image_file_path, "wb") as f:
        f.write(file)

ChatGPT

As an OpenAI fan, ChatGPT's release was a big deal for me. I look for every excuse to use it in my everyday life. Using it to add a silly little introduction to each recipe was a no-brainer. The OpenAI API for this is pretty simple, so I added a call to ChatGPT while constructing the recipe markdown:

# Get an intro paragraph from ChatGPT
response = openai.ChatCompletion.create(
    model="GPT-4",
    messages=[{"role": "user",
               "content": markdown + """\n=====\nplease write a one-paragraph intro for this recipe. 
               Include one or two tips and/or some slight variations or serving suggestions"""}],
    n=1,
)
intro_paragraph = response['choices'][0]['message']['content']

# generate the markdown for the whole recipe and append it to the full output.
markdown = f"""
\pagebreak
# {title}
{intro_paragraph}
Ingredients:
{ingredients_string}
Directions:
{directions_string}
---
{image_embed_string}
"""

output.append(markdown)

Export the PDF

At this point I had a large markdown file of recipes, a folder of images, and a book cover image. The next was to export it to Epub and PDF to publish it somewhere. With a little guidance from a friend, I was able to use Pandoc to get the files I needed. I had never used Pandoc or LaTeX before, so this was a bit of a learning experience. After generating dozens of PDF files, I got a working script. In title.txt, I needed to add a bit of metadata for the book:

---
title: "This Cookbook Does Not Exist"
author: John Hollingsworth
rights: All rights reserved
language: en-US
geometry: paperheight=9in,paperwidth=6in,margin=1in
output:
    pdf_document
graphics: yes
---

to embed the images for each recipe formatted correctly, I needed to add a little bit of LaTeX to the markdown in order to vertically center the image and size it correctly:

\begin{center}
\vspace*{\fill}
\begin{figure}
\includegraphics[height=250pt]{images/<IMAGE>.png}
\caption{<RECIPE TITLE>}
\label{fig:<RECIPE TITLE>}
\end{figure}
\vspace*{\fill}
\end{center}

and finally, the script to generate the PDF:

pandoc.exe -o this-cookbook-does-not-exist.pdf --toc title.txt introduction.md recipes.md conclusion.md

Publish It

I settled on using Amazon KDP to publish the book. KDP was a good choice for me because there is no upfront cost - they print it on demand and take a cut of the sale. I chose a 6x9 inch hardcover book and paperback, uploaded the PDF files, and just had to wait for it to be approved. Here's where I ran into really annoying stuff:

  1. I had originally set the margins of the PDF to 0.75 inches, but the uploader on their site was failing with an unspecified error until I bumped the margins to 1 inch. I couldn't find any documentation from Amazon about minimum margin sizes, so this took me a day or two of tedious trial and error to nail down. Make sure the margins are 1 inch
  2. The cover image needs to be exactly the right size. I had used the KDP Cover Template Calculator to grab a template for a specific number of pages when I made the original cover image, but because I edited the book slightly, the size of the cover changed. Every time I made an edit to the PDF, I needed to open up Photoshop and save a new version of the cover that was a few pixels different. Wait until your manuscript is uploaded correctly to create your book cover
  3. The paperback version (but not the hardcover) of the cover kept getting denied because of a typo. The typo in the cover is definitely a feature, not a bug, because I think it's hilarious when Stable Diffusion tries to generate text. I tried to resubmit the "typo" cover two more times before finally giving up and creating a new cover with the actual text "This Cookbook Does Not Exist" on it. Oh well, at least the hardcover has the cool version

And that's it! After hammering out the kinks, I was able to get the book live on Amazon: https://www.amazon.com/This-Cookbook-Does-Exist-Computational/dp/B0C2RG14P5

Honestly, seeing an Amazon store page with my name on it was really cool to see. Although I'm not expecting to actually sell any copies, I'm really happy with how the book turned out.


The footer is actually pretty cool