Animal Facts!

https://github.com/nelsonlove/animal-facts

Animal Facts is a Flask-based web app that generates absurdly false facts about animals.

The app is built using Flask and was deployed using AWS Elastic Beanstalk. It uses Amazon S3 for file storage. Here’s how it works:

  1. When a user searches for an animal, the app forwards the search to the Pixabay Developer API, which responds with a list of URLs for matching images. Search results for a given query are stored in a dict to reduce API hits.
  2. A random URL is chosen from the search result. The corresponding image is downloaded, given a unique filename, and pushed to an S3 bucket with public accessibility.
    • To further reduce API hits, the app checks the S3 bucket for the image prior to download.
  3. The app then obtains a completion from GPT-3 using the OpenAI API.
    • GPT-3’s input is a short list of silly animal facts with a piece of introductory text setting up the task.
    • The result is run through OpenAI’s content filter engine to ensure that it is safe for the public — an important step when working with stochastic language models.
    • Repeated unsafe responses result in a default caption.
  4. The image is captioned with the text using the Pillow image library for Python.
  5. The caption is hashed and appended to the filename for upload to the S3 bucket.
    • Aside from allowing durable hard links for captioned images, this also serves as another caching measure; if the above steps would result in an identical captioned image, the app will skip directly to pulling the image from S3.
  6. The URL is embedded in the template and the user sees the page.
  7. Hilarity ensues.