Money Forward Developers Blog

株式会社マネーフォワード公式開発者向けブログです。技術や開発手法、イベント登壇などを発信します。サービスに関するご質問は、各サービス窓口までご連絡ください。

20230215130734

Slack App that uses Stable Diffusion to generate images

This article is a part of MoneyForward Tech Blogathon 2022.

Slack App that uses Stable Diffusion to generate images

Hello. I'm MORI Masakazu. I'm working for Money Forward, Inc. as a Machine Learning engineer. Recently I've been wandering between Splatlands and Outlands.

I made a Slack App that uses Stable Diffusion to generate images. In this article, I introduce how to install and use it.

How to install

1. Hugging Face

This App uses Stable Diffusion v1-4 to generate images. You need the access token to download the Stable Diffusion model from the Hugging Face website. The app automatically downloads the model using the access token.

  1. Register your account in the Hugging Face.
  2. Get your access token.
  3. Accept the license agreement of the Stable Diffusion.

2. Machine

I used an AWS EC2 instance g4dn.xlarge to confirm it was working well. It has 4 vCPU and 16 GiB memory. It also has an NVIDIA T4 GPU with 16GB of memory. You should use an NVIDIA GPU with at least 10GB of memory.

I don't introduce the detailed instructions to build an EC2 instance. Here are some attentions when you build it.

  • You don't need to open an inbound port to receive requests to run the Slack App because the Slack App runs in the Socket Mode.
  • It would be best if you chose "AWS Deep Learning AMI." You don't need to install NVIDIA GPU Driver, CUDA Driver, or conda because they are already installed in that image. I used AWS Deep Learning AMI GPU PyTorch 1.12 (Ubuntu 20.04).

It's beneficial and secures not be needed to open an inbound port. In that sense, Socket Mode is good.

3. Create Slack App in your workspace

You can create your app in your workspace acccording to this document. You need to set these configurations in the app.

Socket Mode

  • turn Enable Socket Mode on
  • input Token Name
  • note Token down because you will need it later when you set SLACK_APP_TOKEN

Slash Commands

  • hit the Create New Command button
  • input these settings
    • Command: you can set any word that starts /, such as /sdbot
      • note it down because you will need it later when you set COMMAND and generate images
    • Short Description: Generate an image
    • Usage Hint: [prompt]

OAuth & Permissions - Bot Token Scopes

  • grant these OAuth Scopes
    • chat:write
    • files:write
    • files:read

OAuth & Permissions - OAuth Tokens for Your Workspace

  • hit the Install to Workspace button
  • note Bot User OAuth Token down because you will need it later when you set SLACK_BOT_TOKEN

4. install and run

The Git Repository of this Slack App is published here. Please download it on the machine you created before with the following commands.

git clone git@github.com:marisakamozz/stable-diffusion-bot.git
cd stable-diffusion-bot

After downloading, run the following commands on the machine to create a conda environment and enter it.

conda env create -f environment.yaml
conda activate sdbot

Then, create .env file in the directory and set the HuggingFace token that was got in step 1, Slack App Token, Slack Bot Token, and Slack Slash Command that were got in the step 3 in the .env file.

YOUR_TOKEN=hf_xxxxxxxxxxxxxx
SLACK_BOT_TOKEN=xoxb-999999999999999999999999
SLACK_APP_TOKEN=xapp-999999999999999999999999
COMMAND=/sdbot

After you make the .env file, you can run it using the following command.

python3 scripts/main.py

How to use

You need to install the app in an arbitrary channel.

You can generate an image using the slash command and any prompt like this.

/sdbot a jumping cat in a garden

You can display help messages using the following slash command.

/sdbot help

That's all. Please enjoy it.

Acknowledgments

This app is forked from this repository by Soichiro Yoshimura. Thanks for creating the original one. I made these changes from the original one.

  • use slash commands to generate images
  • remove some features such as waifu-diffusion

マネーフォワードでは、エンジニアを募集しています。 ご応募お待ちしています。

【会社情報】 ■Wantedly株式会社マネーフォワード福岡開発拠点関西開発拠点(大阪/京都)

【SNS】 ■マネーフォワード公式noteTwitter - 【公式】マネーフォワードTwitter - Money Forward Developersconnpass - マネーフォワードYouTube - Money Forward Developers