elevenlabs / elevenlabs-python

The official Python API for ElevenLabs Text to Speech.
https://elevenlabs.io/docs/api-reference/getting-started
MIT License
2.19k stars 253 forks source link

Getting the list of voices is really slow because of Pydantic validation #77

Closed yunusdemir closed 1 year ago

yunusdemir commented 1 year ago

The observation

When a user is getting the list of voices, either via voices() in simple.py or Voices.from_api(), the load time gets longer the more voices users have in their accounts.

The findings

We currently have 19 voices in our account and the loading time of voices() is about 31 seconds. After investigating, this is caused by computed_settings method on the Voice class. The method does a request per Voice instance to the ElevenLabs servers to get the VoiceSettings because there are no settings given in the get voices route via the ElevenLabs API ("settings":null).

The problem lies in lines 133-136 of api/voice.py:

@validator("settings")
def computed_settings(cls, v: VoiceSettings, values) -> VoiceSettings:
    url = f"{api_base_url_v1}/voices/{values['voice_id']}/settings"
    return v if v else VoiceSettings(**API.get(url).json())

It returns VoiceSettings if given, but since there are no settings given in the API, they are not passed and the method will get them one by one via the API, which is slow.

Proposed solutions

tagawa commented 1 year ago

I get the same thing (about 11 seconds). Using the API directly (Curl) to get the voices list is fine.

flavioschneider commented 1 year ago

Hi @yunusdemir! This has been fixed in v0.2.25, the settings are not fetched by default anymore. You can call voice.fetch_settings() on the desired voice to individually fetch settings

yunusdemir commented 1 year ago

Awesome, thanks for the update @flavioschneider!