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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Setup HF Spaces Secrets via API
|
| 4 |
+
Easily configure sensitive credentials for your Space
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import os
|
| 8 |
+
import sys
|
| 9 |
+
import requests
|
| 10 |
+
import json
|
| 11 |
+
from typing import Dict, Tuple
|
| 12 |
+
|
| 13 |
+
def add_space_secret(hf_token: str, space_id: str, key: str, value: str) -> Tuple[bool, str]:
|
| 14 |
+
"""
|
| 15 |
+
Add a secret to HF Space via API.
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
hf_token: Hugging Face API token
|
| 19 |
+
space_id: Space ID (username/space-name)
|
| 20 |
+
key: Secret key name
|
| 21 |
+
value: Secret value
|
| 22 |
+
|
| 23 |
+
Returns:
|
| 24 |
+
Tuple of (success: bool, message: str)
|
| 25 |
+
"""
|
| 26 |
+
|
| 27 |
+
headers = {"Authorization": f"Bearer {hf_token}"}
|
| 28 |
+
url = f"https://huggingface.co/api/spaces/{space_id}/secrets"
|
| 29 |
+
data = {"key": key, "value": value}
|
| 30 |
+
|
| 31 |
+
try:
|
| 32 |
+
response = requests.post(url, json=data, headers=headers)
|
| 33 |
+
|
| 34 |
+
if response.status_code == 200:
|
| 35 |
+
return True, f"β Secret '{key}' added successfully"
|
| 36 |
+
elif response.status_code == 409:
|
| 37 |
+
return False, f"β οΈ Secret '{key}' already exists (remove it first to update)"
|
| 38 |
+
else:
|
| 39 |
+
return False, f"β Error ({response.status_code}): {response.text[:100]}"
|
| 40 |
+
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return False, f"β Connection error: {str(e)}"
|
| 43 |
+
|
| 44 |
+
def list_space_secrets(hf_token: str, space_id: str) -> Dict:
|
| 45 |
+
"""List all secrets in a Space."""
|
| 46 |
+
headers = {"Authorization": f"Bearer {hf_token}"}
|
| 47 |
+
url = f"https://huggingface.co/api/spaces/{space_id}/secrets"
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
response = requests.get(url, headers=headers)
|
| 51 |
+
if response.status_code == 200:
|
| 52 |
+
return response.json()
|
| 53 |
+
else:
|
| 54 |
+
return {"error": f"Error ({response.status_code})"}
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return {"error": str(e)}
|
| 57 |
+
|
| 58 |
+
def delete_space_secret(hf_token: str, space_id: str, key: str) -> Tuple[bool, str]:
|
| 59 |
+
"""Delete a secret from a Space."""
|
| 60 |
+
headers = {"Authorization": f"Bearer {hf_token}"}
|
| 61 |
+
url = f"https://huggingface.co/api/spaces/{space_id}/secrets/{key}"
|
| 62 |
+
|
| 63 |
+
try:
|
| 64 |
+
response = requests.delete(url, headers=headers)
|
| 65 |
+
if response.status_code == 204:
|
| 66 |
+
return True, f"β Secret '{key}' deleted"
|
| 67 |
+
else:
|
| 68 |
+
return False, f"β Error ({response.status_code})"
|
| 69 |
+
except Exception as e:
|
| 70 |
+
return False, f"β Error: {str(e)}"
|
| 71 |
+
|
| 72 |
+
def main():
|
| 73 |
+
print("\n" + "="*70)
|
| 74 |
+
print("π HF Spaces Secrets Manager")
|
| 75 |
+
print("="*70)
|
| 76 |
+
|
| 77 |
+
# Get HF token
|
| 78 |
+
hf_token = os.environ.get("HF_TOKEN")
|
| 79 |
+
if not hf_token:
|
| 80 |
+
print("\nβ ERROR: HF_TOKEN not set")
|
| 81 |
+
print(" Set it with: export HF_TOKEN='hf_...'")
|
| 82 |
+
print(" Get your token: https://huggingface.co/settings/tokens")
|
| 83 |
+
sys.exit(1)
|
| 84 |
+
|
| 85 |
+
# Get Space ID
|
| 86 |
+
import argparse
|
| 87 |
+
parser = argparse.ArgumentParser(description="Configure HF Spaces secrets")
|
| 88 |
+
parser.add_argument("--space", required=True, help="Space ID (username/space-name)")
|
| 89 |
+
parser.add_argument("--qwen-key", help="Qwen API key")
|
| 90 |
+
parser.add_argument("--qwen-url", help="Qwen base URL")
|
| 91 |
+
parser.add_argument("--qwen-model", help="Qwen model name")
|
| 92 |
+
parser.add_argument("--list", action="store_true", help="List all secrets")
|
| 93 |
+
parser.add_argument("--delete", help="Delete a secret by key")
|
| 94 |
+
|
| 95 |
+
args = parser.parse_args()
|
| 96 |
+
space_id = args.space
|
| 97 |
+
|
| 98 |
+
print(f"\nπ Space: {space_id}")
|
| 99 |
+
print(f"π Using HF Token: {hf_token[:20]}...")
|
| 100 |
+
|
| 101 |
+
# List secrets
|
| 102 |
+
if args.list:
|
| 103 |
+
print("\n[Listing secrets...]")
|
| 104 |
+
secrets = list_space_secrets(hf_token, space_id)
|
| 105 |
+
if "error" in secrets:
|
| 106 |
+
print(f"β Error: {secrets['error']}")
|
| 107 |
+
else:
|
| 108 |
+
for s in secrets:
|
| 109 |
+
print(f" β’ {s.get('name', 'unknown')}")
|
| 110 |
+
return
|
| 111 |
+
|
| 112 |
+
# Delete secret
|
| 113 |
+
if args.delete:
|
| 114 |
+
print(f"\n[Deleting secret: {args.delete}]")
|
| 115 |
+
success, message = delete_space_secret(hf_token, space_id, args.delete)
|
| 116 |
+
print(f" {message}")
|
| 117 |
+
return
|
| 118 |
+
|
| 119 |
+
# Add secrets
|
| 120 |
+
secrets_to_add = []
|
| 121 |
+
|
| 122 |
+
if args.qwen_key:
|
| 123 |
+
secrets_to_add.append(("QWEN_API_KEY", args.qwen_key))
|
| 124 |
+
|
| 125 |
+
if args.qwen_url:
|
| 126 |
+
secrets_to_add.append(("QWEN_BASE_URL", args.qwen_url))
|
| 127 |
+
|
| 128 |
+
if args.qwen_model:
|
| 129 |
+
secrets_to_add.append(("QWEN_MODEL", args.qwen_model))
|
| 130 |
+
|
| 131 |
+
if not secrets_to_add:
|
| 132 |
+
print("\n[Interactive mode]")
|
| 133 |
+
print("\nEnter secrets (leave blank to skip):")
|
| 134 |
+
|
| 135 |
+
qwen_key = input(" QWEN_API_KEY [sk-...]: ").strip()
|
| 136 |
+
if qwen_key:
|
| 137 |
+
secrets_to_add.append(("QWEN_API_KEY", qwen_key))
|
| 138 |
+
|
| 139 |
+
qwen_url = input(" QWEN_BASE_URL [leave blank]: ").strip()
|
| 140 |
+
if qwen_url:
|
| 141 |
+
secrets_to_add.append(("QWEN_BASE_URL", qwen_url))
|
| 142 |
+
|
| 143 |
+
qwen_model = input(" QWEN_MODEL [leave blank]: ").strip()
|
| 144 |
+
if qwen_model:
|
| 145 |
+
secrets_to_add.append(("QWEN_MODEL", qwen_model))
|
| 146 |
+
|
| 147 |
+
if not secrets_to_add:
|
| 148 |
+
print("\nβ No secrets to add")
|
| 149 |
+
return
|
| 150 |
+
|
| 151 |
+
# Add secrets
|
| 152 |
+
print("\n[Adding secrets]")
|
| 153 |
+
success_count = 0
|
| 154 |
+
|
| 155 |
+
for key, value in secrets_to_add:
|
| 156 |
+
success, message = add_space_secret(hf_token, space_id, key, value)
|
| 157 |
+
print(f" {message}")
|
| 158 |
+
if success:
|
| 159 |
+
success_count += 1
|
| 160 |
+
|
| 161 |
+
# Summary
|
| 162 |
+
print("\n" + "="*70)
|
| 163 |
+
print(f"β Configured {success_count}/{len(secrets_to_add)} secrets")
|
| 164 |
+
print(f"π Visit: https://huggingface.co/spaces/{space_id}")
|
| 165 |
+
print("="*70 + "\n")
|
| 166 |
+
|
| 167 |
+
if __name__ == "__main__":
|
| 168 |
+
main()
|