Skip to content

Commit

Permalink
(chore): test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
fern-bot committed Mar 12, 2024
1 parent 8af35d0 commit d398760
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 25 deletions.
7 changes: 4 additions & 3 deletions src/elevenlabs/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def clone(
name: str,
files: typing.List[str],
description: str,
labels: typing.Optional[str] = OMIT,
labels: typing.Optional[str] = None,
request_options: typing.Optional[RequestOptions] = None
) -> Voice:
"""
Expand Down Expand Up @@ -156,15 +156,16 @@ def generate(
voice_id = voice
elif isinstance(voice, str):
voices_response = self.voices.get_all(request_options=request_options)
print("voices_response", voices_response)
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, Voice):
voice_id = voice.voice_id
if voice_settings != DEFAULT_VOICE.settings \
and voice.settings is not None:
voice = voice.settings
else:
voice_settings = voice.settings
else:
voice_id = DEFAULT_VOICE.voice_id

if isinstance(model, str):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_history.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import time
from random import randint

from elevenlabs import History, \
from elevenlabs import GetSpeechHistoryResponse, \
play

from .utils import IN_GITHUB, client
Expand All @@ -10,7 +10,7 @@
def test_history():
page_size = 5
history = client.history.get_all(page_size=page_size)
assert isinstance(history, History)
assert isinstance(history, GetSpeechHistoryResponse)


def test_history_item_delete():
Expand Down
22 changes: 9 additions & 13 deletions tests/test_voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,32 +23,31 @@ def test_voice_clone():
"https://user-images.githubusercontent.com/12028621/235474694-584f7103-dab2-4c39-bb9a-8e5f00be85da.webm",
]

with as_local_files(voice_file_urls) as files:
for file in as_local_files(voice_file_urls):
voice = client.clone(
name="Alex",
description=(
"An old American male voice with a slight hoarseness in his throat."
" Perfect for news"
),
files=files,
files=[file],
)

assert isinstance(voice, Voice)
assert isinstance(voice, Voice) # type: ignore
assert voice.voice_id is not None
assert voice.name == "Alex"
assert voice.category == "cloned"
assert len(voice.samples) == len(voice_file_urls)
assert len(voice.samples or []) == len(voice_file_urls)

audio = client.generate(
text="Voice clone test successful.",
voice=voice,
)
assert isinstance(audio, bytes) and len(audio) > 0

client.voices.delete(voice.voice_id)

if not IN_GITHUB:
play(audio)

client.voices.delete(voice.voice_id)


def test_voice_design():
Expand All @@ -63,18 +62,15 @@ def test_voice_design():
accent_strength=1.5,
)

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

if not IN_GITHUB:
play(audio)


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

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

for voice in eleven_voices:
for voice in response.voices:
assert isinstance(voice, Voice)
12 changes: 5 additions & 7 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
from contextlib import contextmanager
import os
import tempfile
import requests
from typing import Sequence, Generator
from typing import Sequence, Generator, List

from elevenlabs.client import ElevenLabs

IN_GITHUB = "GITHUB_ACTIONS" in os.environ

client = ElevenLabs()

@contextmanager
def as_local_files(urls: Sequence[str]) -> Generator[list[str], None, None]:

def as_local_files(urls: Sequence[str]) -> Generator[str, None, None]:
"""Util to download files from urls and return local file paths"""
file_paths = []

temp_files = []
for url in urls:
response = requests.get(url)
temp_file = tempfile.NamedTemporaryFile()
temp_file.write(response.content)
file_paths.append(temp_file.name)
temp_files.append(temp_file)
yield file_paths
yield temp_file.name
# Remove the files
for temp_file in temp_files:
temp_file.close()

0 comments on commit d398760

Please sign in to comment.