GithubHelp home page GithubHelp logo

andbobsyouruncle / stable-diffusion-discord-bot Goto Github PK

View Code? Open in Web Editor NEW
129.0 6.0 35.0 72 KB

A Discord bot, written in Go, that interfaces with the Automatic 1111's API interface.

License: MIT License

Go 100.00%

stable-diffusion-discord-bot's Introduction

Stable Diffusion Discord Bot

This is a Discord bot that interfaces with the Automatic1111 API, from this project: https://github.com/AUTOMATIC1111/stable-diffusion-webui

Video showing off the current features: https://www.youtube.com/watch?v=of5MBh3ueMk

Installation

  1. Download the appropriate version for your system from the releases page: https://github.com/AndBobsYourUncle/stable-diffusion-discord-bot/releases
    1. Windows users will need to use the windows-amd64 version
    2. Intel Macs will need to use the darwin-amd64 version
    3. M1 Macs will need to use the darwin-arm64 version
    4. Devices like a Raspberry Pi will need to use the linux-arm64 version
    5. Most other Linux devices will need to use the linux-amd64 version
  2. Extract the archive folder to a location of your choice

Building (optional, only if you want to build from source)

  1. Clone this repository
  2. Install Go
    • This varies with your operating system, but the easiest way is to use the official installer: https://golang.org/dl/
  3. Build the bot with go build

Usage

  1. Create a Discord bot and get the token
  2. Add the Discord bot to your Discord server. It needs permissions to post messages, use slash commands, mentioning anyone, and uploading files.
  3. Ensure that the Automatic 1111 webui is running with --api (and also --listen if it is running on a different computer than the bot).
  4. Run the bot with ./stable_diffusion_bot -token <token> -guild <guild ID> -host <webui host, e.g. http://127.0.0.1:7860>
    • It's important that the -host parameter matches the IP address where the A1111 is running. If the bot is on the same computer, 127.0.0.1 will work.
    • There needs to be no trailing slash after the port number (which is 7860 in this example). So, instead of http://127.0.0.1:7860/, it should be http://127.0.0.1:7860.
  5. The first run will generate a new SQLite DB file in the current working directory.

The -imagine <new command name> flag can be used to have the bot use a different command when running, so that it doesn't collide with a Midjourney bot running on the same Discord server.

Commands

/imagine_settings

Responds with a message that has buttons to allow updating of the default settings for the /imagine command.

By default, the size is 512x512. However, if you are running the Stable Diffusion 2.0 768 model, you might want to change this to 768x768.

Choosing an option will cause the bot to update the setting, and edit the message in place, allowing further edits.

Screenshot 2023-01-06 at 10 41 36 AM

/imagine

Creates an image from a text prompt. (e.g. /imagine cute kitten riding a skateboard)

Available options:

  • Aspect Ratio
    • --ar <width>:<height> (e.g. /imagine cute kitten riding a skateboard --ar 16:9)
    • Uses the default width or height, and calculates the final value for the other based on the aspect ratio. It then rounds that value up to the nearest multiple of 8, to match the expectations of the underlying neural model and SD API.
    • Under the hood, it will use the "Hires fix" option in the API, which will generate an image with the bot's default width/height, and then resize it to the desired aspect ratio.

How it Works

The bot implements a FIFO queue (first in, first out). When a user issues the /imagine command (or uses an interaction button), they are added to the end of the queue.

The bot then checks the queue every second. If the queue is not empty, and there is nothing currently being processed, it will send the top interaction to the Automatic1111 WebUI API, and then remove it from the queue.

After the Automatic1111 has finished processing the interaction, the bot will then update the reply message with the finished result.

Buttons are added to the Discord response message for interactions like re-roll, variations, and up-scaling.

All image generations are saved into a local SQLite database, so that the parameters of the image can be retrieved later for variations or up-scaling.

Screenshot 2022-12-22 at 4 25 03 PM

Screenshot 2022-12-22 at 4 25 18 PM

Options like aspect ratio are extracted and sanitized from the text prompt, and then the resulting options are stored in the database record for the image generation (for further variations or upscaling):

Screenshot 2022-12-28 at 4 30 43 PM

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

