diff --git a/main/xiaozhi-server/core/providers/tts/base.py b/main/xiaozhi-server/core/providers/tts/base.py index 296625e5c..fdc7819cf 100644 --- a/main/xiaozhi-server/core/providers/tts/base.py +++ b/main/xiaozhi-server/core/providers/tts/base.py @@ -8,7 +8,7 @@ from datetime import datetime from core.utils import textUtils from abc import ABC, abstractmethod from config.logger import setup_logging -from core.utils.util import audio_to_data, audio_bytes_to_data +from core.utils.util import audio_to_data, audio_bytes_to_data, replace_other_chars from core.utils.tts import MarkdownCleaner from core.utils.output_counter import add_device_output from core.handle.reportHandle import enqueue_tts_report @@ -111,7 +111,7 @@ class TTSProviderBase(ABC): try: while not os.path.exists(tmp_file) and max_repeat_time > 0: try: - asyncio.run(self.text_to_speak(text, tmp_file)) + asyncio.run(self.text_to_speak(replace_other_chars(text), tmp_file)) except Exception as e: logger.bind(tag=TAG).warning( f"语音生成失败{5 - max_repeat_time + 1}次: {text},错误: {e}" diff --git a/main/xiaozhi-server/core/utils/util.py b/main/xiaozhi-server/core/utils/util.py index cf52bfc25..5cc8a28a7 100644 --- a/main/xiaozhi-server/core/utils/util.py +++ b/main/xiaozhi-server/core/utils/util.py @@ -981,3 +981,17 @@ def is_valid_image_file(file_data: bytes) -> bool: def sanitize_tool_name(name: str) -> str: """Sanitize tool names for OpenAI compatibility.""" return re.sub(r"[^a-zA-Z0-9_-]", "_", name) + + +def replace_other_chars(t: str) -> str: + # 移除Markdown標題 + t = re.sub(r"#*", "", t) + # 移除Markdown列表項目 + t = re.sub(r"\*", "", t) + # 移除Markdown鏈接 + t = re.sub(r"\[.*?]\(.*?\)", "", t) + # 移除多餘的空格和換行符 + t = t.replace("\n", " ").strip() + # 移除連結 + # t = re.sub(r"https?://\S+", "", t) + return t diff --git a/main/xiaozhi-server/performance_tester.py b/main/xiaozhi-server/performance_tester.py index a5fc52219..5461cce0f 100644 --- a/main/xiaozhi-server/performance_tester.py +++ b/main/xiaozhi-server/performance_tester.py @@ -12,6 +12,7 @@ from config.settings import load_config from core.utils.asr import create_instance as create_stt_instance from core.utils.llm import create_instance as create_llm_instance from core.utils.tts import create_instance as create_tts_instance +from core.utils.util import replace_other_chars # 设置全局日志级别为WARNING,抑制INFO级别日志 logging.basicConfig(level=logging.WARNING) @@ -100,7 +101,7 @@ class AsyncPerformanceTester: for i, sentence in enumerate(self.test_sentences[:2], 1): start = time.time() tmp_file = tts.generate_filename() - await tts.text_to_speak(sentence, tmp_file) + await tts.text_to_speak(replace_other_chars(sentence), tmp_file) duration = time.time() - start total_time += duration