TrueV1sion123 commited on
Commit
dbad084
Β·
verified Β·
1 Parent(s): f05d627

Upload scripts/deploy_to_hf_space.sh with huggingface_hub

Browse files
Files changed (1) hide show
  1. scripts/deploy_to_hf_space.sh +160 -0
scripts/deploy_to_hf_space.sh ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+ # ═══════════════════════════════════════════════════════════════
3
+ # Deploy RAE Training to HuggingFace Spaces
4
+ # ═══════════════════════════════════════════════════════════════
5
+ #
6
+ # Creates an AutoTrain Space with GPU hardware for cloud training.
7
+ # This is the zero-local-GPU path β€” HF handles the compute.
8
+ #
9
+ # Prerequisites:
10
+ # - HF account with billing enabled
11
+ # - HF_TOKEN with write access
12
+ # - huggingface_hub CLI installed
13
+ #
14
+ # Usage:
15
+ # export HF_TOKEN=hf_xxxxx
16
+ # ./scripts/deploy_to_hf_space.sh
17
+ # ═══════════════════════════════════════════════════════════════
18
+
19
+ set -euo pipefail
20
+
21
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
22
+ PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
23
+ cd "$PROJECT_DIR"
24
+
25
+ # ── Configuration ─────────────────────────────────────────────
26
+ SPACE_NAME="${HF_USERNAME:-TrueV1sion123}/rae-training"
27
+ HARDWARE="t4-medium" # Options: cpu-basic, t4-small, t4-medium, a10g-small, a10g-large, a100-large
28
+
29
+ echo "═══════════════════════════════════════════════════════"
30
+ echo " DEPLOY RAE TRAINING TO HF SPACES"
31
+ echo " Space: $SPACE_NAME"
32
+ echo " Hardware: $HARDWARE"
33
+ echo "═══════════════════════════════════════════════════════"
34
+
35
+ # Check token
36
+ if [ -z "${HF_TOKEN:-}" ]; then
37
+ echo "Error: HF_TOKEN not set"
38
+ echo " export HF_TOKEN=hf_your_write_token"
39
+ exit 1
40
+ fi
41
+
42
+ # Install huggingface_hub if needed
43
+ pip install -q huggingface_hub 2>/dev/null || true
44
+
45
+ # ── Option 1: AutoTrain Space (Recommended) ──────────────────
46
+ # Creates a Space using the official AutoTrain Docker image
47
+ # You then upload your data and config through the web UI
48
+
49
+ echo ""
50
+ echo "β–Ά Creating AutoTrain Space..."
51
+ echo " This creates a GPU-backed Space with the AutoTrain UI."
52
+ echo " After creation, upload your training data and start training."
53
+ echo ""
54
+
55
+ python3 << 'PYTHON_SCRIPT'
56
+ import os
57
+ from huggingface_hub import HfApi, create_repo
58
+
59
+ api = HfApi(token=os.environ["HF_TOKEN"])
60
+ space_name = os.environ.get("SPACE_NAME", "rae-training")
61
+ username = api.whoami()["name"]
62
+ repo_id = f"{username}/{space_name}"
63
+
64
+ # Create the Space
65
+ try:
66
+ create_repo(
67
+ repo_id=repo_id,
68
+ repo_type="space",
69
+ space_sdk="docker",
70
+ space_hardware="t4-medium",
71
+ private=True,
72
+ token=os.environ["HF_TOKEN"],
73
+ )
74
+ print(f"βœ“ Space created: https://huggingface.co/spaces/{repo_id}")
75
+ except Exception as e:
76
+ if "already exists" in str(e).lower():
77
+ print(f"βœ“ Space already exists: https://huggingface.co/spaces/{repo_id}")
78
+ else:
79
+ print(f"βœ— Error creating space: {e}")
80
+ raise
81
+
82
+ # Upload the AutoTrain Dockerfile
83
+ dockerfile_content = """FROM huggingface/autotrain-advanced:latest
84
+
85
+ # RAE Training Environment
86
+ COPY configs/autotrain_rae_sft.yaml /app/config.yaml
87
+ COPY data/ /app/data/
88
+
89
+ # Set environment
90
+ ENV AUTOTRAIN_CONFIG=/app/config.yaml
91
+
92
+ # Default command
93
+ CMD ["autotrain", "--config", "/app/config.yaml"]
94
+ """
95
+
96
+ api.upload_file(
97
+ path_or_fileobj=dockerfile_content.encode(),
98
+ path_in_repo="Dockerfile",
99
+ repo_id=repo_id,
100
+ repo_type="space",
101
+ token=os.environ["HF_TOKEN"],
102
+ )
103
+ print("βœ“ Dockerfile uploaded")
104
+
105
+ # Upload config
106
+ api.upload_file(
107
+ path_or_fileobj="configs/autotrain_rae_sft.yaml",
108
+ path_in_repo="configs/autotrain_rae_sft.yaml",
109
+ repo_id=repo_id,
110
+ repo_type="space",
111
+ token=os.environ["HF_TOKEN"],
112
+ )
113
+ print("βœ“ Config uploaded")
114
+
115
+ print(f"\n{'═' * 50}")
116
+ print(f" Space ready: https://huggingface.co/spaces/{repo_id}")
117
+ print(f" Next steps:")
118
+ print(f" 1. Upload training data (data/rae_training_data/)")
119
+ print(f" 2. Start the Space to begin training")
120
+ print(f" 3. Monitor via the Space UI or TensorBoard")
121
+ print(f"{'═' * 50}")
122
+ PYTHON_SCRIPT
123
+
124
+ # ── Option 2: Push dataset to HF Hub ─────────────────────────
125
+ echo ""
126
+ echo "β–Ά Pushing training dataset to Hub..."
127
+
128
+ python3 << 'PYTHON_SCRIPT2'
129
+ import os
130
+ from huggingface_hub import HfApi
131
+
132
+ api = HfApi(token=os.environ["HF_TOKEN"])
133
+ username = api.whoami()["name"]
134
+ dataset_repo = f"{username}/rae-training-data"
135
+
136
+ try:
137
+ api.create_repo(dataset_repo, repo_type="dataset", private=True, exist_ok=True)
138
+
139
+ # Upload training data if it exists
140
+ import glob
141
+ data_files = glob.glob("data/rae_training_data/*")
142
+
143
+ if data_files:
144
+ for f in data_files:
145
+ api.upload_file(
146
+ path_or_fileobj=f,
147
+ path_in_repo=os.path.basename(f),
148
+ repo_id=dataset_repo,
149
+ repo_type="dataset",
150
+ )
151
+ print(f" βœ“ Uploaded {os.path.basename(f)}")
152
+ print(f"βœ“ Dataset repo: https://huggingface.co/datasets/{dataset_repo}")
153
+ else:
154
+ print(" ⚠ No training data found. Run generate_dataset.sh first.")
155
+ except Exception as e:
156
+ print(f" ⚠ Dataset upload: {e}")
157
+ PYTHON_SCRIPT2
158
+
159
+ echo ""
160
+ echo "Deployment complete!"