There are lots more features that could be added to this bot, such as:

  • Moving defaults to the database
  • Per-user defaults/settings, as well as enforcing limits on a user's usage of the bot
  • Ability to easily re-roll an image
  • Generating multiple images at once
  • Ability to upscale the resulting images
  • Ability to generate variations on a grid image
  • Ability to tweak more settings when issuing the /imagine command (like aspect ratio)
  • Image to image processing

I'll probably be adding a few of these over time, but any contributions are also welcome.

Why Go?

I like Go a lot better than Python, and for me it's a lot easier to maintain dependencies with Go modules versus running a bunch of different Anaconda environments.

It's also able to be cross-compiled to a wide range of platforms, which is nice.

stable-diffusion-discord-bot's People

Contributors

andbobsyouruncle avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

stable-diffusion-discord-bot's Issues

can't generate anything

Bot connects, I can change the set size just fine, but when I try to /imagine it ends up spitting this out:

./2023/02/18 23:30:08 Processing imagine #1076752530656198707: the saddest shit you've ever seen
2023/02/18 23:30:09 API URL: http://localhost:7860//sdapi/v1/txt2img
2023/02/18 23:30:09 Unexpected API response: {"detail":"Not Found"}
2023/02/18 23:30:09 Error processing image: unexpected end of JSON input

Bot overrides NSFW Settings of Automatic1111 SD

Hi there,
unfortunately the sd bot creates nsfw content which is causing issues for us.
Any way to enable nsfw via the sd bot and let only admins change it?

Would be highly appreciated
Thanks and regards

More comprehensive install instructions

Would love to have more comprehensive install instructions for ignorant noobs such as myself. I struggled with step 2 in the usage instructions, never having made a discord bot before it took a bit to figure out how to even invite it to my server. Eventually figured out I had to make my own URL in a notepad to make it work. But now on the step 4.... how do we: "Run the bot with ./stable_diffusion_bot -token -guild -host <webui host, e.g. http://127.0.0.1:7860>"

Where do I type this line? What is guild ID? Is it a necessary parameter or can we skip it if we want it to work with any server? Do we enter the token with the or without?

Minimal Discord bot premissions required

Hello,
The bot app works great! Thanks for making it. Currently I have it working on my private Discord server where I have given the bot "Admin" privileges. However, I would like to add it to a friend's Discord server where the permissions should be more minimalistic. Are these the appropriate permissions to use for the bot? (I based this off of the Midjourney bot requirements):

My planned SD_bot credentials (minimalistic):
SD bot credentials

Midjourney bot credentials for reference:

Midjourney Bot credentials

Imagine interations through Discord leads to "Error processing image: unexpected end of JSON input"

Apologies as this is my first time in Github.

I've built both the Stable Diffusion instance and the discord bot and the bot is able to detect the host.

Unfortunately when it comes to actually generating through the bot using /imagine, the console outputs

2022/12/25 02:29:30 Processing imagine #1056277455129825370: apple
2022/12/25 02:29:31 Error processing image: unexpected end of JSON input

I assume that the bot is able to detect the host but cannot seem to communicate with it.

Would appreciate help in troubleshooting

Feature Request - Ability to set a style

In the API there is a way to set a "style" to use:

http://127.0.0.1:7860/docs#/default/text2imgapi_sdapi_v1_txt2img_post

"styles": [
    "string"
  ],

Where "string" is the name of the saved style in A1111. Styles are essentially just saved positive/negative prompts stored in a styles.csv in the root of A1111 folder. In the API it should be useful to add positive/negative prompts to a users prompt that they don't see (This is somewhat the magic behind MidJourney's bot). Styles solve this problem. Styles can also insert the users prompt into itself with a {prompt} in the style. This is added not as part of the "prompt" field for the API so the user should never see it, it's kind of like a backend style wrapper for prompts. If I concatenate extra positive prompts to the "prompt" in your backend files it will always be returned to the user and they will see the added positive prompts. Styles are a way to do this without the user ever seeing it but it's still part of the prompt when actually submitted to be rendered.

I think you're working on some way to set variables like sampler/cfg in a file users can configure or similar. Would be great to be able to set a style there. There are 2 styles and why it's an array, but I'd be happy with the ability to set one.

Documentation change and feature request

