File size: 2,031 Bytes
546d015
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
"""
文件同步脚本:将 /data/workspace 的文件同步到 Hugging Face Dataset
用于持久化存储 ClawDBot 生成的文件

使用方法:
1. 创建一个 Dataset: huggingface-cli repo create clawdbot-data --type dataset
2. 设置环境变量: HF_DATASET_REPO=acpr123/clawdbot-data
3. 在 entrypoint.sh 中添加定期运行此脚本的 cron job
"""
import os
import time
from pathlib import Path
from huggingface_hub import HfApi, CommitOperationAdd

def sync_workspace_to_hf():
    """同步 workspace 目录到 HF Dataset"""

    hf_token = os.environ.get("HF_TOKEN") or os.environ.get("HUGGINGFACE_API_KEY")
    dataset_repo = os.environ.get("HF_DATASET_REPO", "acpr123/clawdbot-data")
    workspace_dir = Path("/data/workspace")

    if not hf_token:
        print("⚠️  未配置 HF_TOKEN,跳过文件同步")
        return

    if not workspace_dir.exists():
        print(f"⚠️  Workspace 目录不存在: {workspace_dir}")
        return

    # 收集所有文件
    operations = []
    for file_path in workspace_dir.rglob("*"):
        if file_path.is_file():
            relative_path = file_path.relative_to(workspace_dir)
            operations.append(
                CommitOperationAdd(
                    path_in_repo=str(relative_path),
                    path_or_fileobj=str(file_path),
                )
            )

    if not operations:
        print("📁 Workspace 为空,无需同步")
        return

    try:
        api = HfApi()
        api.create_commit(
            repo_id=dataset_repo,
            repo_type="dataset",
            operations=operations,
            commit_message=f"Sync workspace files at {time.strftime('%Y-%m-%d %H:%M:%S')}",
            token=hf_token,
        )
        print(f"✅ 已同步 {len(operations)} 个文件到 {dataset_repo}")
    except Exception as e:
        print(f"❌ 同步失败: {e}")

if __name__ == "__main__":
    print("🔄 开始同步文件到 Hugging Face Dataset...")
    sync_workspace_to_hf()