| |
| """Upload secrets from a .env file to a Hugging Face Space. |
| |
| Usage: |
| python scripts/upload_secrets.py <space_id> <env_file> |
| |
| Example: |
| python scripts/upload_secrets.py zequn-fireworks/fireaction-a2a .env |
| """ |
|
|
| from __future__ import annotations |
|
|
| import sys |
| from pathlib import Path |
|
|
| from huggingface_hub import HfApi |
|
|
|
|
| def parse_env_file(path: Path) -> dict[str, str]: |
| """Parse a .env file into a dict, skipping comments and blank lines.""" |
| secrets = {} |
| for line in path.read_text().splitlines(): |
| line = line.strip() |
| if not line or line.startswith("#"): |
| continue |
| if "=" not in line: |
| continue |
| key, _, value = line.partition("=") |
| key = key.strip() |
| value = value.strip().strip("'\"") |
| if key and value: |
| secrets[key] = value |
| return secrets |
|
|
|
|
| def main() -> None: |
| if len(sys.argv) != 3: |
| print(f"Usage: {sys.argv[0]} <space_id> <env_file>") |
| print(f"Example: {sys.argv[0]} zequn-fireworks/fireaction-a2a .env") |
| sys.exit(1) |
|
|
| space_id = sys.argv[1] |
| env_file = Path(sys.argv[2]) |
|
|
| if not env_file.exists(): |
| print(f"Error: {env_file} not found") |
| sys.exit(1) |
|
|
| secrets = parse_env_file(env_file) |
| if not secrets: |
| print(f"No secrets found in {env_file}") |
| sys.exit(1) |
|
|
| api = HfApi() |
| print(f"Uploading {len(secrets)} secrets to {space_id}...") |
|
|
| for key, value in secrets.items(): |
| api.add_space_secret(repo_id=space_id, key=key, value=value) |
| print(f" + {key}") |
|
|
| print("Done.") |
|
|
|
|
| if __name__ == "__main__": |
| main() |
|
|