This commit is contained in:
2025-08-31 13:05:50 +08:00
parent d9a29a9d30
commit 1bdf7a3af4
4 changed files with 258 additions and 61 deletions

View File

@@ -8,8 +8,7 @@ from fastapi import APIRouter, WebSocket, WebSocketDisconnect
from Util.ASRClient import ASRClient
from Util.ObsUtil import ObsUploader
from Util.TTS_Pipeline import stream_and_split_text, StreamingVolcanoTTS
from Util.XueBanUtil import get_xueban_response_async
from Util.XueBanUtil import get_xueban_response_async, stream_and_split_text, StreamingVolcanoTTS
# 创建路由路由器
router = APIRouter(prefix="/api", tags=["学伴"])
@@ -104,18 +103,19 @@ async def streaming_chat(websocket: WebSocket):
logger.error(f"发送音频块失败: {str(e)}")
raise
# 修改streaming_chat函数中的相关部分
# 实时获取LLM流式输出并处理
logger.info("开始LLM流式处理和TTS合成...")
try:
# 直接LLM流式响应接入TTS
llm_stream = get_xueban_response_async(asr_result['text'], stream=True)
text_stream = stream_and_split_text(llm_stream) # 异步函数调用
# 直接使用stream_and_split_text获取LLM流式响应并断句
text_stream = stream_and_split_text(query_text=asr_result['text'])
# 初始化TTS处理器
tts = StreamingVolcanoTTS(max_concurrency=1)
# 异步迭代文本流
# 异步迭代文本流按句合成TTS
async for text_chunk in text_stream:
logger.info(f"正在处理句子: {text_chunk}")
await tts._synthesize_single_with_semaphore(text_chunk, audio_callback)
logger.info("TTS合成完成")
except Exception as e: