{ "cells": [ { "cell_type": "markdown", "id": "0abc7dd7", "metadata": {}, "source": [ "### Master Notebook for Hugging Face repos Upload and Download V16\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 hf_transfer --upgrade\n", "\n", "!pip install hf_xet --upgrade\n", "\n", "!pip install --upgrade jupyterlab-widgets\n", "\n", "!pip install --upgrade ipywidgets\n", "\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 = 'replace_with_your_HF_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": "01529333-a7c2-4e13-bfc9-ebf053691051", "metadata": {}, "source": [ "### Very fast new upload - Wait till UPLOAD COMPLETED printed" ] }, { "cell_type": "raw", "id": "85bd218b-d08f-46b7-8c19-953ccc019bf6", "metadata": {}, "source": [ "INFO : On Windows give the path like this E:\\\\MMAudio_v9 - use double \\\\" ] }, { "cell_type": "code", "execution_count": null, "id": "30b96a4f", "metadata": {}, "outputs": [], "source": [ "!huggingface-cli upload-large-folder \"YourUserName/reponame\" \"/home/Ubuntu/apps/StableSwarmUI/Models/diffusion_models\" --repo-type=model --no-bars\n", "\n", "print(\".\\n.\\nUPLOAD COMPLETED\")" ] }, { "cell_type": "markdown", "id": "4e7a6316", "metadata": {}, "source": [ "### Recommended - 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", "api = HfApi()\n", "\n", "api.upload_large_folder(repo_id=\"YourUserName/reponame\", \n", " repo_type=\"model\", \n", " folder_path=r\"/home/Ubuntu/apps/StableSwarmUI/Models/diffusion_models\")\n", "\n", "print(\".\\n.\\nUPLOAD COMPLETED\")" ] }, { "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": "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 (ipykernel)", "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.10.12" } }, "nbformat": 4, "nbformat_minor": 5 }