Development

Make an AI Art Generating Slack Bot in 10 Minutes

February 3, 2023
Make an AI Art Generating Slack Bot in 10 Minutes

AI art gen­er­a­tion has been explod­ing in the last year, and the once dif­fi­cult to man­age tools are now incred­i­bly easy to use, mod­u­lar, and ver­sa­tile. Last year I cre­at­ed a Dis­cord bot that gen­er­at­ed AI art from my own pri­vate­ly host­ed DALL‑E 2 pub­lic mod­el. This was quite cum­ber­some to set­up, and it wasn’t very flex­i­ble if I want­ed to make small changes. After explor­ing what’s changed in the last year, I can con­fi­dent­ly say that all of my pain points have been resolved by the lat­est iter­a­tion of art gen­er­a­tion: Sta­ble Dif­fu­sion. In this post, I want­ed to share how easy it tru­ly is to cre­ate your own AI art gen­er­at­ing Slack bot. I chose Slack this time, because at Michi­gan­Labs we use Slack as our com­mu­ni­ca­tion tool, so it’s a lit­tle more rel­e­vant to our own work. But there is a lot of over­lap between Slack and Dis­cord, if you’re using their respec­tive Type­script libraries. With that said, start your timer, because this should only take about 10 min­utes (as long as your inter­net speed is fast enough!).

Require­ments #

You just need a com­put­er with a 4GB+ GPU.

You can use Lin­ux or Apple Sil­i­con, but it’s a bit eas­i­er (with respect to NVIDIA dri­vers) with Win­dows. For my set­up, I like mod­u­lar­i­ty and low-pro­file hard­ware. I use the following:

  1. Intel NUC 8 — One of the few mini PCs that has Thun­der­bolt support
  2. Raz­er Core X — This is an excel­lent plug-and-play enclo­sure that uses Thunderbolt
  3. RTX 3080 — This gen­er­ates about one image per sec­ond, which has been plen­ty fast for my needs

Get­ting Start­ed #

Next, you’ll need to install a cou­ple depen­den­cies and acti­vate a new Slack application.

The Sta­ble Dif­fu­sion Web UI

This sta­ble dif­fu­sion web UI project gives us more than what we’re try­ing to accom­plish, but that’s a good thing! It’s high­ly exten­si­ble, with tons of options for adding more mod­els and cus­tomiza­tions. It can also pro­vide a real­ly great web fron­tend to inter­act with your host­ed mod­el. For our Slack bot pur­pos­es how­ev­er, fol­low these steps to get it ready for a dif­fer­ent interface:

  1. Install all of the depen­den­cies from the Web UI readme (the git repo, Python 3.10+, and the Sta­ble Dif­fu­sion 4GB model)

  2. Go into the stable-diffusion-webui repo and open webui-user.bat, and change the COMMANDLINE_ARGS so that your file looks like this:

     @echo off
        
     set PYTHON=
     set GIT=
     set VENV_DIR=
     set COMMANDLINE_ARGS=--api --nowebui --listen
        
     call webui.bat
    
    • --api tells the web UI to gen­er­ate a swag­ger doc at the server’s <IP>/doc web­site path. This is option­al, but help­ful when you’re try­ing to learn about what your new api can do 
    • --nowebui makes it start a bit faster by skip­ping the fron­tend. This is optional 
    • --listen tells the api serv­er to run on the local net­work, instead of the local host. This is required for our Slack bot to be able to access it 
  3. Run webui-user.bat and the serv­er should start up in a com­mand prompt window

To ver­i­fy that it’s run­ning prop­er­ly, try going to <IP_ADDRESS>:7861/docs in a brows­er. You should see a Swag­ger UI page.

The Slack Bot

