dockurr-windows / upload_model.py
gendisjawi's picture
Upload folder using huggingface_hub
359264c verified
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)