Spaces:
Paused
Paused
| import os | |
| #!/usr/bin/env python3 | |
| """Add environment variables to HuggingFace Space""" | |
| from huggingface_hub import HfApi | |
| import json | |
| import sys | |
| HF_TOKEN = os.environ.get("HF_TOKEN") | |
| SPACE_NAME = "Kraft102/widgetdc-cortex" | |
| print("=" * 50) | |
| print("Adding Environment Variables to HF Space") | |
| print("=" * 50) | |
| print(f"\nSpace: {SPACE_NAME}\n") | |
| # Load environment variables from JSON | |
| with open('env_vars.json', 'r', encoding='utf-8') as f: | |
| env_vars = json.load(f) | |
| print(f"Loaded {len(env_vars)} environment variables\n") | |
| # Initialize API | |
| api = HfApi(token=HF_TOKEN) | |
| # Production overrides for HF Space | |
| production_overrides = { | |
| 'NODE_ENV': 'production', | |
| 'PORT': '7860', | |
| 'LOG_LEVEL': 'info', | |
| 'CORS_ORIGIN': '*', | |
| # Use local Neo4j/Postgres/Redis as they're in .env (localhost) | |
| # These should be changed to remote URLs if available | |
| } | |
| # Merge with overrides | |
| env_vars.update(production_overrides) | |
| # Add each variable as a secret | |
| success_count = 0 | |
| error_count = 0 | |
| for key, value in env_vars.items(): | |
| try: | |
| # Skip empty values | |
| if not value or value == '': | |
| print(f" SKIP {key} (empty value)") | |
| continue | |
| # Add secret to Space | |
| api.add_space_secret( | |
| repo_id=SPACE_NAME, | |
| key=key, | |
| value=value, | |
| token=HF_TOKEN | |
| ) | |
| # Show masked value | |
| masked = value[:10] + "..." if len(value) > 10 else value | |
| print(f" OK {key} = {masked}") | |
| success_count += 1 | |
| except Exception as e: | |
| print(f" ERROR {key}: {e}") | |
| error_count += 1 | |
| print("\n" + "=" * 50) | |
| print(f"SUCCESS: {success_count} variables added") | |
| if error_count > 0: | |
| print(f"ERRORS: {error_count} variables failed") | |
| print("=" * 50) | |
| if error_count == 0: | |
| print("\nAll environment variables configured successfully!") | |
| print(f"\nSpace will restart automatically...") | |
| print(f"Check logs: https://huggingface.co/spaces/{SPACE_NAME}/logs") | |
| sys.exit(0) | |
| else: | |
| print("\nSome variables failed - check errors above") | |
| sys.exit(1) | |