No audio heard on Google Colab when running TTS API

,

Hi,
When I use the code provided by OpenAI for streaming a Text-To-Speech output, there is no audio can be heard when I run this code on Google Colab:

import io
from openai import OpenAI
from pydub import AudioSegment
from pydub.playback import play

client = OpenAI()

def stream_and_play(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")

  # Play the audio
  play(audio)

if __name__ == "__main__":
  text = input("Enter text: ")
  stream_and_play(text)

Any idea why ?

It’s probably trying to play on the remote server you are running Colab on. So either generate the file and download it to your local device and play, or figure out a way to stream it back to your computer to play.

2 Likes

Thanks Curt. Audio files can be played on Colab, so I solved this issue by just displaying the object (audio) as is without any PLAY function and I get to press play button manually.

1 Like