added openai option
This commit is contained in:
parent
91502fb29b
commit
3e9391d184
1 changed files with 38 additions and 0 deletions
|
|
@ -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))
|
||||
Loading…
Add table
Add a link
Reference in a new issue