File size: 1,957 Bytes
c001f24
 
 
7a709ef
c001f24
 
 
 
 
 
 
 
 
 
 
7a709ef
 
 
 
 
 
 
 
 
c001f24
7a709ef
 
c001f24
 
 
 
 
 
 
 
 
 
7a709ef
 
 
 
 
 
 
 
c001f24
7a709ef
 
c001f24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7a709ef
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
import os
import sys
import shutil
from huggingface_hub import snapshot_download, HfApi

# Configuration
REPO_ID = os.environ.get("DATASET_REPO_ID")
HF_TOKEN = os.environ.get("HF_TOKEN")

def download():
    if not REPO_ID:
        print("DATASET_REPO_ID not set, skipping download.")
        return
    
    print(f"Downloading data from {REPO_ID}...")
    try:
        # snapshot_download is more efficient for many files than the CLI
        snapshot_download(
            repo_id=REPO_ID,
            repo_type="dataset",
            local_dir="data_repo",
            token=HF_TOKEN,
            max_workers=8
        )
        print("Download successful.")
    except Exception as e:
        print(f"Download failed: {e}")

def upload():
    if not REPO_ID:
        print("DATASET_REPO_ID not set, skipping upload.")
        return
    if not HF_TOKEN:
        print("HF_TOKEN not set, skipping upload.")
        return

    print(f"Uploading data to {REPO_ID}...")
    try:
        api = HfApi(token=HF_TOKEN)
        api.upload_folder(
            folder_path="data_repo",
            repo_id=REPO_ID,
            repo_type="dataset",
            # This handles large folders by committing in chunks if necessary
        )
        print("Upload successful.")
    except Exception as e:
        print(f"Upload failed: {e}")

def init_local():
    """Ensure data_repo has the necessary structure if download failed or it's new."""
    os.makedirs("data_repo/output", exist_ok=True)
    os.makedirs("data_repo/processed", exist_ok=True)
    os.makedirs("data_repo/uploads", exist_ok=True)

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print("Usage: python hf_sync.py [download|upload|init]")
        sys.exit(1)
    
    action = sys.argv[1]
    if action == "download":
        download()
    elif action == "upload":
        upload()
    elif action == "init":
        init_local()
    else:
        print(f"Unknown action: {action}")