File size: 1,155 Bytes
5057096
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# upload_env_to_hf.py
from huggingface_hub import HfApi
import os, re

SPACE_ID = "Mohamed424/stroke-segmentation-backend"
ENV_PATH = ".env"

# Keys that should be stored as **Secrets** (private)
SECRET_KEYS = {
    "SECRET_KEY",
    "MONGODB_URL",
    "CLOUDINARY_API_KEY",
    "CLOUDINARY_API_SECRET",
}

api = HfApi()

def parse_env(path):
    items = []
    with open(path, "r", encoding="utf-8") as f:
        for raw in f:
            line = raw.strip()
            if not line or line.startswith("#"):
                continue
            # split on the first "=" only
            if "=" not in line:
                continue
            k, v = line.split("=", 1)
            k = k.strip()
            v = v.strip().strip('"').strip("'")
            items.append((k, v))
    return items

pairs = parse_env(ENV_PATH)

for key, val in pairs:
    if key in SECRET_KEYS:
        print(f"[secret] {key}")
        api.add_space_secret(repo_id=SPACE_ID, key=key, value=val)
    else:
        print(f"[variable] {key}")
        api.add_space_variable(repo_id=SPACE_ID, key=key, value=val)

print("✅ Done. Variables & secrets are now set on the Space.")