added openai option
This commit is contained in:
parent
91502fb29b
commit
3e9391d184
1 changed files with 38 additions and 0 deletions
|
|
@ -47,6 +47,29 @@ class TextToSpeech(commands.Cog):
|
||||||
f.write(chunk)
|
f.write(chunk)
|
||||||
return filepath
|
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
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_prompt_from_ctx(self, ctx):
|
def get_prompt_from_ctx(self, ctx):
|
||||||
|
|
@ -72,5 +95,20 @@ class TextToSpeech(commands.Cog):
|
||||||
await ctx.send("Error in tts")
|
await ctx.send("Error in tts")
|
||||||
self.bot.logger.exception("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):
|
async def setup(bot):
|
||||||
await bot.add_cog(TextToSpeech(bot))
|
await bot.add_cog(TextToSpeech(bot))
|
||||||
Loading…
Add table
Add a link
Reference in a new issue