mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-11-28 04:59:52 +01:00
fix: 修復tts會說出markdown格式
https://github.com/xinnan-tech/xiaozhi-esp32-server/issues/1473
This commit is contained in:
parent
3fbfbeaf0f
commit
2290f2a279
3 changed files with 18 additions and 3 deletions
|
|
@ -8,7 +8,7 @@ from datetime import datetime
|
||||||
from core.utils import textUtils
|
from core.utils import textUtils
|
||||||
from abc import ABC, abstractmethod
|
from abc import ABC, abstractmethod
|
||||||
from config.logger import setup_logging
|
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.tts import MarkdownCleaner
|
||||||
from core.utils.output_counter import add_device_output
|
from core.utils.output_counter import add_device_output
|
||||||
from core.handle.reportHandle import enqueue_tts_report
|
from core.handle.reportHandle import enqueue_tts_report
|
||||||
|
|
@ -111,7 +111,7 @@ class TTSProviderBase(ABC):
|
||||||
try:
|
try:
|
||||||
while not os.path.exists(tmp_file) and max_repeat_time > 0:
|
while not os.path.exists(tmp_file) and max_repeat_time > 0:
|
||||||
try:
|
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:
|
except Exception as e:
|
||||||
logger.bind(tag=TAG).warning(
|
logger.bind(tag=TAG).warning(
|
||||||
f"语音生成失败{5 - max_repeat_time + 1}次: {text},错误: {e}"
|
f"语音生成失败{5 - max_repeat_time + 1}次: {text},错误: {e}"
|
||||||
|
|
|
||||||
|
|
@ -981,3 +981,17 @@ def is_valid_image_file(file_data: bytes) -> bool:
|
||||||
def sanitize_tool_name(name: str) -> str:
|
def sanitize_tool_name(name: str) -> str:
|
||||||
"""Sanitize tool names for OpenAI compatibility."""
|
"""Sanitize tool names for OpenAI compatibility."""
|
||||||
return re.sub(r"[^a-zA-Z0-9_-]", "_", name)
|
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
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,7 @@ from config.settings import load_config
|
||||||
from core.utils.asr import create_instance as create_stt_instance
|
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.llm import create_instance as create_llm_instance
|
||||||
from core.utils.tts import create_instance as create_tts_instance
|
from core.utils.tts import create_instance as create_tts_instance
|
||||||
|
from core.utils.util import replace_other_chars
|
||||||
|
|
||||||
# 设置全局日志级别为WARNING,抑制INFO级别日志
|
# 设置全局日志级别为WARNING,抑制INFO级别日志
|
||||||
logging.basicConfig(level=logging.WARNING)
|
logging.basicConfig(level=logging.WARNING)
|
||||||
|
|
@ -100,7 +101,7 @@ class AsyncPerformanceTester:
|
||||||
for i, sentence in enumerate(self.test_sentences[:2], 1):
|
for i, sentence in enumerate(self.test_sentences[:2], 1):
|
||||||
start = time.time()
|
start = time.time()
|
||||||
tmp_file = tts.generate_filename()
|
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
|
duration = time.time() - start
|
||||||
total_time += duration
|
total_time += duration
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue