File size: 1,599 Bytes
30e5fc4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
#!/usr/bin/env python3
"""
Helper script to set Hugging Face Space secrets from the terminal.
Requires `huggingface_hub` to be installed and logged in.
"""
import os
import getpass
from huggingface_hub import HfApi
def set_secrets():
print("π Hugging Face Space Secrets Manager")
print("-------------------------------------")
# Get Repo ID
default_repo = os.getenv("GITHUB_REPO") # Fallback or try to detect
repo_id = input(f"Enter Space Repo ID (e.g., username/space-name): ").strip()
if not repo_id:
print("β Repo ID is required.")
return
api = HfApi()
secrets_to_set = [
"ANTHROPIC_API_KEY",
"OPENAI_API_KEY",
"GOOGLE_API_KEY",
"HF_TOKEN"
]
print(f"\nSetting secrets for: {repo_id}")
print("Press Enter to skip a secret if you don't want to update it.\n")
for secret in secrets_to_set:
value = getpass.getpass(f"Enter value for {secret}: ").strip()
if value:
try:
api.add_space_secret(repo_id=repo_id, key=secret, value=value)
print(f"β
Set {secret}")
except Exception as e:
print(f"β Failed to set {secret}: {e}")
else:
print(f"βοΈ Skipped {secret}")
print("\n⨠Done! Your secrets are updated.")
if __name__ == "__main__":
try:
set_secrets()
except ImportError:
print("β 'huggingface_hub' library not found. Run: pip install huggingface_hub")
except Exception as e:
print(f"β An error occurred: {e}")
|