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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
{
 "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
}