IdleCloud commited on
Commit
c020d21
·
1 Parent(s): 8d98638

Scope HF token to gated model download

Browse files
Files changed (2) hide show
  1. app.py +13 -5
  2. tests/test_image_inversion_job_api.py +4 -4
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import os
2
 
3
- # 模型仓库均为公开资源;必须在任何 Hub 间接依赖导入前禁用隐式私有 token。
4
  os.environ.setdefault("HF_HUB_DISABLE_IMPLICIT_TOKEN", "1")
5
 
6
  import json
@@ -138,6 +138,7 @@ def validate_and_open_image(image_path: str) -> Image.Image:
138
  # ------------------------------------------------------------------
139
  ASSETS_REPO_ID = os.environ.get("ASSETS_REPO_ID", "pixai-labs/pixai-tagger-v0.9")
140
  ASSETS_REVISION = os.environ.get("ASSETS_REVISION")
 
141
  MODEL_DIR = os.environ.get("MODEL_DIR", "./assets")
142
 
143
  REQUIRED_FILES = [
@@ -148,9 +149,15 @@ REQUIRED_FILES = [
148
 
149
 
150
  def ensure_assets(repo_id: str, revision: Optional[str], target_dir: str) -> None:
151
- """
152
- 下载 pixai-labs/pixai-tagger-v0.9 所需资源,并复制到 handler 期望的本地目录。
153
- 如果文件已经存在,则不会重复下载。
 
 
 
 
 
 
154
  """
155
  target = Path(target_dir)
156
  target.mkdir(parents=True, exist_ok=True)
@@ -163,7 +170,8 @@ def ensure_assets(repo_id: str, revision: Optional[str], target_dir: str) -> Non
163
  repo_id=repo_id,
164
  revision=revision,
165
  allow_patterns=REQUIRED_FILES,
166
- token=False,
 
167
  )
168
 
169
  for fname in REQUIRED_FILES:
 
1
  import os
2
 
3
+ # 禁止 Hub 依赖隐式读取私有 token;受限模型只在下方下载调用中显式使用 Space Secret。
4
  os.environ.setdefault("HF_HUB_DISABLE_IMPLICIT_TOKEN", "1")
5
 
6
  import json
 
138
  # ------------------------------------------------------------------
139
  ASSETS_REPO_ID = os.environ.get("ASSETS_REPO_ID", "pixai-labs/pixai-tagger-v0.9")
140
  ASSETS_REVISION = os.environ.get("ASSETS_REVISION")
141
+ ASSETS_HF_TOKEN = os.environ.get("HF_TOKEN")
142
  MODEL_DIR = os.environ.get("MODEL_DIR", "./assets")
143
 
144
  REQUIRED_FILES = [
 
149
 
150
 
151
  def ensure_assets(repo_id: str, revision: Optional[str], target_dir: str) -> None:
152
+ """下载受限标签模型资源,并复制到 handler 期望的本地目录。
153
+
154
+ Args:
155
+ repo_id: Hugging Face 模型仓库 ID。
156
+ revision: 可选模型版本;为空时使用仓库默认版本。
157
+ target_dir: handler 读取模型资源的本地目录。
158
+
159
+ Returns:
160
+ None: 文件已存在或下载复制完成后不返回数据。
161
  """
162
  target = Path(target_dir)
163
  target.mkdir(parents=True, exist_ok=True)
 
170
  repo_id=repo_id,
171
  revision=revision,
172
  allow_patterns=REQUIRED_FILES,
173
+ # 该受限仓库必须鉴权;token 只发往 Hub 下载端点,不参与任何 Job API 请求。
174
+ token=ASSETS_HF_TOKEN or False,
175
  )
176
 
177
  for fname in REQUIRED_FILES:
tests/test_image_inversion_job_api.py CHANGED
@@ -739,8 +739,8 @@ class ImageInversionJobAPITest(unittest.TestCase):
739
  with self.assertRaises(ImageSourceError):
740
  fetch_remote_image("https://allowed.example/rebound", settings)
741
 
742
- def test_app_wires_job_lifespan_and_disables_private_hf_token(self) -> None:
743
- """静态验证应用接线、六字段结果与匿名模型下载设置。"""
744
  app_path = Path(__file__).resolve().parents[1] / "app.py"
745
  app_source = app_path.read_text(encoding="utf-8")
746
  app_tree = ast.parse(app_source)
@@ -764,9 +764,9 @@ class ImageInversionJobAPITest(unittest.TestCase):
764
  self.assertEqual(result_keys, set(EXPECTED_RESULT))
765
  self.assertIn("ssr_mode=False", app_source)
766
  self.assertIn('app_kwargs={"lifespan": JOB_API_LIFESPAN}', app_source)
767
- self.assertIn('token=False', app_source)
 
768
  self.assertIn('HF_HUB_DISABLE_IMPLICIT_TOKEN', app_source)
769
- self.assertNotIn('os.environ.get("HF_TOKEN")', app_source)
770
 
771
 
772
  if __name__ == "__main__":
 
739
  with self.assertRaises(ImageSourceError):
740
  fetch_remote_image("https://allowed.example/rebound", settings)
741
 
742
+ def test_app_wires_job_lifespan_and_scopes_hf_token(self) -> None:
743
+ """静态验证应用接线、六字段结果与受限模型下载令牌边界。"""
744
  app_path = Path(__file__).resolve().parents[1] / "app.py"
745
  app_source = app_path.read_text(encoding="utf-8")
746
  app_tree = ast.parse(app_source)
 
764
  self.assertEqual(result_keys, set(EXPECTED_RESULT))
765
  self.assertIn("ssr_mode=False", app_source)
766
  self.assertIn('app_kwargs={"lifespan": JOB_API_LIFESPAN}', app_source)
767
+ self.assertIn('ASSETS_HF_TOKEN = os.environ.get("HF_TOKEN")', app_source)
768
+ self.assertIn('token=ASSETS_HF_TOKEN or False', app_source)
769
  self.assertIn('HF_HUB_DISABLE_IMPLICIT_TOKEN', app_source)
 
770
 
771
 
772
  if __name__ == "__main__":