Spaces:
Sleeping
Sleeping
hf-actions
commited on
Commit
·
3513553
1
Parent(s):
102b219
feat: enable daily runs by default; add set_hf_secrets.py helper
Browse files- app.py +2 -1
- set_hf_secrets.py +90 -0
app.py
CHANGED
|
@@ -124,7 +124,8 @@ if __name__ == "__main__":
|
|
| 124 |
|
| 125 |
return ok
|
| 126 |
|
| 127 |
-
|
|
|
|
| 128 |
if run_daily:
|
| 129 |
logger.info("RUN_DAILY_REPLICATE enabled — validating tokens before starting background job")
|
| 130 |
if validate_tokens():
|
|
|
|
| 124 |
|
| 125 |
return ok
|
| 126 |
|
| 127 |
+
# Default to true so the Space runs daily by default; override via Space Secrets if needed
|
| 128 |
+
run_daily = os.getenv("RUN_DAILY_REPLICATE", "true").lower() in ("1", "true", "yes")
|
| 129 |
if run_daily:
|
| 130 |
logger.info("RUN_DAILY_REPLICATE enabled — validating tokens before starting background job")
|
| 131 |
if validate_tokens():
|
set_hf_secrets.py
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""set_hf_secrets.py
|
| 2 |
+
|
| 3 |
+
Simple helper to add/update Hugging Face Space Secrets via the HF HTTP API.
|
| 4 |
+
|
| 5 |
+
Usage (from repo root):
|
| 6 |
+
python set_hf_secrets.py --owner <username> --repo <repo> # reads secrets from .env
|
| 7 |
+
|
| 8 |
+
The script reads a set of known keys from `.env` and will POST them to
|
| 9 |
+
the Space secrets endpoint using the HF token found in `.env` under HF_TOKEN or HF.
|
| 10 |
+
|
| 11 |
+
Warning: ensure your `.env` is safe and do not commit it.
|
| 12 |
+
"""
|
| 13 |
+
import os
|
| 14 |
+
import sys
|
| 15 |
+
import json
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
|
| 18 |
+
try:
|
| 19 |
+
from dotenv import load_dotenv
|
| 20 |
+
except Exception:
|
| 21 |
+
load_dotenv = None
|
| 22 |
+
|
| 23 |
+
import requests
|
| 24 |
+
|
| 25 |
+
ROOT = Path.cwd()
|
| 26 |
+
|
| 27 |
+
def load_token():
|
| 28 |
+
if load_dotenv:
|
| 29 |
+
p = ROOT / '.env'
|
| 30 |
+
if p.exists():
|
| 31 |
+
load_dotenv(p)
|
| 32 |
+
for k in ('HF_TOKEN', 'HUGGINGFACEHUB_API_TOKEN', 'HUGGINGFACE_TOKEN', 'HF'):
|
| 33 |
+
v = os.getenv(k)
|
| 34 |
+
if v:
|
| 35 |
+
return v
|
| 36 |
+
return None
|
| 37 |
+
|
| 38 |
+
DEFAULT_KEYS = [
|
| 39 |
+
'FB_PAGE_ACCESS_TOKEN',
|
| 40 |
+
'FB_PAGE_ID',
|
| 41 |
+
'REPLICATE_API_TOKEN',
|
| 42 |
+
'OPENAI_API_KEY',
|
| 43 |
+
'RUN_DAILY_REPLICATE',
|
| 44 |
+
'DAILY_INTERVAL_HOURS',
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
def set_secret(token: str, owner: str, repo: str, key: str, value: str):
|
| 48 |
+
repo_id = f"{owner}/{repo}"
|
| 49 |
+
url = f"https://huggingface.co/api/spaces/{owner}/{repo}/secrets"
|
| 50 |
+
headers = {"Authorization": f"Bearer {token}", "Content-Type": "application/json"}
|
| 51 |
+
payload = {"key": key, "value": value}
|
| 52 |
+
r = requests.post(url, headers=headers, data=json.dumps(payload), timeout=15)
|
| 53 |
+
if r.status_code not in (200, 201):
|
| 54 |
+
raise RuntimeError(f"Failed setting secret {key}: {r.status_code} {r.text}")
|
| 55 |
+
return r.json()
|
| 56 |
+
|
| 57 |
+
def main():
|
| 58 |
+
import argparse
|
| 59 |
+
parser = argparse.ArgumentParser()
|
| 60 |
+
parser.add_argument('--owner', required=True, help='HF username or org (e.g. gowshiselva)')
|
| 61 |
+
parser.add_argument('--repo', default=ROOT.name, help='Space repo name (default: repo folder name)')
|
| 62 |
+
parser.add_argument('--keys', nargs='*', help='List of env keys to publish (default: common keys)')
|
| 63 |
+
args = parser.parse_args()
|
| 64 |
+
|
| 65 |
+
token = load_token()
|
| 66 |
+
if not token:
|
| 67 |
+
print('No HF token found in .env (HF_TOKEN/HF). Aborting.')
|
| 68 |
+
sys.exit(2)
|
| 69 |
+
|
| 70 |
+
keys = args.keys or DEFAULT_KEYS
|
| 71 |
+
to_publish = {}
|
| 72 |
+
for k in keys:
|
| 73 |
+
v = os.getenv(k)
|
| 74 |
+
if v is not None and v != '':
|
| 75 |
+
to_publish[k] = v
|
| 76 |
+
|
| 77 |
+
if not to_publish:
|
| 78 |
+
print('No configured keys found in environment to publish. Exiting.')
|
| 79 |
+
return
|
| 80 |
+
|
| 81 |
+
print(f'Publishing {len(to_publish)} secrets to {args.owner}/{args.repo}...')
|
| 82 |
+
for k, v in to_publish.items():
|
| 83 |
+
try:
|
| 84 |
+
res = set_secret(token, args.owner, args.repo, k, v)
|
| 85 |
+
print('OK', k)
|
| 86 |
+
except Exception as e:
|
| 87 |
+
print('ERROR', k, str(e))
|
| 88 |
+
|
| 89 |
+
if __name__ == '__main__':
|
| 90 |
+
main()
|