Spaces:
Sleeping
Sleeping
Upload upload_hf.py with huggingface_hub
Browse files- upload_hf.py +40 -0
upload_hf.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from huggingface_hub import HfApi
|
| 3 |
+
|
| 4 |
+
def upload_workspace():
|
| 5 |
+
api = HfApi()
|
| 6 |
+
repo_id = os.environ["HF_SPACE_REPO_ID"]
|
| 7 |
+
token = os.environ["HF_TOKEN"]
|
| 8 |
+
ignore_dirs = {".venv", "__pycache__", ".git"}
|
| 9 |
+
ignore_exts = {".pyc", ".pyo"}
|
| 10 |
+
|
| 11 |
+
print(f"Connecting to Hugging Face: {repo_id}...")
|
| 12 |
+
|
| 13 |
+
for root, dirs, files in os.walk("."):
|
| 14 |
+
# filter dirs in place
|
| 15 |
+
dirs[:] = [d for d in dirs if d not in ignore_dirs]
|
| 16 |
+
|
| 17 |
+
for file in files:
|
| 18 |
+
_, ext = os.path.splitext(file)
|
| 19 |
+
if ext in ignore_exts:
|
| 20 |
+
continue
|
| 21 |
+
|
| 22 |
+
local_path = os.path.join(root, file)
|
| 23 |
+
# Make path relative to repo root using forward slashes
|
| 24 |
+
repo_path = os.path.relpath(local_path, ".").replace("\\", "/")
|
| 25 |
+
|
| 26 |
+
print(f"Uploading [{repo_path}] ... ", end="", flush=True)
|
| 27 |
+
try:
|
| 28 |
+
api.upload_file(
|
| 29 |
+
path_or_fileobj=local_path,
|
| 30 |
+
path_in_repo=repo_path,
|
| 31 |
+
repo_id=repo_id,
|
| 32 |
+
repo_type="space",
|
| 33 |
+
token=token
|
| 34 |
+
)
|
| 35 |
+
print("DONE")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
print(f"FAILED: {e}")
|
| 38 |
+
|
| 39 |
+
if __name__ == "__main__":
|
| 40 |
+
upload_workspace()
|