Next, we’ll cre­ate a very sim­ple Slack appli­ca­tion using Type­script. This bot will be invoked in a Slack chan­nel by typ­ing /art <PROMPT>, and the bot will reply with the result from our Sta­ble Dif­fu­sion model.

  1. Make sure you have Node.js installed

  2. Clone my bare-bones Type­script Slack bot tem­plate to fol­low along quicker

     git clone https://github.com/DaveAldon/AI-Art-Slack-Bot
    
  3. Inside this repo, install the depen­den­cies with a quick npm install. Then cre­ate an .env file that match­es the .env.example file. For the BACKEND_URL field, add your Sta­ble Dif­fu­sion server’s IP address and port like this: http://<IP>:7861. The API runs on port 7861 by default

  4. Go to the Slack appli­ca­tion dash­board, and cre­ate a new app from an app man­i­fest. This will make con­fig­u­ra­tion easier

    You can use the man­i­fest I used for this basic exam­ple below:

     display_information:
       name: AI-Art-Bot
     features:
       bot_user:
         display_name: AI Art Bot
         always_online: false
       slash_commands:
         - command: /art
           description: Get AI generated art from your prompt
           should_escape: false
     oauth_config:
       scopes:
         bot:
           - commands
           - users:read
           - files:write
     settings:
       interactivity:
         is_enabled: true
       org_deploy_enabled: false
       socket_mode_enabled: true
       token_rotation_enabled: false
    

    We need the files:write per­mis­sion, which can be dif­fer­ent from ordi­nary Slack bots, because of how we’ll upload our pho­tos into the channel.

  5. Install to a workspace

    Press the Install App to Workspace but­ton, and autho­rize the bot to be installed in your workspace.

  6. Retrieve the need­ed tokens

    Go to App-Lev­el Tokens in your bot’s set­tings page, and add a new one with what­ev­er name you want, and the connection:write scope. This will be used to send mes­sages to the chan­nel. Place this token in your .env file as SLACK_APP_TOKEN.

    Last­ly, we need to gen­er­ate an OAuth token. Go to Install App and copy the Bot User OAuth Token. Put this in your .env file as SLACK_BOT_TOKEN

  7. Run the bot, and test it out in a chan­nel in your workspace

     npm run start
    

    Go to a chan­nel and type /art a red apple. You should see the bot reply with a gen­er­at­ed image.

    If you try it and you get a slack bot error: 'not_in_channel' error, add your bot to the spe­cif­ic chan­nel by typ­ing /invite @<BOT_NAME>.

Con­clu­sion #

If you were able to fol­low along and get your Slack bot to return a pho­to, con­grat­u­la­tions! You’ve just cre­at­ed a sim­ple inter­face into an AI model!

I hope that you enjoyed walk­ing through this tuto­r­i­al, and are tak­ing time to look through the Sta­ble Dif­fu­sion Web UI docs to dis­cov­er more ways you can inter­act with these amaz­ing models!

If you had issues, the beau­ty of how this works is that we can trace every­thing back quite eas­i­ly. There aren’t a lot of mov­ing parts to this.

Trou­bleshoot­ing #

Check that your Sta­ble Dif­fu­sion mod­el is run­ning and acces­si­ble on your local network

Try going to <IP_ADDRESS>:7861/docs in your brows­er. Is this work­ing on your local machine?

If you remove the --nowebui flag, can you go to <IP_ADDRESS>:7861 and see the web UI?

Check that your Slack bot is run­ning properly

Try a basic use-case of just return­ing some sta­t­ic text. Replace the app.command func­tion with this:

app.command("/art", async ({ body, ack }) => {
    await ack({
    response_type: "in_channel",
    text: "test!",
    });
});

Does your bot reply with test! in the channel?

David Crawford
David Crawford
Software Developer

Looking for more like this?

Sign up for our monthly newsletter to receive helpful articles, case studies, and stories from our team.

A Study of Human-Centered Design Off Screen - Burning Man Festival
Design

A Study of Human-Centered Design Off Screen - Burning Man Festival

February 13, 2023

Kourtney examines the Burning Man Festival and its fascinating history of human-centered design

Read more
MichiganLabs’ approach to product design: A strategic, problem-solving process
Design Team

MichiganLabs’ approach to product design: A strategic, problem-solving process

February 12, 2024

Product design, or UX design, is a strategic problem-solving process that leads to a valuable digital product. Learn what to expect when working with product designers for your custom software.

Read more
From bits to qubits: The future of quantum computing
Development

From bits to qubits: The future of quantum computing

July 10, 2024

Learn how quantum computing, which uses qubits capable of representing both 0 and 1 simultaneously, revolutionizes data processing. Discover the impact it could have on industries like finance and pharmaceuticals by enhancing risk assessment, fraud detection, and drug discovery.

Read more
View more articles