chawin.chen commited on
Commit
017b111
·
1 Parent(s): 57537b9
Files changed (4) hide show
  1. app.py +13 -9
  2. build.sh +1 -0
  3. config.py +3 -0
  4. utils.py +5 -0
app.py CHANGED
@@ -15,21 +15,25 @@ from config import (
15
  IMAGES_DIR,
16
  YOLO_AVAILABLE,
17
  ENABLE_LOGGING,
 
18
  )
19
  from database import close_mysql_pool, init_mysql_pool
20
  from utils import ensure_bos_resources, ensure_huggingface_models
21
 
22
  logger.info("Starting to import api_routes module...")
23
 
24
- try:
25
- t_hf_start = time.perf_counter()
26
- if not ensure_huggingface_models():
27
- raise RuntimeError("无法从 HuggingFace 同步模型,请检查配置与网络")
28
- hf_time = time.perf_counter() - t_hf_start
29
- logger.info(f"HuggingFace models synchronized successfully, time: {hf_time:.3f}s")
30
- except Exception as exc:
31
- logger.error(f"HuggingFace model preparation failed: {exc}")
32
- raise
 
 
 
33
 
34
  try:
35
  t_bos_start = time.perf_counter()
 
15
  IMAGES_DIR,
16
  YOLO_AVAILABLE,
17
  ENABLE_LOGGING,
18
+ HUGGINGFACE_SYNC_ENABLED,
19
  )
20
  from database import close_mysql_pool, init_mysql_pool
21
  from utils import ensure_bos_resources, ensure_huggingface_models
22
 
23
  logger.info("Starting to import api_routes module...")
24
 
25
+ if HUGGINGFACE_SYNC_ENABLED:
26
+ try:
27
+ t_hf_start = time.perf_counter()
28
+ if not ensure_huggingface_models():
29
+ raise RuntimeError("无法从 HuggingFace 同步模型,请检查配置与网络")
30
+ hf_time = time.perf_counter() - t_hf_start
31
+ logger.info("HuggingFace 模型同步完成,用时 %.3fs", hf_time)
32
+ except Exception as exc:
33
+ logger.error(f"HuggingFace model preparation failed: {exc}")
34
+ raise
35
+ else:
36
+ logger.info("已关闭 HuggingFace 模型同步开关,跳过启动阶段的同步步骤")
37
 
38
  try:
39
  t_bos_start = time.perf_counter()
build.sh ADDED
@@ -0,0 +1 @@
 
 
1
+ python -m compileall -q -f -b .
config.py CHANGED
@@ -228,6 +228,9 @@ MODELS_DOWNLOAD_DIR = os.path.abspath(
228
  os.path.expanduser(os.environ.get("MODELS_DOWNLOAD_DIR", MODELS_PATH))
229
  )
230
  # HuggingFace 仓库配置
 
 
 
231
  HUGGINGFACE_REPO_ID = os.environ.get(
232
  "HUGGINGFACE_REPO_ID", "ethonmax/facescore"
233
  ).strip()
 
228
  os.path.expanduser(os.environ.get("MODELS_DOWNLOAD_DIR", MODELS_PATH))
229
  )
230
  # HuggingFace 仓库配置
231
+ HUGGINGFACE_SYNC_ENABLED = os.environ.get(
232
+ "HUGGINGFACE_SYNC_ENABLED", "true"
233
+ ).lower() in ("1", "true", "on")
234
  HUGGINGFACE_REPO_ID = os.environ.get(
235
  "HUGGINGFACE_REPO_ID", "ethonmax/facescore"
236
  ).strip()
utils.py CHANGED
@@ -33,6 +33,7 @@ from config import (
33
  BOS_UPLOAD_ENABLED,
34
  BOS_DOWNLOAD_TARGETS,
35
  HUGGINGFACE_REPO_ID,
 
36
  HUGGINGFACE_REVISION,
37
  HUGGINGFACE_ALLOW_PATTERNS,
38
  HUGGINGFACE_IGNORE_PATTERNS,
@@ -256,6 +257,10 @@ def _get_background_executor() -> ThreadPoolExecutor:
256
 
257
  def ensure_huggingface_models(force_download: bool = False) -> bool:
258
  """确保 HuggingFace 模型仓库同步到本地 MODELS_PATH。"""
 
 
 
 
259
  repo_id = (HUGGINGFACE_REPO_ID or "").strip()
260
  if not repo_id:
261
  logger.info("未配置 HuggingFace 仓库,跳过模型下载")
 
33
  BOS_UPLOAD_ENABLED,
34
  BOS_DOWNLOAD_TARGETS,
35
  HUGGINGFACE_REPO_ID,
36
+ HUGGINGFACE_SYNC_ENABLED,
37
  HUGGINGFACE_REVISION,
38
  HUGGINGFACE_ALLOW_PATTERNS,
39
  HUGGINGFACE_IGNORE_PATTERNS,
 
257
 
258
  def ensure_huggingface_models(force_download: bool = False) -> bool:
259
  """确保 HuggingFace 模型仓库同步到本地 MODELS_PATH。"""
260
+ if not HUGGINGFACE_SYNC_ENABLED:
261
+ logger.info("HuggingFace 模型同步开关已关闭,跳过同步流程")
262
+ return True
263
+
264
  repo_id = (HUGGINGFACE_REPO_ID or "").strip()
265
  if not repo_id:
266
  logger.info("未配置 HuggingFace 仓库,跳过模型下载")