From 164dada46a6aac5fff32328c30b882ea0c035bad Mon Sep 17 00:00:00 2001 From: whosmyqueen Date: Mon, 12 May 2025 14:38:58 +0800 Subject: [PATCH] =?UTF-8?q?feat(core):=20=E4=B8=BA=20FunASR=20=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1=E6=B7=BB=E5=8A=A0=20API=20=E5=AF=86=E9=92=A5=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=B9=B6=E4=BC=98=E5=8C=96=E7=BB=93=E6=9E=9C=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 在 FunASRWebSocket 类中添加 API密钥配置 - 在 WebSocket连接时添加认证头信息- 使用正则表达式处理返回结果,提取有用信息 --- main/xiaozhi-server/core/providers/asr/fun_server.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/main/xiaozhi-server/core/providers/asr/fun_server.py b/main/xiaozhi-server/core/providers/asr/fun_server.py index 53eb4e7f..a446c6ac 100644 --- a/main/xiaozhi-server/core/providers/asr/fun_server.py +++ b/main/xiaozhi-server/core/providers/asr/fun_server.py @@ -9,6 +9,7 @@ import wave import websockets from config.logger import setup_logging import asyncio +import re TAG = __name__ logger = setup_logging() @@ -24,6 +25,7 @@ class ASRProvider(ASRProviderBase): super().__init__() self.host = config.get("host", "localhost") self.port = config.get("port", 10095) + self.api_key = config.get('api_key', 'none') self.is_ssl = config.get("is_ssl", True) self.output_dir = config.get("output_dir") self.delete_audio_file = delete_audio_file @@ -130,9 +132,9 @@ class ASRProvider(ASRProviderBase): pass else: file_path = self.save_audio_to_file(pcm_data, session_id) - + auth_header = {'Authorization': 'Bearer; {}'.format(self.api_key)} async with websockets.connect( - self.uri, subprotocols=["binary"], ping_interval=None, ssl=self.ssl_context + self.uri, additional_headers=auth_header, subprotocols=["binary"], ping_interval=None, ssl=self.ssl_context ) as ws: try: # Use asyncio to handle WebSocket communication @@ -157,6 +159,9 @@ 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() return ( result, file_path,