davidtran999 commited on
Commit
ffbdfbe
·
verified ·
1 Parent(s): 3044b83

Upload backend/hue_portal/hue-portal-backendDocker/check_space_vars.py with huggingface_hub

Browse files
backend/hue_portal/hue-portal-backendDocker/check_space_vars.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Script để kiểm tra tất cả variables và secrets trên HF Space để tìm collisions.
4
+ """
5
+
6
+ from __future__ import annotations
7
+
8
+ import sys
9
+ from pathlib import Path
10
+
11
+ from huggingface_hub import HfApi, login
12
+
13
+
14
+ DEFAULT_SPACE_ID = "davidtran999/hue-portal-backend"
15
+
16
+
17
+ def get_hf_token() -> str | None:
18
+ """Resolve Hugging Face token from env or cache file."""
19
+ import os
20
+
21
+ if os.environ.get("HF_TOKEN"):
22
+ return os.environ["HF_TOKEN"].strip()
23
+ cache_file = Path.home() / ".cache" / "huggingface" / "token"
24
+ if cache_file.exists():
25
+ return cache_file.read_text(encoding="utf-8").strip()
26
+ return None
27
+
28
+
29
+ def check_collisions(space_id: str) -> None:
30
+ """Kiểm tra và liệt kê tất cả variables và secrets để tìm collisions."""
31
+ hf_token = get_hf_token()
32
+ if not hf_token:
33
+ print("❌ Không tìm thấy HF token. Chạy `huggingface-cli login` hoặc set HF_TOKEN.")
34
+ sys.exit(1)
35
+
36
+ login(token=hf_token)
37
+ api = HfApi()
38
+
39
+ print(f"🔍 Đang kiểm tra Space: {space_id}")
40
+ print("=" * 60)
41
+
42
+ # Lấy tất cả variables
43
+ try:
44
+ variables = api.get_space_variables(repo_id=space_id)
45
+ print(f"\n📋 Variables ({len(variables)}):")
46
+ var_keys = set()
47
+ for key, value in variables.items():
48
+ var_keys.add(key)
49
+ masked_value = value[:20] + "..." if len(value) > 20 else value
50
+ print(f" - {key}: {masked_value}")
51
+ except Exception as e:
52
+ print(f"⚠️ Không thể lấy variables: {e}")
53
+ var_keys = set()
54
+
55
+ # Lấy tất cả secrets
56
+ try:
57
+ secrets = api.get_space_secrets(repo_id=space_id)
58
+ print(f"\n🔐 Secrets ({len(secrets)}):")
59
+ secret_keys = set()
60
+ for key in secrets.keys():
61
+ secret_keys.add(key)
62
+ print(f" - {key}: ***")
63
+ except Exception as e:
64
+ print(f"⚠️ Không thể lấy secrets: {e}")
65
+ secret_keys = set()
66
+
67
+ # Tìm collisions
68
+ collisions = var_keys & secret_keys
69
+ if collisions:
70
+ print(f"\n❌ Tìm thấy {len(collisions)} collision(s):")
71
+ for key in collisions:
72
+ print(f" - {key} (có cả variable và secret)")
73
+ else:
74
+ print(f"\n✅ Không có collision nào!")
75
+
76
+ print("=" * 60)
77
+
78
+
79
+ def main() -> None:
80
+ import argparse
81
+
82
+ parser = argparse.ArgumentParser(description="Kiểm tra collisions trên HF Space")
83
+ parser.add_argument(
84
+ "--space-id",
85
+ default=DEFAULT_SPACE_ID,
86
+ help="ID của Space",
87
+ )
88
+ args = parser.parse_args()
89
+
90
+ check_collisions(args.space_id)
91
+
92
+
93
+ if __name__ == "__main__":
94
+ main()
95
+
96
+
97
+
98
+