Hi Team,
I made a small tweak to the provided TTS code from OpenAI documentation page and it worked. The output is an embedded audio file on which I manually clicked ’ Play’. Try this code:
import io
from openai import OpenAI
from pydub import AudioSegment
from pydub.playback import play
from dotenv import load_dotenv, find_dotenv
from IPython.display import Audio, display
load_dotenv(find_dotenv(), override=True)
client = OpenAI()
if __name__ == "__main__":
text = input("Enter text: ")
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input=text,
)
# Convert the binary response content to a byte stream
byte_stream = io.BytesIO(response.content)
# Read the audio data from the byte stream
audio = AudioSegment.from_file(byte_stream, format="mp3")
audio