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