tts using eleven labs rachel voice

This commit is contained in:
phixxy 2024-02-08 01:03:02 -08:00
parent 346a6b0298
commit 91502fb29b

76
extensions/tts.py Normal file
View file

@ -0,0 +1,76 @@
import time
import os
import discord
from discord.ext import commands
class TextToSpeech(commands.Cog):
def __init__(self, bot):
self.bot = bot
self.working_dir = "tmp/tts/"
self.data_dir = "data/tts/"
self.folder_setup()
def folder_setup(self):
try:
if not os.path.exists(self.working_dir):
os.mkdir(self.working_dir)
if not os.path.exists(self.data_dir):
os.mkdir(self.data_dir)
except:
self.bot.logger.exception("TextToSpeech failed to make directories")
async def text_to_speech(self, prompt):
CHUNK_SIZE = 1024
url = "https://api.elevenlabs.io/v1/text-to-speech/21m00Tcm4TlvDq8ikWAM"
api_key = os.getenv("eleven_labs")
headers = {
"Accept": "audio/mpeg",
"Content-Type": "application/json",
"xi-api-key": api_key
}
data = {
"text": prompt,
"model_id": "eleven_monolingual_v1",
"voice_settings": {
"stability": 0.5,
"similarity_boost": 0.5
}
}
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):
try:
prompt = ctx.message.content.split(" ", maxsplit=1)[1]
prompt = ' '.join(list(filter(lambda x: '=' not in x,prompt.split(' '))))
return prompt
except:
return None
@commands.command()
async def tts(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.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))