cartesia 1.3.1__py3-none-any.whl → 2.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. cartesia/__init__.py +302 -3
  2. cartesia/api_status/__init__.py +6 -0
  3. cartesia/api_status/client.py +104 -0
  4. cartesia/api_status/requests/__init__.py +5 -0
  5. cartesia/api_status/requests/api_info.py +8 -0
  6. cartesia/api_status/types/__init__.py +5 -0
  7. cartesia/api_status/types/api_info.py +20 -0
  8. cartesia/base_client.py +156 -0
  9. cartesia/client.py +163 -40
  10. cartesia/core/__init__.py +50 -0
  11. cartesia/core/api_error.py +15 -0
  12. cartesia/core/client_wrapper.py +55 -0
  13. cartesia/core/datetime_utils.py +28 -0
  14. cartesia/core/file.py +67 -0
  15. cartesia/core/http_client.py +499 -0
  16. cartesia/core/jsonable_encoder.py +101 -0
  17. cartesia/core/pagination.py +88 -0
  18. cartesia/core/pydantic_utilities.py +296 -0
  19. cartesia/core/query_encoder.py +58 -0
  20. cartesia/core/remove_none_from_dict.py +11 -0
  21. cartesia/core/request_options.py +35 -0
  22. cartesia/core/serialization.py +272 -0
  23. cartesia/datasets/__init__.py +24 -0
  24. cartesia/datasets/requests/__init__.py +15 -0
  25. cartesia/datasets/requests/create_dataset_request.py +7 -0
  26. cartesia/datasets/requests/dataset.py +9 -0
  27. cartesia/datasets/requests/dataset_file.py +9 -0
  28. cartesia/datasets/requests/paginated_dataset_files.py +10 -0
  29. cartesia/datasets/requests/paginated_datasets.py +10 -0
  30. cartesia/datasets/types/__init__.py +17 -0
  31. cartesia/datasets/types/create_dataset_request.py +19 -0
  32. cartesia/datasets/types/dataset.py +21 -0
  33. cartesia/datasets/types/dataset_file.py +21 -0
  34. cartesia/datasets/types/file_purpose.py +5 -0
  35. cartesia/datasets/types/paginated_dataset_files.py +21 -0
  36. cartesia/datasets/types/paginated_datasets.py +21 -0
  37. cartesia/embedding/__init__.py +5 -0
  38. cartesia/embedding/types/__init__.py +5 -0
  39. cartesia/embedding/types/embedding.py +201 -0
  40. cartesia/environment.py +7 -0
  41. cartesia/infill/__init__.py +2 -0
  42. cartesia/infill/client.py +318 -0
  43. cartesia/tts/__init__.py +167 -0
  44. cartesia/{_async_websocket.py → tts/_async_websocket.py} +212 -85
  45. cartesia/tts/_websocket.py +479 -0
  46. cartesia/tts/client.py +407 -0
  47. cartesia/tts/requests/__init__.py +76 -0
  48. cartesia/tts/requests/cancel_context_request.py +17 -0
  49. cartesia/tts/requests/controls.py +11 -0
  50. cartesia/tts/requests/generation_request.py +58 -0
  51. cartesia/tts/requests/mp_3_output_format.py +11 -0
  52. cartesia/tts/requests/output_format.py +30 -0
  53. cartesia/tts/requests/phoneme_timestamps.py +10 -0
  54. cartesia/tts/requests/raw_output_format.py +11 -0
  55. cartesia/tts/requests/speed.py +7 -0
  56. cartesia/tts/requests/tts_request.py +24 -0
  57. cartesia/tts/requests/tts_request_embedding_specifier.py +16 -0
  58. cartesia/tts/requests/tts_request_id_specifier.py +16 -0
  59. cartesia/tts/requests/tts_request_voice_specifier.py +7 -0
  60. cartesia/tts/requests/wav_output_format.py +7 -0
  61. cartesia/tts/requests/web_socket_base_response.py +11 -0
  62. cartesia/tts/requests/web_socket_chunk_response.py +11 -0
  63. cartesia/tts/requests/web_socket_done_response.py +7 -0
  64. cartesia/tts/requests/web_socket_error_response.py +7 -0
  65. cartesia/tts/requests/web_socket_flush_done_response.py +9 -0
  66. cartesia/tts/requests/web_socket_phoneme_timestamps_response.py +9 -0
  67. cartesia/tts/requests/web_socket_raw_output_format.py +11 -0
  68. cartesia/tts/requests/web_socket_request.py +7 -0
  69. cartesia/tts/requests/web_socket_response.py +70 -0
  70. cartesia/tts/requests/web_socket_stream_options.py +8 -0
  71. cartesia/tts/requests/web_socket_timestamps_response.py +9 -0
  72. cartesia/tts/requests/web_socket_tts_output.py +18 -0
  73. cartesia/tts/requests/web_socket_tts_request.py +25 -0
  74. cartesia/tts/requests/word_timestamps.py +10 -0
  75. cartesia/tts/socket_client.py +302 -0
  76. cartesia/tts/types/__init__.py +90 -0
  77. cartesia/tts/types/cancel_context_request.py +28 -0
  78. cartesia/tts/types/context_id.py +3 -0
  79. cartesia/tts/types/controls.py +22 -0
  80. cartesia/tts/types/emotion.py +34 -0
  81. cartesia/tts/types/flush_id.py +3 -0
  82. cartesia/tts/types/generation_request.py +71 -0
  83. cartesia/tts/types/mp_3_output_format.py +23 -0
  84. cartesia/tts/types/natural_specifier.py +5 -0
  85. cartesia/tts/types/numerical_specifier.py +3 -0
  86. cartesia/tts/types/output_format.py +58 -0
  87. cartesia/tts/types/phoneme_timestamps.py +21 -0
  88. cartesia/tts/types/raw_encoding.py +5 -0
  89. cartesia/tts/types/raw_output_format.py +22 -0
  90. cartesia/tts/types/speed.py +7 -0
  91. cartesia/tts/types/supported_language.py +7 -0
  92. cartesia/tts/types/tts_request.py +35 -0
  93. cartesia/tts/types/tts_request_embedding_specifier.py +27 -0
  94. cartesia/tts/types/tts_request_id_specifier.py +27 -0
  95. cartesia/tts/types/tts_request_voice_specifier.py +7 -0
  96. cartesia/tts/types/wav_output_format.py +17 -0
  97. cartesia/tts/types/web_socket_base_response.py +22 -0
  98. cartesia/tts/types/web_socket_chunk_response.py +22 -0
  99. cartesia/tts/types/web_socket_done_response.py +17 -0
  100. cartesia/tts/types/web_socket_error_response.py +19 -0
  101. cartesia/tts/types/web_socket_flush_done_response.py +21 -0
  102. cartesia/tts/types/web_socket_phoneme_timestamps_response.py +20 -0
  103. cartesia/tts/types/web_socket_raw_output_format.py +22 -0
  104. cartesia/tts/types/web_socket_request.py +7 -0
  105. cartesia/tts/types/web_socket_response.py +125 -0
  106. cartesia/tts/types/web_socket_stream_options.py +19 -0
  107. cartesia/tts/types/web_socket_timestamps_response.py +20 -0
  108. cartesia/tts/types/web_socket_tts_output.py +29 -0
  109. cartesia/tts/types/web_socket_tts_request.py +37 -0
  110. cartesia/tts/types/word_timestamps.py +21 -0
  111. cartesia/{_constants.py → tts/utils/constants.py} +2 -2
  112. cartesia/tts/utils/tts.py +64 -0
  113. cartesia/tts/utils/types.py +70 -0
  114. cartesia/version.py +3 -1
  115. cartesia/voice_changer/__init__.py +27 -0
  116. cartesia/voice_changer/client.py +395 -0
  117. cartesia/voice_changer/requests/__init__.py +15 -0
  118. cartesia/voice_changer/requests/streaming_response.py +38 -0
  119. cartesia/voice_changer/types/__init__.py +17 -0
  120. cartesia/voice_changer/types/output_format_container.py +5 -0
  121. cartesia/voice_changer/types/streaming_response.py +64 -0
  122. cartesia/voices/__init__.py +81 -0
  123. cartesia/voices/client.py +1218 -0
  124. cartesia/voices/requests/__init__.py +29 -0
  125. cartesia/voices/requests/create_voice_request.py +23 -0
  126. cartesia/voices/requests/embedding_response.py +8 -0
  127. cartesia/voices/requests/embedding_specifier.py +10 -0
  128. cartesia/voices/requests/get_voices_response.py +24 -0
  129. cartesia/voices/requests/id_specifier.py +10 -0
  130. cartesia/voices/requests/localize_dialect.py +11 -0
  131. cartesia/voices/requests/localize_voice_request.py +28 -0
  132. cartesia/voices/requests/mix_voice_specifier.py +7 -0
  133. cartesia/voices/requests/mix_voices_request.py +9 -0
  134. cartesia/voices/requests/update_voice_request.py +15 -0
  135. cartesia/voices/requests/voice.py +43 -0
  136. cartesia/voices/requests/voice_metadata.py +36 -0
  137. cartesia/voices/types/__init__.py +53 -0
  138. cartesia/voices/types/base_voice_id.py +5 -0
  139. cartesia/voices/types/clone_mode.py +5 -0
  140. cartesia/voices/types/create_voice_request.py +34 -0
  141. cartesia/voices/types/embedding_response.py +20 -0
  142. cartesia/voices/types/embedding_specifier.py +22 -0
  143. cartesia/voices/types/gender.py +5 -0
  144. cartesia/voices/types/gender_presentation.py +5 -0
  145. cartesia/voices/types/get_voices_response.py +34 -0
  146. cartesia/voices/types/id_specifier.py +22 -0
  147. cartesia/voices/types/localize_dialect.py +11 -0
  148. cartesia/voices/types/localize_english_dialect.py +5 -0
  149. cartesia/voices/types/localize_french_dialect.py +5 -0
  150. cartesia/voices/types/localize_portuguese_dialect.py +5 -0
  151. cartesia/voices/types/localize_spanish_dialect.py +5 -0
  152. cartesia/voices/types/localize_target_language.py +7 -0
  153. cartesia/voices/types/localize_voice_request.py +39 -0
  154. cartesia/voices/types/mix_voice_specifier.py +7 -0
  155. cartesia/voices/types/mix_voices_request.py +20 -0
  156. cartesia/voices/types/update_voice_request.py +27 -0
  157. cartesia/voices/types/voice.py +54 -0
  158. cartesia/voices/types/voice_expand_options.py +5 -0
  159. cartesia/voices/types/voice_id.py +3 -0
  160. cartesia/voices/types/voice_metadata.py +48 -0
  161. cartesia/voices/types/weight.py +3 -0
  162. cartesia-2.0.0.dist-info/METADATA +414 -0
  163. cartesia-2.0.0.dist-info/RECORD +165 -0
  164. {cartesia-1.3.1.dist-info → cartesia-2.0.0.dist-info}/WHEEL +1 -1
  165. cartesia/_async_sse.py +0 -95
  166. cartesia/_logger.py +0 -3
  167. cartesia/_sse.py +0 -143
  168. cartesia/_types.py +0 -70
  169. cartesia/_websocket.py +0 -358
  170. cartesia/async_client.py +0 -82
  171. cartesia/async_tts.py +0 -63
  172. cartesia/resource.py +0 -44
  173. cartesia/tts.py +0 -137
  174. cartesia/utils/deprecated.py +0 -55
  175. cartesia/utils/retry.py +0 -87
  176. cartesia/utils/tts.py +0 -78
  177. cartesia/voices.py +0 -208
  178. cartesia-1.3.1.dist-info/METADATA +0 -661
  179. cartesia-1.3.1.dist-info/RECORD +0 -23
  180. cartesia-1.3.1.dist-info/licenses/LICENSE.md +0 -21
  181. /cartesia/{utils/__init__.py → py.typed} +0 -0
