fixed dalle generation code to be more reusable

This commit is contained in:
phixxy 2024-01-30 02:01:06 -08:00
parent 8f0014bb17
commit 4a64865076

View file

@ -211,70 +211,50 @@ class ChatGPT(commands.Cog):
await f.close() await f.close()
return destination return destination
@commands.command( async def generate_dalle_image(self, ctx, model, quality=None, size=None):
description="Dalle 2", if ctx.author.get_role(self.premium_role):
help="Generate an image with Dalle 2 Usage: !dalle2 (prompt)", prompt = ctx.message.content.split(" ", maxsplit=1)[1]
brief="Generate Image" image_url = await self.get_dalle(prompt, model=model, quality=quality, size=size)
) my_filename = str(time.time_ns()) + ".png"
async def dalle2(self, ctx): image_filepath = f"{self.data_dir}dalle/{my_filename}"
if ctx.author.get_role(self.premium_role): await self.download_image(image_url, image_filepath)
prompt = ctx.message.content.split(" ", maxsplit=1)[1] with open(image_filepath, "rb") as fh:
img_url = await self.get_dalle(prompt) f = discord.File(fh, filename=image_filepath)
my_filename = str(time.time_ns()) + ".png" log_data = f'Author: {ctx.author.name}, Prompt: {prompt}, Filename: {my_filename}\n'
filepath = f"{self.data_dir}dalle2/{my_filename}" with open(f"{self.data_dir}logs/dalle3.log", 'a') as log_filepath:
await self.download_image(img_url, filepath) log_filepath.writelines(log_data)
with open(filepath, "rb") as fh: await ctx.send(f'Generated by: {ctx.author.name}\nPrompt: {prompt}', file=f)
f = discord.File(fh, filename=filepath) else:
log_data = f'Author: {ctx.author.name}, Prompt: {prompt}, Filename: {my_filename}\n' await ctx.send("Sorry you must be a premium member to use this command. (!donate)")
with open(f"{self.data_dir}logs/dalle2.log", 'a') as log_file:
log_file.writelines(log_data)
await ctx.send(f'Generated by: {ctx.author.name}\nPrompt: {prompt}', file=f)
else:
await ctx.send("Sorry you must be a premium member to use this command. (!donate)")
@commands.command(
description="Dalle 3",
help="Generate an image with Dalle 3 Usage: !dalle3 (prompt)",
brief="Generate Image",
aliases = ['dalle']
)
async def dalle3(self, ctx):
if ctx.author.get_role(self.premium_role):
prompt = ctx.message.content.split(" ", maxsplit=1)[1]
img_url = await self.get_dalle(prompt, model="dall-e-3")
my_filename = str(time.time_ns()) + ".png"
filepath = f"{self.data_dir}dalle/{my_filename}"
await self.download_image(img_url, filepath)
with open(filepath, "rb") as fh:
f = discord.File(fh, filename=filepath)
log_data = f'Author: {ctx.author.name}, Prompt: {prompt}, Filename: {my_filename}\n'
with open(f"{self.data_dir}logs/dalle3.log", 'a') as log_file:
log_file.writelines(log_data)
await ctx.send(f'Generated by: {ctx.author.name}\nPrompt: {prompt}', file=f)
else:
await ctx.send("Sorry you must be a premium member to use this command. (!donate)")
@commands.command( @commands.command(
description="Dalle 3 HD", description="Dalle 2",
help="Generate an HD image with Dalle 3 Usage: !dalle3 (prompt)", help="Generate an image with Dalle 2 Usage: !dalle2 (prompt)",
brief="Generate HD Image", brief="Generate Image"
) )
async def dalle3hd(self, ctx): async def dalle2(self, ctx):
if ctx.author.get_role(self.premium_role): await self.generate_dalle_image(ctx, model="dall-e-2")
prompt = ctx.message.content.split(" ", maxsplit=1)[1]
image_url = await self.get_dalle(prompt, model="dall-e-3", quality="hd", size="1792x1024")
my_filename = str(time.time_ns()) + ".png" @commands.command(
image_filepath = f"{self.data_dir}dalle/{my_filename}" description="Dalle 3",
await self.download_image(image_url, image_filepath) help="Generate an image with Dalle 3 Usage: !dalle3 (prompt)",
with open(image_filepath, "rb") as fh: brief="Generate Image",
f = discord.File(fh, filename=image_filepath) aliases = ['dalle']
log_data = f'Author: {ctx.author.name}, Prompt: {prompt}, Filename: {my_filename}\n' )
with open(f"{self.data_dir}logs/dalle3.log", 'a') as log_filepath: async def dalle3(self, ctx):
log_filepath.writelines(log_data) await self.generate_dalle_image(ctx, model="dall-e-3")
await ctx.send(f'Generated by: {ctx.author.name}\nPrompt: {prompt}', file=f)
else:
await ctx.send("Sorry you must be a premium member to use this command. (!donate)") @commands.command(
description="Dalle 3 HD",
help="Generate an HD image with Dalle 3 Usage: !dalle3 (prompt)",
brief="Generate HD Image",
)
async def dalle3hd(self, ctx):
await self.generate_dalle_image(ctx, model="dall-e-3", quality="hd", size="1792x1024")
@commands.command( @commands.command(
description="Image GPT4", description="Image GPT4",
help="Ask GPT4 a question about an image. Usage: !question_gpt4 (link) (question)", help="Ask GPT4 a question about an image. Usage: !question_gpt4 (link) (question)",