Spaces:
Sleeping
Sleeping
Delete upload_to_hf.py with huggingface_hub
Browse files- upload_to_hf.py +0 -113
upload_to_hf.py
DELETED
|
@@ -1,113 +0,0 @@
|
|
| 1 |
-
import os
|
| 2 |
-
from huggingface_hub import HfApi
|
| 3 |
-
from dotenv import load_dotenv
|
| 4 |
-
|
| 5 |
-
load_dotenv()
|
| 6 |
-
|
| 7 |
-
REPO_ID = "hisham-cmd/VortexCommerceBackend"
|
| 8 |
-
HF_TOKEN = os.environ.get("HF_TOKEN")
|
| 9 |
-
|
| 10 |
-
api = HfApi(token=HF_TOKEN)
|
| 11 |
-
|
| 12 |
-
def get_files_to_upload():
|
| 13 |
-
files = []
|
| 14 |
-
ignore_dirs = {".git", "__pycache__", ".venv", "venv", "env", "node_modules", ".pytest_cache"}
|
| 15 |
-
ignore_files = {".env", "vortex.db"}
|
| 16 |
-
ignore_extensions = {".db", ".sqlite", ".sqlite3", ".pyc"}
|
| 17 |
-
|
| 18 |
-
for root, dirs, files_list in os.walk("."):
|
| 19 |
-
# Modify dirs in-place to exclude ignored directories
|
| 20 |
-
for d in list(dirs):
|
| 21 |
-
if d in ignore_dirs:
|
| 22 |
-
dirs.remove(d)
|
| 23 |
-
|
| 24 |
-
for f in files_list:
|
| 25 |
-
if f in ignore_files or any(f.endswith(ext) for ext in ignore_extensions):
|
| 26 |
-
continue
|
| 27 |
-
|
| 28 |
-
file_path = os.path.join(root, f)
|
| 29 |
-
# Remove leading .\ or ./
|
| 30 |
-
prefix = f".{os.sep}"
|
| 31 |
-
if file_path.startswith(prefix):
|
| 32 |
-
repo_path = file_path[len(prefix):].replace("\\", "/")
|
| 33 |
-
else:
|
| 34 |
-
repo_path = file_path.replace("\\", "/")
|
| 35 |
-
|
| 36 |
-
files.append((file_path, repo_path))
|
| 37 |
-
|
| 38 |
-
return files
|
| 39 |
-
|
| 40 |
-
def wipe_remote():
|
| 41 |
-
"""Wipes all files from the remote repository to ensure a clean sync."""
|
| 42 |
-
print(f"\n>>> Wiping remote repository: {REPO_ID}...")
|
| 43 |
-
try:
|
| 44 |
-
# List files to see if the repo is already empty
|
| 45 |
-
files = api.list_repo_files(repo_id=REPO_ID, repo_type="space")
|
| 46 |
-
to_delete = [f for f in files if f not in [".gitattributes"]]
|
| 47 |
-
|
| 48 |
-
if to_delete:
|
| 49 |
-
print(f"[*] Deleting {len(to_delete)} remote files/folders...")
|
| 50 |
-
# We can use delete_file in a loop or delete_folder for root if it works
|
| 51 |
-
# Surgical deletion of top-level items is safer
|
| 52 |
-
top_level_items = set()
|
| 53 |
-
for f in to_delete:
|
| 54 |
-
top_level_items.add(str(f).split('/')[0])
|
| 55 |
-
|
| 56 |
-
for item in top_level_items:
|
| 57 |
-
try:
|
| 58 |
-
# Try to delete as file, if it fails it might be a folder
|
| 59 |
-
api.delete_file(path_in_repo=item, repo_id=REPO_ID, repo_type="space")
|
| 60 |
-
except:
|
| 61 |
-
try:
|
| 62 |
-
api.delete_folder(path_in_repo=item, repo_id=REPO_ID, repo_type="space")
|
| 63 |
-
except:
|
| 64 |
-
pass
|
| 65 |
-
print(f"[OK] Remote repository is now clean.")
|
| 66 |
-
else:
|
| 67 |
-
print(f"[*] Remote repository is already clean.")
|
| 68 |
-
except Exception as e:
|
| 69 |
-
print(f"[!] Wipe warning: {e}")
|
| 70 |
-
|
| 71 |
-
def upload():
|
| 72 |
-
import shutil
|
| 73 |
-
# Ensure font is copied to backend before upload
|
| 74 |
-
font_src = os.path.join("..", "frontend", "public", "ArbFONTS-cocon-next-arabic.ttf")
|
| 75 |
-
font_dest_dir = "fonts"
|
| 76 |
-
font_dest = os.path.join(font_dest_dir, "ArbFONTS-cocon-next-arabic.ttf")
|
| 77 |
-
if os.path.exists(font_src):
|
| 78 |
-
os.makedirs(font_dest_dir, exist_ok=True)
|
| 79 |
-
shutil.copy(font_src, font_dest)
|
| 80 |
-
print(f"[*] Copied font from {font_src} to {font_dest}")
|
| 81 |
-
|
| 82 |
-
# 1. Clean up remote first
|
| 83 |
-
wipe_remote()
|
| 84 |
-
|
| 85 |
-
print(f"\n>>> Uploading latest local version to: https://huggingface.co/spaces/{REPO_ID}")
|
| 86 |
-
|
| 87 |
-
try:
|
| 88 |
-
print(f"[*] Creating/Updating repository...")
|
| 89 |
-
api.create_repo(repo_id=REPO_ID, repo_type="space", space_sdk="docker", private=False, exist_ok=True)
|
| 90 |
-
|
| 91 |
-
print(f"[*] Syncing local folder to remote...")
|
| 92 |
-
api.upload_folder(
|
| 93 |
-
folder_path=".",
|
| 94 |
-
repo_id=REPO_ID,
|
| 95 |
-
repo_type="space",
|
| 96 |
-
commit_message="Full Sync: Wiped and Re-uploaded (Stateless Architecture)",
|
| 97 |
-
ignore_patterns=[
|
| 98 |
-
".git*", "__pycache__*", "venv*", "env*",
|
| 99 |
-
"*.db", "*.sqlite*", ".*", "*.pyc", "uploads/*", "logs/*", "backups/*"
|
| 100 |
-
]
|
| 101 |
-
)
|
| 102 |
-
|
| 103 |
-
print("\n" + "="*50)
|
| 104 |
-
print(f"[OK] SUCCESS!")
|
| 105 |
-
print(f"[SYS] Environment is now PURE and CLEAN.")
|
| 106 |
-
print(f"[LINK] https://huggingface.co/spaces/{REPO_ID}")
|
| 107 |
-
print("="*50)
|
| 108 |
-
|
| 109 |
-
except Exception as e:
|
| 110 |
-
print(f"\n[ERROR] {e}")
|
| 111 |
-
|
| 112 |
-
if __name__ == "__main__":
|
| 113 |
-
upload()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|