mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-29 04:53:55 +08:00
Merge pull request #2552 from Packeting1/multi_lang_tts
feat: 为阿里百炼流式TTS支持多语言音色
This commit is contained in:
@@ -8,8 +8,9 @@ import asyncio
|
||||
|
||||
from config.logger import setup_logging
|
||||
from typing import Optional, Tuple, List
|
||||
from core.providers.asr.base import ASRProviderBase
|
||||
from core.providers.asr.utils import lang_tag_filter
|
||||
from funasr import AutoModel
|
||||
from funasr.utils.postprocess_utils import rich_transcription_postprocess
|
||||
from core.providers.asr.base import ASRProviderBase
|
||||
from core.providers.asr.dto.dto import InterfaceType
|
||||
|
||||
@@ -102,7 +103,9 @@ class ASRProvider(ASRProviderBase):
|
||||
use_itn=True,
|
||||
batch_size_s=60,
|
||||
)
|
||||
text = await asyncio.to_thread(rich_transcription_postprocess, result[0]["text"])
|
||||
# text = await asyncio.to_thread(rich_transcription_postprocess, result[0]["text"])
|
||||
# 使用lang_tag_filter处理识别结果
|
||||
text = await asyncio.to_thread(lang_tag_filter, result[0]["text"])
|
||||
logger.bind(tag=TAG).debug(
|
||||
f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text}"
|
||||
)
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
from typing import Optional, Tuple, List
|
||||
from core.providers.asr.base import ASRProviderBase
|
||||
from core.providers.asr.utils import lang_tag_filter
|
||||
from core.providers.asr.dto.dto import InterfaceType
|
||||
import ssl
|
||||
import json
|
||||
import websockets
|
||||
from config.logger import setup_logging
|
||||
import asyncio
|
||||
import re
|
||||
|
||||
TAG = __name__
|
||||
logger = setup_logging()
|
||||
@@ -151,9 +151,13 @@ class ASRProvider(ASRProviderBase):
|
||||
|
||||
# Get the result from the receive task
|
||||
result = receive_task.result()
|
||||
match = re.match(r"<\|(.*?)\|><\|(.*?)\|><\|(.*?)\|>(.*)", result)
|
||||
if match:
|
||||
result = match.group(4).strip()
|
||||
|
||||
# match = re.match(r"<\|(.*?)\|><\|(.*?)\|><\|(.*?)\|>(.*)", result)
|
||||
# if match:
|
||||
# result = match.group(4).strip()
|
||||
|
||||
# Handle language tags
|
||||
result = lang_tag_filter(result)
|
||||
return (
|
||||
result,
|
||||
file_path,
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
import re
|
||||
from config.logger import setup_logging
|
||||
|
||||
TAG = __name__
|
||||
logger = setup_logging()
|
||||
|
||||
|
||||
def lang_tag_filter(text):
|
||||
"""
|
||||
过滤函数:只保留语言标签,移除其他所有标签
|
||||
|
||||
用于FunASR识别结果的处理,保留语言标签(如<|zh|>、<|en|>等),
|
||||
但移除其他所有格式的标签(如时间戳、情感标签等)
|
||||
|
||||
Args:
|
||||
text: ASR识别的原始文本,可能包含多种标签
|
||||
|
||||
Returns:
|
||||
str: 处理后的文本,只保留语言标签(如果存在)
|
||||
|
||||
Examples:
|
||||
>>> lang_tag_filter("<|zh|><|emotion:happy|>你好")
|
||||
'<|zh|>你好'
|
||||
>>> lang_tag_filter("<|en|>hello world")
|
||||
'<|en|>hello world'
|
||||
"""
|
||||
# 定义语言标签模式
|
||||
lang_pattern = r"<\|(zh|en|yue|ja|ko|nospeech)\|>"
|
||||
lang_tags = re.findall(lang_pattern, text)
|
||||
|
||||
# 移除所有 < | ... | > 格式的标签
|
||||
clean_text = re.sub(r"<\|.*?\|>", "", text)
|
||||
|
||||
# 在开头添加语言标签(如果存在)
|
||||
if lang_tags:
|
||||
if len(lang_tags) > 1:
|
||||
logger.bind(tag=TAG).warning(
|
||||
f"检测到多个语言标签: {lang_tags},仅使用第一个: {lang_tags[0]}"
|
||||
)
|
||||
clean_text = f"<|{lang_tags[0]}|>{clean_text}"
|
||||
|
||||
return clean_text.strip()
|
||||
|
||||
Reference in New Issue
Block a user