Docker image for a Telegram bot that connects to a self-hosted instance of Stable Diffusion
1.2K
Stable Diffusion Telegram Bot
A Docker container that allows you to host your own Telegram bot and generate images using the Stable Diffusion API. Can also be run as just a script without Docker.
Features
!dream command to generate images from prompts.!dream command in an image caption to use both the prompt and image to generate an image.!dream messages or captions to modify the prompt and generate new variations.Examples
| Generating image from text | Generating image from image | Prompt Modification |
|---|---|---|
![]() | ![]() | ![]() |
| - | - | - |
| Retrying | Generating Variation | Upscaling |
![]() | ![]() | ![]() |
Environment Variables
The bot uses several environment variables that can be set when running the Docker container. These variables include:
SERVER_URL: The URL of the Stable Diffusion API server (default: http://127.0.0.1:7860/).TG_TOKEN: The Telegram API token for your bot (default: None).MODEL: The name of the Stable diffusion 1.5 or XL model to use (default: None).SAMPLER: The sampler name to use for image generation (default: DPM++ 2M).SCHEDULER: The scheduler name to use for image generation (default: Karras).CFG_SCALE: An integer between 6 and 14 that controls the CFG scale (default: 7).DENOISING_STRENGTH: A float between 0 and 1 that controls the denoising strength (default: 0.75).STEPS: An integer between 1 and 150 (default: 25).CLIP_LAYERS: An integer between 1 and 12 (default: 2).SEED: A random Integer with value > 0. Value of -1 will generate a ranom number (default: -1).WIDTH: Width of the generated image (default: 512).HEIGHT: Height of the generated image (default: 512).RESTORE_FACES: Enable or Disable face restoring (default: True)NEGATIVE_PROMPT: The default negative prompt for all generations (default: '').RESIZE_MODE: An integer between 0 and 3 for array: [just resize, crop and resize, resize and fill, just resize (latent upscale)] that controls how images are resized (default: 0).LORAS: A string that provides default additions to prompts such as for LORAs (default: '').CONTROLNET_ENABLE: A boolean that enables or disables using ControlNet for img2img generation (default: False).CONTROLNET_MODULE: The name of the ControlNet module to use for img2img generation (default: 'none').CONTROLNET_MODE: An integer between 0 and 2 for array: [Balanced, My prompt is more important, ControlNet is more important] that controls how ControlNet is used for img2img generation (default: 0).RECURSIVE_UPSCALE: A boolean that enables or disables recursive upscaling (default: False).CONTROLNET_UPSCALE_MODEL: Controlnet model to use for tile resample upscale (default: 'none').Getting Started
Telegram BotFather
To use this bot, you will need to create a Telegram Bot and obtain a bot API token. To do so, message /newbot to @BotFather on telegram. BotFather will reply with a series of steps for you to follow including choosing the name of your bot as well as it's username. Once the steps have been completed, BotFather will share your bot API token with you. The token will be a string like
4839574812:AAFD39kkdpWt3ywyRZergyOLMaJhac60qc
Stable Diffusion This bot relies on access to a local Stable Diffusion instance. Please follow the README for Automatic1111 for complete Windows installation steps.
For NVIDIA GPUs:
https://github.com/AUTOMATIC1111/stable-diffusion-webui.git with git clone and navigate to the directory.webui.bat --api --listenFor AMD GPUs:
We'll take advantage of ZLUDA to run Stable Diffusion on an AMD GPU. The Repo by lshqqytiger is an AMD-specific fork of Automatic1111
Note, ZLUDA only works on HIP SDK supported AMD GPUs
Ensure you de-select the
Visual Studio Codeoptions during install or you may get an error during install
%HIP_PATH%bin to PATHhttps://github.com/lshqqytiger/stable-diffusion-webui-amdgpu.git with git clone and navigate to the directory.webui.bat --use-zluda --api --listenNote, first image generation will take a very long time and will look like it is doing nothing. I recommend using the webui at 127.0.0.1:7860/ with a basic prompt such as
a person riding a bike. Leave your computer be for quite some time (20+ minutes) and keep an eye on the terminal window for a progress bar.
Bot Usage
Option 1: Python To get started with Python, ensure you have Python 3.10.6 installed and added to your PATH prior to following the steps below:
git clone and navigate to the directory.python -m pip install -r requirements.txt to install the required packages on your local machine.exmaple.env to .env and replace the values.python bot.py.Option 2: Docker
Use the below steps if you would like to make code changes and build the image locally. Otherwise, use the image available on Docker Hub,
vshadbolt/stable-diffusion-telegram-bot:latest, in the compose file.
To get started with Docker, ensure you have Docker Desktop installed or otherwise have Docker running on your machine prior to following the steps below:
git clone and navigate to the directory.docker build -t stable-diffusion-telegram-bot . to build the Docker image.docker-compose.yml file environment variables.docker compose up.The Telegram bot should now be running and accessible.
If using Docker Portainer, use the following steps instead:
docker build -t stable-diffusion-telegram-bot . to build the Docker image.docker save -o stable-diffusion-telegram-bot.tar stable-diffusion-telegram-bot to save the built docker image to a .tar file in the repository directoryDeploy the stack and the Telegram bot should be running and accessible.
Common Errors
If the bot does not immediately start, ensure the Telegram Token TG_TOKEN is set in the .env or docker compose file. If left as None the bot will not be able to start and will show an error similar to below in the logs:
The token 'None' was rejected by the server.
If the bot is unable to access the stable diffusion API server, it will hang on Dreaming... before eventually sending a message that Stable diffusion is not currently running, come back later.
If the bot hangs after Dreaming..., Dreaming again..., Generating dream variation..., or Upscaling dream..., ensure that the bot is still not actively generating the image or otherwise check for error messages in the API response (can be found in the container logs). Attempting to use a model you do not have installed in Stable Diffusion, using ControlNet without the extension installed, or otherwise having a malformed Environment variable will often lead to API errors.
Content type
Image
Digest
sha256:2183a775f…
Size
52.9 MB
Last updated
over 2 years ago
docker pull vshadbolt/stable-diffusion-telegram-bot