Slack bot for generating images with Stable Diffusion
You will need to create some resources outside of this project in order to get it to work. The required resources and their information we will need is as follows:
- Hugging Face
- Create a User access token in your Hugging Face account
- Note Access token for later
- Read License and agree to terms for Stable Diffusion v1.4
- Create a User access token in your Hugging Face account
- Slack
- Create a Slack app with the following user scopes
chat:write
users:read
- Note Signing Secret, Client ID and Client Secret for later
- Create a Slack app with the following user scopes
- GCP
- Create Pub/Sub topic and subscription
- Storage bucket for image uploads
- Note Topic, Subscription and Bucket for later
Only the request processor here is required to be built, this can be done as follows:
$ docker build -t slack-diffusion .
Authentication handler functions.AuthenticationFunction
This can be deployed using any method you wish that is publicly accessible. Should you choose to use Google Cloud Functions, all the required code in this project can be found in functions.zip
.
Required env vars
PROJECT_ID
// GCP Project ID
Once function has been deployed, update request URL in Slack to point to deployment.
Slash command handler functions.SlashFunction
This can be deployed using any method you wish that is publicly accessible. Should you choose to use Google Cloud Functions, all the required code in this project can be found in functions.zip
.
Required env vars
PROJECT_ID
// GCP project IDPUBSUB_TOPIC
// Pub/Sub topic created earlierSLACK_SIGNING_SECRET
// Slack signing secret (recommend using Google Secret Manager here if running on Cloud Functions)SLACK_CLIENT_ID
// Slack client ID (recommend GSM)SLACK_CLIENT_SECRET
// Slack client secret (recommend GSM)
Once function has been deployed, create a slash command in Slack and point to deployment.
At this point you should be able to successfully call the diffusion bot from within Slack, with status hanging on Sending...
Request processor / Image generation main.main
Ensure your application-default-credentials have been updated with $ gcloud auth login --update-adc
.
Recommended to run on a machine with graphics card with >8GB memory.
$ docker run --rm --gpus=all \
-v huggingface:/home/huggingface/.cache/huggingface \
-v ~/.config/gcloud:/home/huggingface/.config/gcloud \
--env-file docker.envs \
slack-diffusion
The following environment variables can be filled into docker.envs
Required env vars
HUGGINGFACE_TOKEN
// user access token from Hugging FacePROJECT_ID
// GCP project IDPUBSUB_SUBSCRIPTION
// Pub/Sub subscription created earlierSTORAGE_BUCKET
// Storage bucket created earlier
Optional env vars
IMAGE_WIDTH
// width of generated images, must be divisible by 8, defaults to 512IMAGE_HEIGHT
// height of generated images, must be divisible by 8, defaults to 512
You should now be able to generate images through Slack using Stable Diffusion, great success 👍
Based on works by: