Skip to content

Commit

Permalink
feat: add listable items, fix voices to return voices object
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioschneider committed Jun 1, 2023
1 parent 58006e2 commit b7ebd84
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 84 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
__pycache__
.mypy_cache
.pytest_cache
.DS_Store
TODO.md
NOTES.md
19 changes: 17 additions & 2 deletions elevenlabs/api/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import Optional
from typing import Optional, Sequence

import requests # type: ignore
from pydantic import BaseModel
Expand All @@ -12,7 +12,7 @@
UnauthenticatedRateLimitError,
)

api_base_url_v1 = "https://api.elevenlabs.io/v1"
api_base_url_v1 = os.environ.get("ELEVEN_BASE_URL", "https://api.elevenlabs.io/v1")


class API(BaseModel):
Expand Down Expand Up @@ -67,3 +67,18 @@ def post(url: str, *args, **kwargs):
@staticmethod
def delete(url: str, *args, **kwargs):
return API.request(url, method="delete", *args, **kwargs) # type: ignore


class Listable:
@property
def items(self) -> Sequence:
raise NotImplementedError

def __getitem__(self, idx: int):
return self.items[idx]

def __iter__(self):
return iter(self.items)

def __len__(self) -> int:
return len(self.items)
12 changes: 5 additions & 7 deletions elevenlabs/api/history.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from pydantic import root_validator

from .base import API, api_base_url_v1
from .base import API, Listable, api_base_url_v1
from .voice import VoiceSettings


Expand Down Expand Up @@ -59,7 +59,7 @@ def audio(self) -> bytes:
return self._audio


class History(API):
class History(Listable, API):
history: List[HistoryItem]

@classmethod
Expand All @@ -68,8 +68,6 @@ def from_api(cls) -> History:
response = API.get(url).json()
return cls(**response)

def __getitem__(self, idx: int) -> HistoryItem:
return self.history[idx]

def __iter__(self):
return iter(self.history)
@property
def items(self):
return self.history
12 changes: 5 additions & 7 deletions elevenlabs/api/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from typing import List, Optional

from .base import API, api_base_url_v1
from .base import API, Listable, api_base_url_v1


class Model(API):
Expand All @@ -12,7 +12,7 @@ class Model(API):
description: Optional[str]


class Models(API):
class Models(Listable, API):
models: List[Model]

@classmethod
Expand All @@ -21,8 +21,6 @@ def from_api(cls) -> Models:
response = cls.get(url).json()
return cls(models=response)

def __getitem__(self, idx: int) -> Model:
return self.models[idx]

def __iter__(self):
return iter(self.models)
@property
def items(self):
return self.models
12 changes: 5 additions & 7 deletions elevenlabs/api/voice.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pydantic import Field, root_validator, validator

from .base import API, api_base_url_v1
from .base import API, Listable, api_base_url_v1
from .error import APIError


Expand Down Expand Up @@ -138,7 +138,7 @@ def delete(self):
API.delete(f"{api_base_url_v1}/voices/{self.voice_id}")


class Voices(API):
class Voices(Listable, API):
voices: List[Voice]

@classmethod
Expand All @@ -150,8 +150,6 @@ def from_api(cls, api_key: Optional[str] = None):
def add_clone(self, voice_clone: VoiceClone) -> Voice:
pass

def __getitem__(self, idx: int) -> Voice:
return self.voices[idx]

def __iter__(self):
return iter(self.voices)
@property
def items(self):
return self.voices
122 changes: 62 additions & 60 deletions elevenlabs/simple.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import re
from typing import Iterator, List, Optional, Union
from typing import Iterator, Optional, Union

from .api import TTS, Model, Voice, VoiceClone, Voices, VoiceSettings

Expand All @@ -14,65 +14,67 @@ def get_api_key() -> Optional[str]:


# Save default voices to avoid querying the API for unathorized users
VOICES_CACHE = [
Voice(
voice_id="21m00Tcm4TlvDq8ikWAM",
name="Rachel",
category="premade",
settings=VoiceSettings(stability=0.75, similarity_boost=0.75),
),
Voice(
voice_id="AZnzlk1XvdvUeBnXmlld",
name="Domi",
category="premade",
settings=VoiceSettings(stability=0.1, similarity_boost=0.75),
),
Voice(
voice_id="EXAVITQu4vr4xnSDxMaL",
name="Bella",
category="premade",
settings=VoiceSettings(stability=0.245, similarity_boost=0.75),
),
Voice(
voice_id="ErXwobaYiN019PkySvjV",
name="Antoni",
category="premade",
settings=VoiceSettings(stability=0.195, similarity_boost=0.75),
),
Voice(
voice_id="MF3mGyEYCl7XYWbV9V6O",
name="Elli",
category="premade",
settings=VoiceSettings(stability=0.755, similarity_boost=0.75),
),
Voice(
voice_id="TxGEqnHWrfWFTfGW9XjX",
name="Josh",
category="premade",
settings=VoiceSettings(stability=0.15, similarity_boost=0.51),
),
Voice(
voice_id="VR6AewLTigWG4xSOukaG",
name="Arnold",
category="premade",
settings=VoiceSettings(stability=0.15, similarity_boost=0.75),
),
Voice(
voice_id="pNInz6obpgDQGcFmaJgB",
name="Adam",
category="premade",
settings=VoiceSettings(stability=0.2, similarity_boost=0.75),
),
Voice(
voice_id="yoZ06aMxZJJ28mfd3POQ",
name="Sam",
category="premade",
settings=VoiceSettings(stability=0.25, similarity_boost=0.75),
),
]


def voices() -> List[Voice]:
VOICES_CACHE = Voices(
voices=[
Voice(
voice_id="21m00Tcm4TlvDq8ikWAM",
name="Rachel",
category="premade",
settings=VoiceSettings(stability=0.75, similarity_boost=0.75),
),
Voice(
voice_id="AZnzlk1XvdvUeBnXmlld",
name="Domi",
category="premade",
settings=VoiceSettings(stability=0.1, similarity_boost=0.75),
),
Voice(
voice_id="EXAVITQu4vr4xnSDxMaL",
name="Bella",
category="premade",
settings=VoiceSettings(stability=0.245, similarity_boost=0.75),
),
Voice(
voice_id="ErXwobaYiN019PkySvjV",
name="Antoni",
category="premade",
settings=VoiceSettings(stability=0.195, similarity_boost=0.75),
),
Voice(
voice_id="MF3mGyEYCl7XYWbV9V6O",
name="Elli",
category="premade",
settings=VoiceSettings(stability=0.755, similarity_boost=0.75),
),
Voice(
voice_id="TxGEqnHWrfWFTfGW9XjX",
name="Josh",
category="premade",
settings=VoiceSettings(stability=0.15, similarity_boost=0.51),
),
Voice(
voice_id="VR6AewLTigWG4xSOukaG",
name="Arnold",
category="premade",
settings=VoiceSettings(stability=0.15, similarity_boost=0.75),
),
Voice(
voice_id="pNInz6obpgDQGcFmaJgB",
name="Adam",
category="premade",
settings=VoiceSettings(stability=0.2, similarity_boost=0.75),
),
Voice(
voice_id="yoZ06aMxZJJ28mfd3POQ",
name="Sam",
category="premade",
settings=VoiceSettings(stability=0.25, similarity_boost=0.75),
),
]
)


def voices() -> Voices:
"""Lists all voices in the API, if authenticated for the current user"""
api_key = get_api_key()
global VOICES_CACHE
Expand Down

0 comments on commit b7ebd84

Please sign in to comment.