From 3e9391d18436819f7dae7ab19f73fe71eb2837cb Mon Sep 17 00:00:00 2001 From: phixxy Date: Thu, 8 Feb 2024 01:45:43 -0800 Subject: [PATCH] added openai option --- extensions/tts.py | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/extensions/tts.py b/extensions/tts.py index 1183395..d1d7339 100644 --- a/extensions/tts.py +++ b/extensions/tts.py @@ -46,6 +46,29 @@ class TextToSpeech(commands.Cog): if chunk: f.write(chunk) return filepath + + async def open_text_to_speech(self, prompt): + CHUNK_SIZE = 1024 + url = "https://api.openai.com/v1/audio/speech" + api_key = os.getenv("openai.api_key") + headers = { + "Content-Type": "application/json", + "Authorization": f"Bearer {api_key}" + } + + data = { + "input": prompt, + "model": "tts-1", + "voice": "alloy" + } + filename = f"{time.time_ns()}.mp3" + filepath = f"{self.data_dir}{filename}" + response = await self.bot.http_session.post(url, json=data, headers=headers) + with open(filepath, 'wb') as f: + async for chunk in response.content.iter_chunked(CHUNK_SIZE): + if chunk: + f.write(chunk) + return filepath @@ -72,5 +95,20 @@ class TextToSpeech(commands.Cog): await ctx.send("Error in tts") self.bot.logger.exception("Error in tts") + @commands.command() + async def opentts(self, ctx): + prompt = self.get_prompt_from_ctx(ctx) + if prompt is None: + await ctx.send("Please provide a prompt") + return + else: + await ctx.send("Generating...") + try: + filepath = await self.open_text_to_speech(prompt) + await ctx.send(file=discord.File(filepath)) + except: + await ctx.send("Error in tts") + self.bot.logger.exception("Error in tts") + async def setup(bot): await bot.add_cog(TextToSpeech(bot)) \ No newline at end of file