From 7c818ced2baf56f58540009e68deded69167c456 Mon Sep 17 00:00:00 2001 From: Soulter <905617992@qq.com> Date: Sun, 12 Jan 2025 11:44:24 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E6=96=87=E4=BB=B6=E5=92=8C=E8=AF=AD?= =?UTF-8?q?=E9=9F=B3=E5=8A=9F=E8=83=BD=E9=80=82=E9=85=8D=20Lagrange?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/pipeline/preprocess_stage/stage.py | 6 ++- .../aiocqhttp/aiocqhttp_platform_adapter.py | 41 +++++++++++++------ .../provider/sources/whisper_api_source.py | 10 ++++- .../sources/whisper_selfhosted_source.py | 11 ++++- 4 files changed, 49 insertions(+), 19 deletions(-) diff --git a/astrbot/core/pipeline/preprocess_stage/stage.py b/astrbot/core/pipeline/preprocess_stage/stage.py index 455fd35c8..b55a1a82d 100644 --- a/astrbot/core/pipeline/preprocess_stage/stage.py +++ b/astrbot/core/pipeline/preprocess_stage/stage.py @@ -28,9 +28,11 @@ class PreProcessStage(Stage): if stt_provider: message_chain = event.get_messages() for idx, component in enumerate(message_chain): - if isinstance(component, Record) and component.path: + if isinstance(component, Record) and component.url: - path = component.path + path = component.url + + path.removeprefix("file:///") retry = 5 diff --git a/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py b/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py index 3d1439a98..862edb25b 100644 --- a/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py +++ b/astrbot/core/platform/sources/aiocqhttp/aiocqhttp_platform_adapter.py @@ -13,6 +13,7 @@ from .aiocqhttp_message_event import AiocqhttpMessageEvent from astrbot.core.platform.astr_message_event import MessageSesion from ...register import register_platform_adapter from aiocqhttp.exceptions import ActionFailed +from astrbot.core.utils.io import download_file @register_platform_adapter("aiocqhttp", "适用于 OneBot 标准的消息平台适配器,支持反向 WebSockets。") class AiocqhttpAdapter(Platform): @@ -81,22 +82,36 @@ class AiocqhttpAdapter(Platform): if t == 'text': message_str += m['data']['text'].strip() elif t == 'file': - try: - # Napcat, LLBot - ret = await self.bot.call_action(action="get_file", file_id=event.message[0]['data']['file_id']) - if not ret.get('file', None): - raise ValueError(f"无法解析文件响应: {ret}") - if not os.path.exists(ret['file']): - raise FileNotFoundError(f"文件不存在: {ret['file']}。如果您使用 Docker 部署了 AstrBot 或者消息协议端(Napcat等),暂时无法获取用户上传的文件。") + if m['data']['url'] and m['data']['url'].startswith("http"): + # Lagrange + logger.info("guessing lagrange") + + file_name = m['data'].get('file_name', "file") + path = os.path.join("data/temp", file_name) + await download_file(m['data']['url'], path) m['data'] = { - "file": ret['file'], - "name": ret['file_name'] + "file": path, + "name": file_name } - except ActionFailed as e: - logger.error(f"获取文件失败: {e},此消息段将被忽略。") - except BaseException as e: - logger.error(f"获取文件失败: {e},此消息段将被忽略。") + + else: + try: + # Napcat, LLBot + ret = await self.bot.call_action(action="get_file", file_id=event.message[0]['data']['file_id']) + if not ret.get('file', None): + raise ValueError(f"无法解析文件响应: {ret}") + if not os.path.exists(ret['file']): + raise FileNotFoundError(f"文件不存在: {ret['file']}。如果您使用 Docker 部署了 AstrBot 或者消息协议端(Napcat等),暂时无法获取用户上传的文件。") + + m['data'] = { + "file": ret['file'], + "name": ret['file_name'] + } + except ActionFailed as e: + logger.error(f"获取文件失败: {e},此消息段将被忽略。") + except BaseException as e: + logger.error(f"获取文件失败: {e},此消息段将被忽略。") a = ComponentTypes[t](**m['data']) # noqa: F405 abm.message.append(a) diff --git a/astrbot/core/provider/sources/whisper_api_source.py b/astrbot/core/provider/sources/whisper_api_source.py index 7159bf9d4..8ff33fa9b 100644 --- a/astrbot/core/provider/sources/whisper_api_source.py +++ b/astrbot/core/provider/sources/whisper_api_source.py @@ -73,15 +73,21 @@ class ProviderOpenAIWhisperAPI(STTProvider): async def get_text(self, audio_url: str) -> str: '''only supports mp3, mp4, mpeg, m4a, wav, webm''' + is_tencent = False + if audio_url.startswith("http"): + if "multimedia.nt.qq.com.cn" in audio_url: + is_tencent = True + name = str(uuid.uuid4()) path = os.path.join("data/temp", name) - audio_url = await download_file(audio_url, path) + await download_file(audio_url, path) + audio_url = path if not os.path.exists(audio_url): raise FileNotFoundError(f"文件不存在: {audio_url}") - if audio_url.endswith(".amr") or audio_url.endswith(".silk"): + if audio_url.endswith(".amr") or audio_url.endswith(".silk") or is_tencent: is_silk = await self._is_silk_file(audio_url) if is_silk: logger.info("Converting silk file to wav ...") diff --git a/astrbot/core/provider/sources/whisper_selfhosted_source.py b/astrbot/core/provider/sources/whisper_selfhosted_source.py index 6f16d559a..94c9a2be2 100644 --- a/astrbot/core/provider/sources/whisper_selfhosted_source.py +++ b/astrbot/core/provider/sources/whisper_selfhosted_source.py @@ -74,15 +74,22 @@ class ProviderOpenAIWhisperSelfHost(STTProvider): async def get_text(self, audio_url: str) -> str: loop = asyncio.get_event_loop() + + is_tencent = False + if audio_url.startswith("http"): + if "multimedia.nt.qq.com.cn" in audio_url: + is_tencent = True + name = str(uuid.uuid4()) path = os.path.join("data/temp", name) - audio_url = await download_file(audio_url, path) + await download_file(audio_url, path) + audio_url = path if not os.path.exists(audio_url): raise FileNotFoundError(f"文件不存在: {audio_url}") - if audio_url.endswith(".amr") or audio_url.endswith(".silk"): + if audio_url.endswith(".amr") or audio_url.endswith(".silk") or is_tencent: is_silk = await self._is_silk_file(audio_url) if is_silk: logger.info("Converting silk file to wav ...")