From 68a20dcc507cf04e10b682f41bc91479a236a095 Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Thu, 5 Jan 2023 20:18:32 +0000 Subject: [PATCH 1/3] Changed the image resizing to go by the guild filesize limit Since boosted servers have higher limits, even for bots --- cogs/draw_image_generation.py | 2 +- models/openai_model.py | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/cogs/draw_image_generation.py b/cogs/draw_image_generation.py index bb7fb11..7cc80a5 100644 --- a/cogs/draw_image_generation.py +++ b/cogs/draw_image_generation.py @@ -47,7 +47,7 @@ class DrawDallEService(discord.Cog, name="DrawDallEService"): try: file, image_urls = await self.model.send_image_request( - prompt, vary=vary if not draw_from_optimizer else None + ctx, prompt, vary=vary if not draw_from_optimizer else None ) except ValueError as e: ( diff --git a/models/openai_model.py b/models/openai_model.py index 71ffa5a..f5bf94b 100644 --- a/models/openai_model.py +++ b/models/openai_model.py @@ -410,7 +410,7 @@ class Model: return response - async def send_image_request(self, prompt, vary=None) -> tuple[File, list[Any]]: + async def send_image_request(self, ctx, prompt, vary=None) -> tuple[File, list[Any]]: # Validate that all the parameters are in a good state before we send the request words = len(prompt.split(" ")) if words < 3 or words > 75: @@ -533,17 +533,18 @@ class Model: ) # Print the filesize of new_im, in mega bytes - image_size = os.path.getsize(temp_file.name) / 1000000 + image_size = os.path.getsize(temp_file.name) / 1048576 + guild_file_limit = ctx.guild.filesize_limit / 1048576 # If the image size is greater than 8MB, we can't return this to the user, so we will need to downscale the # image and try again safety_counter = 0 - while image_size > 8: + while image_size > guild_file_limit: safety_counter += 1 if safety_counter >= 3: break print( - f"Image size is {image_size}MB, which is too large for discord. Downscaling and trying again" + f"Image size is {image_size}MB, which is too large for this server {guild_file_limit}MB. Downscaling and trying again" ) # We want to do this resizing asynchronously, so that it doesn't block the main thread during the resize. # We can use the asyncio.run_in_executor method to do this From 0835d849fc4c3bbaca7152e34257772531163eb4 Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Thu, 5 Jan 2023 21:41:05 +0000 Subject: [PATCH 2/3] Add fallback if guild is None --- models/openai_model.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/models/openai_model.py b/models/openai_model.py index f5bf94b..5e73654 100644 --- a/models/openai_model.py +++ b/models/openai_model.py @@ -534,7 +534,10 @@ class Model: # Print the filesize of new_im, in mega bytes image_size = os.path.getsize(temp_file.name) / 1048576 - guild_file_limit = ctx.guild.filesize_limit / 1048576 + if ctx.guild is None: + guild_file_limit = 8 + else: + guild_file_limit = ctx.guild.filesize_limit / 1048576 # If the image size is greater than 8MB, we can't return this to the user, so we will need to downscale the # image and try again From e0150c526dd4a99e09ba1fb49178ab08411d6946 Mon Sep 17 00:00:00 2001 From: Rene Teigen Date: Thu, 5 Jan 2023 21:51:51 +0000 Subject: [PATCH 3/3] Fix ctx passthrough with draw button --- cogs/image_prompt_optimizer.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/image_prompt_optimizer.py b/cogs/image_prompt_optimizer.py index 281b663..c136e71 100644 --- a/cogs/image_prompt_optimizer.py +++ b/cogs/image_prompt_optimizer.py @@ -183,7 +183,7 @@ class DrawButton(discord.ui.Button["OptimizeView"]): await self.image_service_cog.encapsulated_send( user_id, prompt, - None, + interaction, msg, True, True,