feat: 完成ChatTTS集成,连接192.168.2.5:12002
This commit is contained in:
Binary file not shown.
BIN
audio_cache/17aeba5b5a9e4593bd4b385cace035cf.mp3
Normal file
BIN
audio_cache/17aeba5b5a9e4593bd4b385cace035cf.mp3
Normal file
Binary file not shown.
BIN
audio_cache/94e455ca455f44e9914ebf6125fc3264.mp3
Normal file
BIN
audio_cache/94e455ca455f44e9914ebf6125fc3264.mp3
Normal file
Binary file not shown.
BIN
audio_cache/d294e0f73aff47c7ac66dd96808b4161.mp3
Normal file
BIN
audio_cache/d294e0f73aff47c7ac66dd96808b4161.mp3
Normal file
Binary file not shown.
BIN
logs/server.log
BIN
logs/server.log
Binary file not shown.
@@ -108,32 +108,70 @@ class EdgeTTSProvider(TTSProvider):
|
|||||||
|
|
||||||
|
|
||||||
class ChatTTSProvider(TTSProvider):
|
class ChatTTSProvider(TTSProvider):
|
||||||
"""ChatTTS 提供者(本地部署,预留接口)"""
|
"""ChatTTS 提供者(本地部署)"""
|
||||||
|
|
||||||
# 预留配置
|
# ChatTTS 服务地址
|
||||||
CHATTTS_URL = os.getenv("CHATTTS_URL", "http://localhost:19020")
|
CHATTTS_URL = os.getenv("CHATTTS_URL", "http://192.168.2.5:12002")
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._available = False # 暂不可用
|
self._available = None
|
||||||
|
|
||||||
async def synthesize(self, text: str) -> Tuple[str, str]:
|
async def synthesize(self, text: str) -> Tuple[str, str]:
|
||||||
"""
|
"""使用 ChatTTS 合成语音"""
|
||||||
使用 ChatTTS 合成语音
|
import aiohttp
|
||||||
TODO: 后续实现
|
|
||||||
"""
|
async with aiohttp.ClientSession() as session:
|
||||||
raise NotImplementedError("ChatTTS 尚未实现,请先部署 ChatTTS 服务")
|
form = aiohttp.FormData()
|
||||||
|
form.add_field('text', text)
|
||||||
|
|
||||||
|
async with session.post(
|
||||||
|
f"{self.CHATTTS_URL}/synthesize",
|
||||||
|
data=form,
|
||||||
|
timeout=aiohttp.ClientTimeout(total=60)
|
||||||
|
) as resp:
|
||||||
|
if resp.status != 200:
|
||||||
|
error = await resp.text()
|
||||||
|
raise Exception(f"ChatTTS error: {error}")
|
||||||
|
|
||||||
|
data = await resp.json()
|
||||||
|
# ChatTTS 返回的 URL 是相对路径,需要拼接
|
||||||
|
audio_url = f"{self.CHATTTS_URL}{data['audio_url']}"
|
||||||
|
return None, audio_url
|
||||||
|
|
||||||
def get_name(self) -> str:
|
def get_name(self) -> str:
|
||||||
return "ChatTTS"
|
return "ChatTTS"
|
||||||
|
|
||||||
def is_available(self) -> bool:
|
def is_available(self) -> bool:
|
||||||
"""检查 ChatTTS 是否可用"""
|
"""检查 ChatTTS 是否可用"""
|
||||||
# TODO: 后续实现检测逻辑
|
if self._available is None:
|
||||||
|
try:
|
||||||
|
import aiohttp
|
||||||
|
import asyncio
|
||||||
|
|
||||||
|
async def check():
|
||||||
|
try:
|
||||||
|
async with aiohttp.ClientSession() as session:
|
||||||
|
async with session.get(
|
||||||
|
f"{self.CHATTTS_URL}/health",
|
||||||
|
timeout=aiohttp.ClientTimeout(total=5)
|
||||||
|
) as resp:
|
||||||
|
if resp.status == 200:
|
||||||
|
data = await resp.json()
|
||||||
|
return data.get("status") == "ok"
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
return False
|
||||||
|
|
||||||
|
self._available = asyncio.get_event_loop().run_until_complete(check())
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning(f"ChatTTS check failed: {e}")
|
||||||
|
self._available = False
|
||||||
return self._available
|
return self._available
|
||||||
|
|
||||||
def set_available(self, available: bool):
|
def set_url(self, url: str):
|
||||||
"""设置可用状态(部署后调用)"""
|
"""设置服务地址"""
|
||||||
self._available = available
|
self.CHATTTS_URL = url
|
||||||
|
self._available = None # 重新检测
|
||||||
|
|
||||||
|
|
||||||
class NoTTSProvider(TTSProvider):
|
class NoTTSProvider(TTSProvider):
|
||||||
|
|||||||
Reference in New Issue
Block a user