elevenlabs / elevenlabs-python

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

Direct voice pick using voice ID #142

Closed jujutsu764e closed 7 months ago

jujutsu764e commented 10 months ago

At the moment I grab all voices from API and then use the number in the list, like voice = voices[23]. But the problem is that when I update my voices lib, some of them can be replaced with new positions in the list. For example, I deleted some of them and when I tried to pick one which was the last one recently, I got an "out of list indexes" error. Is there any way to set a direct pick for voice in Python using it's voice id?

kgovind0001 commented 10 months ago

Lets say that you are interested in a particular voice. Then you can grab the id of the that particular voice and use it. I hope it helps.

uri = f"wss://api.elevenlabs.io/v1/text-to-speech/{voice_id}/stream-input?model_id=eleven_monolingual_v1"

from elevenlabs import voices
from elevenlabs import generate, stream
from elevenlabs import set_api_key
from elevenlabs import generate, stream
from dotenv import load_dotenv
import os
import requests

load_dotenv()
set_api_key(os.getenv("ELEVEN_API_KEY"))

def get_voice_setting(voice_id):

    url = f"https://api.elevenlabs.io/v1/voices/{voice_id}/settings"

    headers = {
    "Accept": "application/json",
    "xi-api-key": os.getenv("ELEVEN_API_KEY")
    }
    response = requests.get(url, headers=headers)

    return response.text

for voice in voices():
    print(100*"-")
    voice_setting = get_voice_setting(voice_id=voice.voice_id)
    print(voice_setting)
dsinghvi commented 7 months ago

@jujutsu764e @kgovind0001 ElevenLabs has just published a new v3 SDK that is automatically generated from its OpenAPI spec. As part of this SDK release, you can use the method client.voices.get_settings("your_voice_id"). More details here.