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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
# -*- coding: utf-8 -*-
|
| 3 |
+
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from concurrent.futures import ThreadPoolExecutor, as_completed
|
| 6 |
+
from modelscope.hub.api import HubApi
|
| 7 |
+
|
| 8 |
+
# ========= 配置区 =========
|
| 9 |
+
repo_id = "cangyeone/SeismicX-Cont"
|
| 10 |
+
repo_type = "dataset"
|
| 11 |
+
max_workers = 1
|
| 12 |
+
root_dir = Path(".")
|
| 13 |
+
# =========================
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
exclude_dirs = {
|
| 17 |
+
".git",
|
| 18 |
+
"__pycache__",
|
| 19 |
+
".pytest_cache",
|
| 20 |
+
".ipynb_checkpoints",
|
| 21 |
+
".vscode",
|
| 22 |
+
".idea",
|
| 23 |
+
".cache",
|
| 24 |
+
"publish_mini",
|
| 25 |
+
"essd",
|
| 26 |
+
"eval_picks",
|
| 27 |
+
"figures",
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
exclude_names = {
|
| 31 |
+
".DS_Store",
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
exclude_suffixes = {
|
| 35 |
+
".pyc",
|
| 36 |
+
".pyo",
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
# 过滤 macOS 在 exFAT / FAT / SMB 等文件系统上生成的 AppleDouble 文件
|
| 40 |
+
exclude_name_prefixes = (
|
| 41 |
+
"._",
|
| 42 |
+
)
|
| 43 |
+
|
| 44 |
+
exclude_prefixes = [
|
| 45 |
+
"data/hdf5/",
|
| 46 |
+
"data/index/",
|
| 47 |
+
"data/label/",
|
| 48 |
+
"data/picks/",
|
| 49 |
+
"data/hdf5_hour_subsets/",
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def should_upload(path: Path) -> bool:
|
| 54 |
+
if not path.is_file():
|
| 55 |
+
return False
|
| 56 |
+
|
| 57 |
+
rel = path.relative_to(root_dir).as_posix()
|
| 58 |
+
|
| 59 |
+
# 过滤目录
|
| 60 |
+
if any(part in exclude_dirs for part in path.parts):
|
| 61 |
+
return False
|
| 62 |
+
|
| 63 |
+
# 过滤指定文件名,例如 .DS_Store
|
| 64 |
+
if path.name in exclude_names:
|
| 65 |
+
return False
|
| 66 |
+
|
| 67 |
+
# 过滤指定前缀文件名,例如 ._xxx
|
| 68 |
+
if path.name.startswith(exclude_name_prefixes):
|
| 69 |
+
return False
|
| 70 |
+
|
| 71 |
+
# 过滤后缀,例如 .pyc / .pyo
|
| 72 |
+
if path.suffix in exclude_suffixes:
|
| 73 |
+
return False
|
| 74 |
+
|
| 75 |
+
# 过滤指定目录前缀
|
| 76 |
+
for prefix in exclude_prefixes:
|
| 77 |
+
if rel.startswith(prefix):
|
| 78 |
+
return False
|
| 79 |
+
|
| 80 |
+
return True
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
def collect_files():
|
| 84 |
+
files = []
|
| 85 |
+
for p in root_dir.rglob("*"):
|
| 86 |
+
if should_upload(p):
|
| 87 |
+
rel = p.relative_to(root_dir).as_posix()
|
| 88 |
+
files.append((p, rel))
|
| 89 |
+
return files
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
def upload_one(item):
|
| 93 |
+
local_path, remote_path = item
|
| 94 |
+
api = HubApi()
|
| 95 |
+
|
| 96 |
+
print(f"[UPLOAD START] {local_path} -> {repo_id}/{remote_path}")
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
api.upload_file(
|
| 100 |
+
path_or_fileobj=str(local_path),
|
| 101 |
+
path_in_repo=remote_path,
|
| 102 |
+
repo_id=repo_id,
|
| 103 |
+
repo_type=repo_type,
|
| 104 |
+
commit_message=f"Upload {remote_path}",
|
| 105 |
+
)
|
| 106 |
+
return True, str(local_path), "OK"
|
| 107 |
+
except Exception as e:
|
| 108 |
+
return False, str(local_path), str(e)
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
def main():
|
| 112 |
+
api = HubApi()
|
| 113 |
+
|
| 114 |
+
# 如果没有命令行登录,可以取消下面这行
|
| 115 |
+
# api.login("your_token")
|
| 116 |
+
|
| 117 |
+
try:
|
| 118 |
+
api.create_repo(repo_id=repo_id, repo_type=repo_type)
|
| 119 |
+
print(f"[INFO] Repo created: {repo_id}")
|
| 120 |
+
except Exception as e:
|
| 121 |
+
print(f"[INFO] Repo may already exist: {repo_id}")
|
| 122 |
+
print(f" {e}")
|
| 123 |
+
|
| 124 |
+
files_to_upload = collect_files()
|
| 125 |
+
|
| 126 |
+
print(f"[INFO] Files to upload: {len(files_to_upload)}")
|
| 127 |
+
for _, rel in files_to_upload:
|
| 128 |
+
print(f" - {rel}")
|
| 129 |
+
|
| 130 |
+
failed = []
|
| 131 |
+
|
| 132 |
+
with ThreadPoolExecutor(max_workers=max_workers) as executor:
|
| 133 |
+
futures = {
|
| 134 |
+
executor.submit(upload_one, item): item
|
| 135 |
+
for item in files_to_upload
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
+
for future in as_completed(futures):
|
| 139 |
+
ok, local_path, msg = future.result()
|
| 140 |
+
|
| 141 |
+
if ok:
|
| 142 |
+
print(f"[DONE] {local_path}")
|
| 143 |
+
else:
|
| 144 |
+
print(f"[FAILED] {local_path}")
|
| 145 |
+
print(f" {msg}")
|
| 146 |
+
failed.append((local_path, msg))
|
| 147 |
+
|
| 148 |
+
print("\n========== SUMMARY ==========")
|
| 149 |
+
print(f"success: {len(files_to_upload) - len(failed)}")
|
| 150 |
+
print(f"failed : {len(failed)}")
|
| 151 |
+
|
| 152 |
+
if failed:
|
| 153 |
+
print("\n失败文件:")
|
| 154 |
+
for local_path, msg in failed:
|
| 155 |
+
print(f"- {local_path}: {msg}")
|
| 156 |
+
else:
|
| 157 |
+
print("全部上传完成。")
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
if __name__ == "__main__":
|
| 161 |
+
main()
|