From b8dfbe88d0406c84358bbcb9e625d7fa4ef45ca7 Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Wed, 5 Oct 2022 17:53:53 -0700 Subject: [PATCH] await stable diffusion now --- app/cogs/actual_utils.py | 26 ++++++++++++++------------ app/requirements.txt | 3 ++- app/stable_diffusion.py | 7 ++++--- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/app/cogs/actual_utils.py b/app/cogs/actual_utils.py index 70accaff..08491c78 100644 --- a/app/cogs/actual_utils.py +++ b/app/cogs/actual_utils.py @@ -84,19 +84,21 @@ class ActualUtils(commands.Cog): except: ip = "192.168.1.80" - try: - 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, + # try: + file_path = await 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): diff --git a/app/requirements.txt b/app/requirements.txt index 8aa13617..96448438 100755 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -9,4 +9,5 @@ git+https://github.com/pycord-development/pycord@8df222d86319dd16a5e559585246343 requests wikipedia wolframalpha -openai \ No newline at end of file +openai +httpx \ No newline at end of file diff --git a/app/stable_diffusion.py b/app/stable_diffusion.py index 1168ddc4..06d8e2d5 100644 --- a/app/stable_diffusion.py +++ b/app/stable_diffusion.py @@ -1,11 +1,12 @@ import base64 import json import requests +import httpx import socket import tempfile -def generate_image(ip, port, prompt): +async 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", @@ -56,8 +57,8 @@ def generate_image(ip, port, prompt): "", ], } - - response = requests.post(url, headers=headers, json=json_data) + client = httpx.AsyncClient() + response = await client.post(url, headers=headers, json=json_data, timeout=60) imageb64 = response.json()["data"][0][0].split("data:image/png;base64,")[1] imgdata = base64.b64decode(imageb64)