rexlapix commited on
Commit
a5b1eb0
·
verified ·
1 Parent(s): 7df5d01

Upload Hugging_Face_Upload_Download_V11.ipynb

Browse files
some-imp-files/Hugging_Face_Upload_Download_V11.ipynb ADDED
@@ -0,0 +1,417 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "markdown",
5
+ "id": "0abc7dd7",
6
+ "metadata": {},
7
+ "source": [
8
+ "### Master Notebook for Hugging Face repos Upload and Download V11\n",
9
+ "\n",
10
+ "### Always get latest version before starting to use : https://www.patreon.com/posts/104672510\n",
11
+ "\n",
12
+ "### Execute below cell once to install libraries\n",
13
+ "\n",
14
+ "### Don't forget to set accurate folder and file paths and repo names befor executing cells"
15
+ ]
16
+ },
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": null,
20
+ "id": "caa74bef",
21
+ "metadata": {},
22
+ "outputs": [],
23
+ "source": [
24
+ "!pip install huggingface_hub --upgrade\n",
25
+ "\n",
26
+ "!pip install ipywidgets --upgrade\n",
27
+ "\n",
28
+ "!pip install git+https://github.com/huggingface/huggingface_hub --upgrade\n",
29
+ "\n",
30
+ "!pip install hf_transfer --upgrade\n",
31
+ "\n",
32
+ "!pip install --upgrade jupyterlab-widgets\n",
33
+ "!pip install --upgrade ipywidgets\n",
34
+ "!jupyter nbextension enable --py widgetsnbextension"
35
+ ]
36
+ },
37
+ {
38
+ "cell_type": "markdown",
39
+ "id": "502249bf",
40
+ "metadata": {},
41
+ "source": [
42
+ "### Use below cell to paste your Hugging Face token key. \n",
43
+ "\n",
44
+ "### Access Tokens are here : https://huggingface.co/settings/tokens"
45
+ ]
46
+ },
47
+ {
48
+ "cell_type": "code",
49
+ "execution_count": null,
50
+ "id": "0d8027c8",
51
+ "metadata": {},
52
+ "outputs": [],
53
+ "source": [
54
+ "import os\n",
55
+ "import subprocess\n",
56
+ "import platform\n",
57
+ "\n",
58
+ "hugging_face_token = 'TOKEN'\n",
59
+ "\n",
60
+ "# Set the environment variable\n",
61
+ "os.environ['HUGGING_FACE_HUB_TOKEN'] = hugging_face_token\n",
62
+ "os.environ['HF_HUB_ENABLE_HF_TRANSFER'] = \"1\"\n",
63
+ "os.environ['HF_HUB_VERBOSITY'] = \"debug\"\n",
64
+ "\n",
65
+ "\n",
66
+ "# Determine the operating system\n",
67
+ "system = platform.system()\n",
68
+ "\n",
69
+ "if system == \"Linux\":\n",
70
+ " export_command = f'export HUGGING_FACE_HUB_TOKEN={hugging_face_token}'\n",
71
+ " subprocess.run(export_command, shell=True, check=True)\n",
72
+ " export_command = f'export HF_HUB_ENABLE_HF_TRANSFER=1'\n",
73
+ " subprocess.run(export_command, shell=True, check=True) \n",
74
+ " export_command = f'export HF_HUB_VERBOSITY=\"debug\"'\n",
75
+ " subprocess.run(export_command, shell=True, check=True) \n",
76
+ "elif system == \"Windows\":\n",
77
+ " set_command = f'set HUGGING_FACE_HUB_TOKEN={hugging_face_token}'\n",
78
+ " subprocess.run(set_command, shell=True, check=True)\n",
79
+ " export_command = f'set HF_HUB_ENABLE_HF_TRANSFER=1'\n",
80
+ " subprocess.run(export_command, shell=True, check=True) \n",
81
+ " export_command = f'set HF_HUB_VERBOSITY=\"debug\"'\n",
82
+ " subprocess.run(export_command, shell=True, check=True) \n",
83
+ "\n",
84
+ "# Command to log in using the token\n",
85
+ "login_command = ['huggingface-cli', 'login', '--token', hugging_face_token]\n",
86
+ "\n",
87
+ "# Execute the login command and capture output\n",
88
+ "try:\n",
89
+ " result = subprocess.run(login_command, check=True, capture_output=True, text=True)\n",
90
+ " print(\"Output:\", result.stdout)\n",
91
+ " print(\"Error:\", result.stderr)\n",
92
+ "except subprocess.CalledProcessError as e:\n",
93
+ " print(\"Command failed with exit code:\", e.returncode)\n",
94
+ " print(\"Output:\", e.output)\n",
95
+ " print(\"Error:\", e.stderr)"
96
+ ]
97
+ },
98
+ {
99
+ "cell_type": "markdown",
100
+ "id": "afb149da",
101
+ "metadata": {},
102
+ "source": [
103
+ "### Very fast new upload - Wait till UPLOAD COMPLETED printed"
104
+ ]
105
+ },
106
+ {
107
+ "cell_type": "code",
108
+ "execution_count": null,
109
+ "id": "30b96a4f",
110
+ "metadata": {},
111
+ "outputs": [],
112
+ "source": [
113
+ "!huggingface-cli upload-large-folder \"YourUserName/reponame\" r\"/home/Ubuntu/apps/StableSwarmUI/Models/Lora\" --repo-type=model --no-bars\n",
114
+ "\n",
115
+ "print(\".\\n.\\nUPLOAD COMPLETED\")"
116
+ ]
117
+ },
118
+ {
119
+ "cell_type": "markdown",
120
+ "id": "4e7a6316",
121
+ "metadata": {},
122
+ "source": [
123
+ "### Very fast new upload V2 - Wait till UPLOAD COMPLETED printed"
124
+ ]
125
+ },
126
+ {
127
+ "cell_type": "code",
128
+ "execution_count": null,
129
+ "id": "75a18901",
130
+ "metadata": {},
131
+ "outputs": [],
132
+ "source": [
133
+ "from huggingface_hub import HfApi\n",
134
+ "\n",
135
+ "# Initialize the HfApi instance\n",
136
+ "api = HfApi()\n",
137
+ "\n",
138
+ "# Define the repo_id and the local folder to upload\n",
139
+ "repo_id = \"rexlapix/amy\" # Your Hugging Face repo\n",
140
+ "folder_path = r\"/home/Ubuntu/Desktop/workspace/test\" # Path to your local folder\n",
141
+ "\n",
142
+ "# Upload the folder to the specific subdirectory in the repo\n",
143
+ "api.upload_large_folder(\n",
144
+ " repo_id=repo_id, \n",
145
+ " repo_type=\"model\", \n",
146
+ " folder_path=folder_path, \n",
147
+ " path_in_repo=\"amy_FLUX-FT/model_fp16\" # Target directory inside the repo\n",
148
+ ")\n",
149
+ "\n",
150
+ "print(\"\\nUPLOAD COMPLETED\")\n"
151
+ ]
152
+ },
153
+ {
154
+ "cell_type": "code",
155
+ "execution_count": null,
156
+ "id": "999f28d5",
157
+ "metadata": {},
158
+ "outputs": [],
159
+ "source": [
160
+ "import os\n",
161
+ "from huggingface_hub import HfApi\n",
162
+ "\n",
163
+ "# Initialize the API\n",
164
+ "api = HfApi()\n",
165
+ "\n",
166
+ "# Define the local file path\n",
167
+ "local_file_path = r\"/home/Ubuntu/Desktop/workspace/cooked/amy_FLUX-FT/ml/model_fp8/amy_flux_fp8-000200.safetensors\"\n",
168
+ "\n",
169
+ "# Automatically extract the default file name from the local path\n",
170
+ "default_file_name = os.path.basename(local_file_path)\n",
171
+ "\n",
172
+ "# Define the path in the repository using the extracted file name\n",
173
+ "path_in_repo = f\"amy_FLUX-FT/model_fp8/{default_file_name}\"\n",
174
+ "\n",
175
+ "# Upload the file\n",
176
+ "api.upload_file(\n",
177
+ " path_or_fileobj=local_file_path,\n",
178
+ " path_in_repo=path_in_repo,\n",
179
+ " repo_id=\"rexlapix/amy\",\n",
180
+ " repo_type=\"model\",\n",
181
+ ")\n",
182
+ "\n",
183
+ "print(\".\\n.\\nUPLOAD COMPLETED\")\n"
184
+ ]
185
+ },
186
+ {
187
+ "cell_type": "markdown",
188
+ "id": "58da07da-2e0b-4a32-a36d-ddf511800c58",
189
+ "metadata": {},
190
+ "source": [
191
+ "### Upload a single file with specific name to remote repo - Wait till UPLOAD COMPLETED printed"
192
+ ]
193
+ },
194
+ {
195
+ "cell_type": "code",
196
+ "execution_count": null,
197
+ "id": "0502703b-8b44-4e58-af2c-82f67d713548",
198
+ "metadata": {},
199
+ "outputs": [],
200
+ "source": [
201
+ "# This cell is used to upload single file into a repo with certain name\n",
202
+ "\n",
203
+ "from huggingface_hub import HfApi\n",
204
+ "api = HfApi()\n",
205
+ "api.upload_file(\n",
206
+ " path_or_fileobj=r\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion/model_name.safetensors\",\n",
207
+ " path_in_repo=\"model_name.safetensors\",\n",
208
+ " repo_id=\"YourUserName/reponame\",\n",
209
+ " repo_type=\"model\",\n",
210
+ ")\n",
211
+ "\n",
212
+ "print(\".\\n.\\nUPLOAD COMPLETED\")"
213
+ ]
214
+ },
215
+ {
216
+ "cell_type": "markdown",
217
+ "id": "2dd50994-5595-46a6-b315-595ede0cf922",
218
+ "metadata": {},
219
+ "source": [
220
+ "### Upload a folder fast but not suggested for too big and many files - Wait till UPLOAD COMPLETED printed"
221
+ ]
222
+ },
223
+ {
224
+ "cell_type": "code",
225
+ "execution_count": null,
226
+ "id": "a89ece44-8f27-43bb-a7fc-e966409d6f5a",
227
+ "metadata": {},
228
+ "outputs": [],
229
+ "source": [
230
+ "# This cell is used to upload a folder into a repo with single commit\n",
231
+ "\n",
232
+ "from huggingface_hub import HfApi\n",
233
+ "api = HfApi()\n",
234
+ "api.upload_folder(\n",
235
+ " folder_path=r\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\",\n",
236
+ " repo_id=\"YourUserName/reponame\",\n",
237
+ " repo_type=\"model\",\n",
238
+ ")\n",
239
+ "\n",
240
+ "print(\".\\n.\\nUPLOAD COMPLETED\")"
241
+ ]
242
+ },
243
+ {
244
+ "cell_type": "markdown",
245
+ "id": "4d26f8d6-31cd-42a5-9477-ba1e77cd271c",
246
+ "metadata": {},
247
+ "source": [
248
+ "### To upload all files in given folder to the target Hugging Face repository use below - Wait till UPLOAD COMPLETED printed"
249
+ ]
250
+ },
251
+ {
252
+ "cell_type": "code",
253
+ "execution_count": null,
254
+ "id": "6fc96104",
255
+ "metadata": {},
256
+ "outputs": [],
257
+ "source": [
258
+ "# This cell uploads a folder into remote repo with multi commit\n",
259
+ "# Supports continue feature so if gets interrupted you can run again to continue / resume\n",
260
+ "\n",
261
+ "from huggingface_hub import HfApi\n",
262
+ "from huggingface_hub import get_collection, delete_collection_item\n",
263
+ "from huggingface_hub import upload_file\n",
264
+ "from huggingface_hub import (\n",
265
+ " HfFolder,\n",
266
+ " ModelCard,\n",
267
+ " ModelCardData,\n",
268
+ " create_repo,\n",
269
+ " hf_hub_download,\n",
270
+ " upload_folder,\n",
271
+ " whoami,\n",
272
+ ")\n",
273
+ "api = HfApi()\n",
274
+ "upload_folder(\n",
275
+ " folder_path=r\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\",\n",
276
+ " repo_id=\"YourUserName/reponame\",\n",
277
+ " repo_type=\"model\",\n",
278
+ " multi_commits=True,\n",
279
+ " multi_commits_verbose=True,\n",
280
+ ")\n",
281
+ "\n",
282
+ "print(\".\\n.\\nUPLOAD COMPLETED\")"
283
+ ]
284
+ },
285
+ {
286
+ "cell_type": "markdown",
287
+ "id": "7381726f",
288
+ "metadata": {},
289
+ "source": [
290
+ "### To download all files in given Hugging Face repository use below - Wait till DOWNLOAD COMPLETED printed"
291
+ ]
292
+ },
293
+ {
294
+ "cell_type": "code",
295
+ "execution_count": null,
296
+ "id": "90f6c9b1",
297
+ "metadata": {},
298
+ "outputs": [],
299
+ "source": [
300
+ "# You can run this cell multiple times if any error occurs it will resume\n",
301
+ "\n",
302
+ "from huggingface_hub import snapshot_download\n",
303
+ "import os\n",
304
+ "\n",
305
+ "# Ensure the local directory exists\n",
306
+ "\n",
307
+ "\n",
308
+ "repo_id=\"YourUserName/reponame\"\n",
309
+ "local_dir = \"/home/Ubuntu/apps/StableSwarmUI/Models/diffusion_models\"\n",
310
+ "\n",
311
+ "os.makedirs(local_dir, exist_ok=True)\n",
312
+ "\n",
313
+ "snapshot_download(repo_id=repo_id,local_dir=local_dir)\n",
314
+ "\n",
315
+ "print(\".\\n.\\nDOWNLOAD COMPLETED\")"
316
+ ]
317
+ },
318
+ {
319
+ "cell_type": "markdown",
320
+ "id": "09f0f192-4d6b-4511-8366-6c676cd2404c",
321
+ "metadata": {},
322
+ "source": [
323
+ "### Download a specific sub directory / folder - Wait till DOWNLOAD COMPLETED printed"
324
+ ]
325
+ },
326
+ {
327
+ "cell_type": "code",
328
+ "execution_count": null,
329
+ "id": "download_subdirectory",
330
+ "metadata": {},
331
+ "outputs": [],
332
+ "source": [
333
+ "# This cell downloads a specific subdirectory from a given repo\n",
334
+ "\n",
335
+ "from huggingface_hub import snapshot_download\n",
336
+ "import os\n",
337
+ "\n",
338
+ "# Define the repository and subdirectory\n",
339
+ "repo_id = \"YourUserName/reponame\"\n",
340
+ "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",
341
+ "local_dir = \"/home/Ubuntu/apps/StableSwarmUI/Models/diffusion_models\"\n",
342
+ "\n",
343
+ "# Ensure the local directory exists\n",
344
+ "os.makedirs(local_dir, exist_ok=True)\n",
345
+ "\n",
346
+ "# Download only the specific subdirectory\n",
347
+ "snapshot_download(\n",
348
+ " repo_id=repo_id,\n",
349
+ " local_dir=local_dir,\n",
350
+ " allow_patterns=subdirectory\n",
351
+ ")\n",
352
+ "\n",
353
+ "print(f\"Downloaded {subdirectory} from {repo_id} to {local_dir}\")\n",
354
+ "print(\".\\n.\\nDOWNLOAD COMPLETED\")"
355
+ ]
356
+ },
357
+ {
358
+ "cell_type": "markdown",
359
+ "id": "47b04ce9-9653-4e4f-9bd9-1b6ed383f502",
360
+ "metadata": {},
361
+ "source": [
362
+ "### Download a specific files - Ultra Blazing Fast Download"
363
+ ]
364
+ },
365
+ {
366
+ "cell_type": "code",
367
+ "execution_count": null,
368
+ "id": "download_specific_file",
369
+ "metadata": {},
370
+ "outputs": [],
371
+ "source": [
372
+ "# This cell downloads a specific files from a given repo\n",
373
+ "# You can download 1 or more specific files ultra fast\n",
374
+ "# Wait until you see DOWNLOAD COMPLETED - status not shown on RunPod proxy\n",
375
+ "\n",
376
+ "from huggingface_hub import snapshot_download\n",
377
+ "import os\n",
378
+ "\n",
379
+ "# Define the repository, file path, and local directory\n",
380
+ "repo_id_set = \"YourUserName/reponame\"\n",
381
+ "local_dir_set = f\"/home/Ubuntu/apps/StableSwarmUI/Models/diffusion_models\"\n",
382
+ "\n",
383
+ "# Ensure the local directory exists\n",
384
+ "os.makedirs(local_dir_set, exist_ok=True)\n",
385
+ "print(\".\\n.\\nDOWNLOAD Started...\")\n",
386
+ "snapshot_download(\n",
387
+ " repo_id=repo_id_set,\n",
388
+ " allow_patterns=[\"1024_no_caption-000060.safetensors\", \"1024_with_caption.safetensors\",\"file3.zip\"],\n",
389
+ " local_dir=local_dir_set,\n",
390
+ " )\n",
391
+ "\n",
392
+ "print(\".\\n.\\nDOWNLOAD COMPLETED\")"
393
+ ]
394
+ }
395
+ ],
396
+ "metadata": {
397
+ "kernelspec": {
398
+ "display_name": "Python 3",
399
+ "language": "python",
400
+ "name": "python3"
401
+ },
402
+ "language_info": {
403
+ "codemirror_mode": {
404
+ "name": "ipython",
405
+ "version": 3
406
+ },
407
+ "file_extension": ".py",
408
+ "mimetype": "text/x-python",
409
+ "name": "python",
410
+ "nbconvert_exporter": "python",
411
+ "pygments_lexer": "ipython3",
412
+ "version": "3.11.0rc1"
413
+ }
414
+ },
415
+ "nbformat": 4,
416
+ "nbformat_minor": 5
417
+ }