stamp-dataset / setup_auto_sync.py
selen-kim's picture
Auto-sync from GitHub: 58ae4db1675cf90aa640fc9d857fab0a123155ef
02ff59a verified
#!/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()