Terrible quotes, daily.
You can view this site live a https://www.robharper.ca/inspiration-ai
This is a really simple project that wires together a bunch of technology with a little bit of code to create a website of daily terrible "inspirational" quotes. Technology used:
- Google Cloud Scheduler
- Google Cloud Functions
- OpenAI GPT 3.5
- OpenAI DALL-E
- Google Cloud Storage
- GitHub Pages
- Jekyll
The following diagram shows how things are wired together:
Before running locally or deploying, create an .env.yaml
file in the _job
directory containing:
OPENAI_API_KEY: {key from OpenAI}
IMAGES_BUCKET_NAME: {Cloud Storage Bucket Name}
GCP_PROJECT_ID: {Project ID}
GITHUB_REPO: {GitHub repo: username/reponame}
GITHUB_TOKEN: {GitHub token, must be able to create content at the above repo}
Before continuing, ensure that the following APIs are enabled:
- Cloud Run
- Cloud Build
- EventArc
- Cloud Functions
- Cloud Storage
Create it
gcloud iam service-accounts create inspiration-ai-uploader --project inspiration-ai
Give it permissions:
# Allow it to upload to Cloud Storage
gcloud projects add-iam-policy-binding inspiration-ai --member="serviceAccount:inspiration-ai-uploader@inspiration-ai.iam.gserviceaccount.com" --role=roles/storage.objectCreator
# Allow it to invoke a Cloud Function
gcloud projects add-iam-policy-binding inspiration-ai --member="serviceAccount:inspiration-ai-uploader@inspiration-ai.iam.gserviceaccount.com" --role=roles/run.invoker
gcloud storage buckets create gs://static-inspiration-ai --project=inspiration-ai --default-storage-class=standard --location=europe-north1 --uniform-bucket-level-access
Make the bucket public:
gcloud storage buckets add-iam-policy-binding gs://static-inspiration-ai --member=allUsers --role=roles/storage.objectViewer
To create a pubsub topic to trigger the cloud function, run:
gcloud pubsub topics create inspiration-ai_generate
gcloud functions deploy inspiration-ai-function \
--project=inspiration-ai \
--gen2 \
--runtime=python311 \
--region=northamerica-northeast1 \
--source=. \
--entry-point=generate_inspiration \
--trigger-topic=inspiration-ai_generate \
--service-account=inspiration-ai-uploader@inspiration-ai.iam.gserviceaccount.com \
--env-vars-file .env.yaml
Run the function in local mode:
cd _job
./run-function.sh
Simulate a pubsub event, run the function:
cd _job
./test-function.sh
To generate a quote for a specific day, run:
gcloud pubsub topics publish inspiration-ai_generate --message="{\"date\":\"2023-10-27\"}" --project=inspiration-ai