capslock / stable-diffusion-bot

Telegram bot for Stable Diffusion, written in Rust.
https://capslock.github.io/stable-diffusion-bot/
MIT License
14 stars 4 forks source link
comfyui rust stable-diffusion stable-diffusion-webui telegram telegram-bot

stable-diffusion-bot

CI

A Telegram bot written in Rust that can be connected to a Stable Diffusion web UI or ComfyUI backend to generate images.

Usage

Install

Using Cargo

The simplest way to install the bot is to use Cargo, the Rust package manager. If you don't already have it installed, follow the official instructions to install Rust.

Then you can simply install the bot:

cargo install --git https://github.com/capslock/stable-diffusion-bot

or a specific version:

cargo install --git https://github.com/capslock/stable-diffusion-bot --tag v0.1.0
Building from source

If you don't already have Rust installed, see the above section.

Check out the project source:

git clone https://github.com/capslock/stable-diffusion-bot.git

And build the project:

cd stable-diffusion-bot
cargo build

Or cargo build --release for a release build.

Output will be in the target/debug (or target/release) directory:

./target/debug/stable-diffusion-bot --help

After making changes, you can install your custom version using cargo:

cargo install --path .

Using Nix Flakes

If you are using the Nix package manager with flakes enabled, you can invoke the bot directly from the provided flake:

nix run github:capslock/stable-diffusion-bot

Using NixOS with Flakes

If you are running NixOS, you can add the flake to your system flake.nix to use the provided module:

{
  inputs.stable-diffusion-bot.url = "github:capslock/stable-diffusion-bot";

  outputs = {
    self,
    nixpkgs,
    stable-diffusion-bot
  }: {
    nixosConfigurations.<yourhostnamehere> = nixpkgs.lib.nixosSystem {
      system = "x86_64-linux";
      modules = [
        ./configuration.nix
        stable-diffusion-bot.nixosModules.default
      ];
    };
  };
}

Then simply configure and enable the service in your configuration.nix:

services.stableDiffusionBot = {
  enable = true;
  telegramApiKeyFile = "/path/to/your/api/key/file.toml";
  settings = {
    # All other settings, as described below
    allowed_users = [ list of telegram ids ];
    db_path = "/path/to/your/db.sqlite";
    sd_api_url = "http://localhost:7860";
  };
};

Using Docker

If the above options don't suit you, you can use the pre-built docker container. Docker support is minimal right now, but you can quickly get up and running using docker run and specifying configuration through environment variables and CLI arguments:

docker run \
  -e SD_TELEGRAM_API_KEY="your_api_key_here" \
  -v /path/to/config.toml:/config.toml \
  ghcr.io/capslock/stable-diffusion-bot:latest -c /config.toml

:construction: TODO: Expand this section.

Configure

Prerequisites

Setup

Create a config.toml file:

# Replace this with your telegram bot API key
api_key = "your_telegram_bot_api_key"
# Replace this with the telegram IDs of users or chats that you want to allow
allowed_users = [ 123, 456, 789 ]
# Path to where the DB should be stored. If not provided, user settings are not persisted.
db_path = "./db.sqlite"
# URL of the backend to use to generate images.
sd_api_url = "http://localhost:7860"

For ComfyUI, you'll additionally want to set the api_type to "ComfyUI" and specify a workflow to use for text-to-image and image-to-image:

api_type = "ComfyUI"

[comfyui]
txt2img_prompt_file = "path/to/workflow_api.json"
img2img_prompt_file = "path/to/workflow_api.json"

To get a workflow in API format, open your workflow in ComfyUI and check the "Enable Dev mode Options" box in the settings. Then, press the new "Save (API Format)" button below the normal "Save" button.

You can take a look at ComfyUI Examples for some example txt2img and img2img workflows.

Run

stable-diffusion-bot

Alternatively, if you don't want to store your API key in the config file, you can pass it through as an environment variable:

SD_TELEGRAM_API_KEY="your_telegram_bot_api_key" stable-diffusion-bot

CLI Options

$ stable-diffusion-bot --help
Usage: stable-diffusion-bot [OPTIONS]

Options:
  -c, --config <CONFIG>  Path to the configuration file [default: config.toml]
      --log-to-systemd   Output logs directly to systemd
  -h, --help             Print help

Using the bot

txt2img

Send the bot a prompt and it will generate an image using the default generation settings. The reply will also have an inline keyboard, giving you options to:

img2img

Send the bot an image with a caption and it will generate a new image based on that image and the prompt.

Advanced

Configuration

Specifying allowed users

Stable Diffusion Settings

For ComfyUI, you can configure the base workflow to contain the default settings you want to use.

For the Stable Diffusion WebUI, you can set the default txt2img and img2img parameters via the config.toml file:

[txt2img]
steps = 50
cfg_scale = 10
width = 768
height = 768

[img2img]
width = 768
height = 768

See the documentation for Txt2ImgRequest and Img2ImgRequest for all of the available options.

Group Chats

Here's a few tips for configuring the bot for use in a group chat:

Using the sub-crates.

This projects consists of four crates:

All can be used as standalone libraries:

stable-diffusion-bot

Crate Documentation

You can use this to integrate the full bot into another program. This crate does not expose much of an API surface for the bot aside from initial configuration structs.

cargo add --git https://github.com/capslock/stable-diffusion-bot stable-diffusion-bot

stable-diffusion-api

README

Crate Documentation

You can use these simple API bindings to build another application that utilizes the Stable Diffusion web UI API.

cargo add --git https://github.com/capslock/stable-diffusion-bot stable-diffusion-api

comfyui-api

README

Crate Documentation

You can use these simple API bindings to build another application that utilizes the ComfyUI API.

cargo add --git https://github.com/capslock/stable-diffusion-bot comfyui-api

sal-e-api

README

Crate Documentation

You can use these API bindings to build an application that accepts either a Stable Diffusion web UI API or ComfyUI API backend.

cargo add --git https://github.com/capslock/stable-diffusion-bot sal-e-api