The official Python library for the Cartesia API.
Project description
Cartesia Python API Library
The official Cartesia Python library which provides convenient access to the Cartesia REST and Websocket API from any Python 3.8+ application.
Note: This API is still in alpha. Please expect breaking changes and report any issues you encounter.
[!IMPORTANT] The client library introduces breaking changes in v1.0.0, which was released on June 24th 2024. See the release notes here and reach out to us on Discord if you have any questions!
Documentation
Our complete API documentation can be found on docs.cartesia.ai.
Installation
pip install cartesia
# pip install in editable mode w/ dev dependencies
pip install -e '.[dev]'
Voices
from cartesia import Cartesia
import os
client = Cartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
# Get all available voices
voices = client.voices.list()
print(voices)
# Get a specific voice
voice = client.voices.get(id="a0e99841-438c-4a64-b679-ae501e7d6091")
print("The embedding for", voice["name"], "is", voice["embedding"])
# Clone a voice using filepath
cloned_voice_embedding = client.voices.clone(filepath="path/to/voice")
# Create a new voice
new_voice = client.voices.create(name="New Voice", description="A clone of my own voice", embedding=cloned_voice_embedding)
Text-to-Speech
Server-Sent Events (SSE)
from cartesia import Cartesia
import pyaudio
import os
client = Cartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
voice_name = "Barbershop Man"
voice_id = "a0e99841-438c-4a64-b679-ae501e7d6091"
voice = client.voices.get(id=voice_id)
transcript = "Hello! Welcome to Cartesia"
# You can check out our models at [docs.cartesia.ai](https://docs.cartesia.ai/getting-started/available-models).
model_id = "sonic-english"
# You can find the supported `output_format`s in our [API Reference](https://docs.cartesia.ai/api-reference/endpoints/stream-speech-server-sent-events).
output_format = {
"container": "raw",
"encoding": "pcm_f32le",
"sample_rate": 44100,
}
p = pyaudio.PyAudio()
rate = 44100
stream = None
# Generate and stream audio
for output in client.tts.sse(model_id=model_id, transcript=transcript, voice_embedding=voice["embedding"], stream=True, output_format=output_format):
buffer = output["audio"]
if not stream:
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=rate,
output=True)
# Write the audio data to the stream
stream.write(buffer)
stream.stop_stream()
stream.close()
p.terminate()
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:
from cartesia import AsyncCartesia
import asyncio
import pyaudio
import os
async def write_stream():
client = AsyncCartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
voice_name = "Barbershop Man"
voice_id = "a0e99841-438c-4a64-b679-ae501e7d6091"
voice = client.voices.get(id=voice_id)
transcript = "Hello! Welcome to Cartesia"
# You can check out our models at [docs.cartesia.ai](https://docs.cartesia.ai/getting-started/available-models).
model_id = "sonic-english"
# You can find the supported `output_format`s in our [API Reference](https://docs.cartesia.ai/api-reference/endpoints/stream-speech-server-sent-events).
output_format = {
"container": "raw",
"encoding": "pcm_f32le",
"sample_rate": 44100,
}
p = pyaudio.PyAudio()
rate = 44100
stream = None
# Generate and stream audio
async for output in await client.tts.sse(model_id=model_id, transcript=transcript, voice_embedding=voice["embedding"], stream=True, output_format=output_format
):
buffer = output["audio"]
if not stream:
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=rate,
output=True)
# Write the audio data to the stream
stream.write(buffer)
stream.stop_stream()
stream.close()
p.terminate()
asyncio.run(write_stream())
WebSocket
from cartesia import Cartesia
import pyaudio
import os
client = Cartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
voice_name = "Barbershop Man"
voice_id = "a0e99841-438c-4a64-b679-ae501e7d6091"
voice = client.voices.get(id=voice_id)
transcript = "Hello! Welcome to Cartesia"
# You can check out our models at [docs.cartesia.ai](https://docs.cartesia.ai/getting-started/available-models).
model_id = "sonic-english"
# You can find the supported `output_format`s in our [API Reference](https://docs.cartesia.ai/api-reference/endpoints/stream-speech-server-sent-events).
output_format = {
"container": "raw",
"encoding": "pcm_f32le",
"sample_rate": 22050,
}
p = pyaudio.PyAudio()
rate = 22050
stream = None
# Set up the websocket connection
ws = client.tts.websocket()
# Generate and stream audio using the websocket
for output in ws.send(model_id=model_id, transcript=transcript, voice_embedding=voice["embedding"], stream=True, output_format=output_format):
buffer = output["audio"]
if not stream:
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=rate,
output=True)
# Write the audio data to the stream
stream.write(buffer)
stream.stop_stream()
stream.close()
p.terminate()
ws.close() # Close the websocket connection
Multilingual Text-to-Speech [Alpha]
You can use our sonic-multilingual
model to generate audio in multiple languages. The languages supported are available at docs.cartesia.ai.
from cartesia import Cartesia
import pyaudio
import os
client = Cartesia(api_key=os.environ.get("CARTESIA_API_KEY"))
voice_name = "Barbershop Man"
voice_id = "a0e99841-438c-4a64-b679-ae501e7d6091"
voice = client.voices.get(id=voice_id)
transcript = "Hola! Bienvenido a Cartesia"
language = "es" # Language code corresponding to the language of the transcript
# Make sure you use the multilingual model! You can check out all models at [docs.cartesia.ai](https://docs.cartesia.ai/getting-started/available-models).
model_id = "sonic-multilingual"
# You can find the supported `output_format`s in our [API Reference](https://docs.cartesia.ai/api-reference/endpoints/stream-speech-server-sent-events).
output_format = {
"container": "raw",
"encoding": "pcm_f32le",
"sample_rate": 44100,
}
p = pyaudio.PyAudio()
rate = 44100
stream = None
# Pass in the corresponding language code to the `language` parameter to generate and stream audio.
for output in client.tts.sse(model_id=model_id, transcript=transcript, voice_embedding=voice["embedding"], stream=True, output_format=output_format, language=language):
buffer = output["audio"]
if not stream:
stream = p.open(format=pyaudio.paFloat32,
channels=1,
rate=rate,
output=True)
stream.write(buffer)
stream.stop_stream()
stream.close()
p.terminate()
If you are using Jupyter Notebook or JupyterLab, you can use IPython.display.Audio to play the generated audio directly in the notebook. Additionally, in these notebook examples we show how to use the client as a context manager (though this is not required).
from IPython.display import Audio
import io
import os
import numpy as np
from cartesia import Cartesia
with Cartesia(api_key=os.environ.get("CARTESIA_API_KEY")) as client:
output_format = {
"container": "raw",
"encoding": "pcm_f32le",
"sample_rate": 8000,
}
rate = 8000
voice_id = "a0e99841-438c-4a64-b679-ae501e7d6091"
voice = client.voices.get(id=voice_id)
transcript = "Hey there! Welcome to Cartesia"
# Create a BytesIO object to store the audio data
audio_data = io.BytesIO()
# Generate and stream audio
for output in client.tts.sse(model_id="sonic-english", transcript=transcript, voice_embedding=voice["embedding"], stream=True, output_format=output_format
):
buffer = output["audio"]
audio_data.write(buffer)
# Set the cursor position to the beginning of the BytesIO object
audio_data.seek(0)
# Create an Audio object from the BytesIO data
audio = Audio(np.frombuffer(audio_data.read(), dtype=np.float32), rate=rate)
# Display the Audio object
display(audio)
Below is the same example using the async client:
from IPython.display import Audio
import io
import os
import numpy as np
from cartesia import AsyncCartesia
async with AsyncCartesia(api_key=os.environ.get("CARTESIA_API_KEY")) as client:
output_format = {
"container": "raw",
"encoding": "pcm_f32le",
"sample_rate": 8000,
}
rate = 8000
voice_id = "248be419-c632-4f23-adf1-5324ed7dbf1d"
transcript = "Hey there! Welcome to Cartesia"
# Create a BytesIO object to store the audio data
audio_data = io.BytesIO()
# Generate and stream audio
async for output in client.tts.sse(model_id="sonic-english", transcript=transcript, voice_id=voice_id, stream=True, output_format=output_format
):
buffer = output["audio"]
audio_data.write(buffer)
# Set the cursor position to the beginning of the BytesIO object
audio_data.seek(0)
# Create an Audio object from the BytesIO data
audio = Audio(np.frombuffer(audio_data.read(), dtype=np.float32), rate=rate)
# Display the Audio object
display(audio)
To avoid storing your API key in the source code, we recommend doing one of the following:
- Use
python-dotenv
to addCARTESIA_API_KEY="my-api-key"
to your .env file. - Set the
CARTESIA_API_KEY
environment variable, preferably to a secure shell init file (e.g.~/.zshrc
,~/.bashrc
)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for cartesia-1.0.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d0620dfe4ed29a409f5994b5ec4ffd7d6957b3c2427924b3225a05a7c6aeef1e |
|
MD5 | 2a434c2865e96df193b5097d00d27d23 |
|
BLAKE2b-256 | 0625bcfc5b55cb9472ac161efbb24a49f5c0c7707795326ebb105e7ec9f10965 |