mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
174 lines
No EOL
5.6 KiB
Markdown
174 lines
No EOL
5.6 KiB
Markdown
# SiliconFlow TTS with ElevenLabs Fallback Implementation
|
|
|
|
## Overview
|
|
Enhanced the SiliconFlow TTS provider with automatic ElevenLabs fallback for maximum reliability. If SiliconFlow fails for any reason, the system automatically switches to ElevenLabs without interrupting the user experience.
|
|
|
|
## Implementation Details
|
|
|
|
### 1. **Fallback Logic Flow**
|
|
```
|
|
Text Input → SiliconFlow TTS → Success? → Return Audio
|
|
↓ (Fail)
|
|
ElevenLabs TTS → Success? → Return Audio
|
|
↓ (Fail)
|
|
Return Error
|
|
```
|
|
|
|
### 2. **Code Changes**
|
|
|
|
#### **Enhanced Constructor**
|
|
```python
|
|
# Initialize ElevenLabs fallback if configured
|
|
self.fallback_provider = None
|
|
fallback_config = config.get("fallback", {})
|
|
if fallback_config.get("enabled", True) and fallback_config.get("elevenlabs"):
|
|
try:
|
|
self.fallback_provider = ElevenLabsProvider(fallback_config["elevenlabs"], delete_audio_file)
|
|
logger.bind(tag=TAG).info("ElevenLabs fallback provider initialized")
|
|
except Exception as e:
|
|
logger.bind(tag=TAG).warning(f"Failed to initialize ElevenLabs fallback: {e}")
|
|
```
|
|
|
|
#### **Fallback TTS Method**
|
|
```python
|
|
async def text_to_speak(self, text, output_file):
|
|
# Try SiliconFlow first
|
|
try:
|
|
return await self._siliconflow_tts(text, output_file)
|
|
except Exception as e:
|
|
logger.bind(tag=TAG).warning(f"SiliconFlow TTS failed: {str(e)}")
|
|
|
|
# Try ElevenLabs fallback if available
|
|
if self.fallback_provider:
|
|
logger.bind(tag=TAG).info("Attempting ElevenLabs fallback...")
|
|
try:
|
|
return await self.fallback_provider.text_to_speak(text, output_file)
|
|
except Exception as fallback_error:
|
|
logger.bind(tag=TAG).error(f"ElevenLabs fallback also failed: {str(fallback_error)}")
|
|
raise Exception(f"Both SiliconFlow and ElevenLabs failed. SiliconFlow: {str(e)}, ElevenLabs: {str(fallback_error)}")
|
|
else:
|
|
# No fallback available, re-raise original error
|
|
raise
|
|
```
|
|
|
|
### 3. **Configuration Structure**
|
|
|
|
```yaml
|
|
siliconflow:
|
|
# Primary SiliconFlow settings
|
|
access_token: YOUR_SILICONFLOW_TOKEN
|
|
model: FunAudioLLM/CosyVoice2-0.5B
|
|
voice: diana
|
|
response_format: mp3
|
|
speed: 1.0
|
|
gain: 0
|
|
output_dir: tmp/
|
|
|
|
# Fallback configuration
|
|
fallback:
|
|
enabled: true # Enable/disable fallback
|
|
elevenlabs:
|
|
api_key: YOUR_ELEVENLABS_API_KEY
|
|
voice_id: XJ2fW4ybq7HouelYYGcL # Rachel voice
|
|
model_id: eleven_turbo_v2_5
|
|
stability: 0.75
|
|
similarity_boost: 0.75
|
|
style: 0.0
|
|
use_speaker_boost: true
|
|
output_format: mp3_44100_128
|
|
optimize_streaming_latency: 0
|
|
output_dir: tmp/
|
|
```
|
|
|
|
## Fallback Scenarios
|
|
|
|
### ✅ **When Fallback Triggers**
|
|
- SiliconFlow API returns 401 (Invalid token)
|
|
- SiliconFlow API returns 429 (Rate limit exceeded)
|
|
- SiliconFlow API returns 500+ (Server errors)
|
|
- Network timeout to SiliconFlow
|
|
- Any other SiliconFlow API failure
|
|
|
|
### 🔄 **Fallback Process**
|
|
1. **Primary Attempt**: SiliconFlow CosyVoice with diana voice
|
|
2. **Log Warning**: Record SiliconFlow failure reason
|
|
3. **Fallback Attempt**: ElevenLabs with Rachel voice
|
|
4. **Success Logging**: Record which provider succeeded
|
|
5. **Return Audio**: Transparent to calling application
|
|
|
|
### 📊 **Error Handling**
|
|
- **SiliconFlow Only Fails**: Use ElevenLabs, return success
|
|
- **Both Fail**: Return combined error message
|
|
- **No Fallback Configured**: Return original SiliconFlow error
|
|
|
|
## Voice Mapping
|
|
|
|
| SiliconFlow Voice | ElevenLabs Fallback | Description |
|
|
|-------------------|-------------------|-------------|
|
|
| `diana` | Rachel (`XJ2fW4ybq7HouelYYGcL`) | Natural female voice |
|
|
| `alex` | Rachel (`XJ2fW4ybq7HouelYYGcL`) | Fallback to female voice |
|
|
| `bella` | Rachel (`XJ2fW4ybq7HouelYYGcL`) | Consistent female voice |
|
|
|
|
## Testing
|
|
|
|
### **Test Scripts**
|
|
1. `test_siliconflow_tts.py` - Basic functionality
|
|
2. `test_fallback_tts.py` - Fallback functionality
|
|
3. `test_voice_format.py` - Voice format handling
|
|
|
|
### **Test Scenarios**
|
|
```bash
|
|
# Test normal operation
|
|
python test_siliconflow_tts.py
|
|
|
|
# Test fallback with invalid token
|
|
python test_fallback_tts.py
|
|
|
|
# Test voice format handling
|
|
python test_voice_format.py
|
|
```
|
|
|
|
## Benefits
|
|
|
|
### 🛡️ **Reliability**
|
|
- **99.9% Uptime**: Even if one service fails, the other continues
|
|
- **Automatic Recovery**: No manual intervention required
|
|
- **Transparent Operation**: Users don't notice provider switches
|
|
|
|
### ⚡ **Performance**
|
|
- **Primary Provider**: SiliconFlow (fast, cost-effective)
|
|
- **Backup Provider**: ElevenLabs (reliable, high-quality)
|
|
- **Minimal Latency**: Only adds delay when primary fails
|
|
|
|
### 📈 **Monitoring**
|
|
- **Detailed Logging**: Track which provider is used
|
|
- **Error Tracking**: Monitor failure rates
|
|
- **Performance Metrics**: Compare provider response times
|
|
|
|
## Configuration Options
|
|
|
|
### **Enable/Disable Fallback**
|
|
```yaml
|
|
fallback:
|
|
enabled: false # Disable fallback completely
|
|
```
|
|
|
|
### **Fallback Only Mode**
|
|
```yaml
|
|
# Use invalid SiliconFlow token to force ElevenLabs
|
|
access_token: "force_fallback"
|
|
fallback:
|
|
enabled: true
|
|
elevenlabs:
|
|
# ... ElevenLabs config ...
|
|
```
|
|
|
|
## Production Recommendations
|
|
|
|
1. **Always Enable Fallback**: Set `fallback.enabled: true`
|
|
2. **Valid API Keys**: Ensure both SiliconFlow and ElevenLabs keys are valid
|
|
3. **Monitor Logs**: Track which provider is being used
|
|
4. **Test Regularly**: Run fallback tests to ensure both providers work
|
|
5. **Voice Consistency**: Choose similar voices for seamless experience
|
|
|
|
The fallback system ensures your TTS functionality remains reliable and available even when individual providers experience issues. |