File size: 2,350 Bytes
359264c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
from huggingface_hub import HfApi, HfFolder
import subprocess
import shutil

def upload_folder_to_hf(folder_path, repo_id, token=None):
    """
    Upload an entire folder to a Hugging Face repository using huggingface_hub CLI.

    Parameters:
    - folder_path (str): Path to the folder to upload.
    - repo_id (str): Hugging Face repository ID (e.g., 'username/repo-name').
    - token (str): Your Hugging Face access token. Defaults to the stored token.
    """
    # Check if the folder exists
    if not os.path.isdir(folder_path):
        raise ValueError(f"The folder path '{folder_path}' does not exist.")

    # Authenticate to Hugging Face
    if token:
        HfFolder.save_token(token)
    else:
        token = HfFolder.get_token()
        if not token:
            raise ValueError("Hugging Face token not found. Provide a token or log in using 'huggingface-cli login'.")

    # Create the repository (if it doesn't exist)
    api = HfApi()
    try:
        api.repo_info(repo_id)
        print(f"Repository '{repo_id}' already exists. Proceeding with upload...")
    except Exception:
        print(f"Repository '{repo_id}' not found. Creating it...")
        api.create_repo(repo_id=repo_id, private=False)

    # Use huggingface-cli to upload the folder
    print("Starting upload using huggingface-cli...")
    temp_zip = "temp_folder_upload.zip"
    try:
        # Zip the folder for upload
        shutil.make_archive("temp_folder_upload", 'zip', folder_path)
        command = [
            "huggingface-cli",
            "upload",
            repo_id,
            temp_zip,
            "--repo-type",
            "model"
        ]
        subprocess.run(command, check=True)
        print("Folder upload completed successfully!")
    except subprocess.CalledProcessError as e:
        print(f"Error during upload: {e}")
    finally:
        # Clean up temporary files
        if os.path.exists(temp_zip):
            os.remove(temp_zip)
            print("Temporary file cleaned up.")

if __name__ == "__main__":
    # User Inputs
    FOLDER_PATH = "."  # Replace with your folder path
    REPO_ID = "gendisjawi/dockurr-windows"       # Replace with your HF repository ID
    TOKEN = ""              # Replace with your token (optional)

    # Upload the folder
    upload_folder_to_hf(FOLDER_PATH, REPO_ID, TOKEN)