Download tts_handler.py from mgbam/rentbot: direct link, hf CLI and curl.
- Browser
- Download file 864 Bytes
-
https://huggingface.co/spaces/mgbam/rentbot/resolve/eae2044d6d7e5b0aea62e74480d60520c795f3b7/tts_handler.py
- Command line
-
hf download hf://spaces/mgbam/rentbot@eae2044d6d7e5b0aea62e74480d60520c795f3b7/tts_handler.py
-
curl -L -o tts_handler.py https://huggingface.co/spaces/mgbam/rentbot/resolve/eae2044d6d7e5b0aea62e74480d60520c795f3b7/tts_handler.py
864 Bytes
| # rentbot/tts_handler.py | |
| import os | |
| from elevenlabs.client import AsyncElevenLabs | |
| from elevenlabs import stream | |
| client = AsyncElevenLabs(api_key=os.getenv("ELEVENLABS_API_KEY")) | |
| async def text_to_speech_stream(text_iterator): | |
| """ | |
| Streams text to ElevenLabs and yields audio chunks. | |
| Twilio requires audio in 8-bit μ-law format. | |
| """ | |
| voice_id = os.getenv("ELEVENLABS_VOICE_ID", "21m00Tcm4TlvDq8ikWAM") | |
| try: | |
| audio_stream = await client.generate( | |
| model="eleven_turbo_v2", | |
| voice=voice_id, | |
| text=text_iterator, | |
| stream=True, | |
| output_format="ulaw_8000", # Critical for Twilio | |
| ) | |
| # Stream the audio chunks | |
| async for chunk in audio_stream: | |
| yield chunk | |
| except Exception as e: | |
| print(f"Error in TTS stream: {e}") |