You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
01/software/source/server/services/tts/openai/tts.py

45 lines
1.2 KiB

import ffmpeg
import tempfile
from openai import OpenAI
import os
from source.server.utils.logs import logger
from source.server.utils.logs import setup_logging
setup_logging()
# If this TTS service is used, the OPENAI_API_KEY environment variable must be set
if not os.getenv("OPENAI_API_KEY"):
logger.error("")
logger.error(
"OpenAI API key not found. Please set the OPENAI_API_KEY environment variable, or run 01 with the --local option."
)
logger.error("Aborting...")
logger.error("")
os._exit(1)
client = OpenAI()
class Tts:
def __init__(self, config):
pass
def tts(self, text):
response = client.audio.speech.create(
model="tts-1",
voice=os.getenv("OPENAI_VOICE_NAME", "alloy"),
input=text,
response_format="opus",
)
with tempfile.NamedTemporaryFile(suffix=".opus", delete=False) as temp_file:
response.stream_to_file(temp_file.name)
# TODO: hack to format audio correctly for device
9 months ago
outfile = tempfile.gettempdir() + "/" + "output.wav"
ffmpeg.input(temp_file.name).output(
9 months ago
outfile, f="wav", ar="16000", ac="1", loglevel="panic"
).run()
return outfile