Skip to main content

Python

Learn how to build your own custom voice using a dataset through the Resemble AI platform with Python.

In order to best use this guide ensure that you have:

  • Signed up for a Resemble.ai account and confirmed your e-mail
  • Obtained your API key for use with the API - for additional information see the Authentication page
  • Have a subscription to at least a PRO on the Resemble platform.

Guide

First, you will need to initialize your Python project, it’s virtual environment.

# Create virtual environment
python3 -m venv venv

# Activate virtual environment
source venv/bin/activate

With your project setup, we can install the Resemble Python SDK via pip install and freeze our dependency list for later use:

# Install Resemble python SDK
pip install resemble

# Freeze dependencies
pip freeze > requirements.txt

Create a new main.py file which will be used to house our voice cloning example.

We will initialize the script by creating a basic run_main function responsible for running our process, fill in your main.py with the code snippet below.

# resemble-clone-voice/main.py

# This is the main function that contains the example
def run_main():
# TODO: Fill me in!

if __name__ == '__main__':
run_main()

With our basic backbone in place, import the Resemble class from the resemble sdk, the base64 module, and os module from Python’s standard library. Next we can create a setup function to initialize the API client called initialize_resemble_client:

# resemble-clone-voice/main.py
import os
import base64
from resemble import Resemble

# This function sets up the Resemble Python SDK
def initialize_resemble_client():
try:
# Attempt to retrieve the value of the environment variable
resemble_api_key = os.environ['RESEMBLE_API_KEY']

Resemble.api_key(resemble_api_key)
except KeyError:
# If the environment variable is not found, raise an error
raise EnvironmentError(f"The 'RESEMBLE_API_KEY' environment variable is not set.")

# This is the main function that contains the example
def run_main():
# TODO: Fill me in!

# -- snipped --

Notice that the Python script expects the environment variable RESEMBLE_API_KEY to be present containing the necessary API key.

With our client initialized we can update the run_main function.

# resemble-clone-voice/main.py

# -- snipped --

# This is the main function that contains the example
def run_main():
initialize_resemble_client()

Lets now build the create_voice function responsible for cloning a voice. Add the following code snippet below to your resemble-clone-voice/main.py file.

# resemble-clone-voice/main.py
import base64
import os
from resemble import Resemble

def create_voice():
voice_name = 'My Voice Clone'

dataset = 'FIXME: add your dataset URL here!'

base64_consent =''

print(f"Submitting request to Resemble to create a voice: {voice_name}")

# In order to clone a voice, you MUST provide a base64 encoded consent file
#
# https://docs.app.resemble.ai/docs/create_voices/resource_voice/create#voice-consent
#
# FIXME: You will need update this function to the path to your consent file
with open('FIXME: path/to/consent/file', 'rb') as file:
file_contents = file.read()

# Encode the file contents as Base64
base64_consent = base64.b64encode(file_contents).decode('utf-8')

# Make request to the API, note that we do not provide a callback_uri so this
# will request will execute synchronously.
response = Resemble.v2.voices.create(voice_name, dataset_url=dataset, consent=base64_consent)

voice = response['item']

if response['success']:
voice = response['item']
voice_status = voice['status']
voice_uuid = voice['uuid']

print(f"Response was successful! {voice_name} has been created with UUID {voice_uuid}. The voice is currently {voice_status}.")
else:
print("Response was unsuccessful!")

# In case of an error, print the error to STDOUT
print(response)

# This function sets up the Resemble Python SDK
def initialize_resemble_client():
# -- snipped --

# -- snipped --

There are two options for creating a Voice using the Resemble API:

  1. Providing a dataset URL; or
  2. Uploading individual recordings via the API

In this guide, we will use option #1: providing a dataset URL. If you’re interested in building a voice using option #2, you can see this guide.

In the above code snippet, the dataset ① must be set to a public accessible URL containing a supported Resemble dataset format. To avoid delays in Voice cloning, double check that your dataset URL is accessible via the curl package

curl -I https://url/to/your/dataset.zip

In commitment to Resemble's Ethical Statement all voice clone requests must be authorized via a consent file. You can read more about this in the Voice Consent section. In the above snippet, the code reads the provided consent file, encodes to base64 and provides in the request ②.

Once you have updated the dataset value, consent file path, and validated access update the run_main function:

# -- snipped -

# This is the main function that contains the example
def run_main():
# Initialize the client using the environment variable RESEMBLE_API_KEY set
initialize_resemble_client()
# Run the voice creation function to call the Resemble API
create_voice()

if __name__ == '__main__':
run_main()

Finally, test your script by running the same command via CLI as before:

# Be sure to prepend your API key to the command
RESEMBLE_API_KEY=... python3 main.py

# Output
Submitting request to Resemble to create a voice: My Voice Clone

{
'success': True,
'item': {
'uuid': 'abc1234',
'name': 'My Voice Clone',
'status': 'initializing',
# -- snipped --
}
}

Response was successful!

My Voice Clone has been created with UUID abc1234.

The voice is currently initializing.

Congratulations, your voice clone has begun building! To check the status of your voice, you can programmatically access via the API or use the web application as well.