Spaces:
Paused
Paused
Bulk upload all files
Browse files- push_update.py +64 -0
- upload_all.py +32 -0
push_update.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import time
|
| 4 |
+
from huggingface_hub import HfApi
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def main():
|
| 8 |
+
token = os.environ.get("HF_TOKEN")
|
| 9 |
+
repo_id = os.environ.get("HF_SPACE_ID")
|
| 10 |
+
if not token or not repo_id:
|
| 11 |
+
print("Missing HF_TOKEN or HF_SPACE_ID env vars", file=sys.stderr)
|
| 12 |
+
sys.exit(1)
|
| 13 |
+
|
| 14 |
+
api = HfApi(token=token)
|
| 15 |
+
|
| 16 |
+
base_dir = os.path.dirname(__file__)
|
| 17 |
+
files_to_push = [
|
| 18 |
+
(os.path.join(base_dir, "requirements.txt"), "requirements.txt"),
|
| 19 |
+
(os.path.join(base_dir, "flux_pipeline.py"), "flux_pipeline.py"),
|
| 20 |
+
(os.path.join(base_dir, "app.py"), "app.py"),
|
| 21 |
+
(os.path.join(base_dir, "Dockerfile"), "Dockerfile"),
|
| 22 |
+
]
|
| 23 |
+
for local, remote in files_to_push:
|
| 24 |
+
if not os.path.exists(local):
|
| 25 |
+
print(f"Missing {local}", file=sys.stderr)
|
| 26 |
+
sys.exit(1)
|
| 27 |
+
print(f"Uploading {remote} to {repo_id}...")
|
| 28 |
+
api.upload_file(
|
| 29 |
+
path_or_fileobj=local,
|
| 30 |
+
path_in_repo=remote,
|
| 31 |
+
repo_id=repo_id,
|
| 32 |
+
repo_type="space",
|
| 33 |
+
)
|
| 34 |
+
print("Uploaded files. Restarting Space...")
|
| 35 |
+
try:
|
| 36 |
+
api.restart_space(repo_id=repo_id)
|
| 37 |
+
print("Restart triggered.")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Warning: restart_space failed: {e}")
|
| 40 |
+
|
| 41 |
+
# Poll runtime until RUNNING
|
| 42 |
+
print("Waiting for Space to enter RUNNING stage...")
|
| 43 |
+
start = time.time()
|
| 44 |
+
last_stage = None
|
| 45 |
+
while time.time() - start < 900: # up to 15 minutes
|
| 46 |
+
try:
|
| 47 |
+
rt = api.get_space_runtime(repo_id)
|
| 48 |
+
stage = getattr(rt, "stage", None)
|
| 49 |
+
if stage != last_stage:
|
| 50 |
+
print(f"Stage: {stage}")
|
| 51 |
+
last_stage = stage
|
| 52 |
+
if stage in ("RUNNING", "SUCCESS"):
|
| 53 |
+
print("Space is running.")
|
| 54 |
+
return
|
| 55 |
+
except Exception as e:
|
| 56 |
+
# transient errors during rebuild
|
| 57 |
+
pass
|
| 58 |
+
time.sleep(5)
|
| 59 |
+
print("Timeout waiting for Space to run.", file=sys.stderr)
|
| 60 |
+
sys.exit(2)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
if __name__ == "__main__":
|
| 64 |
+
main()
|
upload_all.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
from pathlib import Path
|
| 4 |
+
from huggingface_hub import HfApi, upload_folder
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def main():
|
| 8 |
+
token = os.environ.get("HF_TOKEN")
|
| 9 |
+
repo_id = os.environ.get("HF_SPACE_ID")
|
| 10 |
+
if not token or not repo_id:
|
| 11 |
+
print("Missing HF_TOKEN or HF_SPACE_ID", file=sys.stderr)
|
| 12 |
+
sys.exit(1)
|
| 13 |
+
api = HfApi(token=token)
|
| 14 |
+
src_dir = Path(__file__).parent
|
| 15 |
+
print(f"Uploading folder {src_dir} to {repo_id}...")
|
| 16 |
+
upload_folder(
|
| 17 |
+
repo_id=repo_id,
|
| 18 |
+
repo_type="space",
|
| 19 |
+
folder_path=str(src_dir),
|
| 20 |
+
token=token,
|
| 21 |
+
commit_message="Bulk upload all files",
|
| 22 |
+
ignore_patterns=["__pycache__", "*.pyc", ".venv", "deploy_hf_space.py"],
|
| 23 |
+
)
|
| 24 |
+
print("Uploaded. Restarting Space...")
|
| 25 |
+
try:
|
| 26 |
+
api.restart_space(repo_id=repo_id)
|
| 27 |
+
except Exception as e:
|
| 28 |
+
print(f"Warning: restart failed: {e}")
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
if __name__ == "__main__":
|
| 32 |
+
main()
|