From 0a5196a47554d559801b87bd1b5dd3b6574de7dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BB=BA=E6=AD=A6?= Date: Fri, 9 May 2025 10:27:16 +0800 Subject: [PATCH] =?UTF-8?q?refactor(utils):=20=E6=9B=B4=E6=96=B0=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B=E7=9B=AE=E5=BD=95=E8=8E=B7=E5=8F=96=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E4=BB=A5=E6=94=AF=E6=8C=81=E6=89=93=E5=8C=85=E8=BF=90=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit -增加对打包状态的判断,使用不同的目录路径 -打包时将模型目录设置为 APPDATA 或 ~/.cache 下的 BiliNote/models - 开发时仍使用项目根目录下的 models目录 - 确保兼容性和可移植性 --- backend/app/utils/path_helper.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/backend/app/utils/path_helper.py b/backend/app/utils/path_helper.py index 99b29da..9a9e44e 100644 --- a/backend/app/utils/path_helper.py +++ b/backend/app/utils/path_helper.py @@ -1,4 +1,6 @@ import os +import sys +from pathlib import Path PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")) @@ -10,11 +12,15 @@ def get_data_dir(): def get_model_dir(subdir: str = "whisper") -> str: - base = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../models")) - path = os.path.join(base, subdir) + # 判断是否为打包状态(PyInstaller) + if getattr(sys, 'frozen', False): + # exe 执行,放在 APPDATA 或 ~/.cache 下 + base_dir = os.path.join(os.getenv("APPDATA") or str(Path.home()), "BiliNote", "models") + else: + # 开发时,相对项目根目录 + base_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../models")) + + path = os.path.join(base_dir, subdir) os.makedirs(path, exist_ok=True) return path - -if __name__ == '__main__': - print(get_data_dir())