zach commited on
Commit
681c05f
·
1 Parent(s): 136ff40

Update ElevenLabs integration to randmonly pick a voice from a predefined list of top voices

Browse files
Files changed (1) hide show
  1. src/integrations/elevenlabs_api.py +19 -4
src/integrations/elevenlabs_api.py CHANGED
@@ -21,6 +21,7 @@ Functions:
21
  # Standard Library Imports
22
  from dataclasses import dataclass
23
  import logging
 
24
  from typing import Optional
25
  # Third-Party Library Imports
26
  from elevenlabs import ElevenLabs
@@ -34,7 +35,8 @@ from src.utils import validate_env_var, truncate_text
34
  class ElevenLabsConfig:
35
  """Immutable configuration for interacting with the ElevenLabs TTS API."""
36
  api_key: str = validate_env_var("ELEVENLABS_API_KEY")
37
- voice_id: str = "pNInz6obpgDQGcFmaJgB" # Adam (popular ElevenLabs pre-made voice)
 
38
  model_id: str = "eleven_multilingual_v2" # ElevenLab's most emotionally expressive model
39
  output_format: str = "mp3_44100_128" # Output format of the generated audio.
40
 
@@ -42,12 +44,25 @@ class ElevenLabsConfig:
42
  # Validate that required attributes are set
43
  if not self.api_key:
44
  raise ValueError("ElevenLabs API key is not set.")
45
- if not self.voice_id:
46
- raise ValueError("ElevenLabs Voice ID is not set.")
47
  if not self.model_id:
48
  raise ValueError("ElevenLabs Model ID is not set.")
49
  if not self.output_format:
50
  raise ValueError("ElevenLabs Output Format is not set.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
 
52
  @property
53
  def client(self) -> ElevenLabs:
@@ -96,7 +111,7 @@ def text_to_speech_with_elevenlabs(text: str) -> bytes:
96
  # Generate audio using the ElevenLabs SDK
97
  audio_iterator = elevenlabs_config.client.text_to_speech.convert(
98
  text=text,
99
- voice_id=elevenlabs_config.voice_id,
100
  model_id=elevenlabs_config.model_id,
101
  output_format=elevenlabs_config.output_format,
102
  )
 
21
  # Standard Library Imports
22
  from dataclasses import dataclass
23
  import logging
24
+ import random
25
  from typing import Optional
26
  # Third-Party Library Imports
27
  from elevenlabs import ElevenLabs
 
35
  class ElevenLabsConfig:
36
  """Immutable configuration for interacting with the ElevenLabs TTS API."""
37
  api_key: str = validate_env_var("ELEVENLABS_API_KEY")
38
+ # voice_id: str = "pNInz6obpgDQGcFmaJgB" # Adam (popular ElevenLabs pre-made voice)
39
+ top_voices: list[str] = None # Predefined top default voices
40
  model_id: str = "eleven_multilingual_v2" # ElevenLab's most emotionally expressive model
41
  output_format: str = "mp3_44100_128" # Output format of the generated audio.
42
 
 
44
  # Validate that required attributes are set
45
  if not self.api_key:
46
  raise ValueError("ElevenLabs API key is not set.")
 
 
47
  if not self.model_id:
48
  raise ValueError("ElevenLabs Model ID is not set.")
49
  if not self.output_format:
50
  raise ValueError("ElevenLabs Output Format is not set.")
51
+ if not self.top_voices:
52
+ # Predefined top default voice IDs
53
+ object.__setattr__(self, "top_voices", [
54
+ "pNInz6obpgDQGcFmaJgB", # Adam
55
+ "ErXwobaYiN019PkySvjV", # Antoni
56
+ "21m00Tcm4TlvDq8ikWAM", # Rachel
57
+ "txTPZhQpfI89VbqtG6v7", # Matilda
58
+ ])
59
+
60
+ @property
61
+ def random_voice_id(self) -> str:
62
+ """
63
+ Randomly selects a voice ID from the top default voices, ensuring different voices across calls.
64
+ """
65
+ return random.choice(self.top_voices)
66
 
67
  @property
68
  def client(self) -> ElevenLabs:
 
111
  # Generate audio using the ElevenLabs SDK
112
  audio_iterator = elevenlabs_config.client.text_to_speech.convert(
113
  text=text,
114
+ voice_id=elevenlabs_config.random_voice_id, # Randomly chosen voice ID
115
  model_id=elevenlabs_config.model_id,
116
  output_format=elevenlabs_config.output_format,
117
  )