Model details
Example usage
To start sending data to this model first the client needs to send a JSON metadata object containing model params. Then the client can stream text over the same websocket connection. The server will return raw audio bytes.
1import asyncio
2import os
3
4import pyaudio
5from aiohttp import ClientSession, WSMsgType
6from dotenv import load_dotenv
7
8load_dotenv()
9
10API_KEY = os.getenv("BASETEN-API-KEY")
11MODEL_ID = ""
12
13WS_URL = f"wss://model-{MODEL_ID}.api.baseten.co/environments/production/websocket"
14
15VOICE = "tara"
16MAX_TOKENS = 2000
17BUFFER_SIZE = 10 # words / chunk
18SAMPLE_RATE = 24000
19WIDTH = pyaudio.paInt16
20CHANNELS = 1
21
22
23async def stream_tts(text: str):
24 pa = pyaudio.PyAudio()
25 stream = pa.open(format=pyaudio.paInt16, channels=1, rate=SAMPLE_RATE, output=True)
26
27 headers = {"Authorization": f"Api-Key {API_KEY}"}
28 print(f"Connecting to WebSocket: {WS_URL}")
29 async with ClientSession(headers=headers) as sess:
30 try:
31 async with sess.ws_connect(WS_URL) as ws:
32 print("✅ WS connected")
33
34 # send metadata once
35 await ws.send_json(
36 {
37 "voice": VOICE,
38 "max_tokens": MAX_TOKENS,
39 "buffer_size": BUFFER_SIZE,
40 }
41 )
42 print("📤 metadata sent")
43
44 # start audio receiver
45 async def receiver():
46 async for msg in ws:
47 if msg.type == WSMsgType.BINARY:
48 print(f"⏯️ playing {len(msg.data)} bytes")
49 stream.write(msg.data)
50 elif msg.type in (WSMsgType.CLOSE, WSMsgType.CLOSED):
51 print("🔒 server closed")
52 return
53
54 recv = asyncio.create_task(receiver())
55
56 # send words
57 for w in text.strip().split():
58 await ws.send_str(w)
59 print("📤 words sent")
60
61 # signal end-of-text
62 await ws.send_str("__END__")
63 print("📤 END sentinel sent — waiting for audio")
64
65 # wait until server closes
66 await recv
67
68 except Exception as e:
69 print(f"❌ Connection error: {e}")
70
71 stream.stop_stream()
72 stream.close()
73 pa.terminate()
74 print("🎉 done")
75
76
77if __name__ == "__main__":
78 sample = (
79 "Nothing beside remains. Round the decay of that colossal wreck, "
80 "boundless and bare, The lone and level sands stretch far away."
81 )
82
83 async def main():
84 await stream_tts(sample)
85
86 asyncio.run(main())
Input
JSON output
1null