Download create_dev_space.py from chen470/drl-trading-bot-dev2: direct link, hf CLI and curl.
- Browser
- Download file 3.32 kB
-
https://huggingface.co/spaces/chen470/drl-trading-bot-dev2/resolve/main/create_dev_space.py
- Command line
-
hf download hf://spaces/chen470/drl-trading-bot-dev2/create_dev_space.py
-
curl -L -o create_dev_space.py https://huggingface.co/spaces/chen470/drl-trading-bot-dev2/resolve/main/create_dev_space.py
3.32 kB
| #!/usr/bin/env python3 | |
| """ | |
| Create a dev Space on Hugging Face for testing changes from the dev branch. | |
| This script: | |
| 1. Duplicates the production Space | |
| 2. Configures it with the same secrets | |
| 3. Sets up tracking for the dev branch | |
| """ | |
| import os | |
| from dotenv import load_dotenv | |
| from huggingface_hub import HfApi | |
| # Load environment variables | |
| load_dotenv() | |
| HF_TOKEN = os.getenv("HF_TOKEN") | |
| if not HF_TOKEN: | |
| raise ValueError("HF_TOKEN not found in .env file") | |
| # Initialize API | |
| api = HfApi(token=HF_TOKEN) | |
| # Configuration | |
| PROD_SPACE = "Chen4700/drl-trading-bot" | |
| DEV_SPACE = "Chen4700/drl-trading-bot-dev" | |
| print(f"π Creating dev Space: {DEV_SPACE}") | |
| print(f"π¦ Duplicating from: {PROD_SPACE}") | |
| print() | |
| # Step 1: Duplicate the production Space | |
| print("Step 1: Duplicating Space...") | |
| try: | |
| # Get current space secrets (we'll need to re-add them) | |
| # Note: We can't read secret VALUES, but we know what keys exist | |
| # Duplicate the space | |
| api.duplicate_space( | |
| from_id=PROD_SPACE, | |
| to_id=DEV_SPACE, | |
| private=True, # Keep dev space private | |
| hardware="cpu-basic", # Use free tier for dev | |
| ) | |
| print(f"β Space duplicated successfully: https://huggingface.co/spaces/{DEV_SPACE}") | |
| except Exception as e: | |
| if "already exists" in str(e).lower(): | |
| print(f"β οΈ Space {DEV_SPACE} already exists") | |
| else: | |
| print(f"β Error duplicating Space: {e}") | |
| raise | |
| # Step 2: Add necessary secrets and variables to dev Space | |
| print("\nStep 2: Configuring secrets and environment...") | |
| # Add ENVIRONMENT variable to distinguish dev from prod | |
| try: | |
| api.add_space_variable(repo_id=DEV_SPACE, key="ENVIRONMENT", value="dev") | |
| print(f" β Added variable: ENVIRONMENT=dev") | |
| except Exception as e: | |
| print(f" β οΈ Could not add ENVIRONMENT variable: {e}") | |
| # Add secrets | |
| secrets_to_add = { | |
| "HF_TOKEN": HF_TOKEN, | |
| "BINANCE_API_KEY": os.getenv("BINANCE_API_KEY", ""), | |
| "BINANCE_SECRET": os.getenv("BINANCE_SECRET", ""), | |
| "MONGO_URI": os.getenv("MONGO_URI", ""), | |
| "HELIUS_API_KEY": os.getenv("HELIUS_API_KEY", ""), | |
| } | |
| for key, value in secrets_to_add.items(): | |
| if value: | |
| try: | |
| api.add_space_secret(repo_id=DEV_SPACE, key=key, value=value) | |
| print(f" β Added secret: {key}") | |
| except Exception as e: | |
| print(f" β οΈ Could not add {key}: {e}") | |
| else: | |
| print(f" β οΈ Skipping {key} (not found in .env)") | |
| print("\n" + "="*60) | |
| print("π Dev Space Setup Complete!") | |
| print("="*60) | |
| print() | |
| print(f"Production Space: https://huggingface.co/spaces/{PROD_SPACE}") | |
| print(f"Dev Space: https://huggingface.co/spaces/{DEV_SPACE}") | |
| print() | |
| print("Next Steps:") | |
| print("1. The dev Space is now running (it will auto-deploy from main branch initially)") | |
| print("2. To switch it to dev branch, push your dev branch to the Space:") | |
| print() | |
| print(" cd /Users/chenluigi/WebstormProjects/drl-trading-system") | |
| print(f" git remote add hf-dev https://huggingface.co/spaces/{DEV_SPACE}") | |
| print(" git push hf-dev dev:main --force") | |
| print() | |
| print(" This pushes your local 'dev' branch to the Space's 'main' branch") | |
| print() | |
| print("3. The Space will automatically rebuild with your dev branch code") | |
| print() | |
| print("β οΈ IMPORTANT: The production Space remains untouched!") | |
| print() | |