From d510da4a5d681c4960efbef4deed1ba9d160f53d Mon Sep 17 00:00:00 2001 From: Luke Robles <98352913+lrobles-iterable@users.noreply.github.com> Date: Tue, 4 Oct 2022 15:44:15 -0700 Subject: [PATCH] Holy shit, we did it, adding stable diffusion to dale bot. Depends on my windows computer being online and running it though --- app/animals.py | 0 app/cogs/actual_utils.py | 24 ++++++++++++++ app/help_methods.py | 0 app/nft.py | 0 app/questions.py | 0 app/stable_diffusion.py | 68 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 92 insertions(+) mode change 100644 => 100755 app/animals.py mode change 100644 => 100755 app/help_methods.py mode change 100644 => 100755 app/nft.py mode change 100644 => 100755 app/questions.py create mode 100755 app/stable_diffusion.py diff --git a/app/animals.py b/app/animals.py old mode 100644 new mode 100755 diff --git a/app/cogs/actual_utils.py b/app/cogs/actual_utils.py index f36388e9..bfddb34a 100644 --- a/app/cogs/actual_utils.py +++ b/app/cogs/actual_utils.py @@ -66,6 +66,30 @@ class ActualUtils(commands.Cog): questions.open_ai(query), ) + @commands.command(name="sd") + async def sd(self, ctx: commands.Context, *, prompt): + if ctx.message.author.id != 144986109804412928: + await ctx.send("Sorry, this is a paid dale-bot feature") + return + + import stable_diffusion + + try: + ip = "192.168.1.188" + port = "7860" + file_path = stable_diffusion.generate_image(ip=ip, port=port, prompt=prompt) + await ctx.reply( + file=discord.File( + file_path, + filename="unknown.png", + ) + ) + os.remove(file_path) + except Exception as e: + await ctx.reply( + "Stable diffusion isnt running right now, sorry.\n%s" % e, + ) + @commands.command(name="trackdays") async def trackdays(self, ctx: commands.Context): role = discord.utils.find( diff --git a/app/help_methods.py b/app/help_methods.py old mode 100644 new mode 100755 diff --git a/app/nft.py b/app/nft.py old mode 100644 new mode 100755 diff --git a/app/questions.py b/app/questions.py old mode 100644 new mode 100755 diff --git a/app/stable_diffusion.py b/app/stable_diffusion.py new file mode 100755 index 00000000..b514c0a4 --- /dev/null +++ b/app/stable_diffusion.py @@ -0,0 +1,68 @@ +import base64 +import json +import requests +import socket +import tempfile + + +def generate_image(ip, port, prompt): + url = "http://" + ip + ":" + port + "/api/predict/" + headers = { + "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:105.0) Gecko/20100101 Firefox/105.0", + "Accept": "*/*", + "Accept-Language": "en-US,en;q=0.5", + "DNT": "1", + "Connection": "keep-alive", + } + + json_data = { + "fn_index": 11, + "data": [ + prompt, + "", + "None", + "None", + 20, + "Euler a", + False, + False, + 1, + 1, + 7, + -1, + -1, + 0, + 0, + 0, + False, + 512, + 512, + False, + False, + 0.7, + "None", + False, + False, + None, + "", + "Seed", + "", + "Steps", + "", + True, + False, + None, + "", + "", + ], + } + + response = requests.post(url, headers=headers, json=json_data) + imageb64 = response.json()["data"][0][0].split("data:image/png;base64,")[1] + + imgdata = base64.b64decode(imageb64) + file, file_path = tempfile.mkstemp() + + with open(file_path, "wb") as f: + f.write(imgdata) + return file_path