Wawaworker commited on
Commit
c90a2c7
·
verified ·
1 Parent(s): 97dcba1

Upload Hugging_Face_Upload_Download_V3.ipynb

Browse files
Files changed (1) hide show
  1. Hugging_Face_Upload_Download_V3.ipynb +412 -0
Hugging_Face_Upload_Download_V3.ipynb ADDED
@@ -0,0 +1,412 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 V3\n",
9
+ "\n",
10
+ "Latest version on : https://www.patreon.com/posts/104672510"
11
+ ]
12
+ },
13
+ {
14
+ "cell_type": "code",
15
+ "execution_count": null,
16
+ "id": "caa74bef",
17
+ "metadata": {},
18
+ "outputs": [],
19
+ "source": [
20
+ "!pip install huggingface_hub --upgrade\n",
21
+ "\n",
22
+ "!pip install ipywidgets --upgrade"
23
+ ]
24
+ },
25
+ {
26
+ "cell_type": "markdown",
27
+ "id": "502249bf",
28
+ "metadata": {},
29
+ "source": [
30
+ "Use below cell to paste your Hugging Face token key. \n",
31
+ "\n",
32
+ "Access Tokens are here : https://huggingface.co/settings/tokens"
33
+ ]
34
+ },
35
+ {
36
+ "cell_type": "code",
37
+ "execution_count": null,
38
+ "id": "0d8027c8",
39
+ "metadata": {},
40
+ "outputs": [],
41
+ "source": [
42
+ "# Use this cell to enter your Hugging Face token and authenticate \n",
43
+ "\n",
44
+ "hugging_face_token = 'your_HF_Token'\n",
45
+ "\n",
46
+ "!export HUGGING_FACE_HUB_TOKEN=hugging_face_token\n",
47
+ "\n",
48
+ "import subprocess\n",
49
+ "\n",
50
+ "# Command to log in using the token\n",
51
+ "command = ['huggingface-cli', 'login', '--token', hugging_face_token]\n",
52
+ "\n",
53
+ "# Execute the command\n",
54
+ "subprocess.run(command, check=True)"
55
+ ]
56
+ },
57
+ {
58
+ "cell_type": "code",
59
+ "execution_count": null,
60
+ "id": "0502703b-8b44-4e58-af2c-82f67d713548",
61
+ "metadata": {},
62
+ "outputs": [],
63
+ "source": [
64
+ "# This cell is used to upload single file into a repo with certain name\n",
65
+ "\n",
66
+ "from huggingface_hub import HfApi\n",
67
+ "api = HfApi()\n",
68
+ "api.upload_file(\n",
69
+ " path_or_fileobj=r\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion/model_name.safetensors\",\n",
70
+ " path_in_repo=\"model_name.safetensors\",\n",
71
+ " repo_id=\"YourUserName/reponame\",\n",
72
+ " repo_type=\"model\",\n",
73
+ ")"
74
+ ]
75
+ },
76
+ {
77
+ "cell_type": "code",
78
+ "execution_count": null,
79
+ "id": "3be9367c-a04d-4d05-83ad-c8504e0383b4",
80
+ "metadata": {},
81
+ "outputs": [],
82
+ "source": [
83
+ "# This cell is used to upload a folder into a repo with single commit\n",
84
+ "\n",
85
+ "from huggingface_hub import HfApi\n",
86
+ "api = HfApi()\n",
87
+ "api.upload_folder(\n",
88
+ " folder_path=r\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\",\n",
89
+ " repo_id=\"YourUserName/reponame\",\n",
90
+ " repo_type=\"model\",\n",
91
+ ")"
92
+ ]
93
+ },
94
+ {
95
+ "cell_type": "markdown",
96
+ "id": "8c5ff0b4",
97
+ "metadata": {},
98
+ "source": [
99
+ "To upload all files in given folder to the target Hugging Face repository use below"
100
+ ]
101
+ },
102
+ {
103
+ "cell_type": "code",
104
+ "execution_count": null,
105
+ "id": "6fc96104",
106
+ "metadata": {},
107
+ "outputs": [],
108
+ "source": [
109
+ "# This cell uploads a folder into remote repo with multi commit\n",
110
+ "# Supports continue feature so if gets interrupted you can run again to continue / resume\n",
111
+ "\n",
112
+ "from huggingface_hub import HfApi\n",
113
+ "from huggingface_hub import get_collection, delete_collection_item\n",
114
+ "from huggingface_hub import upload_file\n",
115
+ "from huggingface_hub import (\n",
116
+ " HfFolder,\n",
117
+ " ModelCard,\n",
118
+ " ModelCardData,\n",
119
+ " create_repo,\n",
120
+ " hf_hub_download,\n",
121
+ " upload_folder,\n",
122
+ " whoami,\n",
123
+ ")\n",
124
+ "api = HfApi()\n",
125
+ "upload_folder(\n",
126
+ " folder_path=r\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\",\n",
127
+ " repo_id=\"YourUserName/reponame\",\n",
128
+ " repo_type=\"model\",\n",
129
+ " multi_commits=True,\n",
130
+ " multi_commits_verbose=True,\n",
131
+ ")\n"
132
+ ]
133
+ },
134
+ {
135
+ "cell_type": "markdown",
136
+ "id": "7381726f",
137
+ "metadata": {},
138
+ "source": [
139
+ "To download all files in given Hugging Face repository use below"
140
+ ]
141
+ },
142
+ {
143
+ "cell_type": "code",
144
+ "execution_count": null,
145
+ "id": "90f6c9b1",
146
+ "metadata": {},
147
+ "outputs": [],
148
+ "source": [
149
+ "# This cell downloads all files from given repo 1 by 1 \n",
150
+ "# This is not same as clone and it is faster and more convenient to use \n",
151
+ "\n",
152
+ "!pip install tqdm --upgrade\n",
153
+ "\n",
154
+ "import os\n",
155
+ "import requests\n",
156
+ "from huggingface_hub import list_repo_files, hf_hub_url\n",
157
+ "from huggingface_hub.utils import HfFolder\n",
158
+ "from tqdm import tqdm\n",
159
+ "\n",
160
+ "# Define the repository and target folder\n",
161
+ "repo_id = \"YourUserName/reponame\"\n",
162
+ "target_folder = \"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\"\n",
163
+ "\n",
164
+ "# Retrieve the token from the .huggingface folder or set it manually\n",
165
+ "token = HfFolder.get_token()\n",
166
+ "# or set your token directly\n",
167
+ "# token = \"your_huggingface_token\"\n",
168
+ "\n",
169
+ "if not token:\n",
170
+ " raise ValueError(\"Hugging Face token not found. Please log in using `huggingface-cli login` or set the token manually.\")\n",
171
+ "\n",
172
+ "headers = {\n",
173
+ " \"Authorization\": f\"Bearer {token}\"\n",
174
+ "}\n",
175
+ "\n",
176
+ "# List all files in the repository\n",
177
+ "files = list_repo_files(repo_id)\n",
178
+ "\n",
179
+ "# Ensure the target folder exists\n",
180
+ "os.makedirs(target_folder, exist_ok=True)\n",
181
+ "\n",
182
+ "# Download each file directly to the target folder\n",
183
+ "for file in files:\n",
184
+ " try:\n",
185
+ " # Define the target path for the file\n",
186
+ " target_path = os.path.join(target_folder, file)\n",
187
+ " \n",
188
+ " # Check if the file already exists\n",
189
+ " if os.path.exists(target_path):\n",
190
+ " print(f\"File {file} already exists. Skipping download.\")\n",
191
+ " continue\n",
192
+ " \n",
193
+ " # Get the URL for the file\n",
194
+ " file_url = hf_hub_url(repo_id, filename=file, repo_type='model')\n",
195
+ " \n",
196
+ " # Ensure subdirectories exist\n",
197
+ " os.makedirs(os.path.dirname(target_path), exist_ok=True)\n",
198
+ " \n",
199
+ " # Download the file with authentication\n",
200
+ " response = requests.get(file_url, headers=headers, stream=True)\n",
201
+ " response.raise_for_status() # Raise an error for bad status codes\n",
202
+ " \n",
203
+ " # Get the total file size from the response headers\n",
204
+ " total_size = int(response.headers.get('content-length', 0))\n",
205
+ " \n",
206
+ " # Progress bar setup\n",
207
+ " with tqdm(total=total_size, unit='B', unit_scale=True, desc=file, initial=0, ascii=True) as pbar:\n",
208
+ " # Write the file to the target path\n",
209
+ " with open(target_path, 'wb') as f:\n",
210
+ " for chunk in response.iter_content(chunk_size=8192):\n",
211
+ " if chunk:\n",
212
+ " f.write(chunk)\n",
213
+ " pbar.update(len(chunk))\n",
214
+ " \n",
215
+ " # Set the correct permissions for the downloaded file\n",
216
+ " os.chmod(target_path, 0o644) # Read and write for owner, read for group and others\n",
217
+ " \n",
218
+ " except Exception as e:\n",
219
+ " print(f\"An error occurred while processing file {file}: {e}\")\n",
220
+ "\n",
221
+ "print(f\"All files have been downloaded to {target_folder}\")"
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
+ "!pip install tqdm --upgrade\n",
234
+ "\n",
235
+ "import os\n",
236
+ "import requests\n",
237
+ "from huggingface_hub import list_repo_files, hf_hub_url\n",
238
+ "from huggingface_hub.utils import HfFolder\n",
239
+ "from tqdm import tqdm\n",
240
+ "\n",
241
+ "# Define the repository, subdirectory, and target folder\n",
242
+ "repo_id = \"YourUserName/reponame\" # example: MonsterMMORPG/kohya_new_test\n",
243
+ "subdirectory = \"path/to/subdirectory\" # example: 6e_6\n",
244
+ "target_folder = \"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\"\n",
245
+ "\n",
246
+ "# Retrieve the token from the .huggingface folder or set it manually\n",
247
+ "token = HfFolder.get_token()\n",
248
+ "# or set your token directly\n",
249
+ "# token = \"your_huggingface_token\"\n",
250
+ "\n",
251
+ "if not token:\n",
252
+ " raise ValueError(\"Hugging Face token not found. Please log in using `huggingface-cli login` or set the token manually.\")\n",
253
+ "\n",
254
+ "headers = {\n",
255
+ " \"Authorization\": f\"Bearer {token}\"\n",
256
+ "}\n",
257
+ "\n",
258
+ "# List all files in the repository\n",
259
+ "files = list_repo_files(repo_id)\n",
260
+ "\n",
261
+ "# Ensure the target folder exists\n",
262
+ "os.makedirs(target_folder, exist_ok=True)\n",
263
+ "\n",
264
+ "# Filter files to only include those in the specified subdirectory\n",
265
+ "subdirectory_files = [file for file in files if file.startswith(subdirectory + '/')]\n",
266
+ "\n",
267
+ "# Download each file directly to the target folder\n",
268
+ "for file in subdirectory_files:\n",
269
+ " try:\n",
270
+ " # Define the target path for the file\n",
271
+ " target_path = os.path.join(target_folder, os.path.relpath(file, subdirectory))\n",
272
+ " \n",
273
+ " # Check if the file already exists\n",
274
+ " if os.path.exists(target_path):\n",
275
+ " print(f\"File {file} already exists. Skipping download.\")\n",
276
+ " continue\n",
277
+ " \n",
278
+ " # Get the URL for the file\n",
279
+ " file_url = hf_hub_url(repo_id, filename=file, repo_type='model')\n",
280
+ " \n",
281
+ " # Ensure subdirectories exist\n",
282
+ " os.makedirs(os.path.dirname(target_path), exist_ok=True)\n",
283
+ " \n",
284
+ " # Download the file with authentication\n",
285
+ " response = requests.get(file_url, headers=headers, stream=True)\n",
286
+ " response.raise_for_status() # Raise an error for bad status codes\n",
287
+ " \n",
288
+ " # Get the total file size from the response headers\n",
289
+ " total_size = int(response.headers.get('content-length', 0))\n",
290
+ " \n",
291
+ " # Progress bar setup\n",
292
+ " with tqdm(total=total_size, unit='B', unit_scale=True, desc=file, initial=0, ascii=True) as pbar:\n",
293
+ " # Write the file to the target path\n",
294
+ " with open(target_path, 'wb') as f:\n",
295
+ " for chunk in response.iter_content(chunk_size=8192):\n",
296
+ " if chunk:\n",
297
+ " f.write(chunk)\n",
298
+ " pbar.update(len(chunk))\n",
299
+ " \n",
300
+ " # Set the correct permissions for the downloaded file\n",
301
+ " os.chmod(target_path, 0o644) # Read and write for owner, read for group and others\n",
302
+ " \n",
303
+ " except Exception as e:\n",
304
+ " print(f\"An error occurred while processing file {file}: {e}\")\n",
305
+ "\n",
306
+ "print(f\"All files from the subdirectory '{subdirectory}' have been downloaded to {target_folder}\")"
307
+ ]
308
+ },
309
+ {
310
+ "cell_type": "code",
311
+ "execution_count": null,
312
+ "id": "download_specific_file",
313
+ "metadata": {},
314
+ "outputs": [],
315
+ "source": [
316
+ "# This cell downloads a specific file from a given repo\n",
317
+ "\n",
318
+ "!pip install tqdm --upgrade\n",
319
+ "\n",
320
+ "import os\n",
321
+ "import requests\n",
322
+ "from huggingface_hub import hf_hub_url\n",
323
+ "from huggingface_hub.utils import HfFolder\n",
324
+ "from tqdm import tqdm\n",
325
+ "\n",
326
+ "# Define the repository, file path, and target folder\n",
327
+ "repo_id = \"YourUserName/reponame\" # example: MonsterMMORPG/kohya_new_test\n",
328
+ "file_path = \"path/to/your/file.ext\" # example: 6e_6/9e_6_TE_1e_6.safetensors download model file inside 6e_6 folder\n",
329
+ "target_folder = \"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\"\n",
330
+ "\n",
331
+ "# Retrieve the token from the .huggingface folder or set it manually\n",
332
+ "token = HfFolder.get_token()\n",
333
+ "# or set your token directly\n",
334
+ "# token = \"your_huggingface_token\"\n",
335
+ "\n",
336
+ "if not token:\n",
337
+ " raise ValueError(\"Hugging Face token not found. Please log in using `huggingface-cli login` or set the token manually.\")\n",
338
+ "\n",
339
+ "headers = {\n",
340
+ " \"Authorization\": f\"Bearer {token}\"\n",
341
+ "}\n",
342
+ "\n",
343
+ "# Ensure the target folder exists\n",
344
+ "os.makedirs(target_folder, exist_ok=True)\n",
345
+ "\n",
346
+ "# Define the target path for the file\n",
347
+ "target_path = os.path.join(target_folder, os.path.basename(file_path))\n",
348
+ "\n",
349
+ "# Check if the file already exists\n",
350
+ "if os.path.exists(target_path):\n",
351
+ " print(f\"File {file_path} already exists. Skipping download.\")\n",
352
+ "else:\n",
353
+ " try:\n",
354
+ " # Get the URL for the file\n",
355
+ " file_url = hf_hub_url(repo_id, filename=file_path, repo_type='model')\n",
356
+ " \n",
357
+ " # Download the file with authentication\n",
358
+ " response = requests.get(file_url, headers=headers, stream=True)\n",
359
+ " response.raise_for_status() # Raise an error for bad status codes\n",
360
+ " \n",
361
+ " # Get the total file size from the response headers\n",
362
+ " total_size = int(response.headers.get('content-length', 0))\n",
363
+ " \n",
364
+ " # Progress bar setup\n",
365
+ " with tqdm(total=total_size, unit='B', unit_scale=True, desc=os.path.basename(file_path), initial=0, ascii=True) as pbar:\n",
366
+ " # Write the file to the target path\n",
367
+ " with open(target_path, 'wb') as f:\n",
368
+ " for chunk in response.iter_content(chunk_size=8192):\n",
369
+ " if chunk:\n",
370
+ " f.write(chunk)\n",
371
+ " pbar.update(len(chunk))\n",
372
+ " \n",
373
+ " # Set the correct permissions for the downloaded file\n",
374
+ " os.chmod(target_path, 0o644) # Read and write for owner, read for group and others\n",
375
+ " \n",
376
+ " except Exception as e:\n",
377
+ " print(f\"An error occurred while processing file {file_path}: {e}\")\n",
378
+ "\n",
379
+ "print(f\"The file '{file_path}' has been downloaded to {target_folder}\")"
380
+ ]
381
+ },
382
+ {
383
+ "cell_type": "code",
384
+ "execution_count": null,
385
+ "id": "4fd5f2fb",
386
+ "metadata": {},
387
+ "outputs": [],
388
+ "source": []
389
+ }
390
+ ],
391
+ "metadata": {
392
+ "kernelspec": {
393
+ "display_name": "Python 3",
394
+ "language": "python",
395
+ "name": "python3"
396
+ },
397
+ "language_info": {
398
+ "codemirror_mode": {
399
+ "name": "ipython",
400
+ "version": 3
401
+ },
402
+ "file_extension": ".py",
403
+ "mimetype": "text/x-python",
404
+ "name": "python",
405
+ "nbconvert_exporter": "python",
406
+ "pygments_lexer": "ipython3",
407
+ "version": "3.9.16"
408
+ }
409
+ },
410
+ "nbformat": 4,
411
+ "nbformat_minor": 5
412
+ }