File size: 1,792 Bytes
05b45a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env rye run python
import asyncio
import time
from pathlib import Path
from openai import AsyncOpenAI

# Initialize async client
openai = AsyncOpenAI(base_url="http://localhost:8880/v1", api_key="not-needed-for-local")

async def save_to_file(text: str, file_id: int) -> None:
    """Save TTS output to file asynchronously"""
    speech_file_path = Path(__file__).parent / f"speech_{file_id}.mp3"
    
    start_time = time.time()
    print(f"Starting file {file_id}")
    
    try:
        # Use streaming endpoint with mp3 format
        async with openai.audio.speech.with_streaming_response.create(
            model="kokoro",
            voice="af_bella",
            input=text,
            response_format="mp3"
        ) as response:
            print(f"File {file_id} - Time to first byte: {int((time.time() - start_time) * 1000)}ms")
            
            # Open file in binary write mode
            with open(speech_file_path, 'wb') as f:
                async for chunk in response.iter_bytes():
                    f.write(chunk)
            
            print(f"File {file_id} completed in {int((time.time() - start_time) * 1000)}ms")
    except Exception as e:
        print(f"Error processing file {file_id}: {e}")

async def main() -> None:
    # Different text samples for variety
    texts = [
        "The quick brown fox jumped over the lazy dogs. I see skies of blue and clouds of white",
        "I see skies of blue and clouds of white. I see skies of blue and clouds of white",
    ]
    
    # Create tasks for saving to files
    file_tasks = [
        save_to_file(text, i) 
        for i, text in enumerate(texts)
    ]
    
    # Run file tasks concurrently
    await asyncio.gather(*file_tasks)

if __name__ == "__main__":
    asyncio.run(main())