Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pronunciation dictionary examples #12

Merged
merged 19 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/pronunciation-dictionaries/python/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ELEVENLABS_API_KEY=
16 changes: 16 additions & 0 deletions examples/pronunciation-dictionaries/python/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>
144 changes: 144 additions & 0 deletions examples/pronunciation-dictionaries/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
import os

from dotenv import load_dotenv
from elevenlabs import (
PronunciationDictionaryRule_Phoneme,
PronunciationDictionaryVersionLocator,
play,
)
from elevenlabs.client import ElevenLabs

load_dotenv()

ELEVENLABS_API_KEY = os.getenv("ELEVENLABS_API_KEY")

if not ELEVENLABS_API_KEY:
raise ValueError("Missing ELEVENLABS_API_KEY")


def print_rules(client: ElevenLabs, dictionary_id: str, version_id: str):
rules = client.pronunciation_dictionary.get_pls_file_with_a_pronunciation_dictionary_version_rules(
dictionary_id=dictionary_id,
version_id=version_id,
)
print("rules", rules)


def main():
if ELEVENLABS_API_KEY is None:
print("Missing API KEY")
return

client = ElevenLabs(api_key=ELEVENLABS_API_KEY)

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"
)

dictionary = client.pronunciation_dictionary.get(
pronunciation_dictionary_id=pronunciation_dictionary.id
)
print("dictionary name", dictionary.name)

print("-- initial rules --")
print_rules(
client, pronunciation_dictionary.id, pronunciation_dictionary.version_id
)

audio_1 = client.generate(
text="Without the dictionary: tomato",
voice="Rachel",
model="eleven_turbo_v2",
)

audio_2 = client.generate(
text="With the dictionary: tomato",
voice="Rachel",
model="eleven_turbo_v2",
pronunciation_dictionary_locators=[
PronunciationDictionaryVersionLocator(
pronunciation_dictionary_id=pronunciation_dictionary.id,
version_id=pronunciation_dictionary.version_id,
)
],
)

pronunciation_dictionary_rules_removed = (
client.pronunciation_dictionary.remove_rules_from_the_pronunciation_dictionary(
pronunciation_dictionary_id=pronunciation_dictionary.id,
rule_strings=["tomato", "Tomato"],
)
)

print("\n\n-- removed rule --\n\n")

print_rules(
client,
pronunciation_dictionary_rules_removed.id,
pronunciation_dictionary_rules_removed.version_id,
)

audio_3 = client.generate(
text="With the rule removed: tomato",
voice="Rachel",
model="eleven_turbo_v2",
pronunciation_dictionary_locators=[
PronunciationDictionaryVersionLocator(
pronunciation_dictionary_id=pronunciation_dictionary_rules_removed.id,
version_id=pronunciation_dictionary_rules_removed.version_id,
)
],
)

print(pronunciation_dictionary.id)

pronunciation_dictionary_rules_added = (
client.pronunciation_dictionary.add_rules_to_the_pronunciation_dictionary(
pronunciation_dictionary_id=pronunciation_dictionary_rules_removed.id,
rules=[
PronunciationDictionaryRule_Phoneme(
type="phoneme",
alphabet="ipa",
string_to_replace="tomato",
phoneme="/tə'meɪtoʊ/",
),
PronunciationDictionaryRule_Phoneme(
type="phoneme",
alphabet="ipa",
string_to_replace="Tomato",
phoneme="/tə'meɪtoʊ/",
),
],
)
)

print("\n\n-- added rule --\n\n")

print_rules(
client,
pronunciation_dictionary_rules_added.id,
pronunciation_dictionary_rules_added.version_id,
)

audio_4 = client.generate(
text="With the rule added again: tomato",
voice="Rachel",
model="eleven_turbo_v2",
pronunciation_dictionary_locators=[
PronunciationDictionaryVersionLocator(
pronunciation_dictionary_id=pronunciation_dictionary_rules_added.id,
version_id=pronunciation_dictionary_rules_added.version_id,
)
],
)

play(audio_1)
play(audio_2)
play(audio_3)
play(audio_4)


if __name__ == "__main__":
main()
Binary file not shown.
Loading