Yet another Telegram bot, this one generates stable diffusion images on request.
In a nutshell, Botatobot listen for users requests for images and push them in a (bounded) worker queue, the queue gets processed by a server running a Stable Diffusion inside a either locally or as a service, like replicate.com
To run Stable Diffusion, you have two options:
- Locally, using a Cog Container.
- Remotely, using Replicate.com.
To run the Telegram bot server you need Go and a Telegram account.
First you need to install Cog, Docker and the NVIDIA Container Toolkit.
Then clone the pre-configured cog-stable-diffusion repository. Follow their instructions in the README to ensure the model is running correctly.
In particular you need to download the weights first
cog run script/download-weights
build your container with
cog build -t stable-diffusion
and run it with
docker run -d -p 5001:5000 --gpus all stable-diffusion
This will download, create and run a container with the stable diffusion image running on port 5001, eg.http://127.0.0.1:5001/predictions
Go to Replicate.com api-tokens and generate your token, keep it safe.
You will also need to choose a Stable Diffusion model version. You can find the available versions here https://replicate.com/stability-ai/stable-diffusion/versions
- Talk to @BotFather on Telegram
- Send
/newbot
to create a new bot - Follow the instructions to set a name and username for your bot
- Copy the token that BotFather gives you
The token looks like this: 123456789:ABCDEF1234567890ABCDEF1234567890ABC
Set the following environment variables or create a .env
file with the following content:
TELEGRAMBOT_TOKEN=123456789:ABCDEF1234567890ABCDEF1234567890ABC
MODEL_URL=http://127.0.0.1:5001/predictions
The BOT_TOKEN
you got from the @BotFather, and MODEL_URL
indicates where the Cog Stable Diffusion is running, most likely a docker container in your local machine.
There is an optional variable OUTPUT_PATH
that indicates the path where the generated images will be saved.
You need the Telegram and Replicate tokens and the Stable Diffusion model version, create an .env
file or make them available in the environment.
BOT_TOKEN=123456789:ABCDEF1234567890ABCDEF1234567890ABC
REPLICATE_TOKEN=1234567890abdfeghijklmnopqrstuvwxyz
REPLICATE_VERSION=a9758cbfbd5f3c2094457d996681af52552901775aa2d6dd0b17fd15df959bef
The BOT_TOKEN
you got from the @BotFather,REPLICATE_TOKEN
and REPLICATE_VERSION
you get from replicate.com.
Additionally you can set REPLICATE_URL
to a custom url, and OUTPUT_PATH
to indicate the path where the generated images will be saved.
Build withgo build -o build/botatobot cmd/botatobot/main.go
and then run ./build/botatobot
Tell the bot /help
to let him self explain.
In some scenarios, like deploying to Heroku or other platforms you need a http rest health endpoint. Botatobot includes such functionality, to activate it include a LOCAL_PORT
variable.
Botatobot uses the excellent go-telegram package to interact with the Telegram API. Go check it out!