G-ACE commited on
Commit
8cbec40
·
verified ·
1 Parent(s): 9a57657

Deploy GitHub main ca449d5337ec43b17fca151e0b75552b8961dafe

Browse files
Files changed (1) hide show
  1. scripts/provision_hf.py +34 -0
scripts/provision_hf.py CHANGED
@@ -4,8 +4,10 @@
4
  # ///
5
  from __future__ import annotations
6
 
 
7
  import json
8
  import os
 
9
  import tempfile
10
  import time
11
  from pathlib import Path
@@ -49,12 +51,43 @@ def runtime_environment() -> dict[str, str]:
49
  else:
50
  missing.append(name)
51
 
 
 
 
 
 
 
52
  if missing:
53
  # Upload and Space configuration do not delete existing HF secrets.
54
  print("HF_RUNTIME_SECRETS_PRESERVED=" + ",".join(missing))
55
  return values
56
 
57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
  def verify_bucket(api: HfApi) -> None:
59
  payload = json.dumps(
60
  {
@@ -160,6 +193,7 @@ def main() -> None:
160
  if not bucket:
161
  raise RuntimeError("HF_BUCKET_CREATE_FAILED")
162
  print(f"HF_PRIVATE_BUCKET_READY={BUCKET_ID}")
 
163
 
164
  volume = Volume(
165
  type="bucket",
 
4
  # ///
5
  from __future__ import annotations
6
 
7
+ import base64
8
  import json
9
  import os
10
+ import secrets
11
  import tempfile
12
  import time
13
  from pathlib import Path
 
51
  else:
52
  missing.append(name)
53
 
54
+ if "CUSTOMER_AI_HMAC_SECRET" not in values:
55
+ generated = base64.b64encode(secrets.token_bytes(48)).decode("ascii")
56
+ values["CUSTOMER_AI_HMAC_SECRET"] = "base64:" + generated
57
+ print("HF_RUNTIME_HMAC_SECRET_BOOTSTRAPPED=true")
58
+ missing = [name for name in missing if name != "CUSTOMER_AI_HMAC_SECRET"]
59
+
60
  if missing:
61
  # Upload and Space configuration do not delete existing HF secrets.
62
  print("HF_RUNTIME_SECRETS_PRESERVED=" + ",".join(missing))
63
  return values
64
 
65
 
66
+ def inspect_persisted_snapshot(api: HfApi) -> None:
67
+ with tempfile.TemporaryDirectory(prefix="hf-kb-manifest-") as temporary:
68
+ local = Path(temporary) / "manifest.json"
69
+ api.download_bucket_files(
70
+ BUCKET_ID,
71
+ files=[("kb/manifest.json", local)],
72
+ raise_on_missing_files=False,
73
+ )
74
+ if not local.exists():
75
+ print("HF_PERSISTED_KB_MANIFEST=missing")
76
+ return
77
+ try:
78
+ manifest = json.loads(local.read_text(encoding="utf-8"))
79
+ except Exception as error:
80
+ print(f"HF_PERSISTED_KB_MANIFEST=invalid:{type(error).__name__}")
81
+ return
82
+ safe = {
83
+ "version": manifest.get("version"),
84
+ "page_count": manifest.get("page_count"),
85
+ "index_count": manifest.get("index_count"),
86
+ "schema_version": manifest.get("schema_version"),
87
+ }
88
+ print("HF_PERSISTED_KB_MANIFEST=" + json.dumps(safe, sort_keys=True))
89
+
90
+
91
  def verify_bucket(api: HfApi) -> None:
92
  payload = json.dumps(
93
  {
 
193
  if not bucket:
194
  raise RuntimeError("HF_BUCKET_CREATE_FAILED")
195
  print(f"HF_PRIVATE_BUCKET_READY={BUCKET_ID}")
196
+ inspect_persisted_snapshot(api)
197
 
198
  volume = Volume(
199
  type="bucket",