Skip to content

Commit

Permalink
Merge branch 'main' into update-websocket-examples
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelthomas5 committed May 7, 2024
2 parents 783a367 + 22dc216 commit 0afb621
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/audio-native/react/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## ElevenLabs React Audio Native

See the [ElevenLabs React Audio Native tutorial](https://elevenlabs.io/docs/audio-native/audio-native-react) for a walk through and details of how to setup ngrok
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>
146 changes: 146 additions & 0 deletions examples/pronunciation-dictionaries/python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
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)

model = "eleven_turbo_v2"

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=model,
)

audio_2 = client.generate(
text="With the dictionary: tomato",
voice="Rachel",
model=model,
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=model,
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=model,
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.

0 comments on commit 0afb621

Please sign in to comment.