From f8395763e020a2af27b6da1631be3952d93111c0 Mon Sep 17 00:00:00 2001 From: Sabi <120003982+cyanogus@users.noreply.github.com> Date: Fri, 16 Jun 2023 12:08:12 +0530 Subject: [PATCH 1/8] Add custom image output resolution for `/generate_image` command --- cogs/utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cogs/utils.py b/cogs/utils.py index 08f6a60e..1a94eae4 100644 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -150,14 +150,15 @@ async def chatgpt(self, ctx: ApplicationContext, message: str): description="Generate an image of your choice using the DALL-E modal." ) @option(name="prompt", description="What image do you want the bot to generate?", type=str) + @option(name="resolution", description="Set a custom resolution for your generated image", type=str, default="512x512") @commands.cooldown(1, 10, commands.BucketType.user) - async def generate_image(self, ctx: ApplicationContext, prompt: str): + async def generate_image(self, ctx: ApplicationContext, prompt: str, resolution: str = "512x512"): await ctx.defer() try: response = openai.Image.create( prompt=prompt, n=1, - size="512x512" + size=resolution ) generated_image_url = response['data'][0]['url'] except openai.error.RateLimitError: return await ctx.respond("The OpenAI API is currently being rate-limited. Try again after some time.", ephemeral=True) From 15be1abec86d8a6be8c5eb723bf8c19c47bfc0c5 Mon Sep 17 00:00:00 2001 From: Sabi <120003982+cyanogus@users.noreply.github.com> Date: Fri, 16 Jun 2023 12:26:52 +0530 Subject: [PATCH 2/8] Add checking for malformed custom resolution format The command will return a frontend error message if the resolution is too high, too low or malformed. Too high: above 4K UHD Too low: under 1 pixel width or height Malformed: too many resolution parameter inputs (eg: 1920x1080x540), or only one resolution parameter specified --- cogs/utils.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cogs/utils.py b/cogs/utils.py index 1a94eae4..492ff2d8 100644 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -150,9 +150,17 @@ async def chatgpt(self, ctx: ApplicationContext, message: str): description="Generate an image of your choice using the DALL-E modal." ) @option(name="prompt", description="What image do you want the bot to generate?", type=str) - @option(name="resolution", description="Set a custom resolution for your generated image", type=str, default="512x512") + @option(name="resolution", description="Set a custom resolution for your generated image (format: {width}x{height})", type=str, default="512x512") @commands.cooldown(1, 10, commands.BucketType.user) async def generate_image(self, ctx: ApplicationContext, prompt: str, resolution: str = "512x512"): + parsed_resolution: list = resolution.split("x") + max_index: int = 0 + for index in parsed_resolution: max_index += 1 + if max_index < 2 or max_index > 2: return await ctx.respond("Your resolution format is malformed. Please check it and try again.", ephemeral=True) + res_width = parsed_resolution[0] + res_height = parsed_resolution[1] + if res_width < 1 or res_height < 1: return await ctx.respond("Your resolution's width and height values need to be at least 1 pixel or higher.", ephermeral=True) + if res_width > 3840 or res_height > 2160: return await ctx.respond("Your image output resolution cannot be higher than 4K UHD. (3840 × 2160)", ephmeral=True) await ctx.defer() try: response = openai.Image.create( From a10a4344c9ec715cfdf62b4afb25716e40954c92 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Fri, 16 Jun 2023 22:50:00 +0530 Subject: [PATCH 3/8] Update agruments list for `/generate_image` --- config/commands.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/commands.json b/config/commands.json index 5921f02c..7bbbc224 100644 --- a/config/commands.json +++ b/config/commands.json @@ -694,7 +694,7 @@ "description": "Generate an image of your choice using the DALL-E modal.", "type": "general utilities", "cooldown": 10, - "args": ["prompt"], + "args": ["prompt", "resolution (optional)"], "usable_by": "everyone", "disabled": false, "bugged": false From b09935c7301793e6b1275c4c97a26f35f7639fb1 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Fri, 16 Jun 2023 23:05:33 +0530 Subject: [PATCH 4/8] Add fixed resolution options instead of custom resolutions Turns out DALL-E only supports 3 resolutions (256p, 512p, 1024p) and nothing else. Kind of strange to be honest. --- cogs/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/utils.py b/cogs/utils.py index 492ff2d8..2739175c 100644 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -150,7 +150,7 @@ async def chatgpt(self, ctx: ApplicationContext, message: str): description="Generate an image of your choice using the DALL-E modal." ) @option(name="prompt", description="What image do you want the bot to generate?", type=str) - @option(name="resolution", description="Set a custom resolution for your generated image (format: {width}x{height})", type=str, default="512x512") + @option(name="resolution", description="Set a custom resolution for your generated image", type=str, default="512x512", choices=["256x256", "512x512", "1024x1024"]) @commands.cooldown(1, 10, commands.BucketType.user) async def generate_image(self, ctx: ApplicationContext, prompt: str, resolution: str = "512x512"): parsed_resolution: list = resolution.split("x") From 3473c3e790884e6c61203f02a94c307f26265be9 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Fri, 16 Jun 2023 23:07:22 +0530 Subject: [PATCH 5/8] Update minimum and maximum value checking --- cogs/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cogs/utils.py b/cogs/utils.py index 2739175c..be246914 100644 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -159,8 +159,8 @@ async def generate_image(self, ctx: ApplicationContext, prompt: str, resolution: if max_index < 2 or max_index > 2: return await ctx.respond("Your resolution format is malformed. Please check it and try again.", ephemeral=True) res_width = parsed_resolution[0] res_height = parsed_resolution[1] - if res_width < 1 or res_height < 1: return await ctx.respond("Your resolution's width and height values need to be at least 1 pixel or higher.", ephermeral=True) - if res_width > 3840 or res_height > 2160: return await ctx.respond("Your image output resolution cannot be higher than 4K UHD. (3840 × 2160)", ephmeral=True) + if res_width < 256 or res_height < 256: return await ctx.respond("Your custom resolution needs to be at least 256p or higher.", ephermeral=True) + if res_width > 1024 or res_height > 1024: return await ctx.respond("Your image output resolution cannot be higher than 1024p.", ephmeral=True) await ctx.defer() try: response = openai.Image.create( From 76d7ffbcb2d30be20c2001d64866335ccd04bf75 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Fri, 16 Jun 2023 23:07:38 +0530 Subject: [PATCH 6/8] Fix a small typo --- cogs/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cogs/utils.py b/cogs/utils.py index be246914..57707312 100644 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -160,7 +160,7 @@ async def generate_image(self, ctx: ApplicationContext, prompt: str, resolution: res_width = parsed_resolution[0] res_height = parsed_resolution[1] if res_width < 256 or res_height < 256: return await ctx.respond("Your custom resolution needs to be at least 256p or higher.", ephermeral=True) - if res_width > 1024 or res_height > 1024: return await ctx.respond("Your image output resolution cannot be higher than 1024p.", ephmeral=True) + if res_width > 1024 or res_height > 1024: return await ctx.respond("Your image output resolution cannot be higher than 1024p.", ephemeral=True) await ctx.defer() try: response = openai.Image.create( From 4482832dbbd76c39a9e38fa8742f3f053cefbcb8 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Fri, 16 Jun 2023 23:08:42 +0530 Subject: [PATCH 7/8] Force `res_width` and `res_height` vars to `int` --- cogs/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cogs/utils.py b/cogs/utils.py index 57707312..ef2750dd 100644 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -157,8 +157,8 @@ async def generate_image(self, ctx: ApplicationContext, prompt: str, resolution: max_index: int = 0 for index in parsed_resolution: max_index += 1 if max_index < 2 or max_index > 2: return await ctx.respond("Your resolution format is malformed. Please check it and try again.", ephemeral=True) - res_width = parsed_resolution[0] - res_height = parsed_resolution[1] + res_width = int(parsed_resolution[0]) + res_height = int(parsed_resolution[1]) if res_width < 256 or res_height < 256: return await ctx.respond("Your custom resolution needs to be at least 256p or higher.", ephermeral=True) if res_width > 1024 or res_height > 1024: return await ctx.respond("Your image output resolution cannot be higher than 1024p.", ephemeral=True) await ctx.defer() From 57618ae8d58b964997ee5dbd087adee201b40714 Mon Sep 17 00:00:00 2001 From: snipe_blaze <72265661+notsniped@users.noreply.github.com> Date: Fri, 16 Jun 2023 23:10:12 +0530 Subject: [PATCH 8/8] Fix name references in exception handling messages --- cogs/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cogs/utils.py b/cogs/utils.py index ef2750dd..5cfa9c87 100644 --- a/cogs/utils.py +++ b/cogs/utils.py @@ -170,8 +170,8 @@ async def generate_image(self, ctx: ApplicationContext, prompt: str, resolution: ) generated_image_url = response['data'][0]['url'] except openai.error.RateLimitError: return await ctx.respond("The OpenAI API is currently being rate-limited. Try again after some time.", ephemeral=True) - except openai.error.ServiceUnavailableError: return await ctx.respond("The ChatGPT service is currently unavailable.\nTry again after some time, or check it's status at https://status.openai.com", ephemeral=True) - except openai.error.APIError: return await ctx.respond("ChatGPT encountered an internal error. Please try again.", ephemeral=True) + except openai.error.ServiceUnavailableError: return await ctx.respond("The OpenAI service is currently unavailable.\nTry again after some time, or check it's status at https://status.openai.com", ephemeral=True) + except openai.error.APIError: return await ctx.respond("DALL-E encountered an internal error. Please try again.", ephemeral=True) except openai.error.Timeout: return await ctx.respond("Your request timed out. Please try again, or wait for a while.", ephemeral=True) localembed = discord.Embed(title="Here's an image generated using your prompt.", color=discord.Color.random()) localembed.set_image(url=generated_image_url)