Download voice_tts.py from NeuMax/Jarvis: direct link, hf CLI and curl.
- Browser
- Download file 1.28 kB
-
https://huggingface.co/NeuMax/Jarvis/resolve/main/voice_tts.py
- Command line
-
hf download hf://NeuMax/Jarvis/voice_tts.py
-
curl -L -o voice_tts.py https://huggingface.co/NeuMax/Jarvis/resolve/main/voice_tts.py
1.28 kB
| # JARVIS Voice TTS — HuggingFace Edition | |
| # Uses edge-tts (Microsoft neural voices, works without a local GPU) | |
| # Falls back to plain text if edge-tts is unavailable. | |
| import asyncio | |
| import io | |
| import os | |
| from config import TTS_VOICE | |
| try: | |
| import edge_tts | |
| EDGE_TTS_AVAILABLE = True | |
| except ImportError: | |
| EDGE_TTS_AVAILABLE = False | |
| async def _generate_audio_bytes(text: str) -> bytes | None: | |
| """Generate TTS audio bytes asynchronously using edge-tts.""" | |
| if not EDGE_TTS_AVAILABLE: | |
| return None | |
| try: | |
| communicate = edge_tts.Communicate(text=text, voice=TTS_VOICE, rate="+0%") | |
| audio = b"" | |
| async for chunk in communicate.stream(): | |
| if chunk["type"] == "audio": | |
| audio += chunk["data"] | |
| return audio if audio else None | |
| except Exception as e: | |
| print(f"[TTS] Edge-TTS error: {e}") | |
| return None | |
| def tts_bytes(text: str) -> bytes | None: | |
| """Synchronous wrapper — returns MP3 bytes or None.""" | |
| try: | |
| loop = asyncio.new_event_loop() | |
| asyncio.set_event_loop(loop) | |
| result = loop.run_until_complete(_generate_audio_bytes(text)) | |
| loop.close() | |
| return result | |
| except Exception as e: | |
| print(f"[TTS] Error: {e}") | |
| return None | |