From 8445af7be09f6631b95e647d6f7f9026f9b89fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E8=88=AA=E5=AE=87?= <3364451258@qq.com> Date: Fri, 10 Jul 2026 15:19:43 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20HF=5FENDPOINT=20=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E5=8F=98=E9=87=8F=E6=93=8D=E4=BD=9C=E6=B7=BB=E5=8A=A0=E7=BA=BF?= =?UTF-8?q?=E7=A8=8B=E9=94=81=E9=98=B2=E7=AB=9E=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/embedder.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/core/embedder.py b/src/core/embedder.py index a11fada..ff35556 100644 --- a/src/core/embedder.py +++ b/src/core/embedder.py @@ -18,6 +18,7 @@ vectors = batch_embed(embedder, long_text_list) """ import os +import threading import logging from typing import Protocol @@ -29,6 +30,8 @@ logger = logging.getLogger("md-vector-db") _HF_MIRROR = os.environ.get("HF_MIRROR", "https://hf-mirror.com") +_HF_ENV_LOCK = threading.Lock() + # -- Provider 默认配置 -- _PROVIDER_DEFAULTS: dict[str, dict[str, str | int]] = { "openai": { @@ -78,16 +81,18 @@ class LocalEmbedder: # 通过 HF_ENDPOINT 环境变量设置镜像(sentence-transformers 依赖 huggingface_hub) # 临时设置仅用于模型下载,下载完成后还原 old_endpoint = os.environ.get("HF_ENDPOINT") - os.environ["HF_ENDPOINT"] = _HF_MIRROR + with _HF_ENV_LOCK: + os.environ["HF_ENDPOINT"] = _HF_MIRROR try: self._model = SentenceTransformer( config.local_model, device=device ) finally: - if old_endpoint is not None: - os.environ["HF_ENDPOINT"] = old_endpoint - else: - os.environ.pop("HF_ENDPOINT", None) + with _HF_ENV_LOCK: + if old_endpoint is not None: + os.environ["HF_ENDPOINT"] = old_endpoint + else: + os.environ.pop("HF_ENDPOINT", None) @property def dimension(self) -> int: