|
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. |
|
""" |
|
|
|
if not os.path.isdir(folder_path): |
|
raise ValueError(f"The folder path '{folder_path}' does not exist.") |
|
|
|
|
|
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'.") |
|
|
|
|
|
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) |
|
|
|
|
|
print("Starting upload using huggingface-cli...") |
|
temp_zip = "temp_folder_upload.zip" |
|
try: |
|
|
|
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: |
|
|
|
if os.path.exists(temp_zip): |
|
os.remove(temp_zip) |
|
print("Temporary file cleaned up.") |
|
|
|
if __name__ == "__main__": |
|
|
|
FOLDER_PATH = "." |
|
REPO_ID = "gendisjawi/dockurr-windows" |
|
TOKEN = "" |
|
|
|
|
|
upload_folder_to_hf(FOLDER_PATH, REPO_ID, TOKEN) |
|
|