Skip to content

Instantly share code, notes, and snippets.

@tbg
Created August 12, 2024 13:06
Show Gist options
  • Save tbg/dc6e26bd4404fe234c4a47a8c4899f98 to your computer and use it in GitHub Desktop.
Save tbg/dc6e26bd4404fe234c4a47a8c4899f98 to your computer and use it in GitHub Desktop.
openai-text-to-speech.py
from pathlib import Path
from openai import OpenAI
client = OpenAI()
input_file_path = Path(__file__).parent / "input.txt"
with open(input_file_path, 'r') as f:
paragraphs = f.read().split('\n\n')
for i, input in enumerate(paragraphs):
response = client.audio.speech.create(
model="tts-1",
voice="alloy",
input=input
)
speech_file_path = Path(__file__).parent / f"output-{i+1}.mp3"
response.stream_to_file(speech_file_path)
print(speech_file_path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment