Skip to content

Commit

Permalink
working example of projects with and without pronunciation dictionaries
Browse files Browse the repository at this point in the history
  • Loading branch information
lharries committed Apr 26, 2024
1 parent 6cf6350 commit 4e6831d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
16 changes: 16 additions & 0 deletions examples/projects/dictionary.pls
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<lexicon version="1.0"
xmlns="http://www.w3.org/2005/01/pronunciation-lexicon"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.w3.org/2005/01/pronunciation-lexicon
http://www.w3.org/TR/2007/CR-pronunciation-lexicon-20071212/pls.xsd"
alphabet="ipa" xml:lang="en-US">
<lexeme>
<grapheme>tomato</grapheme>
<phoneme>/tə'meɪtoʊ/</phoneme>
</lexeme>
<lexeme>
<grapheme>Tomato</grapheme>
<phoneme>/tə'meɪtoʊ/</phoneme>
</lexeme>
</lexicon>
1 change: 1 addition & 0 deletions examples/projects/example.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello this is an ElevenLabs text file example using the Projects API. And this is a tomato
73 changes: 73 additions & 0 deletions examples/projects/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import json
import os

from dotenv import load_dotenv

from elevenlabs.client import ElevenLabs

load_dotenv()

ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")

if ELEVENLABS_API_KEY is None:
raise Exception("Missing API KEY")


def main():
client = ElevenLabs(api_key=ELEVENLABS_API_KEY)

# Specific voice and model identifiers
voice_id = "21m00Tcm4TlvDq8ikWAM"
model_id = "eleven_turbo_v2"

# Text to save and the file name
text_content = "Hello this is an ElevenLabs text file example using the Projects API. And this is a tomato"
file_name = "example.txt"

# Save the string to a text file
with open(file_name, "w") as text_file:
text_file.write(text_content)

# Define the parameters for the new project
name = "Example Text File"

# Create the project
with open(file_name, "rb") as file:
project = client.projects.add(
name=name,
from_document=file,
default_title_voice_id=voice_id,
default_paragraph_voice_id=voice_id,
default_model_id=model_id,
)

with open("dictionary.pls", "rb") as f:
# this dictionary changes how tomato is pronounced
pronunciation_dictionary = client.pronunciation_dictionary.add_from_file(
file=f.read(), name="example"
)

# Create the project
with open(file_name, "rb") as file:
project = client.projects.add(
name=name,
from_document=file,
default_title_voice_id=voice_id,
default_paragraph_voice_id=voice_id,
default_model_id=model_id,
pronunciation_dictionary_locators=[
json.dumps(
{
"pronunciation_dictionary_id": pronunciation_dictionary.id,
"version_id": pronunciation_dictionary.version_id,
}
)
],
)

# Print the response
print(project)


if __name__ == "__main__":
main()

0 comments on commit 4e6831d

Please sign in to comment.