Creates a new pronunciation dictionary from a lexicon .PLS file
POST
/
v1
/
pronunciation-dictionaries
/
add-from-file
xi-api-key
string
xi-api-key
string
Your API key. This is required by most endpoints to access our API programatically. You can view your xi-api-key using the 'Profile' tab on the website.
object
*
name
string
*
name
Required
string
The name of the pronunciation dictionary, used for identification only.
file
file
file
file
A lexicon .pls file which we will use to initialize the project with.
description
string
description
string
A description of the pronunciation dictionary, used for identification only.
workspace_access
enum<string>
workspace_access
enum<string>
Should be one of 'editor' or 'viewer'. If not provided, defaults to no access.
Here is some example code for uploading a pronunciation dictionary and printing the response pronunciation_dictionary_id and version_id. You’ll require these identifiers in the request body if you intend to use pronunciation_dictionary_locators.
All you will need to do is replace API_KEY_HERE with your actual API key and PATH_HERE with the actual path to the PLS file you want to upload.
If you need help in understanding how to properly format a PLS / pronunciation dictionary, please refer to the guide here.
import requestsimport os# Define your API key and the base URL for the Eleven Labs APIXI_API_KEY ="API_KEY_HERE"BASE_URL ="https://api.elevenlabs.io/v1"# Setup the headers for HTTP requests to include the API key and accept JSON responsesheaders ={"Accept":"application/json","xi-api-key": XI_API_KEY}defupload_pronunciation_dictionary(file_path, name, description):""" Uploads a pronunciation dictionary file to the Eleven Labs API and returns its ID and version ID. Parameters:- file_path: The local path to the pronunciation dictionary file.- name: A name for the pronunciation dictionary.- description: A description of the pronunciation dictionary. Returns: A tuple containing the pronunciation dictionary ID and version ID if successful,None otherwise."""# Construct the URL for adding a pronunciation dictionary from a file url =f"{BASE_URL}/pronunciation-dictionaries/add-from-file"# Prepare the file and data to be sent in the request files ={'file':open(file_path,'rb')} data ={'name': name,'description': description}# Make the POST request to upload the dictionary response = requests.post(url, headers=headers, files=files, data=data)# Handle the responseif response.status_code ==200:# Parse the response JSON to get the pronunciation dictionary and version IDs data = response.json() pronunciation_dictionary_id = data.get('id') version_id = data.get('version_id')# Return the IDsreturn pronunciation_dictionary_id, version_idelse:# Print an error message if the request failedprint("Error:", response.status_code)returnNone,Nonedefmain():""" The main function to upload a pronunciation dictionary."""# Define the path to your pronunciation dictionary file and its metadata file_path =r"PATH_HERE" name ="Your Pronunciation Dictionary" description ="My custom pronunciation dictionary"# Upload the pronunciation dictionary and receive its ID and version ID pronunciation_dictionary_id, version_id = upload_pronunciation_dictionary(file_path, name, description)# Check if the upload was successfulif pronunciation_dictionary_id and version_id:print("Pronunciation Dictionary Uploaded Successfully!")print("Pronunciation Dictionary ID:", pronunciation_dictionary_id)print("Version ID:", version_id)else:print("Failed to upload pronunciation dictionary.")# Ensure this script block runs only when executed as a script, not when importedif __name__ =="__main__": main()
Here is some example code on how to use these identifiers or locators in your text-to-speech call.
import requests# Set your API key and base URLXI_API_KEY ="API_KEY_HERE"BASE_URL ="https://api.elevenlabs.io/v1"VOICE_ID ="TxGEqnHWrfWFTfGW9XjX"# Headers for the requestheaders ={"Accept":"application/json","xi-api-key": XI_API_KEY}deftext_to_speech(text, pronunciation_dictionary_id, version_id):""" Sends a text to speech request using a pronunciation dictionary. Returns: An audio file."""# Define the URL for the text-to-speech endpoint url =f"{BASE_URL}/text-to-speech/{VOICE_ID}"# Payload for the request payload ={"model_id":"eleven_monolingual_v1","pronunciation_dictionary_locators":[{"pronunciation_dictionary_id": pronunciation_dictionary_id,"version_id": version_id}],"text": text,"voice_settings":{"stability":0.5,"similarity_boost":0.8,"style":0.0,"use_speaker_boost":True}}# Make the POST request response = requests.post(url, json=payload, headers=headers)# Check the response statusif response.status_code ==200:# Here you can save the audio response to a file if neededprint("Audio file generated successfully.")# Save the audio to a filewithopen("output_audio.mp3","wb")as audio_file: audio_file.write(response.content)else:print("Error:", response.status_code)defmain():# Example text and dictionary IDs (replace with actual values) text ="Hello, world! I can now use pronunciation dictionaries." pronunciation_dictionary_id ="PD_ID_HERE" version_id ="VERSION_ID_HERE"# Call the text to speech function text_to_speech(text, pronunciation_dictionary_id, version_id)if __name__ =="__main__": main()
Headers
xi-api-key
string
Your API key. This is required by most endpoints to access our API programatically. You can view your xi-api-key using the 'Profile' tab on the website.
Body
multipart/form-data
name
string
required
The name of the pronunciation dictionary, used for identification only.
file
file
A lexicon .pls file which we will use to initialize the project with.
description
string
A description of the pronunciation dictionary, used for identification only.
workspace_access
enum<string>
Should be one of 'editor' or 'viewer'. If not provided, defaults to no access.