Prerequisites
- A Discord account
- A Replicate account
- A Software Citadel account, with a registered payment method
Introduction
Replicate is a platform that allows to run and fine-tune open-source models. In this tutorial, we will use Replicate to run Stable Diffusion XL generations.
Software Citadel allows you to deploy applications from a Dockerfile. In this tutorial, we will use Software Citadel to deploy a Discord bot that will generate Stable Diffusion XL generations.
Prerequisites
- A Replicate account
- A Software Citadel account, with a registered payment method
- A Discord account
Setting up the Discord bot
- Log in to the Discord Developer Portal and create a new application. You can name it whatever you want.
- Go to the "Bot" tab, and turn on the "Message Content Intent" option.
In this section, you can also click on "Reset token" to generate a new token for your bot.
- Now, let's invite the bot, so we can test it. Go to the "OAuth2" tab, and the "URL Generator" tab. Select the "bot" scope, and the "Send Messages" permission. Then, copy the generated URL and paste it in your browser. You will be asked to select a server to invite the bot to. Select a server, and click on "Authorize".
Developing the Bot
Initializing the workspace
First, we need to create a new directory for our bot:
mkdir gilbert-bot
cd gilbert-bot
Then, we are going to create a new Python project using Poetry:
# Install Poetry (if you don't have it already)
curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | python -
# Initialize a new Python project
poetry init -n
# Install the dependencies
poetry add discord.py python-dotenv replicate
Let's create new file called .env
in the root of the project. For your Discord token, grab it from the Discord Developer Portal. You also need to get a Replicate API token from the Replicate website. You can find your API token in the settings page.
touch .env
Open the .env
file and add the following line:
DISCORD_TOKEN="YOUR_DISCORD_TOKEN"
REPLICATE_API_TOKEN="YOUR_REPLICATE_API_TOKEN"
Do not forget to add this .env file to your .gitignore
file.
Alright, we are ready to start coding!
Let's create a new file called bot.py
in the root of the project. This file will contain the code for our bot.
touch bot.py
Open the bot.py
file and add the following code:
from discord import Intents
from discord.ext import commands
from dotenv import load_dotenv
import os
import replicate
# We load the environment variables
load_dotenv()
# We set up the intents to allow the bot to read messages
intents = Intents.default()
intents.message_content = True
# We initialize the bot
bot = commands.Bot(
command_prefix="/",
intents=intents,
)
@bot.command()
async def imagine(ctx, *, prompt):
# We send a message to let the user know we're working on it.
msg = await ctx.send(f"“{prompt}”\n> Dreaming up an image...")
# We use the Stable Diffusion XL model to generate images.
output = replicate.run(
"stability-ai/sdxl:39ed52f2a78e934b3ba6e2a89f5b1c712de7dfea535525255b1aa35c5565e08b",
input={
"prompt": prompt,
# Customize the following parameters to your liking
# https://replicate.com/stability-ai/sdxl?input=python
},
)
image = output[0]
# We edit the message to show the generated image.
await msg.edit(content=f"“{prompt}”\n{image}")
# Run the bot with the Discord token from the .env file
bot.run(os.environ["DISCORD_TOKEN"])
Deploying the Bot with Software Citadel
To deploy your bot, you can use Software Citadel. Follow these steps:
- Install Software Citadel CLI:
curl -L https://cli.softwarecitadel.com/install.sh | sh
- Authenticate with Software Citadel:
citadel auth login
- Initialize Software Citadel:
citadel init
- Dockerize your bot:
FROM python:3.11
RUN pip install poetry
WORKDIR /project
COPY poetry.lock pyproject.toml /project/
RUN poetry config virtualenvs.create false && poetry install --no-interaction --no-ansi
COPY . /project
CMD python bot.py
- Deploy your bot:
citadel deploy
Congratulations! You have successfully deployed your Discord bot. To test it, you can send a message to your bot in Discord.
Top comments (0)