Upload dyve_tts/upload_hg.py with huggingface_hub
Browse files- dyve_tts/upload_hg.py +45 -0
dyve_tts/upload_hg.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import HfApi
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# 配置信息
|
| 6 |
+
repo_id = "zeju-0727/dyve_TTS_test_code"
|
| 7 |
+
local_folder = Path("/data/zeju/Dyve_TTS") # 本地文件夹路径
|
| 8 |
+
repo_type = "dataset" # 可以是 "model" 或 "dataset"
|
| 9 |
+
target_dir = "dyve_tts" # 仓库中的目标目录
|
| 10 |
+
|
| 11 |
+
# 登录(更安全的方式,token会保存在缓存中)
|
| 12 |
+
api = HfApi()
|
| 13 |
+
# 建议通过环境变量或 huggingface-cli login 设置token,而不是硬编码
|
| 14 |
+
|
| 15 |
+
def upload_folder_to_hf(local_path, repo_id, repo_type, target_dir=""):
|
| 16 |
+
"""上传整个文件夹到HuggingFace Hub"""
|
| 17 |
+
if not local_path.exists():
|
| 18 |
+
raise ValueError(f"本地路径不存在: {local_path}")
|
| 19 |
+
|
| 20 |
+
print(f"开始上传文件夹: {local_path} → {repo_id}/{target_dir}")
|
| 21 |
+
|
| 22 |
+
# 遍历文件夹并上传所有文件
|
| 23 |
+
for file_path in local_path.glob("**/*"):
|
| 24 |
+
if file_path.is_file():
|
| 25 |
+
relative_path = file_path.relative_to(local_path)
|
| 26 |
+
path_in_repo = str(Path(target_dir) / str(relative_path)) if target_dir else str(relative_path)
|
| 27 |
+
|
| 28 |
+
print(f"正在上传: {relative_path}...", end=" ", flush=True)
|
| 29 |
+
|
| 30 |
+
try:
|
| 31 |
+
api.upload_file(
|
| 32 |
+
path_or_fileobj=str(file_path),
|
| 33 |
+
path_in_repo=path_in_repo,
|
| 34 |
+
repo_id=repo_id,
|
| 35 |
+
repo_type=repo_type,
|
| 36 |
+
)
|
| 37 |
+
print("✓")
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"✗ 失败: {str(e)}")
|
| 40 |
+
continue
|
| 41 |
+
|
| 42 |
+
print("文件夹上传完成!")
|
| 43 |
+
|
| 44 |
+
if __name__ == "__main__":
|
| 45 |
+
upload_folder_to_hf(local_folder, repo_id, repo_type, target_dir)
|