Last active
February 1, 2024 04:32
-
-
Save tiny-rawr/79d7ffd5cd34d96b993ac4bb07166e6c to your computer and use it in GitHub Desktop.
Text-to-speech with OpenAI's TTS model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from openai import OpenAI | |
def choose_voice(gender="female") | |
gender = gender.lower() | |
voice = "shimmer" # gender neutral | |
if gender == "female": | |
voice = "nova" | |
elif gender == "male": | |
voice = "onyx" | |
return voice | |
def text_to_speech(gender="female", text="Hello world!"): | |
api_key = st.session_state.api_key | |
client = OpenAI(api_key=api_key) | |
voice = choose_voice(gender) | |
speech_file_path = "projects/_005_avatar_debate/audio.wav" | |
response = client.audio.speech.create( | |
model="tts-1", | |
voice=voice, | |
input=text | |
) | |
response.stream_to_file(speech_file_path) | |
# Use the simple text-to-speech method | |
print(text_to_speech("female", "Hello, my name is Alina!") | |
# => This produces an audio file of the sentence 'Hello, my name is Alina' with a female voice |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment