forked from openai/openai-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext_to_speech.py
More file actions
executable file
·31 lines (23 loc) · 952 Bytes
/
Copy pathtext_to_speech.py
File metadata and controls
executable file
·31 lines (23 loc) · 952 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env rye run python
import time
import asyncio
from openai import AsyncOpenAI
from openai.helpers import LocalAudioPlayer
# gets OPENAI_API_KEY from your environment variables
openai = AsyncOpenAI()
async def main() -> None:
start_time = time.time()
async with openai.audio.speech.with_streaming_response.create(
model="tts-1",
voice="alloy",
response_format="pcm", # similar to WAV, but without a header chunk at the start.
input="""I see skies of blue and clouds of white
The bright blessed days, the dark sacred nights
And I think to myself
What a wonderful world""",
) as response:
print(f"Time to first byte: {int((time.time() - start_time) * 1000)}ms")
await LocalAudioPlayer().play(response)
print(f"Time to play: {int((time.time() - start_time) * 1000)}ms")
if __name__ == "__main__":
asyncio.run(main())