During the setup, I had difficulty with what GCC to install. I installed a 32-bit version first by accident. Also, I think I had to add the %PATH% environment variable by hand for GO and GCC (maybe not needed if the default location is used). The changes you made to the installation section in the readme should help a little bit but be thinking maybe there could be more specific instructions so more basic users don't get caught up with the same hangups I did and don't bug you for the small stuff. For example, the link you provided to Sourceforge is just the page for that package and not a link to the 64bit package that is needed.

Maybe something more like this for less advanced windows users?

Windows Installation

Clone this repository

Install GIT for windows (this should already be installed if you are installing on the same windows machine as your Automatic1111 install) https://git-scm.com/download/win

Create or traverse to a folder you want the installation directory to be cloned to in windows explorer. In this example, I'm using a folder in the root of my local "C" drive "C:\tools". I open that folder in windows explorer and right-click in the empty space and choose "Git Bash here" from the contextual menu, this opens a git command window already in the directory.

Clone the repository using this git command in the console window you just opened:

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

*Note: from now on if you want to update the code you can do the same process to open a git command window and use the git command:
git pull

The source is in Go and will need to be compiled into an EXE using Go and GCC

Install Go

https://go.dev/doc/install

By default, the installer will install Go to Program Files or Program Files (x86). You can change the location as needed. After installing, you will need to close and reopen any open command prompts so that changes to the environment made by the installer are reflected in the command prompt.

*I've noticed that if you install Go to an alternate location it sets the %PATH% environment variable to be the programs file folder not the alternate folder. You can change it by going to "settings" in the start menu then into "System" then under "about" (very bottom) there is a link under "related Settings" called "Advanced System Settings". This will open the System Control Panel already on the tab with the Environment Variables editor button. Click it to edit the list. The top entries are for the "User" and the bottom are for the "System". You may need to edit both locations. The Variable for go is not in "Path" and is it's own entry called "GOPATH" these should be pointing to the go directory. In my example, I installed go to C:\tools\go instead of the default program files folder and had to change the GOPATH variable manually with that path.

You will need 64bit GCC installed to compile the EXE

https://sourceforge.net/projects/mingw-w64/files/mingw-w64/mingw-w64-release/mingw-w64-v10.0.0.zip/download

You can check if it was installed properly with

GCC --version

It should return something like this if installed correctly:

gcc.exe (GCC) 10.4.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

*The installer should add the location of GCC to your "Path" environmental variables but may not if you choose a non-default location, you can check there if it can't find GCC after installation. Also, don't forget to restart any command windows you had open before installation before using.

Build the bot with go build

Make sure you're in the project directory and build the bot with go build:

go build

If it's successful you will get no errors and return to a ready state. You will now have a stable_diffusion_bot.exe in your directory and are ready to move on to "usage".

*If you make any changes to files or do a "git pull" and the code is updated then you will need to re-build the EXE to gain those new code changes.


Ok I had mentioned this feature request in another thread but didn't want it to get lost. I was wondering if we can add "style" to the queue.go under newGeneration


styles: [
    "string"
],

Save Image

Can you add the ability to save images?

Can I just generate one image instead of four?

Basically what the title sets

Its kinda weird how you can only generate four images at once, I dont want the bot to be working on one request for so long I just want it to go make one image real quick and free up the queue, but I see no way to do it?

Unexpected API response

I'm getting this error

2023/04/17 19:15:12 Processing imagine #1097601169771200582: test
2023/04/17 19:15:12 API URL: http://127.0.0.1:8000/sdapi/v1/txt2img
2023/04/17 19:15:12 Unexpected API response: {"error":"RuntimeError","detail":"","body":"","errors":"There is no current event loop in thread 'AnyIO worker thread'."}
2023/04/17 19:15:12 Error processing image: unexpected end of JSON input

Feature request: allow changing of command

Want to be able to use this on the same server as the midjourney bot, and they both use the same discord command (/imagine). Not a big deal as you can choose which one you want to use with a click, but annoying to users. I can do a PR for it myself if that's what you want, just wanted to make sure you didn't have plans. Maybe make it a command line option?

If there's another way to make this happen directly in discord, I'm just not aware of it.

What is the trick to making '/imagine_settings' work?

The bot is working great otherwise for me, /imagine works and the buttons to interact with the generated images all work, but I get a 'Application did not respond' error when I try to use the /imagine_settings command, and the below coreseponding error is displayed in the terminal output.

Any feedback or suggestions are welcome, but what little I have been able to find of 'HTTP 400 Bad Request' and "code": 50035, "errors" seem to indicate a piece of code being sent to discord api improperly or in such a way that it does not know how to respond such as a capital instead of lowercase, or an improperly formed attribute name.

Of course it is also possible I have not entered a URL correctly somewhere or a permission improperly set.

Error in output:

2024/06/06 22:30:55 Error responding to interaction: HTTP 400 Bad Request, {"message": "Invalid Form Body", "code": 50035, "errors": {"data": {"components": {"0": {"components": {"0": {"options": {"0": {"emoji": {"name": {"_errors": [{"code": "BUTTON_COMPONENT_INVALID_EMOJI", "message": "Invalid emoji"}]}}}, "1": {"emoji": {"name": {"_errors": [{"code": "BUTTON_COMPONENT_INVALID_EMOJI", "message": "Invalid emoji"}]}}}}}}}}}}} 2024/06/07 09:34:42 Error responding to interaction: HTTP 400 Bad Request, {"message": "Invalid Form Body", "code": 50035, "errors": {"data": {"components": {"0": {"components": {"0": {"options": {"0": {"emoji": {"name": {"_errors": [{"code": "BUTTON_COMPONENT_INVALID_EMOJI", "message": "Invalid emoji"}]}}}, "1": {"emoji": {"name": {"_errors": [{"code": "BUTTON_COMPONENT_INVALID_EMOJI", "message": "Invalid emoji"}]}}}}}}}}}}} 2024/06/07 09:41:12 Error responding to interaction: HTTP 400 Bad Request, {"message": "Invalid Form Body", "code": 50035, "errors": {"data": {"components": {"0": {"components": {"0": {"options": {"0": {"emoji": {"name": {"_errors": [{"code": "BUTTON_COMPONENT_INVALID_EMOJI", "message": "Invalid emoji"}]}}}, "1": {"emoji": {"name": {"_errors": [{"code": "BUTTON_COMPONENT_INVALID_EMOJI", "message": "Invalid emoji"}]}}}}}}}}}}}

System crashes when generating an image

Not sure where I could look for a bug report, but when I try to generate an image with this bot. My system completely locks up and reboots. I am able to run the webui just fine though???

Nothing happens when changing the upscaler

First of all good morning, good afternoon or good evening!

I have a problem when changing the upscale in "queue.go" in Upscaler1: "ESRGAN_4x".
I already tried to change to all the other names of upscalers that I have here, but it doesn't seem to have any effect. It seems that even with "ESRGAN_4x" the same does the upscale with the "none" option.
How could I fix this!? Sorry for the lay question, but I just downloaded the code and edited it to add more generation size options and ran into this little problem in Upscaler.
I will also attach an example trying to use the "4x_fatal_Anime_500000_G" upscaler template, one generated by the site locally and another generated by the bot on discord.
sublime_text 9zyZXQrnk6
brave Z5Pk2iXYv2
example

The bot crashes if anyone tries to dm it

This is the error i get.

If anyone could help me i would be so thankful!

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x50 pc=0x81b7ec]

goroutine 83 [running]:
stable_diffusion_bot/discord_bot.(*botImpl).processImagineCommand(0xc0001de7e0, 0x20?, 0xc00000a098)
C:/stable/stable-diffusion-discord-bot-master/stable-diffusion-discord-bot-master/discord_bot/discord_bot.go:331 +0x40c
stable_diffusion_bot/discord_bot.New.func2(0x0?, 0xc00000a098)
C:/stable/stable-diffusion-discord-bot-master/stable-diffusion-discord-bot-master/discord_bot/discord_bot.go:99 +0x430
github.com/bwmarrin/discordgo.interactionCreateEventHandler.Handle(0x7fc34e?, 0xc000222380?, {0x876c40?, 0xc00000a098?}) /github.com/bwmarrin/[email protected]/eventhandlers.go:693 +0x38
created by github.com/bwmarrin/discordgo.(*Session).handle
/github.com/bwmarrin/[email protected]/event.go:171 +0x16a

change batch count of 4 to batch size of 4 instead

Right now the bot creates 4 images one by one as "Batch Count" is set to 4. If you change it to be the "Batch Size" set to 4 instead (and batch count back to 1) Automatic1111 will make all 4 images at once. instead of one by one. This should be a faster way to get the 4 images. Don't know if that throws a wrench in any logic but I did some tests on my system and to generate 4 768x768 it takes 45 seconds using "batch count" but using "batch size" it only takes 40 seconds (Euler a/20 steps, cfg 8, note that 512x512's speed discrepancy is smaller and only 2-3 seconds)

Editing error and Guild ID

2023/07/22 20:27:22 Error editing interaction:
2023/07/22 20:27:23 Error editing interaction:
2023/07/22 20:27:24 Error editing interaction:
2023/07/22 20:27:25 Error editing interaction:
2023/07/22 20:27:26 Error editing interaction:
2023/07/22 20:27:28 Seeds: [1540054415 1540054416 1540054417 1540054418] Subseeds:[3461405818 3461405819 3461405820 3461405821]
2023/07/22 20:27:30 Error editing interaction: HTTP 401 Unauthorized, {"message": "Invalid Webhook Token", "code": 50027}
2023/07/22 20:27:30 Error processing imagine grid: HTTP 401 Unauthorized, {"message": "Invalid Webhook Token", "code": 50027}

I mean its change first, but after some moments have that error editing, also in -guild its not so clear of what I should put

program closes immediately

I ran the command in webui-user.bat
./stable_diffusion_bot -token <MTA5OTcyNDczMDIwMzg5ODAxNg.Gv9MlW.SK00000> -guild <1099726877893075100> --host <webui http://127.08.0.1 sdp -attention --api --listen

also i put stable_diffusion_bot.exe in stable-diffusion-webui folder and copied to /scripts

PS G:\stable-diffusion-webui> ./stable_diffusion_bot -token <MTA5OTcyNDczMDIwMzg5ODAxNg.Gv9MlW.SK00000> -guild <1099726877893075100> -host <webui host, e.g. http://127.0.0.1:7860>

where to enter the command to make it work?

A couple feature requests (Discord status showing model used, and show seeds)

Would love to see:

  • The discord bot status showing the model that is implemented. Like "Playing DreamshaperXL-1.0" or something
  • And easier way to see the seed (and other parameters) of the images generated. Maybe a new button in discord that outputs parameter info? Right now, the generation information isn't even embedded in the output PNG like normal A1111 batch images.
  • If rerolling, or building variations from, or upscaling, include a discord comment link to the text being rerolled/varied/upscaled. So instead of "@user asked me to imagine (prompt)", the text would change depending on context, "@user Re-rolled (discord comment link of previous generation) for a (prompt)" or "@user upscaled (discord comment link of previous gen) for a (prompt)"
  • Allow the user who requested the generation to have a delete button on their own generations, so that we can make multiple generations without overclogging channels. Other users either don't see a delete button, or it doesn't do anything if pressed by anyone else.
  • Addition of 1024 x 1024 resolution.

I'm aware that OP (@AndBobsYourUncle) has sort have abandoned this project, and that the fork with the best feature set is this one:
https://github.com/shtrih/stable-diffusion-discord-bot
Which gives control of: (negative prompt, aspect ratio, restore faces switch, CFG scale, seed)

So this is more of a feature request list for myself, @shtrih, and anyone else that plans to contribute to this bot.

Upscale option doesnt work

Noticed that the output of the upscale using the bot just gives out the original picture. So no change from 512 to 1024 etc.
Tried both the EXE and tested building from clone. Same issue on both.

Issue with the Windows Installation

Good day, I'm more of a Python user never used GO but I read your readme where it you have 1. Windows users will need to use the windows-amd64 version

I did extract it and added it to the desktop but it is not working it will open the terminal for a few seconds and immediately close off. Is this and issue with the exe itself or just on my end because the latest current exe for windows does not work well for me anyway.

Unexpected end of json

I have the webui-user.bat running, and I am able to make images in the web ui
I can run the discord bot, but when using /imagine prompt it fails with

2022/12/28 01:00:41 Processing imagine #1057583858859642880: dogs
2022/12/28 01:00:41 Error processing image: unexpected end of JSON input

Install Guide

Hello!
Would be nice if there were more detailed guide or video how to as some are not aware of Go .
I am stuck here I think so far right.

Screenshot_1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.