added dalle support

This commit is contained in:
phixxy 2024-01-27 17:08:46 -08:00
parent 9c460d56b2
commit 0e8c6d8132

View file

@ -98,6 +98,30 @@ class ChatGPT(commands.Cog):
self.bot.logger.exception("Error occurred in answer_question") self.bot.logger.exception("Error occurred in answer_question")
return "Error occurred in answer_question" return "Error occurred in answer_question"
async def get_dalle(self, prompt, model="dall-e-2"):
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.getenv("openai.api_key")}',
}
data = {
"model": model,
"prompt": prompt,
"size": "1024x1024",
"quality": "standard",
"n":1,
}
url = "https://api.openai.com/v1/images/generations"
try:
async with self.bot.http_session.post(url, headers=headers, json=data) as resp:
response_data = await resp.json()
response = response_data[0]['url']
return response
except Exception as error:
self.bot.logger.exception("Error occurred in dalle")
return "Error occurred in dalle"
@commands.command( @commands.command(
description="Personality", description="Personality",
help="Set the personality of the bot. Usage: !personality (personality)", help="Set the personality of the bot. Usage: !personality (personality)",
@ -173,6 +197,33 @@ class ChatGPT(commands.Cog):
await ctx.send(chunk) await ctx.send(chunk)
else: else:
await ctx.send("Sorry you must be a premium member to use this command. (!donate)") await ctx.send("Sorry you must be a premium member to use this command. (!donate)")
@commands.command(
description="Dalle 2",
help="Generate an image with Dalle 2 Usage: !dalle2 (prompt)",
brief="Generate Image",
aliases = ['dalle']
)
async def dalle2(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)
await ctx.send("Generated by: {ctx.author.name}\nPrompt: {prompt}\n{img_url}")
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"
)
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, "dall-e-3")
await ctx.send("Generated by: {ctx.author.name}\nPrompt: {prompt}\n{img_url}")
else:
await ctx.send("Sorry you must be a premium member to use this command. (!donate)")
@commands.command( @commands.command(
description="Image GPT4", description="Image GPT4",