/genai-kids-stories

Generative AI created stories with Bard / Palm API

Primary LanguageRuby

README

This is an OSS demo of how to get GenAI to work with Google Cloud.

Google Cloud tech stack:

  • Cloud Run (to run the container - currently here)
  • Cloud Build (to automate a new build at every commit! We're serious and lazy here!)
  • Vertex AI GenAI used for:
    • Generating long text (story ideation)
    • Summarization (auto-title! So sweet)
    • Image generation (a different pic per paragraph)
  • Google Translate API to generate a story in A number of languages: ๐Ÿ‡ฎ๐Ÿ‡น๐Ÿ‡ง๐Ÿ‡ท๐Ÿ‡ช๐Ÿ‡ธ๐Ÿ‡ซ๐Ÿ‡ท๐Ÿ‡จ๐Ÿ‡ณ๐Ÿ‡ท๐Ÿ‡บ .. This is the only consumer API used, with an API key
  • Text to Speech API to generate an Audio (italian sample)
  • Google Cloud Storage for ActiveStorage (images and sounds are stored in a GCS bucket)
  • Secrets Manager to hold my app most precious untold secrets: RAILS_MASTER_KEY , TRANSLATE_API_KEY, and service_account_key.json. Actually if You find them in the code, please tell me, as I'm not infallible. I promise you Italian gratitude.

Boring details:

  • Ruby version: 3.2.0
  • Rails version: 7.0.6
  • Frontend: Bootstrap 5.
  • System dependencies: see make install
  • Database: PostgreS
  • ActiveStorage: backend on Google Cloud Storage
  • DelayedJob for job queue (using PGSQL DB , not Redis like most humans - I've just been lazy).
  • direnv to manage ๐ŸŒฑ .envrc and friends. You can create a .envrc.$USER file and it will be auto-slurped by my awesome scripts.

GCP Architecture

This is a simplified architecture of the Google Cloud components used:

App Architecture

This is how it works (thanks Mermaid and stackedit ):

flowchart TD;


    A2["Shakespeare Story ๐Ÿงฉ Prompt"] -- GenAI: Text gen --> B["๐Ÿ“– Story::Body"];
    B -- GenAI: Text summary --> C[Story::Title];
    C -- split --> P1["๐Ÿ“œ Paragraph1"];
    C -- split --> P2["๐Ÿ“œ Paragraph2"];
    C -- split --> Pdot[..];
    C -- split --> P3["๐Ÿ“œ ParagraphN"];

    P1 -- "concat with Kid\n๐Ÿ‘ถ๐Ÿพ visual Description" --> PD1["Par1 + Desc"];
    P2 -- concat.. --> PD2[Par2 + Desc];
    P3 -- concat.. --> PD3[PaN + Desc];

    PD1 -- GenAI Vision --> IMG1["๐Ÿž๏ธ Par1 Image"];
    PD2 -- GenAI Vision --> IMG2["๐Ÿž๏ธ Par2 Image"];
    PD3 -- GenAI Vision --> IMG3["๐Ÿž๏ธ Par3 Image"];

    IMG1 --> END["Story with \n N ๐Ÿ“œ๐Ÿ“œparagraphs \n and N ๐Ÿž๏ธ๐Ÿž๏ธimages"]
    IMG2 --> END
    IMG3 --> END
    P1 --> END;
Loading

INSTALL

  • make sure you create yur own .envrc.yourname and assign the ENV vars you want.
  • Create A svcAcct for GCS and download it under private/sa.json. Do NOT check it in :)
  • Create your own RAILS_MASTER_KEY and keep it aside.
  • Environments:
    • make dev: runs in development, with sqlite3 db and local storage
    • make dev-on-gcp: run in pseudo-dev, but with pgsql DB (and GCS storage)
    • make prod: run in prod, with pgsql DB (and GCS storage)

RUN

  • make reauth-local ensures the GCP has been enabled.
  • make test-gcp if you want to make sure that AI / GCP are working.
  • make dev # or whatever ENV you want to run. Check Makefile for some options
  • Also make sure that the secret key points to a non-expired SvcAcct key ;)

Build on Google

  • Cloud Build (on ricc project ror-goldie).
  • manually created build with:
  1. _RAILS_MASTER_KEY set to cat config/master.key
  2. _DANGEROUS_SA_JSON_VALUE set to cat private/sa.json

Ruby docs

Ruby (and Rails):

Google

This app