Download scripts/detailed_sync.py from khiemdztv/civic-backup: direct link, hf CLI and curl.
- Browser
- Download file 3.85 kB
-
https://huggingface.co/khiemdztv/civic-backup/resolve/main/scripts/detailed_sync.py
- Command line
-
hf download hf://khiemdztv/civic-backup/scripts/detailed_sync.py
-
curl -L -o detailed_sync.py https://huggingface.co/khiemdztv/civic-backup/resolve/main/scripts/detailed_sync.py
3.85 kB
| #!/usr/bin/env python3 | |
| """Detailed sync script to check and upload modified/new files to Hugging Face.""" | |
| import os | |
| import sys | |
| import hashlib | |
| from pathlib import Path | |
| from huggingface_hub import HfApi | |
| REPO_ID = "khiemdztv/civic-backup" | |
| REPO_TYPE = "model" | |
| ROOT_DIR = Path(__file__).resolve().parent.parent | |
| # Files/dirs to exclude completely from backup | |
| EXCLUDE_DIRS = { | |
| ".git", ".pytest_cache", ".claude", "__pycache__", "scratch", | |
| ".cache" | |
| } | |
| EXCLUDE_FILES = { | |
| "runner.log", "server.log", "server.pid", "tunnel.log", "tunnel.pid", | |
| "artifacts/seg_cache.sqlite-wal", "artifacts/seg_cache.sqlite-shm", | |
| "scripts/check_diff.py", "scripts/inspect_onnx.py", | |
| "scripts/prepare_backup_list.py", "scripts/detailed_sync.py" | |
| } | |
| EXCLUDE_EXTS = {".pyc", ".log", ".pid"} | |
| def should_exclude(rel_path_str: str) -> bool: | |
| parts = Path(rel_path_str).parts | |
| for p in parts: | |
| if p in EXCLUDE_DIRS or p.startswith("."): | |
| return True | |
| if rel_path_str in EXCLUDE_FILES: | |
| return True | |
| if any(rel_path_str.endswith(ext) for ext in EXCLUDE_EXTS): | |
| return True | |
| return False | |
| def get_file_sha256(filepath: Path) -> str: | |
| h = hashlib.sha256() | |
| with open(filepath, "rb") as f: | |
| while chunk := f.read(1024 * 1024): | |
| h.update(chunk) | |
| return h.hexdigest() | |
| def get_git_blob_sha1(filepath: Path) -> str: | |
| h = hashlib.sha1() | |
| content = filepath.read_bytes() | |
| header = f"blob {len(content)}\0".encode() | |
| h.update(header + content) | |
| return h.hexdigest() | |
| def main(): | |
| api = HfApi() | |
| print(f"Fetching repo info for {REPO_ID}...") | |
| model_info = api.model_info(repo_id=REPO_ID, files_metadata=True) | |
| remote_files = {s.rfilename: s for s in model_info.siblings} | |
| print(f"Total files on remote repo: {len(remote_files)}") | |
| to_upload = [] # list of (rel_path, reason, size) | |
| check_dirs = ["src", "configs", "docs", "reports", "tests", "scripts", "hf_space", "static_space"] | |
| all_local_paths = [] | |
| for d in check_dirs: | |
| dir_path = ROOT_DIR / d | |
| if dir_path.exists(): | |
| for p in dir_path.rglob("*"): | |
| if p.is_file(): | |
| all_local_paths.append(p) | |
| for p in ROOT_DIR.glob("*"): | |
| if p.is_file(): | |
| all_local_paths.append(p) | |
| onnx_file = ROOT_DIR / "models" / "civic_nlu" / "model_int8.onnx" | |
| if onnx_file.exists() and "models/civic_nlu/model_int8.onnx" not in remote_files: | |
| all_local_paths.append(onnx_file) | |
| for p in all_local_paths: | |
| rel = str(p.relative_to(ROOT_DIR)) | |
| if should_exclude(rel): | |
| continue | |
| size = p.stat().st_size | |
| if rel not in remote_files: | |
| to_upload.append((rel, "NEW", size)) | |
| else: | |
| remote_s = remote_files[rel] | |
| if remote_s.size != size: | |
| to_upload.append((rel, f"SIZE_DIFF (local:{size} != remote:{remote_s.size})", size)) | |
| else: | |
| # Same size, check content | |
| if remote_s.lfs: | |
| local_hash = get_file_sha256(p) | |
| if remote_s.lfs.get("sha256") != local_hash: | |
| to_upload.append((rel, "HASH_DIFF_LFS", size)) | |
| elif remote_s.blob_id: | |
| local_git_sha = get_git_blob_sha1(p) | |
| if remote_s.blob_id != local_git_sha: | |
| to_upload.append((rel, "HASH_DIFF_GIT", size)) | |
| print(f"\nFound {len(to_upload)} files to update/upload:") | |
| total_bytes = sum(item[2] for item in to_upload) | |
| for rel, reason, size in sorted(to_upload, key=lambda x: x[0]): | |
| print(f" [{reason}] {rel} ({size:,} bytes)") | |
| print(f"\nTotal size to upload: {total_bytes:,} bytes (~{total_bytes / (1024*1024):.2f} MB)") | |
| if __name__ == "__main__": | |
| main() | |