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:
- Intel NUC 8 — One of the few mini PCs that has ThunÂderÂbolt support
- RazÂer Core X — This is an excelÂlent plug-and-play encloÂsure that uses Thunderbolt
- 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:
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)
Go into the
stable-diffusion-webui
repo and openwebui-user.bat
, and change theCOMMANDLINE_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
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.
Make sure you have Node.js installed
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
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 theBACKEND_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 defaultGo 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.Install to a workspace
Press the
Install App to Workspace
butÂton, and authoÂrize the bot to be installed in your workspace.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 asSLACK_APP_TOKEN
.LastÂly, we need to genÂerÂate an OAuth token. Go to
Install App
and copy theBot User OAuth Token
. Put this in your.env
file asSLACK_BOT_TOKEN
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?
Looking for more like this?
Sign up for our monthly newsletter to receive helpful articles, case studies, and stories from our team.
Build what matters: Prioritize value over feature count
August 1, 2024Focusing on value delivery—rather than just feature count—combines your business goals with your users’ needs to achieve real software ROI.
Read moreLessons Learned from our Associate Developer
September 13, 2023One of our Associate Software Developers, Rohit, reflects on his time at MichiganLabs working on a short-term project, what he learned about real-world development, and the software consultancy business model.
Read moreSimplifying the 4 forces of AI
April 9, 2024Artificial Intelligence (AI) is becoming more prevalent, but less understood. Through examples of organizations leading the charge in each of these areas, I hope to empower you to make better decisions for your enterprise and career.
Read more