@@ -1,661 +0,0 @@
1
- Metadata-Version: 2.3
2
- Name: cartesia
3
- Version: 1.3.1
4
- Summary: The official Python library for the Cartesia API.
5
- Requires-Python: >=3.9
6
- Requires-Dist: aiohttp>=3.10.10
7
- Requires-Dist: httpx>=0.27.0
8
- Requires-Dist: iterators>=0.2.0
9
- Requires-Dist: requests>=2.31.0
10
- Requires-Dist: websockets>=10.4
11
- Description-Content-Type: text/markdown
12
-
13
- # Cartesia Python API Library
14
-
15
- ![PyPI - Version](https://img.shields.io/pypi/v/cartesia)
16
- [![Discord](https://badgen.net/badge/black/Cartesia/icon?icon=discord&label)](https://discord.gg/cartesia)
17
-
18
- The official Cartesia Python library which provides convenient access to the Cartesia REST and Websocket API from any Python 3.8+ application.
19
-
20
- > [!IMPORTANT]
21
- > The client library introduces breaking changes in v1.0.0, which was released on June 24th 2024. See the [release notes](https://github.com/cartesia-ai/cartesia-python/releases/tag/v1.0.0) and [migration guide](https://github.com/cartesia-ai/cartesia-python/discussions/44). Reach out to us on [Discord](https://discord.gg/cartesia) for any support requests!
22
-
23
- - [Cartesia Python API Library](#cartesia-python-api-library)
24
- - [Documentation](#documentation)
25
- - [Installation](#installation)
26
- - [Voices](#voices)
27
- - [Text-to-Speech](#text-to-speech)
28
- - [Bytes](#bytes)
29
- - [Server-Sent Events (SSE)](#server-sent-events-sse)
30
- - [WebSocket](#websocket)
31
- - [Conditioning speech on previous generations using WebSocket](#conditioning-speech-on-previous-generations-using-websocket)
32
- - [Generating timestamps using WebSocket](#generating-timestamps-using-websocket)
33
- - [Multilingual Text-to-Speech \[Alpha\]](#multilingual-text-to-speech-alpha)
34
- - [Speed and Emotion Control \[Experimental\]](#speed-and-emotion-control-experimental)
35
- - [Jupyter Notebook Usage](#jupyter-notebook-usage)
36
- - [Utility methods](#utility-methods)
37
- - [Output Formats](#output-formats)
38
-
39
-
40
- ## Documentation
41
-
42
- Our complete API documentation can be found [on docs.cartesia.ai](https://docs.cartesia.ai).
43
-
44
- ## Installation
45
-
46
- ```bash
47
- pip install cartesia
48
-
49
- # pip install in editable mode w/ dev dependencies
50
- pip install -e '.[dev]'
51
- ```
52
-
53
- ## Voices
54
-
55
- ```python
56
- from cartesia import Cartesia
57
- import os
58
-
59
- client = Cartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
60
-
61
- # Get all available voices
62
- voices = client.voices.list()
63
- print(voices)
64
-
65
- # Get a specific voice
66
- voice = client.voices.get(id="a0e99841-438c-4a64-b679-ae501e7d6091")
67
- print("The embedding for", voice["name"], "is", voice["embedding"])
68
-
69
- # Clone a voice using filepath
70
- cloned_voice_embedding = client.voices.clone(filepath="path/to/voice")
71
-
72
- # Mix voices together
73
- mixed_voice_embedding = client.voices.mix(
74
- [{ "id": "voice_id_1", "weight": 0.5 }, { "id": "voice_id_2", "weight": 0.25 }, { "id": "voice_id_3", "weight": 0.25 }]
75
- )
76
-
77
- # Create a new voice
78
- new_voice = client.voices.create(
79
- name="New Voice",
80
- description="A clone of my own voice",
81
- embedding=cloned_voice_embedding,
82
- )
83
- ```
84
-
85
- ## Text-to-Speech
86
-
87
- ### Bytes
88
-
89
- ```python
90
- from cartesia import Cartesia
91
- import os
92
-
93
- client = Cartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
94
-
95
- data = client.tts.bytes(
96
- model_id="sonic-english",
97
- transcript="Hello, world! I'm generating audio on Cartesia.",
98
- voice_id="a0e99841-438c-4a64-b679-ae501e7d6091", # Barbershop Man
99
- # You can find the supported `output_format`s at https://docs.cartesia.ai/api-reference/tts/bytes
100
- output_format={
101
- "container": "wav",
102
- "encoding": "pcm_f32le",
103
- "sample_rate": 44100,
104
- },
105
- )
106
-
107
- with open("output.wav", "wb") as f:
108
- f.write(data)
109
- ```
110
-
111
- ### Server-Sent Events (SSE)
112
-
113
- ```python
114
- from cartesia import Cartesia
115
- import pyaudio
116
- import os
117
-
118
- client = Cartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
119
- voice_id = "a0e99841-438c-4a64-b679-ae501e7d6091"
120
- voice = client.voices.get(id=voice_id)
121
-
122
- transcript = "Hello! Welcome to Cartesia"
123
-
124
- # You can check out our models at https://docs.cartesia.ai/getting-started/available-models
125
- model_id = "sonic-english"
126
-
127
- # You can find the supported `output_format`s at https://docs.cartesia.ai/reference/api-reference/rest/stream-speech-server-sent-events
128
- output_format = {
129
- "container": "raw",
130
- "encoding": "pcm_f32le",
131
- "sample_rate": 44100,
132
- }
133
-
134
- p = pyaudio.PyAudio()
135
- rate = 44100
136
-
137
- stream = None
138
-
139
- # Generate and stream audio
140
- for output in client.tts.sse(
141
- model_id=model_id,
142
- transcript=transcript,
143
- voice_embedding=voice["embedding"],
144
- stream=True,
145
- output_format=output_format,
146
- ):
147
- buffer = output["audio"]
148
-
149
- if not stream:
150
- stream = p.open(format=pyaudio.paFloat32, channels=1, rate=rate, output=True)
151
-
152
- # Write the audio data to the stream
153
- stream.write(buffer)
154
-
155
- stream.stop_stream()
156
- stream.close()
157
- p.terminate()
158
- ```
159
-
160
- You can also use the async client if you want to make asynchronous API calls. Simply import `AsyncCartesia` instead of `Cartesia` and use await with each API call:
161
-
162
- ```python
163
- from cartesia import AsyncCartesia
164
- import asyncio
165
- import pyaudio
166
- import os
167
-
168
-
169
- async def write_stream():
170
- client = AsyncCartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
171
- voice_id = "a0e99841-438c-4a64-b679-ae501e7d6091"
172
- voice = client.voices.get(id=voice_id)
173
- transcript = "Hello! Welcome to Cartesia"
174
- # You can check out our models at https://docs.cartesia.ai/getting-started/available-models
175
- model_id = "sonic-english"
176
-
177
- # You can find the supported `output_format`s at https://docs.cartesia.ai/reference/api-reference/rest/stream-speech-server-sent-events
178
- output_format = {
179
- "container": "raw",
180
- "encoding": "pcm_f32le",
181
- "sample_rate": 44100,
182
- }
183
-
184
- p = pyaudio.PyAudio()
185
- rate = 44100
186
-
187
- stream = None
188
-
189
- # Generate and stream audio
190
- async for output in await client.tts.sse(
191
- model_id=model_id,
192
- transcript=transcript,
193
- voice_embedding=voice["embedding"],
194
- stream=True,
195
- output_format=output_format,
196
- ):
197
- buffer = output["audio"]
198
-
199
- if not stream:
200
- stream = p.open(
201
- format=pyaudio.paFloat32, channels=1, rate=rate, output=True
202
- )
203
-
204
- # Write the audio data to the stream
205
- stream.write(buffer)
206
-
207
- stream.stop_stream()
208
- stream.close()
209
- p.terminate()
210
- await client.close()
211
-
212
-
213
- asyncio.run(write_stream())
214
- ```
215
-
216
- ### WebSocket
217
-
218
- ```python
219
- from cartesia import Cartesia
220
- import pyaudio
221
- import os
222
-
223
- client = Cartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
224
- voice_id = "a0e99841-438c-4a64-b679-ae501e7d6091"
225
- voice = client.voices.get(id=voice_id)
226
- transcript = "Hello! Welcome to Cartesia"
227
-
228
- # You can check out our models at https://docs.cartesia.ai/getting-started/available-models
229
- model_id = "sonic-english"
230
-
231
- # You can find the supported `output_format`s at https://docs.cartesia.ai/reference/api-reference/rest/stream-speech-server-sent-events
232
- output_format = {
233
- "container": "raw",
234
- "encoding": "pcm_f32le",
235
- "sample_rate": 22050,
236
- }
237
-
238
- p = pyaudio.PyAudio()
239
- rate = 22050
240
-
241
- stream = None
242
-
243
- # Set up the websocket connection
244
- ws = client.tts.websocket()
245
-
246
- # Generate and stream audio using the websocket
247
- for output in ws.send(
248
- model_id=model_id,
249
- transcript=transcript,
250
- voice_embedding=voice["embedding"],
251
- stream=True,
252
- output_format=output_format,
253
- ):
254
- buffer = output["audio"]
255
-
256
- if not stream:
257
- stream = p.open(format=pyaudio.paFloat32, channels=1, rate=rate, output=True)
258
-
259
- # Write the audio data to the stream
260
- stream.write(buffer)
261
-
262
- stream.stop_stream()
263
- stream.close()
264
- p.terminate()
265
-
266
- ws.close() # Close the websocket connection
267
- ```
268
-
269
- #### Conditioning speech on previous generations using WebSocket
270
-
271
- In some cases, input text may need to be streamed in. In these cases, it would be slow to wait for all the text to buffer before sending it to Cartesia's TTS service.
272
-
273
- To mitigate this, Cartesia offers audio continuations. In this setting, users can send input text, as it becomes available, over a websocket connection.
274
-
275
- To do this, we will create a `context` and send multiple requests without awaiting the response. Then you can listen to the responses in the order they were sent.
276
-
277
- Each `context` will be closed automatically after 5 seconds of inactivity or when the `no_more_inputs` method is called. `no_more_inputs` sends a request with the `continue_=False`, which indicates no more inputs will be sent over this context
278
-
279
- ```python
280
- import asyncio
281
- import os
282
- import pyaudio
283
- from cartesia import AsyncCartesia
284
-
285
- async def send_transcripts(ctx):
286
- # Check out voice IDs by calling `client.voices.list()` or on https://play.cartesia.ai/
287
- voice_id = "87748186-23bb-4158-a1eb-332911b0b708"
288
-
289
- # You can check out our models at https://docs.cartesia.ai/getting-started/available-models
290
- model_id = "sonic-english"
291
-
292
- # You can find the supported `output_format`s at https://docs.cartesia.ai/reference/api-reference/rest/stream-speech-server-sent-events
293
- output_format = {
294
- "container": "raw",
295
- "encoding": "pcm_f32le",
296
- "sample_rate": 44100,
297
- }
298
-
299
- transcripts = [
300
- "Sonic and Yoshi team up in a dimension-hopping adventure! ",
301
- "Racing through twisting zones, they dodge Eggman's badniks and solve ancient puzzles. ",
302
- "In the Echoing Caverns, they find the Harmonic Crystal, unlocking new powers. ",
303
- "Sonic's speed creates sound waves, while Yoshi's eggs become sonic bolts. ",
304
- "As they near Eggman's lair, our heroes charge their abilities for an epic boss battle. ",
305
- "Get ready to spin, jump, and sound-blast your way to victory in this high-octane crossover!"
306
- ]
307
-
308
- for transcript in transcripts:
309
- # Send text inputs as they become available
310
- await ctx.send(
311
- model_id=model_id,
312
- transcript=transcript,
313
- voice_id=voice_id,
314
- continue_=True,
315
- output_format=output_format,
316
- )
317
-
318
- # Indicate that no more inputs will be sent. Otherwise, the context will close after 5 seconds of inactivity.
319
- await ctx.no_more_inputs()
320
-
321
- async def receive_and_play_audio(ctx):
322
- p = pyaudio.PyAudio()
323
- stream = None
324
- rate = 44100
325
-
326
- async for output in ctx.receive():
327
- buffer = output["audio"]
328
-
329
- if not stream:
330
- stream = p.open(
331
- format=pyaudio.paFloat32,
332
- channels=1,
333
- rate=rate,
334
- output=True
335
- )
336
-
337
- stream.write(buffer)
338
-
339
- stream.stop_stream()
340
- stream.close()
341
- p.terminate()
342
-
343
- async def stream_and_listen():
344
- client = AsyncCartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
345
-
346
- # Set up the websocket connection
347
- ws = await client.tts.websocket()
348
-
349
- # Create a context to send and receive audio
350
- ctx = ws.context() # Generates a random context ID if not provided
351
-
352
- send_task = asyncio.create_task(send_transcripts(ctx))
353
- listen_task = asyncio.create_task(receive_and_play_audio(ctx))
354
-
355
- # Call the two coroutine tasks concurrently
356
- await asyncio.gather(send_task, listen_task)
357
-
358
- await ws.close()
359
- await client.close()
360
-
361
- asyncio.run(stream_and_listen())
362
- ```
363
-
364
- You can also use continuations on the synchronous Cartesia client to stream in text as it becomes available. To do this, pass in a text generator that produces text chunks at intervals of less than 1 second, as shown below. This ensures smooth audio playback.
365
-
366
- Note: the sync client has a different API for continuations compared to the async client.
367
-
368
- ```python
369
- from cartesia import Cartesia
370
- import pyaudio
371
- import os
372
-
373
- client = Cartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
374
-
375
- transcripts = [
376
- "The crew engaged in a range of activities designed to mirror those "
377
- "they might perform on a real Mars mission. ",
378
- "Aside from growing vegetables and maintaining their habitat, they faced "
379
- "additional stressors like communication delays with Earth, ",
380
- "up to twenty-two minutes each way, to simulate the distance from Mars to our planet. ",
381
- "These exercises were critical for understanding how astronauts can "
382
- "maintain not just physical health but also mental well-being under such challenging conditions. ",
383
- ]
384
-
385
- # Ending each transcript with a space makes the audio smoother
386
- def chunk_generator(transcripts):
387
- for transcript in transcripts:
388
- if transcript.endswith(" "):
389
- yield transcript
390
- else:
391
- yield transcript + " "
392
-
393
-
394
- # You can check out voice IDs by calling `client.voices.list()` or on https://play.cartesia.ai/
395
- voice_id = "87748186-23bb-4158-a1eb-332911b0b708"
396
-
397
- # You can check out our models at https://docs.cartesia.ai/getting-started/available-models
398
- model_id = "sonic-english"
399
-
400
- # You can find the supported `output_format`s at https://docs.cartesia.ai/reference/api-reference/rest/stream-speech-server-sent-events
401
- output_format = {
402
- "container": "raw",
403
- "encoding": "pcm_f32le",
404
- "sample_rate": 44100,
405
- }
406
-
407
- p = pyaudio.PyAudio()
408
- rate = 44100
409
-
410
- stream = None
411
-
412
- # Set up the websocket connection
413
- ws = client.tts.websocket()
414
-
415
- # Create a context to send and receive audio
416
- ctx = ws.context() # Generates a random context ID if not provided
417
-
418
- # Pass in a text generator to generate & stream the audio
419
- output_stream = ctx.send(
420
- model_id=model_id,
421
- transcript=chunk_generator(transcripts),
422
- voice_id=voice_id,
423
- output_format=output_format,
424
- )
425
-
426
- for output in output_stream:
427
- buffer = output["audio"]
428
-
429
- if not stream:
430
- stream = p.open(format=pyaudio.paFloat32, channels=1, rate=rate, output=True)
431
-
432
- # Write the audio data to the stream
433
- stream.write(buffer)
434
-
435
- stream.stop_stream()
436
- stream.close()
437
- p.terminate()
438
-
439
- ws.close() # Close the websocket connection
440
- ```
441
-
442
- ### Generating timestamps using WebSocket
443
-
444
- The WebSocket endpoint supports timestamps, allowing you to get detailed timing information for each word in the transcript. To enable this feature, pass an `add_timestamps` boolean flag to the `send` method. The results are returned in the `word_timestamps` object, which contains three keys:
445
- - words (list): The individual words in the transcript.
446
- - start (list): The starting timestamp for each word (in seconds).
447
- - end (list): The ending timestamp for each word (in seconds).
448
-
449
- ```python
450
- response = ws.send(
451
- model_id=model_id,
452
- transcript=transcript,
453
- voice_id=voice_id,
454
- output_format=output_format,
455
- stream=False,
456
- add_timestamps=True
457
- )
458
-
459
- # Accessing the word_timestamps object
460
- word_timestamps = response['word_timestamps']
461
-
462
- words = word_timestamps['words']
463
- start_times = word_timestamps['start']
464
- end_times = word_timestamps['end']
465
-
466
- for word, start, end in zip(words, start_times, end_times):
467
- print(f"Word: {word}, Start: {start}, End: {end}")
468
- ```
469
-
470
- ### Multilingual Text-to-Speech [Alpha]
471
-
472
- You can use our `sonic-multilingual` model to generate audio in multiple languages. The languages supported are available at [docs.cartesia.ai](https://docs.cartesia.ai/getting-started/available-models).
473
-
474
- ```python
475
- from cartesia import Cartesia
476
- import pyaudio
477
- import os
478
-
479
- client = Cartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
480
- voice_id = "a0e99841-438c-4a64-b679-ae501e7d6091"
481
- voice = client.voices.get(id=voice_id)
482
-
483
- transcript = "Hola! Bienvenido a Cartesia"
484
- language = "es" # Language code corresponding to the language of the transcript
485
-
486
- # Make sure you use the multilingual model! You can check out all models at https://docs.cartesia.ai/getting-started/available-models
487
- model_id = "sonic-multilingual"
488
-
489
- # You can find the supported `output_format`s at https://docs.cartesia.ai/reference/api-reference/rest/stream-speech-server-sent-events
490
- output_format = {
491
- "container": "raw",
492
- "encoding": "pcm_f32le",
493
- "sample_rate": 44100,
494
- }
495
-
496
- p = pyaudio.PyAudio()
497
- rate = 44100
498
-
499
- stream = None
500
-
501
- # Pass in the corresponding language code to the `language` parameter to generate and stream audio.
502
- for output in client.tts.sse(
503
- model_id=model_id,
504
- transcript=transcript,
505
- voice_embedding=voice["embedding"],
506
- stream=True,
507
- output_format=output_format,
508
- language=language,
509
- ):
510
- buffer = output["audio"]
511
-
512
- if not stream:
513
- stream = p.open(format=pyaudio.paFloat32, channels=1, rate=rate, output=True)
514
-
515
- stream.write(buffer)
516
-
517
- stream.stop_stream()
518
- stream.close()
519
- p.terminate()
520
- ```
521
-
522
- ### Speed and Emotion Control [Experimental]
523
-
524
- You can enhance the voice output by adjusting the `speed` and `emotion` parameters. To do this, pass a `_experimental_voice_controls` dictionary with the desired `speed` and `emotion` values to any `send` method.
525
-
526
- Speed Options:
527
- - `slowest`, `slow`, `normal`, `fast`, `fastest`
528
- - Float values between -1.0 and 1.0, where -1.0 is the slowest speed and 1.0 is the fastest speed.
529
-
530
- Emotion Options:
531
- Use a list of tags in the format `emotion_name:level` where:
532
- - Emotion Names: `anger`, `positivity`, `surprise`, `sadness`, `curiosity`
533
- - Levels: `lowest`, `low`, (omit for medium level), `high`, `highest`
534
- The emotion tag levels add the specified emotion to the voice at the indicated intensity, with the omission of a level tag resulting in a medium intensity.
535
-
536
- ```python
537
- ws.send(
538
- model_id=model_id,
539
- transcript=transcript,
540
- voice_id=voice_id,
541
- output_format=output_format,
542
- _experimental_voice_controls={"speed": "fast", "emotion": ["positivity:high"]},
543
- )
544
- ```
545
-
546
- ### Jupyter Notebook Usage
547
-
548
- If you are using Jupyter Notebook or JupyterLab, you can use IPython.display.Audio to play the generated audio directly in the notebook.
549
- Additionally, in these notebook examples we show how to use the client as a context manager (though this is not required).
550
-
551
- ```python
552
- from IPython.display import Audio
553
- import io
554
- import os
555
- import numpy as np
556
-
557
- from cartesia import Cartesia
558
-
559
- with Cartesia(api_key=os.environ.get("CARTESIA_API_KEY")) as client:
560
- output_format = {
561
- "container": "raw",
562
- "encoding": "pcm_f32le",
563
- "sample_rate": 8000,
564
- }
565
- rate = 8000
566
- voice_id = "a0e99841-438c-4a64-b679-ae501e7d6091"
567
- voice = client.voices.get(id=voice_id)
568
- transcript = "Hey there! Welcome to Cartesia"
569
-
570
- # Create a BytesIO object to store the audio data
571
- audio_data = io.BytesIO()
572
-
573
- # Generate and stream audio
574
- for output in client.tts.sse(
575
- model_id="sonic-english",
576
- transcript=transcript,
577
- voice_embedding=voice["embedding"],
578
- stream=True,
579
- output_format=output_format,
580
- ):
581
- buffer = output["audio"]
582
- audio_data.write(buffer)
583
-
584
- # Set the cursor position to the beginning of the BytesIO object
585
- audio_data.seek(0)
586
-
587
- # Create an Audio object from the BytesIO data
588
- audio = Audio(np.frombuffer(audio_data.read(), dtype=np.float32), rate=rate)
589
-
590
- # Display the Audio object
591
- display(audio)
592
- ```
593
-
594
- Below is the same example using the async client:
595
-
596
- ```python
597
- from IPython.display import Audio
598
- import io
599
- import os
600
- import numpy as np
601
-
602
- from cartesia import AsyncCartesia
603
-
604
- async with AsyncCartesia(api_key=os.environ.get("CARTESIA_API_KEY")) as client:
605
- output_format = {
606
- "container": "raw",
607
- "encoding": "pcm_f32le",
608
- "sample_rate": 8000,
609
- }
610
- rate = 8000
611
- voice_id = "248be419-c632-4f23-adf1-5324ed7dbf1d"
612
- transcript = "Hey there! Welcome to Cartesia"
613
-
614
- # Create a BytesIO object to store the audio data
615
- audio_data = io.BytesIO()
616
-
617
- # Generate and stream audio
618
- async for output in client.tts.sse(
619
- model_id="sonic-english",
620
- transcript=transcript,
621
- voice_id=voice_id,
622
- stream=True,
623
- output_format=output_format,
624
- ):
625
- buffer = output["audio"]
626
- audio_data.write(buffer)
627
-
628
- # Set the cursor position to the beginning of the BytesIO object
629
- audio_data.seek(0)
630
-
631
- # Create an Audio object from the BytesIO data
632
- audio = Audio(np.frombuffer(audio_data.read(), dtype=np.float32), rate=rate)
633
-
634
- # Display the Audio object
635
- display(audio)
636
- ```
637
-
638
- ### Utility methods
639
-
640
- #### Output Formats
641
-
642
- You can use the `client.tts.get_output_format` method to convert string-based output format names into the `output_format` dictionary which is expected by the `output_format` parameter. You can see the `OutputFormatMapping` class in `cartesia._types` for the currently supported output format names. You can also view the currently supported `output_format`s in our [API Reference](https://docs.cartesia.ai/reference/api-reference/rest/stream-speech-server-sent-events).
643
-
644
- ```python
645
- # Get the output format dictionary from string name
646
- output_format = client.tts.get_output_format("raw_pcm_f32le_44100")
647
-
648
- # Pass in the output format dictionary to generate and stream audio
649
- generator = client.tts.sse(
650
- model_id=model,
651
- transcript=transcript,
652
- voice_id=SAMPLE_VOICE_ID,
653
- stream=True,
654
- output_format=output_format,
655
- )
656
- ```
657
-
658
- To avoid storing your API key in the source code, we recommend doing one of the following:
659
-
660
- 1. Use [`python-dotenv`](https://pypi.org/project/python-dotenv/) to add `CARTESIA_API_KEY="my-api-key"` to your .env file.
661
- 1. Set the `CARTESIA_API_KEY` environment variable, preferably to a secure shell init file (e.g. `~/.zshrc`, `~/.bashrc`)
@@ -1,23 +0,0 @@
1
- cartesia/__init__.py,sha256=rS7jIg4iqT0VgnwjzYK25JXxnF5hjZGE_-PGynAqHFo,126
2
- cartesia/_async_sse.py,sha256=76oIvstzVcWZCbcD8Ps419k1FEHF6lOB5qoHwawvj9k,3327
3
- cartesia/_async_websocket.py,sha256=y9YL9fU8eLENZZECJUwRBVTfEx4ZMl96Y5zHaRY2BiI,14787
4
- cartesia/_constants.py,sha256=khGNVpiQVDmv1oZU7pKTd9C1AHjiaM8zQ2He9d5zI_c,435
5
- cartesia/_logger.py,sha256=vU7QiGSy_AJuJFmClUocqIJ-Ltku_8C24ZU8L6fLJR0,53
6
- cartesia/_sse.py,sha256=CugabGUAUM-N2BruxNFxDB20HyxDlRdbN-J_yAzvBMY,5667
7
- cartesia/_types.py,sha256=gixQbKbX-H8xbD7jxHmc02KXLyjEaup19lh_57_YBl8,2570
8
- cartesia/_websocket.py,sha256=nRCq9xB0T9yYHoLqtn0GsJmcap-OAlJdSIrzTl40qMI,14875
9
- cartesia/async_client.py,sha256=y_K_Yuv0weA4k9ZYD0M9bNM3x3frsq07tqkg7R9h0-o,2714
10
- cartesia/async_tts.py,sha256=IbWVRKklNClXASR6ylHaukcMRR304LUguqc4yMopbDU,2076
11
- cartesia/client.py,sha256=OS1ORUSlR8Jg-em1imeTAFfwkC85AQFnw8PYtTdUuC8,2364
12
- cartesia/resource.py,sha256=wpnB3IPcTdxYSp0vxSkpntp4NSvqvnwUWF-0ZpgWV9o,1585
13
- cartesia/tts.py,sha256=kWvqce9K3gZ4QrWD-ciYdK29n49SNkxhd2A7ueTOwMY,4878
14
- cartesia/version.py,sha256=-ypEJktJToAL9by62JJKWEzDo_KPCQtmE5kwFgX24z4,22
15
- cartesia/voices.py,sha256=bDYbs0KoikAROJlmbnLdo4TrW0YwzjMvp70uKG6Alp0,7180
16
- cartesia/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
17
- cartesia/utils/deprecated.py,sha256=2cXvGtrxhPeUZA5LWy2n_U5OFLDv7SHeFtzqhjSJGyk,1674
18
- cartesia/utils/retry.py,sha256=O6fyVWpH9Su8c0Fwupl57xMt6JrwJ52txBwP3faUL7k,3339
19
- cartesia/utils/tts.py,sha256=TbvBZqHR6LxPim6s5RyGiURi4hIfqWt3KUk5QYOOhfc,2177
20
- cartesia-1.3.1.dist-info/METADATA,sha256=EZPPEiwa164rpgnLs4YERYGj47lXKYX1X8bKsuKQ1nc,20976
21
- cartesia-1.3.1.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
22
- cartesia-1.3.1.dist-info/licenses/LICENSE.md,sha256=PT2YG5wEtEX1TNDn5sXkUXqbn-neyr7cZenTxd40ql4,1074
23
- cartesia-1.3.1.dist-info/RECORD,,
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2024 Cartesia AI, Inc.
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
File without changes