{ "cells": [ { "cell_type": "markdown", "id": "0abc7dd7", "metadata": {}, "source": [ "### Master Notebook for Hugging Face repos Upload and Download V11\n", "\n", "### Always get latest version before starting to use : https://www.patreon.com/posts/104672510\n", "\n", "### Execute below cell once to install libraries\n", "\n", "### Don't forget to set accurate folder and file paths and repo names befor executing cells" ] }, { "cell_type": "code", "execution_count": null, "id": "caa74bef", "metadata": {}, "outputs": [], "source": [ "!pip install huggingface_hub --upgrade\n", "\n", "!pip install ipywidgets --upgrade\n", "\n", "!pip install git+https://github.com/huggingface/huggingface_hub --upgrade\n", "\n", "!pip install hf_transfer --upgrade\n", "\n", "!pip install --upgrade jupyterlab-widgets\n", "!pip install --upgrade ipywidgets\n", "!jupyter nbextension enable --py widgetsnbextension" ] }, { "cell_type": "markdown", "id": "502249bf", "metadata": {}, "source": [ "### Use below cell to paste your Hugging Face token key. \n", "\n", "### Access Tokens are here : https://huggingface.co/settings/tokens" ] }, { "cell_type": "code", "execution_count": null, "id": "0d8027c8", "metadata": {}, "outputs": [], "source": [ "import os\n", "import subprocess\n", "import platform\n", "\n", "hugging_face_token = 'TOKEN'\n", "\n", "# Set the environment variable\n", "os.environ['HUGGING_FACE_HUB_TOKEN'] = hugging_face_token\n", "os.environ['HF_HUB_ENABLE_HF_TRANSFER'] = \"1\"\n", "os.environ['HF_HUB_VERBOSITY'] = \"debug\"\n", "\n", "\n", "# Determine the operating system\n", "system = platform.system()\n", "\n", "if system == \"Linux\":\n", " export_command = f'export HUGGING_FACE_HUB_TOKEN={hugging_face_token}'\n", " subprocess.run(export_command, shell=True, check=True)\n", " export_command = f'export HF_HUB_ENABLE_HF_TRANSFER=1'\n", " subprocess.run(export_command, shell=True, check=True) \n", " export_command = f'export HF_HUB_VERBOSITY=\"debug\"'\n", " subprocess.run(export_command, shell=True, check=True) \n", "elif system == \"Windows\":\n", " set_command = f'set HUGGING_FACE_HUB_TOKEN={hugging_face_token}'\n", " subprocess.run(set_command, shell=True, check=True)\n", " export_command = f'set HF_HUB_ENABLE_HF_TRANSFER=1'\n", " subprocess.run(export_command, shell=True, check=True) \n", " export_command = f'set HF_HUB_VERBOSITY=\"debug\"'\n", " subprocess.run(export_command, shell=True, check=True) \n", "\n", "# Command to log in using the token\n", "login_command = ['huggingface-cli', 'login', '--token', hugging_face_token]\n", "\n", "# Execute the login command and capture output\n", "try:\n", " result = subprocess.run(login_command, check=True, capture_output=True, text=True)\n", " print(\"Output:\", result.stdout)\n", " print(\"Error:\", result.stderr)\n", "except subprocess.CalledProcessError as e:\n", " print(\"Command failed with exit code:\", e.returncode)\n", " print(\"Output:\", e.output)\n", " print(\"Error:\", e.stderr)" ] }, { "cell_type": "markdown", "id": "afb149da", "metadata": {}, "source": [ "### Very fast new upload - Wait till UPLOAD COMPLETED printed" ] }, { "cell_type": "code", "execution_count": null, "id": "30b96a4f", "metadata": {}, "outputs": [], "source": [ "!huggingface-cli upload-large-folder \"YourUserName/reponame\" r\"/home/Ubuntu/apps/StableSwarmUI/Models/Lora\" --repo-type=model --no-bars\n", "\n", "print(\".\\n.\\nUPLOAD COMPLETED\")" ] }, { "cell_type": "markdown", "id": "4e7a6316", "metadata": {}, "source": [ "### Very fast new upload V2 - Wait till UPLOAD COMPLETED printed" ] }, { "cell_type": "code", "execution_count": null, "id": "75a18901", "metadata": {}, "outputs": [], "source": [ "from huggingface_hub import HfApi\n", "\n", "# Initialize the HfApi instance\n", "api = HfApi()\n", "\n", "# Define the repo_id and the local folder to upload\n", "repo_id = \"rexlapix/amy\" # Your Hugging Face repo\n", "folder_path = r\"/home/Ubuntu/Desktop/workspace/test\" # Path to your local folder\n", "\n", "# Upload the folder to the specific subdirectory in the repo\n", "api.upload_large_folder(\n", " repo_id=repo_id, \n", " repo_type=\"model\", \n", " folder_path=folder_path, \n", " path_in_repo=\"amy_FLUX-FT/model_fp16\" # Target directory inside the repo\n", ")\n", "\n", "print(\"\\nUPLOAD COMPLETED\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "999f28d5", "metadata": {}, "outputs": [], "source": [ "import os\n", "from huggingface_hub import HfApi\n", "\n", "# Initialize the API\n", "api = HfApi()\n", "\n", "# Define the local file path\n", "local_file_path = r\"/home/Ubuntu/Desktop/workspace/cooked/amy_FLUX-FT/ml/model_fp8/amy_flux_fp8-000200.safetensors\"\n", "\n", "# Automatically extract the default file name from the local path\n", "default_file_name = os.path.basename(local_file_path)\n", "\n", "# Define the path in the repository using the extracted file name\n", "path_in_repo = f\"amy_FLUX-FT/model_fp8/{default_file_name}\"\n", "\n", "# Upload the file\n", "api.upload_file(\n", " path_or_fileobj=local_file_path,\n", " path_in_repo=path_in_repo,\n", " repo_id=\"rexlapix/amy\",\n", " repo_type=\"model\",\n", ")\n", "\n", "print(\".\\n.\\nUPLOAD COMPLETED\")\n" ] }, { "cell_type": "markdown", "id": "58da07da-2e0b-4a32-a36d-ddf511800c58", "metadata": {}, "source": [ "### Upload a single file with specific name to remote repo - Wait till UPLOAD COMPLETED printed" ] }, { "cell_type": "code", "execution_count": null, "id": "0502703b-8b44-4e58-af2c-82f67d713548", "metadata": {}, "outputs": [], "source": [ "# This cell is used to upload single file into a repo with certain name\n", "\n", "from huggingface_hub import HfApi\n", "api = HfApi()\n", "api.upload_file(\n", " path_or_fileobj=r\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion/model_name.safetensors\",\n", " path_in_repo=\"model_name.safetensors\",\n", " repo_id=\"YourUserName/reponame\",\n", " repo_type=\"model\",\n", ")\n", "\n", "print(\".\\n.\\nUPLOAD COMPLETED\")" ] }, { "cell_type": "markdown", "id": "2dd50994-5595-46a6-b315-595ede0cf922", "metadata": {}, "source": [ "### Upload a folder fast but not suggested for too big and many files - Wait till UPLOAD COMPLETED printed" ] }, { "cell_type": "code", "execution_count": null, "id": "a89ece44-8f27-43bb-a7fc-e966409d6f5a", "metadata": {}, "outputs": [], "source": [ "# This cell is used to upload a folder into a repo with single commit\n", "\n", "from huggingface_hub import HfApi\n", "api = HfApi()\n", "api.upload_folder(\n", " folder_path=r\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\",\n", " repo_id=\"YourUserName/reponame\",\n", " repo_type=\"model\",\n", ")\n", "\n", "print(\".\\n.\\nUPLOAD COMPLETED\")" ] }, { "cell_type": "markdown", "id": "4d26f8d6-31cd-42a5-9477-ba1e77cd271c", "metadata": {}, "source": [ "### To upload all files in given folder to the target Hugging Face repository use below - Wait till UPLOAD COMPLETED printed" ] }, { "cell_type": "code", "execution_count": null, "id": "6fc96104", "metadata": {}, "outputs": [], "source": [ "# This cell uploads a folder into remote repo with multi commit\n", "# Supports continue feature so if gets interrupted you can run again to continue / resume\n", "\n", "from huggingface_hub import HfApi\n", "from huggingface_hub import get_collection, delete_collection_item\n", "from huggingface_hub import upload_file\n", "from huggingface_hub import (\n", " HfFolder,\n", " ModelCard,\n", " ModelCardData,\n", " create_repo,\n", " hf_hub_download,\n", " upload_folder,\n", " whoami,\n", ")\n", "api = HfApi()\n", "upload_folder(\n", " folder_path=r\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\",\n", " repo_id=\"YourUserName/reponame\",\n", " repo_type=\"model\",\n", " multi_commits=True,\n", " multi_commits_verbose=True,\n", ")\n", "\n", "print(\".\\n.\\nUPLOAD COMPLETED\")" ] }, { "cell_type": "markdown", "id": "7381726f", "metadata": {}, "source": [ "### To download all files in given Hugging Face repository use below - Wait till DOWNLOAD COMPLETED printed" ] }, { "cell_type": "code", "execution_count": null, "id": "90f6c9b1", "metadata": {}, "outputs": [], "source": [ "# You can run this cell multiple times if any error occurs it will resume\n", "\n", "from huggingface_hub import snapshot_download\n", "import os\n", "\n", "# Ensure the local directory exists\n", "\n", "\n", "repo_id=\"YourUserName/reponame\"\n", "local_dir = \"/home/Ubuntu/apps/StableSwarmUI/Models/diffusion_models\"\n", "\n", "os.makedirs(local_dir, exist_ok=True)\n", "\n", "snapshot_download(repo_id=repo_id,local_dir=local_dir)\n", "\n", "print(\".\\n.\\nDOWNLOAD COMPLETED\")" ] }, { "cell_type": "markdown", "id": "09f0f192-4d6b-4511-8366-6c676cd2404c", "metadata": {}, "source": [ "### Download a specific sub directory / folder - Wait till DOWNLOAD COMPLETED printed" ] }, { "cell_type": "code", "execution_count": null, "id": "download_subdirectory", "metadata": {}, "outputs": [], "source": [ "# This cell downloads a specific subdirectory from a given repo\n", "\n", "from huggingface_hub import snapshot_download\n", "import os\n", "\n", "# Define the repository and subdirectory\n", "repo_id = \"YourUserName/reponame\"\n", "subdirectory = \"folder_path_in_remote_repo/*\" # don't delete /* part just change folder path - if you get click to show javascript error ignore it and follow folder it will work\n", "local_dir = \"/home/Ubuntu/apps/StableSwarmUI/Models/diffusion_models\"\n", "\n", "# Ensure the local directory exists\n", "os.makedirs(local_dir, exist_ok=True)\n", "\n", "# Download only the specific subdirectory\n", "snapshot_download(\n", " repo_id=repo_id,\n", " local_dir=local_dir,\n", " allow_patterns=subdirectory\n", ")\n", "\n", "print(f\"Downloaded {subdirectory} from {repo_id} to {local_dir}\")\n", "print(\".\\n.\\nDOWNLOAD COMPLETED\")" ] }, { "cell_type": "markdown", "id": "47b04ce9-9653-4e4f-9bd9-1b6ed383f502", "metadata": {}, "source": [ "### Download a specific files - Ultra Blazing Fast Download" ] }, { "cell_type": "code", "execution_count": null, "id": "download_specific_file", "metadata": {}, "outputs": [], "source": [ "# This cell downloads a specific files from a given repo\n", "# You can download 1 or more specific files ultra fast\n", "# Wait until you see DOWNLOAD COMPLETED - status not shown on RunPod proxy\n", "\n", "from huggingface_hub import snapshot_download\n", "import os\n", "\n", "# Define the repository, file path, and local directory\n", "repo_id_set = \"YourUserName/reponame\"\n", "local_dir_set = f\"/home/Ubuntu/apps/StableSwarmUI/Models/diffusion_models\"\n", "\n", "# Ensure the local directory exists\n", "os.makedirs(local_dir_set, exist_ok=True)\n", "print(\".\\n.\\nDOWNLOAD Started...\")\n", "snapshot_download(\n", " repo_id=repo_id_set,\n", " allow_patterns=[\"1024_no_caption-000060.safetensors\", \"1024_with_caption.safetensors\",\"file3.zip\"],\n", " local_dir=local_dir_set,\n", " )\n", "\n", "print(\".\\n.\\nDOWNLOAD COMPLETED\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.0rc1" } }, "nbformat": 4, "nbformat_minor": 5 }