feat: optimize document and model usage

This commit is contained in:
saboteur7
2026-02-03 02:58:15 +08:00
parent a7b99cde4e
commit 501866d12a
16 changed files with 514 additions and 404 deletions

View File

@@ -15,11 +15,15 @@ import time
from bridge.reply import Reply, ReplyType
from common.log import logger
from voice.audio_convert import get_pcm_from_wav
from voice.voice import Voice
from voice.ali.ali_api import AliyunTokenGenerator, speech_to_text_aliyun, text_to_speech_aliyun
from config import conf
try:
from voice.audio_convert import get_pcm_from_wav
except ImportError as e:
logger.debug("import voice.audio_convert failed: {}".format(e))
class AliVoice(Voice):
def __init__(self):

View File

@@ -8,7 +8,13 @@ try:
except ImportError:
logger.debug("import pysilk failed, wechaty voice message will not be supported.")
from pydub import AudioSegment
try:
from pydub import AudioSegment
_pydub_available = True
except ImportError:
logger.debug("import pydub failed, voice conversion features will not be supported.")
AudioSegment = None
_pydub_available = False
sil_supports = [8000, 12000, 16000, 24000, 32000, 44100, 48000] # slk转wav时支持的采样率
@@ -44,6 +50,8 @@ def any_to_mp3(any_path, mp3_path):
"""
把任意格式转成mp3文件
"""
if not _pydub_available:
raise ImportError("pydub is required for audio conversion. Please install it with: pip install pydub")
if any_path.endswith(".mp3"):
shutil.copy2(any_path, mp3_path)
return
@@ -58,6 +66,8 @@ def any_to_wav(any_path, wav_path):
"""
把任意格式转成wav文件
"""
if not _pydub_available:
raise ImportError("pydub is required for audio conversion. Please install it with: pip install pydub")
if any_path.endswith(".wav"):
shutil.copy2(any_path, wav_path)
return
@@ -73,6 +83,8 @@ def any_to_sil(any_path, sil_path):
"""
把任意格式转成sil文件
"""
if not _pydub_available:
raise ImportError("pydub is required for audio conversion. Please install it with: pip install pydub")
if any_path.endswith(".sil") or any_path.endswith(".silk") or any_path.endswith(".slk"):
shutil.copy2(any_path, sil_path)
return 10000
@@ -92,6 +104,8 @@ def any_to_amr(any_path, amr_path):
"""
把任意格式转成amr文件
"""
if not _pydub_available:
raise ImportError("pydub is required for audio conversion. Please install it with: pip install pydub")
if any_path.endswith(".amr"):
shutil.copy2(any_path, amr_path)
return
@@ -116,6 +130,8 @@ def split_audio(file_path, max_segment_length_ms=60000):
"""
分割音频文件
"""
if not _pydub_available:
raise ImportError("pydub is required for audio conversion. Please install it with: pip install pydub")
audio = AudioSegment.from_file(file_path)
audio_length_ms = len(audio)
if audio_length_ms <= max_segment_length_ms:

View File

@@ -13,9 +13,13 @@ from bridge.reply import Reply, ReplyType
from common.log import logger
from common.tmp_dir import TmpDir
from config import conf
from voice.audio_convert import get_pcm_from_wav
from voice.voice import Voice
try:
from voice.audio_convert import get_pcm_from_wav
except ImportError as e:
logger.debug("import voice.audio_convert failed: {}".format(e))
class BaiduVoice(Voice):
def __init__(self):
try:

View File

@@ -28,9 +28,15 @@ from config import conf
from voice.voice import Voice
from .xunfei_asr import xunfei_asr
from .xunfei_tts import xunfei_tts
from voice.audio_convert import any_to_mp3
import shutil
from pydub import AudioSegment
try:
from voice.audio_convert import any_to_mp3
from pydub import AudioSegment
_audio_available = True
except ImportError as e:
logger.debug("import audio libraries failed: {}".format(e))
_audio_available = False
class XunfeiVoice(Voice):