reoapae / src /services /texttospeech.py
puzan789's picture
Updated
604255a
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())