KamCastle commited on
Commit
a43405f
·
verified ·
1 Parent(s): bf717d6

Upload Hugging_Face_Upload_Download_V16.ipynb

Browse files
Hugging_Face_Upload_Download_V16.ipynb ADDED
@@ -0,0 +1,315 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 V16\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 hf_transfer --upgrade\n",
29
+ "\n",
30
+ "!pip install hf_xet --upgrade\n",
31
+ "\n",
32
+ "!pip install --upgrade jupyterlab-widgets\n",
33
+ "\n",
34
+ "!pip install --upgrade ipywidgets\n",
35
+ "\n",
36
+ "!jupyter nbextension enable --py widgetsnbextension"
37
+ ]
38
+ },
39
+ {
40
+ "cell_type": "markdown",
41
+ "id": "502249bf",
42
+ "metadata": {},
43
+ "source": [
44
+ "### Use below cell to paste your Hugging Face token key. \n",
45
+ "\n",
46
+ "### Access Tokens are here : https://huggingface.co/settings/tokens"
47
+ ]
48
+ },
49
+ {
50
+ "cell_type": "code",
51
+ "execution_count": null,
52
+ "id": "0d8027c8",
53
+ "metadata": {},
54
+ "outputs": [],
55
+ "source": [
56
+ "import os\n",
57
+ "import subprocess\n",
58
+ "import platform\n",
59
+ "\n",
60
+ "hugging_face_token = 'replace_with_your_HF_Token'\n",
61
+ "\n",
62
+ "# Set the environment variable\n",
63
+ "os.environ['HUGGING_FACE_HUB_TOKEN'] = hugging_face_token\n",
64
+ "os.environ['HF_HUB_ENABLE_HF_TRANSFER'] = \"1\"\n",
65
+ "os.environ['HF_HUB_VERBOSITY'] = \"debug\"\n",
66
+ "\n",
67
+ "\n",
68
+ "# Determine the operating system\n",
69
+ "system = platform.system()\n",
70
+ "\n",
71
+ "if system == \"Linux\":\n",
72
+ " export_command = f'export HUGGING_FACE_HUB_TOKEN={hugging_face_token}'\n",
73
+ " subprocess.run(export_command, shell=True, check=True)\n",
74
+ " export_command = f'export HF_HUB_ENABLE_HF_TRANSFER=1'\n",
75
+ " subprocess.run(export_command, shell=True, check=True) \n",
76
+ " export_command = f'export HF_HUB_VERBOSITY=\"debug\"'\n",
77
+ " subprocess.run(export_command, shell=True, check=True) \n",
78
+ "elif system == \"Windows\":\n",
79
+ " set_command = f'set HUGGING_FACE_HUB_TOKEN={hugging_face_token}'\n",
80
+ " subprocess.run(set_command, shell=True, check=True)\n",
81
+ " export_command = f'set HF_HUB_ENABLE_HF_TRANSFER=1'\n",
82
+ " subprocess.run(export_command, shell=True, check=True) \n",
83
+ " export_command = f'set HF_HUB_VERBOSITY=\"debug\"'\n",
84
+ " subprocess.run(export_command, shell=True, check=True) \n",
85
+ "\n",
86
+ "# Command to log in using the token\n",
87
+ "login_command = ['huggingface-cli', 'login', '--token', hugging_face_token]\n",
88
+ "\n",
89
+ "# Execute the login command and capture output\n",
90
+ "try:\n",
91
+ " result = subprocess.run(login_command, check=True, capture_output=True, text=True)\n",
92
+ " print(\"Output:\", result.stdout)\n",
93
+ " print(\"Error:\", result.stderr)\n",
94
+ "except subprocess.CalledProcessError as e:\n",
95
+ " print(\"Command failed with exit code:\", e.returncode)\n",
96
+ " print(\"Output:\", e.output)\n",
97
+ " print(\"Error:\", e.stderr)"
98
+ ]
99
+ },
100
+ {
101
+ "cell_type": "markdown",
102
+ "id": "01529333-a7c2-4e13-bfc9-ebf053691051",
103
+ "metadata": {},
104
+ "source": [
105
+ "### Very fast new upload - Wait till UPLOAD COMPLETED printed"
106
+ ]
107
+ },
108
+ {
109
+ "cell_type": "raw",
110
+ "id": "85bd218b-d08f-46b7-8c19-953ccc019bf6",
111
+ "metadata": {},
112
+ "source": [
113
+ "INFO : On Windows give the path like this E:\\\\MMAudio_v9 - use double \\\\"
114
+ ]
115
+ },
116
+ {
117
+ "cell_type": "code",
118
+ "execution_count": null,
119
+ "id": "30b96a4f",
120
+ "metadata": {},
121
+ "outputs": [],
122
+ "source": [
123
+ "!huggingface-cli upload-large-folder \"YourUserName/reponame\" \"/home/Ubuntu/apps/StableSwarmUI/Models/diffusion_models\" --repo-type=model --no-bars\n",
124
+ "\n",
125
+ "print(\".\\n.\\nUPLOAD COMPLETED\")"
126
+ ]
127
+ },
128
+ {
129
+ "cell_type": "markdown",
130
+ "id": "4e7a6316",
131
+ "metadata": {},
132
+ "source": [
133
+ "### Recommended - Very fast new upload V2 - Wait till UPLOAD COMPLETED printed"
134
+ ]
135
+ },
136
+ {
137
+ "cell_type": "code",
138
+ "execution_count": null,
139
+ "id": "75a18901",
140
+ "metadata": {},
141
+ "outputs": [],
142
+ "source": [
143
+ "from huggingface_hub import HfApi\n",
144
+ "\n",
145
+ "api = HfApi()\n",
146
+ "\n",
147
+ "api.upload_large_folder(repo_id=\"YourUserName/reponame\", \n",
148
+ " repo_type=\"model\", \n",
149
+ " folder_path=r\"/home/Ubuntu/apps/StableSwarmUI/Models/diffusion_models\")\n",
150
+ "\n",
151
+ "print(\".\\n.\\nUPLOAD COMPLETED\")"
152
+ ]
153
+ },
154
+ {
155
+ "cell_type": "markdown",
156
+ "id": "58da07da-2e0b-4a32-a36d-ddf511800c58",
157
+ "metadata": {},
158
+ "source": [
159
+ "### Upload a single file with specific name to remote repo - Wait till UPLOAD COMPLETED printed"
160
+ ]
161
+ },
162
+ {
163
+ "cell_type": "code",
164
+ "execution_count": null,
165
+ "id": "0502703b-8b44-4e58-af2c-82f67d713548",
166
+ "metadata": {},
167
+ "outputs": [],
168
+ "source": [
169
+ "# This cell is used to upload single file into a repo with certain name\n",
170
+ "\n",
171
+ "from huggingface_hub import HfApi\n",
172
+ "api = HfApi()\n",
173
+ "api.upload_file(\n",
174
+ " path_or_fileobj=r\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion/model_name.safetensors\",\n",
175
+ " path_in_repo=\"model_name.safetensors\",\n",
176
+ " repo_id=\"YourUserName/reponame\",\n",
177
+ " repo_type=\"model\",\n",
178
+ ")\n",
179
+ "\n",
180
+ "print(\".\\n.\\nUPLOAD COMPLETED\")"
181
+ ]
182
+ },
183
+ {
184
+ "cell_type": "markdown",
185
+ "id": "7381726f",
186
+ "metadata": {},
187
+ "source": [
188
+ "### To download all files in given Hugging Face repository use below - Wait till DOWNLOAD COMPLETED printed"
189
+ ]
190
+ },
191
+ {
192
+ "cell_type": "code",
193
+ "execution_count": null,
194
+ "id": "90f6c9b1",
195
+ "metadata": {},
196
+ "outputs": [],
197
+ "source": [
198
+ "# You can run this cell multiple times if any error occurs it will resume\n",
199
+ "\n",
200
+ "from huggingface_hub import snapshot_download\n",
201
+ "import os\n",
202
+ "\n",
203
+ "# Ensure the local directory exists\n",
204
+ "\n",
205
+ "\n",
206
+ "repo_id=\"YourUserName/reponame\"\n",
207
+ "local_dir = \"/home/Ubuntu/apps/StableSwarmUI/Models/diffusion_models\"\n",
208
+ "\n",
209
+ "os.makedirs(local_dir, exist_ok=True)\n",
210
+ "\n",
211
+ "snapshot_download(repo_id=repo_id,local_dir=local_dir)\n",
212
+ "\n",
213
+ "print(\".\\n.\\nDOWNLOAD COMPLETED\")"
214
+ ]
215
+ },
216
+ {
217
+ "cell_type": "markdown",
218
+ "id": "09f0f192-4d6b-4511-8366-6c676cd2404c",
219
+ "metadata": {},
220
+ "source": [
221
+ "### Download a specific sub directory / folder - Wait till DOWNLOAD COMPLETED printed"
222
+ ]
223
+ },
224
+ {
225
+ "cell_type": "code",
226
+ "execution_count": null,
227
+ "id": "download_subdirectory",
228
+ "metadata": {},
229
+ "outputs": [],
230
+ "source": [
231
+ "# This cell downloads a specific subdirectory from a given repo\n",
232
+ "\n",
233
+ "from huggingface_hub import snapshot_download\n",
234
+ "import os\n",
235
+ "\n",
236
+ "# Define the repository and subdirectory\n",
237
+ "repo_id = \"YourUserName/reponame\"\n",
238
+ "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",
239
+ "local_dir = \"/home/Ubuntu/apps/StableSwarmUI/Models/diffusion_models\"\n",
240
+ "\n",
241
+ "# Ensure the local directory exists\n",
242
+ "os.makedirs(local_dir, exist_ok=True)\n",
243
+ "\n",
244
+ "# Download only the specific subdirectory\n",
245
+ "snapshot_download(\n",
246
+ " repo_id=repo_id,\n",
247
+ " local_dir=local_dir,\n",
248
+ " allow_patterns=subdirectory\n",
249
+ ")\n",
250
+ "\n",
251
+ "print(f\"Downloaded {subdirectory} from {repo_id} to {local_dir}\")\n",
252
+ "print(\".\\n.\\nDOWNLOAD COMPLETED\")"
253
+ ]
254
+ },
255
+ {
256
+ "cell_type": "markdown",
257
+ "id": "47b04ce9-9653-4e4f-9bd9-1b6ed383f502",
258
+ "metadata": {},
259
+ "source": [
260
+ "### Download a specific files - Ultra Blazing Fast Download"
261
+ ]
262
+ },
263
+ {
264
+ "cell_type": "code",
265
+ "execution_count": null,
266
+ "id": "download_specific_file",
267
+ "metadata": {},
268
+ "outputs": [],
269
+ "source": [
270
+ "# This cell downloads a specific files from a given repo\n",
271
+ "# You can download 1 or more specific files ultra fast\n",
272
+ "# Wait until you see DOWNLOAD COMPLETED - status not shown on RunPod proxy\n",
273
+ "\n",
274
+ "from huggingface_hub import snapshot_download\n",
275
+ "import os\n",
276
+ "\n",
277
+ "# Define the repository, file path, and local directory\n",
278
+ "repo_id_set = \"YourUserName/reponame\"\n",
279
+ "local_dir_set = f\"/home/Ubuntu/apps/StableSwarmUI/Models/diffusion_models\"\n",
280
+ "\n",
281
+ "# Ensure the local directory exists\n",
282
+ "os.makedirs(local_dir_set, exist_ok=True)\n",
283
+ "print(\".\\n.\\nDOWNLOAD Started...\")\n",
284
+ "snapshot_download(\n",
285
+ " repo_id=repo_id_set,\n",
286
+ " allow_patterns=[\"1024_no_caption-000060.safetensors\", \"1024_with_caption.safetensors\",\"file3.zip\"],\n",
287
+ " local_dir=local_dir_set,\n",
288
+ " )\n",
289
+ "\n",
290
+ "print(\".\\n.\\nDOWNLOAD COMPLETED\")"
291
+ ]
292
+ }
293
+ ],
294
+ "metadata": {
295
+ "kernelspec": {
296
+ "display_name": "Python 3 (ipykernel)",
297
+ "language": "python",
298
+ "name": "python3"
299
+ },
300
+ "language_info": {
301
+ "codemirror_mode": {
302
+ "name": "ipython",
303
+ "version": 3
304
+ },
305
+ "file_extension": ".py",
306
+ "mimetype": "text/x-python",
307
+ "name": "python",
308
+ "nbconvert_exporter": "python",
309
+ "pygments_lexer": "ipython3",
310
+ "version": "3.10.12"
311
+ }
312
+ },
313
+ "nbformat": 4,
314
+ "nbformat_minor": 5
315
+ }