File size: 2,097 Bytes
5a81b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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)