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.
Installation
pip install cartesia
# pip install in editable mode w/ dev dependencies
pip install -e '.[dev]'
Usage
from cartesia.tts import CartesiaTTS
import pyaudio
import os
client = CartesiaTTS(api_key=os.environ.get("CARTESIA_API_KEY"))
voices = client.get_voices()
voice = client.get_voice_embedding(voice_id=voices["Graham"]["id"])
transcript = "Hello! Welcome to Cartesia"
p = pyaudio.PyAudio()
stream = None
# Generate and stream audio
for output in client.generate(transcript=transcript, voice=voice, stream=True):
buffer = output["audio"]
rate = output["sampling_rate"]
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()
If you are using Jupyter Notebook or JupyterLab, you can use IPython.display.Audio to play the generated audio directly in the notebook. Here's an example:
from cartesia.tts import CartesiaTTS
from IPython.display import Audio
import io
import os
client = CartesiaTTS(api_key=os.environ.get("CARTESIA_API_KEY"))
voices = client.get_voices()
voice = client.get_voice_embedding(voice_id=voices["Graham"]["id"])
transcript = "Hello! Welcome to Cartesia"
# Create a BytesIO object to store the audio data
audio_data = io.BytesIO()
# Generate and stream audio
for output in client.generate(transcript=transcript, voice=voice, stream=True):
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(audio_data, rate=output["sampling_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
cartesia-0.0.4.tar.gz
(11.4 kB
view hashes)
Built Distribution
Close
Hashes for cartesia-0.0.4-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc5b3cde7e4087d0b77e6a897d7c8940c2f24849735b449da845e730aa41fa78 |
|
MD5 | f75e2f68cb2e0c839be309da6d15af11 |
|
BLAKE2b-256 | 13e02a8998eb128cc68efb7098d6ea3c0bcedbb938800e3ded48f72cbd2e6dd2 |