Skip to content

Commit

Permalink
(chore): update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fern-bot committed Mar 12, 2024
1 parent d29dc6f commit 80ced27
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 137 deletions.
48 changes: 24 additions & 24 deletions src/elevenlabs/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
from typing import Iterator, Optional, Union, \
Optional, AsyncIterator

from elevenlabs.types.model_response import ModelResponse

from .base_client import \
BaseElevenLabs, AsyncBaseElevenLabs
from .core import RequestOptions, ApiError
from .types import VoiceResponse, VoiceSettings, \
PronunciationDictionaryVersionLocator
from .types import Voice, VoiceSettings, \
PronunciationDictionaryVersionLocator, Model


DEFAULT_VOICE = VoiceResponse(
DEFAULT_VOICE = Voice(
voice_id="EXAVITQu4vr4xnSDxMaL",
name="Rachel",
settings=VoiceSettings(
Expand Down Expand Up @@ -66,9 +64,9 @@ def clone(
name: str,
files: typing.List[str],
description: str,
labels: str,
labels: typing.Optional[str] = OMIT,
request_options: typing.Optional[RequestOptions] = None
) -> VoiceResponse:
) -> Voice:
"""
This is a manually maintained helper function that clones a voice from a set of audio files.
**NOTE**: This function is a helper function and is simply making
Expand Down Expand Up @@ -100,9 +98,9 @@ def generate(
self,
*,
text: Union[str, Iterator[str]],
voice: Union[VoiceId, VoiceName, VoiceResponse] = DEFAULT_VOICE,
voice: Union[VoiceId, VoiceName, Voice] = DEFAULT_VOICE,
voice_settings: typing.Optional[VoiceSettings] = DEFAULT_VOICE.settings,
model: Union[ModelId, ModelResponse] = "eleven_monolingual_v1",
model: Union[ModelId, Model] = "eleven_monolingual_v1",
optimize_streaming_latency: typing.Optional[int] = 0,
stream: bool = False,
output_format: Optional[str] = "mp3_44100_128",
Expand Down Expand Up @@ -161,22 +159,23 @@ def generate(
voice_id = next((v.voice_id for v in voices_response.voices if v.name == voice), None)
if not voice_id:
raise ApiError(body=f"Voice {voice} not found.")
elif isinstance(voice, VoiceResponse):
elif isinstance(voice, Voice):
voice_id = voice.voice_id
if voice_settings != DEFAULT_VOICE.settings:
if voice_settings != DEFAULT_VOICE.settings \
and voice.settings is not None:
voice = voice.settings
else:
voice_id = DEFAULT_VOICE.voice_id

if isinstance(model, str):
model_id = model
elif isinstance(model, ModelResponse):
elif isinstance(model, Model):
model_id = model.model_id


if stream:
if not stream:
if isinstance(text, str):
return self.text_to_speech.convert_as_stream(
return self.text_to_speech.convert(
voice_id=voice_id,
voice_settings=voice_settings,
optimize_streaming_latency=optimize_streaming_latency,
Expand All @@ -187,7 +186,7 @@ def generate(
model_id=model_id
)
elif isinstance(text, Iterator):
return self.text_to_speech.convert_as_stream_input(
return self.text_to_speech.convert_as_stream(
voice_id=voice_id,
model_id=model_id,
voice_settings=voice_settings,
Expand Down Expand Up @@ -245,7 +244,7 @@ async def clone(
description: str,
labels: str,
request_options: typing.Optional[RequestOptions] = None
) -> VoiceResponse:
) -> Voice:
"""
This is a manually mnaintained helper function that generates a
voice from provided text.
Expand Down Expand Up @@ -280,9 +279,9 @@ async def generate(
self,
*,
text: Union[str, Iterator[str]],
voice: Union[VoiceId, VoiceName, VoiceResponse] = DEFAULT_VOICE,
voice: Union[VoiceId, VoiceName, Voice] = DEFAULT_VOICE,
voice_settings: typing.Optional[VoiceSettings] = DEFAULT_VOICE.settings,
model: Union[ModelId, ModelResponse] = "eleven_monolingual_v1",
model: Union[ModelId, Model] = "eleven_monolingual_v1",
optimize_streaming_latency: typing.Optional[int] = 0,
stream: bool = False,
output_format: Optional[str] = "mp3_44100_128",
Expand Down Expand Up @@ -348,21 +347,22 @@ async def generate(
voice_id = next((v.voice_id for v in voices_response.voices if v.name == voice), None)
if not voice_id:
raise ApiError(body=f"Voice {voice} not found.")
elif isinstance(voice, VoiceResponse):
elif isinstance(voice, Voice):
voice_id = voice.voice_id
if voice_settings != DEFAULT_VOICE.settings:
if voice_settings != DEFAULT_VOICE.settings \
and voice.settings is not None:
voice = voice.settings
else:
voice_id = DEFAULT_VOICE.voice_id

if isinstance(model, str):
model_id = model
elif isinstance(model, ModelResponse):
elif isinstance(model, Model):
model_id = model.model_id

if stream:
if not stream:
if isinstance(text, str):
return await self.text_to_speech.convert_as_stream(
return await self.text_to_speech.convert(
voice_id=voice_id,
model_id=model_id,
voice_settings=voice_settings,
Expand All @@ -373,7 +373,7 @@ async def generate(
pronunciation_dictionary_locators=pronunciation_dictionary_locators
)
elif isinstance(text, Iterator):
return await self.text_to_speech.convert_as_stream_input(
return await self.text_to_speech.convert_as_stream(
voice_id=voice_id,
model_id=model_id,
voice_settings=voice_settings,
Expand Down
10 changes: 8 additions & 2 deletions src/elevenlabs/play.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import shutil
import subprocess
from typing import Iterator
from typing import Iterator, Union


def is_installed(lib_name: str) -> bool:
Expand All @@ -10,7 +10,13 @@ def is_installed(lib_name: str) -> bool:
return True


def play(audio: bytes, notebook: bool = False, use_ffmpeg: bool = True) -> None:
def play(
audio: Union[bytes, Iterator[bytes]],
notebook: bool = False,
use_ffmpeg: bool = True
) -> None:
if isinstance(audio, Iterator):
audio = b"".join(audio)
if notebook:
from IPython.display import Audio, display # type: ignore

Expand Down
40 changes: 14 additions & 26 deletions tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import pytest

from elevenlabs import generate, voices, Voice, VoiceSettings, play, stream
from .utils import IN_GITHUB
from elevenlabs import play, \
Voice, VoiceSettings, stream
from .utils import IN_GITHUB, client


def test_voices() -> None:
print("Voices are...", voices())
print("Voices are...", client.voices.get_all())


def test_generate() -> None:
audio = generate(
audio = client.generate(
text="Hello! My name is Bella.",
voice=Voice(
voice_id='EXAVITQu4vr4xnSDxMaL',
settings=VoiceSettings(stability=0.71, similarity_boost=0.5, style=0.0, use_speaker_boost=True)
)
)
settings=VoiceSettings(
stability=0.71,
similarity_boost=0.5,
style=0.0,
use_speaker_boost=True
)
))
if not IN_GITHUB:
play(audio) # type: ignore

Expand All @@ -25,27 +28,12 @@ def text_stream():
yield "Hi there, I'm Eleven "
yield "I'm a text to speech API "

audio_stream = generate(
audio_stream = client.generate(
text=text_stream(),
voice="Nicole",
model="eleven_monolingual_v1",
stream=True
)

if not IN_GITHUB:
stream(audio_stream) # type: ignore


def test_generate_with_settings() -> None:
from elevenlabs import Voice, VoiceSettings, generate

audio = generate(
text="Hello! My name is Bella.",
voice=Voice(
voice_id='EXAVITQu4vr4xnSDxMaL',
settings=VoiceSettings(stability=0.71, similarity_boost=0.5, style=0.0, use_speaker_boost=True)
)
)

if not IN_GITHUB:
play(audio) # type: ignore
stream(audio_stream) # type: ignore
32 changes: 13 additions & 19 deletions tests/test_history.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
from pydoc import cli
from .utils import IN_GITHUB
import time
from random import randint

from elevenlabs import History, \
play

from .utils import IN_GITHUB, client

def test_history():
from elevenlabs import History
from elevenlabs.client import ElevenLabs

client = ElevenLabs()

def test_history():
page_size = 5
# Test that we can get history
history = client.history.get_all(page_size=page_size)
assert isinstance(history, History)


def test_history_item_delete():
import time
from random import randint

from elevenlabs import generate, play
from elevenlabs.client import ElevenLabs

# Random text
text = f"Test {randint(0, 1000)}"
audio = generate(text=text) # Generate a history item to delete
audio = client.generate(text=text)
if not IN_GITHUB:
play(audio)
play(audio) # type: ignore

time.sleep(1)

client = ElevenLabs()
history = client.history.get_all().history
print(history)
history_item = history[0]

# Check that item matches
assert history_item.text == text
client.history.delete(history_item.history_item_id)

# Test that the history item was deleted
history = client.history.get_all(page_size=1).history
assert len(history) == 0 or history[0].text != text
assert len(history) == 0 or history[0].text != text
8 changes: 2 additions & 6 deletions tests/test_model.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
import pytest
from elevenlabs import Model
from .utils import client


def test_model():
from elevenlabs import Model
from elevenlabs.client import ElevenLabs

client = ElevenLabs()

# Test that we can get all models
models = client.models.get_all()
print(models)
Expand Down
44 changes: 12 additions & 32 deletions tests/test_voice.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
from elevenlabs.client import ElevenLabs
from elevenlabs.types.voice_response import VoiceResponse
from .utils import IN_GITHUB
from elevenlabs import Voice, \
VoiceSettings, play
from .utils import IN_GITHUB, as_local_files, client


def test_voice_from_id():
from elevenlabs import Voice, VoiceSettings
from elevenlabs.client import ElevenLabs

# Test that we can get a voice from id
voice_id = "21m00Tcm4TlvDq8ikWAM"

client = ElevenLabs()
voice = client.voices.get(voice_id)
assert isinstance(voice, VoiceResponse)
assert isinstance(voice, Voice)

assert voice.voice_id == voice_id
assert voice.name == "Rachel"
Expand All @@ -22,18 +19,12 @@ def test_voice_from_id():


def test_voice_clone():
from elevenlabs import Voice, clone, generate, play

from .utils import as_local_files

client = ElevenLabs()

voice_file_urls = [
"https://user-images.githubusercontent.com/12028621/235474694-584f7103-dab2-4c39-bb9a-8e5f00be85da.webm",
]

with as_local_files(voice_file_urls) as files:
voice = clone(
voice = client.clone(
name="Alex",
description=(
"An old American male voice with a slight hoarseness in his throat."
Expand All @@ -48,7 +39,7 @@ def test_voice_clone():
assert voice.category == "cloned"
assert len(voice.samples) == len(voice_file_urls)

audio = generate(
audio = client.generate(
text="Voice clone test successful.",
voice=voice,
)
Expand All @@ -61,40 +52,29 @@ def test_voice_clone():


def test_voice_design():
from elevenlabs import Accent, Age, Gender, Voice, generate, play
from elevenlabs.client import ElevenLabs

client = ElevenLabs()

audio = client.voices.design(
name="Lexa",
audio = client.voice_generation.generate(
text=(
"Hi! My name is Lexa, I'm a voice design test. I should have a middle aged"
" female voice with a british accent. "
),
voice_description="Middle aged female with british accent.",
gender=Gender.FEMALE,
age=Age.MIDDLE_AGED,
accent=Accent.BRITISH,
gender="female",
age="middle_aged",
accent="british",
accent_strength=1.5,
)

assert isinstance(audio, bytes) and len(audio) > 0

if not IN_GITHUB:
play(audio)



def test_voices():
from elevenlabs import voices, Voice

# Test that we can get voices from api
eleven_voices = voices()
eleven_voices = client.voices.get_all()

assert len(eleven_voices) > 0
assert isinstance(eleven_voices[0], Voice)

for voice in eleven_voices:
assert isinstance(voice, Voice)

Loading

0 comments on commit 80ced27

Please sign in to comment.