File size: 2,046 Bytes
604255a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import os 
from elevenlabs import ElevenLabs,Voice,VoiceSettings
from typing import Optional
from elevenlabs import play
from dotenv import load_dotenv

import io
load_dotenv()
print(os.getenv("ELEVENLABS_API_KEY"))
class TextToSpeech:
    REQUIRED_ENV_VARS=["ELEVENLABS_API_KEY","ELEVENLABS_VOICE_ID"]
    def __init__(self):
        """Initialize"""
        self._validate_env_vars()
        self._client: Optional[ElevenLabs] = None
    
    def _validate_env_vars(self) -> None:
        """validate that all the environment variables are set"""
        missing_vars=[var for var in self.REQUIRED_ENV_VARS if not os.getenv(var)]
        if missing_vars:
            raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")
        
    @property
    def client(self) -> Optional[ElevenLabs]:
        """Get or create a client instance"""
        if self._client is None:
            self._client = ElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY"))
        return self._client
    async def synthesize(self,text:str)->bytes:
        """Convert text to speech"""
        if not text.strip():
            raise ValueError("Input text cannot be empty")
        if len(text)>5000:
            raise ValueError("Input text cannot exceed 5000 characters")
        try:
            audio_generator =self.client.generate(
                text=text,
                voice=Voice(
                    voice_id=os.getenv("ELEVENLABS_VOICE_ID"),
                    settings=VoiceSettings(stability=0.5, similarity_boost=0.5),
                ),
                model=os.getenv("TTS_MODEL_NAME"),

            )
            audio_bytes = b"".join(audio_generator)
            return audio_bytes
        except Exception as e:
            print(f"Error synthesizing text: {str(e)}")
            return None
            
      
if __name__=="__main__":
    ts=TextToSpeech()
    import asyncio
    async def main():
        audio_buffer = await ts.synthesize("help the poort")
        play(audio_buffer)
    asyncio.run(main())