Generate different language speech using OpenAI text to speech API


How to use openai text to speech to generate speech for different languages?

If your input is English, you can use the following code:


from pathlib import Path
from openai import OpenAI
client = OpenAI(api_key="your_key")

speech_file_path = Path(__file__).parent / "speech.mp3"
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input="Today is a wonderful day to build something people love!"
)

response.stream_to_file(speech_file_path)

but what If you want to say the same input, but different language?

Is there a parameter called “language” in the text to speech API?

Sorry, there isn’t.

But actually, you should can first translate the input to the target language, and then pass the translation to the text to speech API, you can get the speech in your target language.


Author: robot learner
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint policy. If reproduced, please indicate source robot learner !
  TOC