From 95daf8164211eb55d4389ff6e127d50282f636e5 Mon Sep 17 00:00:00 2001 From: "Aryan Donga (MCUxDaredevil)" Date: Thu, 18 May 2023 19:00:38 +0530 Subject: [PATCH 1/4] update gitignote --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6f501321..8dce2325 100644 --- a/.gitignore +++ b/.gitignore @@ -163,4 +163,5 @@ cython_debug/ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. -#.idea/ +.idea/ +Pipfile* \ No newline at end of file From 00ef8b41f2139f4e603c78e6838601d81605d070 Mon Sep 17 00:00:00 2001 From: "Aryan Donga (MCUxDaredevil)" Date: Thu, 18 May 2023 19:21:49 +0530 Subject: [PATCH 2/4] add image amount to draw --- src/art.py | 4 ++-- src/bot.py | 16 ++++++++++++++-- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/art.py b/src/art.py index e12cd8c7..43c731cb 100644 --- a/src/art.py +++ b/src/art.py @@ -12,13 +12,13 @@ # generate 512x512 image and save to a file # return the path of the image as a str -async def draw(prompt) -> str: +async def draw(prompt, amount) -> str: DATA_DIR = Path.cwd() DATA_DIR.mkdir(exist_ok=True) response = await sync_to_async(openai.Image.create)( prompt=prompt, - n=1, + n=amount, size="512x512", response_format="b64_json", ) diff --git a/src/bot.py b/src/bot.py index 768ec30c..e359f030 100644 --- a/src/bot.py +++ b/src/bot.py @@ -197,7 +197,19 @@ async def info(interaction: discord.Interaction): """) @client.tree.command(name="draw", description="Generate an image with the Dalle2 model") - async def draw(interaction: discord.Interaction, *, prompt: str): + @app_commands.choices(amount=[ + app_commands.Choice(name="1", value=1), + app_commands.Choice(name="2", value=2), + app_commands.Choice(name="3", value=3), + app_commands.Choice(name="4", value=4), + app_commands.Choice(name="5", value=5), + app_commands.Choice(name="6", value=6), + app_commands.Choice(name="7", value=7), + app_commands.Choice(name="8", value=8), + app_commands.Choice(name="9", value=9), + app_commands.Choice(name="10", value=10), + ]) + async def draw(interaction: discord.Interaction, *, prompt: str, amount: int = 1): if interaction.user == client.user: return @@ -208,7 +220,7 @@ async def draw(interaction: discord.Interaction, *, prompt: str): await interaction.response.defer(thinking=True, ephemeral=client.isPrivate) try: - path = await art.draw(prompt) + path = await art.draw(prompt, amount) file = discord.File(path, filename="image.png") title = f'> **{prompt}** - <@{str(interaction.user.mention)}' + '> \n\n' From 0b960258d6075114e1bc9b253b43eb2a99644102 Mon Sep 17 00:00:00 2001 From: "Aryan Donga (MCUxDaredevil)" Date: Thu, 18 May 2023 20:08:40 +0530 Subject: [PATCH 3/4] add multiple image generation to draw --- src/art.py | 19 ++++++++++++------- src/bot.py | 11 +++++------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/art.py b/src/art.py index 43c731cb..220e73c0 100644 --- a/src/art.py +++ b/src/art.py @@ -1,5 +1,7 @@ import os import json +from typing import List + import openai from pathlib import Path from base64 import b64decode @@ -12,16 +14,18 @@ # generate 512x512 image and save to a file # return the path of the image as a str -async def draw(prompt, amount) -> str: +async def draw(prompt, amount) -> list[str]: DATA_DIR = Path.cwd() DATA_DIR.mkdir(exist_ok=True) response = await sync_to_async(openai.Image.create)( prompt=prompt, n=amount, - size="512x512", + size="1024x1024", response_format="b64_json", ) + with open("response.log", mode="w", encoding="utf-8") as file: + json.dump(response, file) file_name = DATA_DIR / f"{prompt[:5]}-{response['created']}.json" @@ -29,8 +33,8 @@ async def draw(prompt, amount) -> str: json.dump(response, file) path = await convert(file_name) - - return str(path) + path = [str(p) for p in path] + return path # code stolen from https://realpython.com/generate-images-with-dalle-openai-api/ async def convert(path): @@ -41,15 +45,16 @@ async def convert(path): with open(JSON_FILE, mode="r", encoding="utf-8") as file: response = json.load(file) - + image_files = [] for index, image_dict in enumerate(response["data"]): image_data = b64decode(image_dict["b64_json"]) image_file = IMAGE_DIR / f"{JSON_FILE.stem}-{index}.png" + image_files.append(image_file) with open(image_file, mode="wb") as png: png.write(image_data) # delete uneeded json file - os.remove(path) + os.remove(path) - return image_file + return image_files diff --git a/src/bot.py b/src/bot.py index e359f030..e32c50be 100644 --- a/src/bot.py +++ b/src/bot.py @@ -221,13 +221,12 @@ async def draw(interaction: discord.Interaction, *, prompt: str, amount: int = 1 await interaction.response.defer(thinking=True, ephemeral=client.isPrivate) try: path = await art.draw(prompt, amount) + files = [] + for idx, img in enumerate(path): + files.append(discord.File(img, filename=f"image{idx}.png")) + title = f'> **{prompt}** - {str(interaction.user.name)}#{str(interaction.user.discriminator)} \n\n' - file = discord.File(path, filename="image.png") - title = f'> **{prompt}** - <@{str(interaction.user.mention)}' + '> \n\n' - embed = discord.Embed(title=title) - embed.set_image(url="attachment://image.png") - - await interaction.followup.send(file=file, embed=embed) + await interaction.followup.send(files=files, content=title) except openai.InvalidRequestError: await interaction.followup.send( From e7a46ac94f5ce424d1db6490e50cc83453209fa6 Mon Sep 17 00:00:00 2001 From: Aryan Donga Date: Thu, 18 May 2023 22:17:23 +0530 Subject: [PATCH 4/4] Update bot.py --- src/bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bot.py b/src/bot.py index e32c50be..887936fd 100644 --- a/src/bot.py +++ b/src/bot.py @@ -224,7 +224,7 @@ async def draw(interaction: discord.Interaction, *, prompt: str, amount: int = 1 files = [] for idx, img in enumerate(path): files.append(discord.File(img, filename=f"image{idx}.png")) - title = f'> **{prompt}** - {str(interaction.user.name)}#{str(interaction.user.discriminator)} \n\n' + title = f'> **{prompt}** - {str(interaction.user.mention)} \n\n' await interaction.followup.send(files=files, content=title)