File size: 10,219 Bytes
b0b150b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"""
MEXAR Core Engine - Text-to-Speech Service
Provides text-to-speech capabilities with multiple provider support.
"""

import os
import logging
import hashlib
import requests
from pathlib import Path
from typing import Optional, Dict, Any, List
from dotenv import load_dotenv

load_dotenv()
logger = logging.getLogger(__name__)


class TTSService:
    """
    Text-to-Speech service supporting multiple providers:
    - ElevenLabs (high quality, free tier: 10k chars/month)
    - Web Speech API (browser-based, unlimited, handled client-side)
    """
    
    def __init__(self, cache_dir: str = "data/tts_cache"):
        """
        Initialize TTS service.
        
        Args:
            cache_dir: Directory to cache generated audio files
        """
        self.cache_dir = Path(cache_dir)
        self.cache_dir.mkdir(parents=True, exist_ok=True)
        
        # ElevenLabs configuration
        self.elevenlabs_api_key = os.getenv("ELEVENLABS_API_KEY")
        self.elevenlabs_base_url = "https://api.elevenlabs.io/v1"
        
        # Default voices
        self.default_voices = {
            "elevenlabs": "21m00Tcm4TlvDq8ikWAM",  # Rachel - neutral
            "web_speech": "default"  # Browser default
        }
    
    def generate_speech(
        self,
        text: str,
        provider: str = "elevenlabs",
        voice_id: Optional[str] = None,
        model_id: str = "eleven_monolingual_v1"
    ) -> Dict[str, Any]:
        """
        Generate speech from text using specified provider.
        
        Args:
            text: Text to convert to speech
            provider: "elevenlabs" or "web_speech"
            voice_id: Voice ID (provider-specific)
            model_id: Model ID for ElevenLabs
            
        Returns:
            Dict with audio file path, provider info, and metadata
        """
        if not text or not text.strip():
            return {
                "success": False,
                "error": "Empty text provided"
            }
        
        # Check cache first
        cache_key = self._get_cache_key(text, provider, voice_id)
        cached_file = self.cache_dir / f"{cache_key}.mp3"
        
        if cached_file.exists():
            logger.info(f"Using cached TTS audio: {cache_key}")
            return {
                "success": True,
                "provider": provider,
                "audio_path": str(cached_file),
                "audio_url": f"/api/chat/tts/audio/{cache_key}.mp3",
                "cached": True,
                "text_length": len(text)
            }
        
        # Generate new audio
        if provider == "elevenlabs":
            return self._generate_elevenlabs(text, voice_id, model_id, cached_file)
        elif provider == "web_speech":
            # Web Speech API is client-side only
            return {
                "success": True,
                "provider": "web_speech",
                "client_side": True,
                "text": text,
                "voice_id": voice_id or self.default_voices["web_speech"],
                "message": "Use browser Web Speech API for playback"
            }
        else:
            return {
                "success": False,
                "error": f"Unknown provider: {provider}"
            }
    
    def _generate_elevenlabs(
        self,
        text: str,
        voice_id: Optional[str],
        model_id: str,
        output_path: Path
    ) -> Dict[str, Any]:
        """Generate speech using ElevenLabs API."""
        if not self.elevenlabs_api_key:
            return {
                "success": False,
                "error": "ElevenLabs API key not configured",
                "fallback": "web_speech"
            }
        
        voice = voice_id or self.default_voices["elevenlabs"]
        
        try:
            url = f"{self.elevenlabs_base_url}/text-to-speech/{voice}"
            
            headers = {
                "Accept": "audio/mpeg",
                "Content-Type": "application/json",
                "xi-api-key": self.elevenlabs_api_key
            }
            
            data = {
                "text": text,
                "model_id": model_id,
                "voice_settings": {
                    "stability": 0.5,
                    "similarity_boost": 0.75
                }
            }
            
            response = requests.post(url, json=data, headers=headers, timeout=30)
            
            if response.status_code == 200:
                # Save audio file
                with open(output_path, "wb") as f:
                    f.write(response.content)
                
                logger.info(f"Generated ElevenLabs TTS: {len(text)} chars")
                
                return {
                    "success": True,
                    "provider": "elevenlabs",
                    "audio_path": str(output_path),
                    "audio_url": f"/api/chat/tts/audio/{output_path.name}",
                    "cached": False,
                    "text_length": len(text),
                    "voice_id": voice
                }
            
            elif response.status_code == 401:
                return {
                    "success": False,
                    "error": "Invalid ElevenLabs API key",
                    "fallback": "web_speech"
                }
            
            elif response.status_code == 429:
                return {
                    "success": False,
                    "error": "ElevenLabs quota exceeded",
                    "fallback": "web_speech"
                }
            
            else:
                return {
                    "success": False,
                    "error": f"ElevenLabs API error: {response.status_code}",
                    "fallback": "web_speech"
                }
        
        except Exception as e:
            logger.error(f"ElevenLabs TTS failed: {e}")
            return {
                "success": False,
                "error": str(e),
                "fallback": "web_speech"
            }
    
    def get_available_voices(self, provider: str = "elevenlabs") -> List[Dict[str, str]]:
        """
        Get list of available voices for a provider.
        
        Args:
            provider: "elevenlabs" or "web_speech"
            
        Returns:
            List of voice dictionaries with id, name, and metadata
        """
        if provider == "elevenlabs":
            if not self.elevenlabs_api_key:
                return []
            
            try:
                url = f"{self.elevenlabs_base_url}/voices"
                headers = {"xi-api-key": self.elevenlabs_api_key}
                
                response = requests.get(url, headers=headers, timeout=10)
                
                if response.status_code == 200:
                    data = response.json()
                    return [
                        {
                            "id": voice["voice_id"],
                            "name": voice["name"],
                            "category": voice.get("category", "general"),
                            "preview_url": voice.get("preview_url")
                        }
                        for voice in data.get("voices", [])
                    ]
                
            except Exception as e:
                logger.error(f"Failed to fetch ElevenLabs voices: {e}")
                return []
        
        elif provider == "web_speech":
            # Web Speech API voices are browser-specific
            return [
                {"id": "default", "name": "Browser Default", "category": "system"}
            ]
        
        return []
    
    def check_quota(self) -> Dict[str, Any]:
        """
        Check remaining quota for ElevenLabs.
        
        Returns:
            Dict with quota information
        """
        if not self.elevenlabs_api_key:
            return {
                "provider": "elevenlabs",
                "configured": False
            }
        
        try:
            url = f"{self.elevenlabs_base_url}/user"
            headers = {"xi-api-key": self.elevenlabs_api_key}
            
            response = requests.get(url, headers=headers, timeout=10)
            
            if response.status_code == 200:
                data = response.json()
                subscription = data.get("subscription", {})
                
                return {
                    "provider": "elevenlabs",
                    "configured": True,
                    "character_count": subscription.get("character_count", 0),
                    "character_limit": subscription.get("character_limit", 10000),
                    "remaining": subscription.get("character_limit", 10000) - subscription.get("character_count", 0),
                    "tier": subscription.get("tier", "free")
                }
            
        except Exception as e:
            logger.error(f"Failed to check ElevenLabs quota: {e}")
        
        return {
            "provider": "elevenlabs",
            "configured": True,
            "error": "Failed to fetch quota"
        }
    
    def _get_cache_key(self, text: str, provider: str, voice_id: Optional[str]) -> str:
        """Generate cache key for audio file."""
        content = f"{provider}:{voice_id or 'default'}:{text}"
        return hashlib.md5(content.encode()).hexdigest()
    
    def clear_cache(self) -> int:
        """
        Clear all cached audio files.
        
        Returns:
            Number of files deleted
        """
        count = 0
        for file in self.cache_dir.glob("*.mp3"):
            try:
                file.unlink()
                count += 1
            except Exception as e:
                logger.warning(f"Failed to delete cache file {file}: {e}")
        
        logger.info(f"Cleared {count} cached TTS files")
        return count


# Singleton instance
_tts_service_instance: Optional[TTSService] = None


def get_tts_service() -> TTSService:
    """Get or create the singleton TTS service instance."""
    global _tts_service_instance
    if _tts_service_instance is None:
        _tts_service_instance = TTSService()
    return _tts_service_instance