'commit'
This commit is contained in:
@@ -51,7 +51,37 @@ def stream_and_split_text(query_text):
|
||||
if i+1 < len(sentences):
|
||||
sentence = sentences[i] + sentences[i+1]
|
||||
yield sentence
|
||||
|
||||
|
||||
# 保留不完整的部分
|
||||
buffer = sentences[-1]
|
||||
|
||||
# 处理最后剩余的部分
|
||||
if buffer:
|
||||
yield buffer
|
||||
|
||||
|
||||
# 修改为
|
||||
async def stream_and_split_text(llm_stream):
|
||||
"""
|
||||
流式获取LLM输出并按句子分割
|
||||
@param llm_stream: LLM流式响应生成器
|
||||
@return: 异步生成器,每次产生一个完整句子
|
||||
"""
|
||||
buffer = ""
|
||||
|
||||
# 直接处理LLM流式输出
|
||||
async for content in llm_stream:
|
||||
buffer += content
|
||||
|
||||
# 使用正则表达式检测句子结束
|
||||
sentences = re.split(r'([。!?.!?])', buffer)
|
||||
if len(sentences) > 1:
|
||||
# 提取完整句子
|
||||
for i in range(0, len(sentences)-1, 2):
|
||||
if i+1 < len(sentences):
|
||||
sentence = sentences[i] + sentences[i+1]
|
||||
yield sentence
|
||||
|
||||
# 保留不完整的部分
|
||||
buffer = sentences[-1]
|
||||
|
||||
@@ -85,15 +115,10 @@ class StreamingVolcanoTTS:
|
||||
text_stream: 文本流生成器
|
||||
audio_callback: 音频数据回调函数,接收音频片段
|
||||
"""
|
||||
# 为每个文本片段创建一个WebSocket连接,但限制并发数
|
||||
tasks = []
|
||||
for text in text_stream:
|
||||
if text.strip(): # 忽略空文本
|
||||
task = asyncio.create_task(self._synthesize_single_with_semaphore(text, audio_callback))
|
||||
tasks.append(task)
|
||||
|
||||
# 等待所有任务完成
|
||||
await asyncio.gather(*tasks)
|
||||
# 实时处理每个文本片段(删除任务列表和gather)
|
||||
async for text in text_stream:
|
||||
if text.strip():
|
||||
await self._synthesize_single_with_semaphore(text, audio_callback)
|
||||
|
||||
async def _synthesize_single_with_semaphore(self, text, audio_callback):
|
||||
"""使用信号量控制并发数的单个文本合成"""
|
||||
|
Reference in New Issue
Block a user