File: text_to_speech.py

package info (click to toggle)
python-openai 2.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,528 kB
  • sloc: python: 68,384; sh: 161; makefile: 7
file content (31 lines) | stat: -rwxr-xr-x 952 bytes parent folder | download | duplicates (2)
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())