From 56c3a04809433d08641025c22186d5de25f3c790 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Sun, 4 May 2025 01:19:54 +0800 Subject: [PATCH] =?UTF-8?q?update:=E4=BC=98=E5=8C=96=E7=99=BE=E5=BA=A6ASR?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main/xiaozhi-server/config.yaml | 7 ++--- .../core/providers/asr/baidu.py | 29 ++++++++++++++----- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index 6135f0d7..cef214ac 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -259,11 +259,8 @@ ASR: access_key_secret: 你的阿里云账号access_key_secret output_dir: tmp/ BaiduASR: - # 可以在这里申请百度语音技术的AppID、API Key、Secret Key - # https://console.bce.baidu.com/ai-engine/old/#/ai/speech/app/list - # 启用前需要先安装依赖包: - # pip install baidu-aip==4.16.13 - # pip install chardet==5.2.0 + # 获取AppID、API Key、Secret Key:https://console.bce.baidu.com/ai-engine/old/#/ai/speech/app/list + # 查看资源额度:https://console.bce.baidu.com/ai-engine/old/#/ai/speech/overview/resource/list type: baidu app_id: 你的百度语音技术AppID api_key: 你的百度语音技术APIKey diff --git a/main/xiaozhi-server/core/providers/asr/baidu.py b/main/xiaozhi-server/core/providers/asr/baidu.py index 6edfe128..94996a59 100644 --- a/main/xiaozhi-server/core/providers/asr/baidu.py +++ b/main/xiaozhi-server/core/providers/asr/baidu.py @@ -17,12 +17,16 @@ from config.logger import setup_logging TAG = __name__ logger = setup_logging() + class ASRProvider(ASRProviderBase): def __init__(self, config: dict, delete_audio_file: bool = True): self.app_id = config.get("app_id") self.api_key = config.get("api_key") self.secret_key = config.get("secret_key") - self.dev_pid = config.get("dev_pid") + + dev_pid = config.get("dev_pid", "1537") + self.dev_pid = int(dev_pid) if dev_pid else 1537 + self.output_dir = config.get("output_dir") self.delete_audio_file = delete_audio_file @@ -61,7 +65,9 @@ class ASRProvider(ASRProviderBase): return pcm_data - async def speech_to_text(self, opus_data: List[bytes], session_id: str) -> Tuple[Optional[str], Optional[str]]: + async def speech_to_text( + self, opus_data: List[bytes], session_id: str + ) -> Tuple[Optional[str], Optional[str]]: """将语音数据转换为文本""" if not opus_data: logger.bind(tag=TAG).warn("音频数据为空!") @@ -86,16 +92,25 @@ class ASRProvider(ASRProviderBase): start_time = time.time() # 识别本地文件 - result = self.client.asr(combined_pcm_data, 'pcm', 16000, { - 'dev_pid': str(self.dev_pid), - }) + result = self.client.asr( + combined_pcm_data, + "pcm", + 16000, + { + "dev_pid": str(self.dev_pid), + }, + ) if result and result["err_no"] == 0: - logger.bind(tag=TAG).debug(f"百度语音识别耗时: {time.time() - start_time:.3f}s | 结果: {result}") + logger.bind(tag=TAG).debug( + f"百度语音识别耗时: {time.time() - start_time:.3f}s | 结果: {result}" + ) result = result["result"][0] return result, file_path else: - raise Exception(f"百度语音识别失败,错误码: {result['err_no']},错误信息: {result['err_msg']}") + raise Exception( + f"百度语音识别失败,错误码: {result['err_no']},错误信息: {result['err_msg']}" + ) return None, file_path except Exception as e: