chenchaoyun commited on
Commit
7c323a5
·
1 Parent(s): 0d0c76e
Files changed (2) hide show
  1. api_routes.py +37 -27
  2. app.py +14 -1
api_routes.py CHANGED
@@ -63,10 +63,10 @@ if DEEPFACE_AVAILABLE:
63
  try:
64
  from deepface import DeepFace
65
  deepface_module = DeepFace
66
-
67
  # 为 DeepFace.verify 方法添加兼容性包装
68
  _original_verify = getattr(DeepFace, 'verify', None)
69
-
70
  if _original_verify:
71
  def _wrapped_verify(*args, **kwargs):
72
  """
@@ -88,10 +88,10 @@ if DEEPFACE_AVAILABLE:
88
  )
89
  _recover_deepface_model()
90
  return _original_verify(*args, **kwargs)
91
-
92
  DeepFace.verify = _wrapped_verify
93
  logger.info("Patched DeepFace.verify for SymbolicTensor compatibility")
94
-
95
  try:
96
  from deepface.models import FacialRecognition as df_facial_recognition
97
 
@@ -3526,6 +3526,34 @@ def _extract_tar_archive(archive_path: str, target_dir: str) -> Dict[str, str]:
3526
  }
3527
 
3528
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3529
  def _run_shell_command(command: str, timeout: int = 300) -> Dict[str, Any]:
3530
  """执行外部命令并返回输出。"""
3531
  logger.info(f"准备执行系统命令: {command}")
@@ -3554,36 +3582,18 @@ async def extract_chinese_celeb_dataset():
3554
  """
3555
  解压 MODELS_PATH 下的 chinese_celeb_dataset.tar.gz 到 /opt/data/chinese_celeb_dataset。
3556
  """
3557
- archive_path = os.path.join(MODELS_PATH, "chinese_celeb_dataset.tar.gz")
3558
- target_dir = "/opt/data"
3559
-
3560
- if not os.path.isfile(archive_path):
3561
- raise HTTPException(status_code=404, detail=f"数据集文件不存在: {archive_path}")
3562
-
3563
- try:
3564
- os.makedirs(target_dir, exist_ok=True)
3565
- except OSError as exc:
3566
- logger.error(f"创建目标目录失败: {target_dir}, {exc}")
3567
- raise HTTPException(status_code=500, detail=f"创建目标目录失败: {exc}")
3568
-
3569
  loop = asyncio.get_event_loop()
3570
  try:
3571
- extract_result = await loop.run_in_executor(
3572
- executor, _extract_tar_archive, archive_path, target_dir
3573
  )
 
 
3574
  except Exception as exc:
3575
  logger.error(f"解压 chinese_celeb_dataset 失败: {exc}")
3576
  raise HTTPException(status_code=500, detail=f"解压失败: {exc}")
3577
 
3578
- return {
3579
- "success": True,
3580
- "message": "chinese_celeb_dataset 解压完成",
3581
- "archive_path": archive_path,
3582
- "target_dir": target_dir,
3583
- "command": extract_result.get("command"),
3584
- "stdout": extract_result.get("stdout"),
3585
- "stderr": extract_result.get("stderr"),
3586
- }
3587
 
3588
 
3589
  @api_router.post("/files/upload", tags=["文件管理"])
 
63
  try:
64
  from deepface import DeepFace
65
  deepface_module = DeepFace
66
+
67
  # 为 DeepFace.verify 方法添加兼容性包装
68
  _original_verify = getattr(DeepFace, 'verify', None)
69
+
70
  if _original_verify:
71
  def _wrapped_verify(*args, **kwargs):
72
  """
 
88
  )
89
  _recover_deepface_model()
90
  return _original_verify(*args, **kwargs)
91
+
92
  DeepFace.verify = _wrapped_verify
93
  logger.info("Patched DeepFace.verify for SymbolicTensor compatibility")
94
+
95
  try:
96
  from deepface.models import FacialRecognition as df_facial_recognition
97
 
 
3526
  }
3527
 
3528
 
3529
+ def extract_chinese_celeb_dataset_sync() -> Dict[str, Any]:
3530
+ """
3531
+ 同步执行 chinese_celeb_dataset 解压操作,供启动流程或其他同步场景复用。
3532
+ """
3533
+ archive_path = os.path.join(MODELS_PATH, "chinese_celeb_dataset.tar.gz")
3534
+ target_dir = "/opt/data"
3535
+
3536
+ if not os.path.isfile(archive_path):
3537
+ raise FileNotFoundError(f"数据集文件不存在: {archive_path}")
3538
+
3539
+ try:
3540
+ os.makedirs(target_dir, exist_ok=True)
3541
+ except OSError as exc:
3542
+ logger.error(f"创建目标目录失败: {target_dir}, {exc}")
3543
+ raise RuntimeError(f"创建目标目录失败: {exc}") from exc
3544
+
3545
+ extract_result = _extract_tar_archive(archive_path, target_dir)
3546
+ return {
3547
+ "success": True,
3548
+ "message": "chinese_celeb_dataset 解压完成",
3549
+ "archive_path": archive_path,
3550
+ "target_dir": target_dir,
3551
+ "command": extract_result.get("command"),
3552
+ "stdout": extract_result.get("stdout"),
3553
+ "stderr": extract_result.get("stderr"),
3554
+ }
3555
+
3556
+
3557
  def _run_shell_command(command: str, timeout: int = 300) -> Dict[str, Any]:
3558
  """执行外部命令并返回输出。"""
3559
  logger.info(f"准备执行系统命令: {command}")
 
3582
  """
3583
  解压 MODELS_PATH 下的 chinese_celeb_dataset.tar.gz 到 /opt/data/chinese_celeb_dataset。
3584
  """
 
 
 
 
 
 
 
 
 
 
 
 
3585
  loop = asyncio.get_event_loop()
3586
  try:
3587
+ result = await loop.run_in_executor(
3588
+ executor, extract_chinese_celeb_dataset_sync
3589
  )
3590
+ except FileNotFoundError as exc:
3591
+ raise HTTPException(status_code=404, detail=str(exc)) from exc
3592
  except Exception as exc:
3593
  logger.error(f"解压 chinese_celeb_dataset 失败: {exc}")
3594
  raise HTTPException(status_code=500, detail=f"解压失败: {exc}")
3595
 
3596
+ return result
 
 
 
 
 
 
 
 
3597
 
3598
 
3599
  @api_router.post("/files/upload", tags=["文件管理"])
app.py CHANGED
@@ -43,7 +43,7 @@ except Exception as exc:
43
 
44
  try:
45
  t_start = time.perf_counter()
46
- from api_routes import api_router
47
  import_time = time.perf_counter() - t_start
48
  logger.info(f"api_routes module imported successfully, time: {import_time:.3f}s")
49
  except Exception as e:
@@ -51,6 +51,19 @@ except Exception as e:
51
  logger.error(f"api_routes module import failed, time: {import_time:.3f}s, error: {e}")
52
  raise
53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
54
 
55
  @asynccontextmanager
56
  async def lifespan(app: FastAPI):
 
43
 
44
  try:
45
  t_start = time.perf_counter()
46
+ from api_routes import api_router, extract_chinese_celeb_dataset_sync
47
  import_time = time.perf_counter() - t_start
48
  logger.info(f"api_routes module imported successfully, time: {import_time:.3f}s")
49
  except Exception as e:
 
51
  logger.error(f"api_routes module import failed, time: {import_time:.3f}s, error: {e}")
52
  raise
53
 
54
+ try:
55
+ t_extract_start = time.perf_counter()
56
+ extract_result = extract_chinese_celeb_dataset_sync()
57
+ extract_time = time.perf_counter() - t_extract_start
58
+ logger.info(
59
+ "Chinese celeb dataset extracted successfully, time: %.3fs, target: %s",
60
+ extract_time,
61
+ extract_result.get("target_dir"),
62
+ )
63
+ except Exception as exc:
64
+ logger.error(f"Failed to extract Chinese celeb dataset automatically: {exc}")
65
+ raise
66
+
67
 
68
  @asynccontextmanager
69
  async def lifespan(app: FastAPI):