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

31 lines
856 B

import ffmpeg
import tempfile
from openai import OpenAI
import os
import subprocess
import tempfile
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
outfile = tempfile.gettempdir() + "/" + "raw.dat"
ffmpeg.input(temp_file.name).output(outfile, f="s16le", ar="16000", ac="1", loglevel='panic').run()
return outfile