|
| 1 | +"""Voice selection and testing endpoints. |
| 2 | +
|
| 3 | +Manages active voice configuration (XTTS/OpenVoice/Piper). |
| 4 | +GPU-only — not registered in cloud deployment mode. |
| 5 | +""" |
| 6 | + |
| 7 | +import logging |
| 8 | +import time |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +from fastapi import APIRouter, HTTPException |
| 12 | +from fastapi.responses import FileResponse |
| 13 | +from pydantic import BaseModel |
| 14 | + |
| 15 | +from app.dependencies import get_container |
| 16 | + |
| 17 | + |
| 18 | +logger = logging.getLogger(__name__) |
| 19 | + |
| 20 | +router = APIRouter(tags=["voices"]) |
| 21 | + |
| 22 | +TEMP_DIR = Path("./temp") |
| 23 | +TEMP_DIR.mkdir(exist_ok=True) |
| 24 | + |
| 25 | + |
| 26 | +class VoiceRequest(BaseModel): |
| 27 | + voice: str # anna / marina / marina_openvoice / dmitri / irina |
| 28 | + |
| 29 | + |
| 30 | +@router.get("/admin/voices") |
| 31 | +async def admin_get_voices(): |
| 32 | + """Получить список всех доступных голосов""" |
| 33 | + container = get_container() |
| 34 | + voices = [] |
| 35 | + |
| 36 | + # XTTS голос (Анна) - требует GPU CC >= 7.0 (по умолчанию) |
| 37 | + if container.anna_voice_service: |
| 38 | + voices.append( |
| 39 | + { |
| 40 | + "id": "anna", |
| 41 | + "name": "Анна (XTTS)", |
| 42 | + "engine": "xtts", |
| 43 | + "description": "Клонированный голос Гули (XTTS v2, GPU CC >= 7.0)", |
| 44 | + "available": True, |
| 45 | + "samples_count": len(container.anna_voice_service.voice_samples), |
| 46 | + "default": True, |
| 47 | + } |
| 48 | + ) |
| 49 | + |
| 50 | + # XTTS голос (Марина) - требует GPU CC >= 7.0 |
| 51 | + if container.voice_service: |
| 52 | + voices.append( |
| 53 | + { |
| 54 | + "id": "marina", |
| 55 | + "name": "Марина (XTTS)", |
| 56 | + "engine": "xtts", |
| 57 | + "description": "Клонированный голос Лидии (XTTS v2, GPU CC >= 7.0)", |
| 58 | + "available": True, |
| 59 | + "samples_count": len(container.voice_service.voice_samples), |
| 60 | + } |
| 61 | + ) |
| 62 | + |
| 63 | + # OpenVoice голос (Марина) - работает на GPU CC 6.1+ |
| 64 | + if container.openvoice_service: |
| 65 | + voices.append( |
| 66 | + { |
| 67 | + "id": "marina_openvoice", |
| 68 | + "name": "Марина (OpenVoice)", |
| 69 | + "engine": "openvoice", |
| 70 | + "description": "Клонированный голос (OpenVoice v2, GPU CC 6.1+)", |
| 71 | + "available": True, |
| 72 | + "samples_count": len(container.openvoice_service.voice_samples) |
| 73 | + if container.openvoice_service.voice_samples |
| 74 | + else 0, |
| 75 | + } |
| 76 | + ) |
| 77 | + |
| 78 | + # Piper голоса (CPU) |
| 79 | + if container.piper_service: |
| 80 | + piper_voices = container.piper_service.get_available_voices() |
| 81 | + for voice_id, info in piper_voices.items(): |
| 82 | + voices.append( |
| 83 | + { |
| 84 | + "id": voice_id, |
| 85 | + "name": info["name"], |
| 86 | + "engine": "piper", |
| 87 | + "description": info["description"], |
| 88 | + "available": info["available"], |
| 89 | + } |
| 90 | + ) |
| 91 | + |
| 92 | + return { |
| 93 | + "voices": voices, |
| 94 | + "current": container.current_voice_config, |
| 95 | + } |
| 96 | + |
| 97 | + |
| 98 | +@router.get("/admin/voice") |
| 99 | +async def admin_get_current_voice(): |
| 100 | + """Получить текущий выбранный голос""" |
| 101 | + container = get_container() |
| 102 | + return container.current_voice_config |
| 103 | + |
| 104 | + |
| 105 | +@router.post("/admin/voice") |
| 106 | +async def admin_set_voice(request: VoiceRequest): |
| 107 | + """Установить активный голос""" |
| 108 | + container = get_container() |
| 109 | + voice_id = request.voice.lower() |
| 110 | + |
| 111 | + if voice_id == "anna": |
| 112 | + if not container.anna_voice_service: |
| 113 | + raise HTTPException( |
| 114 | + status_code=503, detail="XTTS service (Анна) not available (requires GPU CC >= 7.0)" |
| 115 | + ) |
| 116 | + new_config = {"engine": "xtts", "voice": "anna"} |
| 117 | + logger.info("🎤 Голос изменён на: Анна (XTTS)") |
| 118 | + |
| 119 | + elif voice_id == "marina": |
| 120 | + if not container.voice_service: |
| 121 | + raise HTTPException( |
| 122 | + status_code=503, |
| 123 | + detail="XTTS service (Марина) not available (requires GPU CC >= 7.0)", |
| 124 | + ) |
| 125 | + new_config = {"engine": "xtts", "voice": "marina"} |
| 126 | + logger.info("🎤 Голос изменён на: Марина (XTTS)") |
| 127 | + |
| 128 | + elif voice_id == "marina_openvoice": |
| 129 | + if not container.openvoice_service: |
| 130 | + raise HTTPException(status_code=503, detail="OpenVoice service not available") |
| 131 | + new_config = {"engine": "openvoice", "voice": "marina_openvoice"} |
| 132 | + logger.info("🎤 Голос изменён на: Марина (OpenVoice)") |
| 133 | + |
| 134 | + elif voice_id in ["dmitri", "irina"]: |
| 135 | + if not container.piper_service: |
| 136 | + raise HTTPException(status_code=503, detail="Piper TTS service not available") |
| 137 | + piper_voices = container.piper_service.get_available_voices() |
| 138 | + if voice_id not in piper_voices or not piper_voices[voice_id]["available"]: |
| 139 | + raise HTTPException(status_code=400, detail=f"Voice model not found: {voice_id}") |
| 140 | + new_config = {"engine": "piper", "voice": voice_id} |
| 141 | + logger.info(f"🎤 Голос изменён на: {piper_voices[voice_id]['name']} (Piper)") |
| 142 | + |
| 143 | + else: |
| 144 | + raise HTTPException( |
| 145 | + status_code=400, |
| 146 | + detail=f"Unknown voice: {voice_id}. Available: anna, marina, marina_openvoice, dmitri, irina", |
| 147 | + ) |
| 148 | + |
| 149 | + container.current_voice_config = new_config |
| 150 | + return {"status": "ok", **new_config} |
| 151 | + |
| 152 | + |
| 153 | +@router.post("/admin/voice/test") |
| 154 | +async def admin_test_voice(request: VoiceRequest): |
| 155 | + """Тестовый синтез выбранным голосом""" |
| 156 | + container = get_container() |
| 157 | + voice_id = request.voice.lower() |
| 158 | + test_text = "Здравствуйте! Это тестовое сообщение для проверки голоса." |
| 159 | + |
| 160 | + output_path = TEMP_DIR / f"voice_test_{voice_id}_{int(time.time())}.wav" |
| 161 | + |
| 162 | + try: |
| 163 | + if voice_id == "anna": |
| 164 | + if not container.anna_voice_service: |
| 165 | + raise HTTPException( |
| 166 | + status_code=503, detail="XTTS (Анна) not available (requires GPU CC >= 7.0)" |
| 167 | + ) |
| 168 | + container.anna_voice_service.synthesize_to_file( |
| 169 | + test_text, str(output_path), preset="natural" |
| 170 | + ) |
| 171 | + |
| 172 | + elif voice_id == "marina": |
| 173 | + if not container.voice_service: |
| 174 | + raise HTTPException( |
| 175 | + status_code=503, detail="XTTS (Марина) not available (requires GPU CC >= 7.0)" |
| 176 | + ) |
| 177 | + container.voice_service.synthesize_to_file( |
| 178 | + test_text, str(output_path), preset="natural" |
| 179 | + ) |
| 180 | + |
| 181 | + elif voice_id == "marina_openvoice": |
| 182 | + if not container.openvoice_service: |
| 183 | + raise HTTPException(status_code=503, detail="OpenVoice not available") |
| 184 | + container.openvoice_service.synthesize_to_file( |
| 185 | + test_text, str(output_path), language="ru" |
| 186 | + ) |
| 187 | + |
| 188 | + elif voice_id in ["dmitri", "irina"]: |
| 189 | + if not container.piper_service: |
| 190 | + raise HTTPException(status_code=503, detail="Piper not available") |
| 191 | + container.piper_service.synthesize_to_file(test_text, str(output_path), voice=voice_id) |
| 192 | + |
| 193 | + else: |
| 194 | + raise HTTPException( |
| 195 | + status_code=400, |
| 196 | + detail=f"Unknown voice: {voice_id}. Available: anna, marina, marina_openvoice, dmitri, irina", |
| 197 | + ) |
| 198 | + |
| 199 | + return FileResponse(output_path, media_type="audio/wav", filename=f"test_{voice_id}.wav") |
| 200 | + |
| 201 | + except HTTPException: |
| 202 | + raise |
| 203 | + except Exception as e: |
| 204 | + logger.error(f"❌ Ошибка тестового синтеза: {e}") |
| 205 | + raise HTTPException(status_code=500, detail=str(e)) |
0 commit comments