diff --git a/extensions/chatgpt.py b/extensions/chatgpt.py index 6626841..5b948d3 100644 --- a/extensions/chatgpt.py +++ b/extensions/chatgpt.py @@ -98,6 +98,30 @@ class ChatGPT(commands.Cog): self.bot.logger.exception("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( description="Personality", help="Set the personality of the bot. Usage: !personality (personality)", @@ -173,6 +197,33 @@ class ChatGPT(commands.Cog): await ctx.send(chunk) else: 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( description="Image GPT4",