File size: 3,956 Bytes
02ff59a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env python3
"""
Setup automatic Hugging Face sync using Git hooks.
This script creates a post-commit hook that automatically uploads
dataset changes to Hugging Face when you commit to Git.
"""

import os
import stat
from pathlib import Path

def setup_git_hook():
    """Setup Git post-commit hook for automatic HF sync."""

    # Find git directory
    current_dir = Path.cwd()
    git_dir = None

    # Search upwards for .git directory
    for parent in [current_dir] + list(current_dir.parents):
        git_path = parent / '.git'
        if git_path.exists():
            git_dir = git_path
            break

    if not git_dir:
        print("❌ Not in a Git repository")
        return False

    # Create hooks directory if it doesn't exist
    hooks_dir = git_dir / 'hooks'
    hooks_dir.mkdir(exist_ok=True)

    # Post-commit hook content
    hook_content = f"""#!/bin/bash
# Auto-generated post-commit hook for Hugging Face dataset sync

# Check if the commit affects the dataset folder
if git diff-tree --name-only --no-commit-id HEAD | grep -q '^dataset/'; then
    echo "🤗 Dataset changes detected, syncing to Hugging Face..."

    # Change to dataset directory
    cd "{current_dir}"

    # Run sync script
    if [ -f "sync_to_hf.sh" ]; then
        bash sync_to_hf.sh
    else
        echo "❌ sync_to_hf.sh not found"
    fi
else
    echo "ℹ️  No dataset changes detected, skipping HF sync"
fi
"""

    # Write post-commit hook
    hook_path = hooks_dir / 'post-commit'
    with open(hook_path, 'w') as f:
        f.write(hook_content)

    # Make executable
    os.chmod(hook_path, stat.S_IRWXU | stat.S_IRGRP | stat.S_IROTH)

    print(f"✅ Git post-commit hook created: {hook_path}")
    print("🔄 Dataset will now automatically sync to Hugging Face after git commits")
    return True

def setup_github_action():
    """Setup GitHub Action for automatic HF sync."""

    # GitHub workflows directory
    github_dir = Path('.github/workflows')
    github_dir.mkdir(parents=True, exist_ok=True)

    # GitHub Action content
    action_content = """name: Sync Dataset to Hugging Face

on:
  push:
    paths:
      - 'dataset/**'
    branches:
      - main
      - master

jobs:
  sync-to-hf:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout repository
      uses: actions/checkout@v3
      with:
        lfs: true

    - name: Set up Python
      uses: actions/setup-python@v4
      with:
        python-version: '3.9'

    - name: Install dependencies
      run: |
        pip install huggingface_hub datasets

    - name: Sync to Hugging Face
      env:
        HF_TOKEN: ${{ secrets.HF_TOKEN }}
      run: |
        cd dataset
        python upload_to_hf.py \\
          --repo-name stamp-dataset \\
          --commit-message "Auto-sync from GitHub: ${{ github.sha }}"
"""

    # Write GitHub Action
    action_path = github_dir / 'sync-hf-dataset.yml'
    with open(action_path, 'w') as f:
        f.write(action_content)

    print(f"✅ GitHub Action created: {action_path}")
    print("🔧 Add your HF_TOKEN to GitHub repository secrets")
    print("   Go to: Settings > Secrets and variables > Actions")
    print("   Add secret: HF_TOKEN = your_huggingface_token")

def main():
    print("🤗 STAMP Dataset Auto-Sync Setup")
    print("=================================")

    print("\\n1. Setting up Git post-commit hook...")
    if setup_git_hook():
        print("✅ Local Git hook setup complete")

    print("\\n2. Setting up GitHub Action...")
    setup_github_action()
    print("✅ GitHub Action setup complete")

    print("\\n🎉 Auto-sync setup completed!")
    print("\\nNext steps:")
    print("1. Configure repository name in sync_to_hf.sh")
    print("2. Login to Hugging Face: huggingface-cli login")
    print("3. For GitHub Action: Add HF_TOKEN to repository secrets")
    print("4. Test with: git commit -m 'test dataset sync'")

if __name__ == "__main__":
    main()