moja72 commited on
Commit
da8c929
·
verified ·
1 Parent(s): e007192

Delete Hugging_Face_Upload_Download_V9.ipynb

Browse files
Files changed (1) hide show
  1. Hugging_Face_Upload_Download_V9.ipynb +0 -359
Hugging_Face_Upload_Download_V9.ipynb DELETED
@@ -1,359 +0,0 @@
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 V9\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"
29
- ]
30
- },
31
- {
32
- "cell_type": "markdown",
33
- "id": "502249bf",
34
- "metadata": {},
35
- "source": [
36
- "### Use below cell to paste your Hugging Face token key. \n",
37
- "\n",
38
- "### Access Tokens are here : https://huggingface.co/settings/tokens"
39
- ]
40
- },
41
- {
42
- "cell_type": "code",
43
- "execution_count": null,
44
- "id": "0d8027c8",
45
- "metadata": {},
46
- "outputs": [],
47
- "source": [
48
- "import os\n",
49
- "import subprocess\n",
50
- "import platform\n",
51
- "\n",
52
- "hugging_face_token = 'replace_with_your_HF_token'\n",
53
- "\n",
54
- "# Set the environment variable\n",
55
- "os.environ['HUGGING_FACE_HUB_TOKEN'] = hugging_face_token\n",
56
- "\n",
57
- "# Determine the operating system\n",
58
- "system = platform.system()\n",
59
- "\n",
60
- "if system == \"Linux\":\n",
61
- " export_command = f'export HUGGING_FACE_HUB_TOKEN={hugging_face_token}'\n",
62
- " subprocess.run(export_command, shell=True, check=True)\n",
63
- "elif system == \"Windows\":\n",
64
- " set_command = f'set HUGGING_FACE_HUB_TOKEN={hugging_face_token}'\n",
65
- " subprocess.run(set_command, shell=True, check=True)\n",
66
- "\n",
67
- "# Command to log in using the token\n",
68
- "login_command = ['huggingface-cli', 'login', '--token', hugging_face_token]\n",
69
- "\n",
70
- "# Execute the login command and capture output\n",
71
- "try:\n",
72
- " result = subprocess.run(login_command, check=True, capture_output=True, text=True)\n",
73
- " print(\"Output:\", result.stdout)\n",
74
- " print(\"Error:\", result.stderr)\n",
75
- "except subprocess.CalledProcessError as e:\n",
76
- " print(\"Command failed with exit code:\", e.returncode)\n",
77
- " print(\"Output:\", e.output)\n",
78
- " print(\"Error:\", e.stderr)"
79
- ]
80
- },
81
- {
82
- "cell_type": "markdown",
83
- "id": "afb149da",
84
- "metadata": {},
85
- "source": [
86
- "### Very fast new upload - Wait till UPLOAD COMPLETED printed"
87
- ]
88
- },
89
- {
90
- "cell_type": "code",
91
- "execution_count": null,
92
- "id": "30b96a4f",
93
- "metadata": {},
94
- "outputs": [],
95
- "source": [
96
- "!huggingface-cli upload-large-folder \"YourUserName/reponame\" r\"/home/Ubuntu/apps/StableSwarmUI/Models/Lora\" --repo-type=model --no-bars\n",
97
- "\n",
98
- "print(\".\\n.\\nUPLOAD COMPLETED\")"
99
- ]
100
- },
101
- {
102
- "cell_type": "markdown",
103
- "id": "4e7a6316",
104
- "metadata": {},
105
- "source": [
106
- "### Very fast new upload V2 - Wait till UPLOAD COMPLETED printed"
107
- ]
108
- },
109
- {
110
- "cell_type": "code",
111
- "execution_count": null,
112
- "id": "75a18901",
113
- "metadata": {},
114
- "outputs": [],
115
- "source": [
116
- "from huggingface_hub import HfApi\n",
117
- "\n",
118
- "api = HfApi()\n",
119
- "\n",
120
- "api.upload_large_folder(repo_id=\"YourUserName/reponame\", \n",
121
- " repo_type=\"model\", \n",
122
- " folder_path=r\"/home/Ubuntu/apps/StableSwarmUI/Models/Lora\")\n",
123
- "\n",
124
- "print(\".\\n.\\nUPLOAD COMPLETED\")"
125
- ]
126
- },
127
- {
128
- "cell_type": "markdown",
129
- "id": "58da07da-2e0b-4a32-a36d-ddf511800c58",
130
- "metadata": {},
131
- "source": [
132
- "### Upload a single file with specific name to remote repo - Wait till UPLOAD COMPLETED printed"
133
- ]
134
- },
135
- {
136
- "cell_type": "code",
137
- "execution_count": null,
138
- "id": "0502703b-8b44-4e58-af2c-82f67d713548",
139
- "metadata": {},
140
- "outputs": [],
141
- "source": [
142
- "# This cell is used to upload single file into a repo with certain name\n",
143
- "\n",
144
- "from huggingface_hub import HfApi\n",
145
- "api = HfApi()\n",
146
- "api.upload_file(\n",
147
- " path_or_fileobj=r\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion/model_name.safetensors\",\n",
148
- " path_in_repo=\"model_name.safetensors\",\n",
149
- " repo_id=\"YourUserName/reponame\",\n",
150
- " repo_type=\"model\",\n",
151
- ")\n",
152
- "\n",
153
- "print(\".\\n.\\nUPLOAD COMPLETED\")"
154
- ]
155
- },
156
- {
157
- "cell_type": "markdown",
158
- "id": "2dd50994-5595-46a6-b315-595ede0cf922",
159
- "metadata": {},
160
- "source": [
161
- "### Upload a folder fast but not suggested for too big and many files - Wait till UPLOAD COMPLETED printed"
162
- ]
163
- },
164
- {
165
- "cell_type": "code",
166
- "execution_count": null,
167
- "id": "a89ece44-8f27-43bb-a7fc-e966409d6f5a",
168
- "metadata": {},
169
- "outputs": [],
170
- "source": [
171
- "# This cell is used to upload a folder into a repo with single commit\n",
172
- "\n",
173
- "from huggingface_hub import HfApi\n",
174
- "api = HfApi()\n",
175
- "api.upload_folder(\n",
176
- " folder_path=r\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\",\n",
177
- " repo_id=\"YourUserName/reponame\",\n",
178
- " repo_type=\"model\",\n",
179
- ")\n",
180
- "\n",
181
- "print(\".\\n.\\nUPLOAD COMPLETED\")"
182
- ]
183
- },
184
- {
185
- "cell_type": "markdown",
186
- "id": "4d26f8d6-31cd-42a5-9477-ba1e77cd271c",
187
- "metadata": {},
188
- "source": [
189
- "### To upload all files in given folder to the target Hugging Face repository use below - Wait till UPLOAD COMPLETED printed"
190
- ]
191
- },
192
- {
193
- "cell_type": "code",
194
- "execution_count": null,
195
- "id": "6fc96104",
196
- "metadata": {},
197
- "outputs": [],
198
- "source": [
199
- "# This cell uploads a folder into remote repo with multi commit\n",
200
- "# Supports continue feature so if gets interrupted you can run again to continue / resume\n",
201
- "\n",
202
- "from huggingface_hub import HfApi\n",
203
- "from huggingface_hub import get_collection, delete_collection_item\n",
204
- "from huggingface_hub import upload_file\n",
205
- "from huggingface_hub import (\n",
206
- " HfFolder,\n",
207
- " ModelCard,\n",
208
- " ModelCardData,\n",
209
- " create_repo,\n",
210
- " hf_hub_download,\n",
211
- " upload_folder,\n",
212
- " whoami,\n",
213
- ")\n",
214
- "api = HfApi()\n",
215
- "upload_folder(\n",
216
- " folder_path=r\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\",\n",
217
- " repo_id=\"YourUserName/reponame\",\n",
218
- " repo_type=\"model\",\n",
219
- " multi_commits=True,\n",
220
- " multi_commits_verbose=True,\n",
221
- ")\n",
222
- "\n",
223
- "print(\".\\n.\\nUPLOAD COMPLETED\")"
224
- ]
225
- },
226
- {
227
- "cell_type": "markdown",
228
- "id": "7381726f",
229
- "metadata": {},
230
- "source": [
231
- "### To download all files in given Hugging Face repository use below - Wait till DOWNLOAD COMPLETED printed"
232
- ]
233
- },
234
- {
235
- "cell_type": "code",
236
- "execution_count": null,
237
- "id": "90f6c9b1",
238
- "metadata": {},
239
- "outputs": [],
240
- "source": [
241
- "# You can run this cell multiple times if any error occurs it will resume\n",
242
- "\n",
243
- "from huggingface_hub import snapshot_download\n",
244
- "import os\n",
245
- "\n",
246
- "# Ensure the local directory exists\n",
247
- "\n",
248
- "\n",
249
- "repo_id=\"YourUserName/reponame\"\n",
250
- "local_dir=\"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\"\n",
251
- "\n",
252
- "os.makedirs(local_dir, exist_ok=True)\n",
253
- "\n",
254
- "snapshot_download(repo_id=repo_id,local_dir=local_dir)\n",
255
- "\n",
256
- "print(\".\\n.\\nDOWNLOAD COMPLETED\")"
257
- ]
258
- },
259
- {
260
- "cell_type": "markdown",
261
- "id": "09f0f192-4d6b-4511-8366-6c676cd2404c",
262
- "metadata": {},
263
- "source": [
264
- "### Download a specific sub directory / folder - Wait till DOWNLOAD COMPLETED printed"
265
- ]
266
- },
267
- {
268
- "cell_type": "code",
269
- "execution_count": null,
270
- "id": "download_subdirectory",
271
- "metadata": {},
272
- "outputs": [],
273
- "source": [
274
- "# This cell downloads a specific subdirectory from a given repo\n",
275
- "\n",
276
- "from huggingface_hub import snapshot_download\n",
277
- "import os\n",
278
- "\n",
279
- "# Define the repository and subdirectory\n",
280
- "repo_id = \"YourUserName/reponame\"\n",
281
- "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",
282
- "local_dir = \"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\"\n",
283
- "\n",
284
- "# Ensure the local directory exists\n",
285
- "os.makedirs(local_dir, exist_ok=True)\n",
286
- "\n",
287
- "# Download only the specific subdirectory\n",
288
- "snapshot_download(\n",
289
- " repo_id=repo_id,\n",
290
- " local_dir=local_dir,\n",
291
- " allow_patterns=subdirectory\n",
292
- ")\n",
293
- "\n",
294
- "print(f\"Downloaded {subdirectory} from {repo_id} to {local_dir}\")\n",
295
- "print(\".\\n.\\nDOWNLOAD COMPLETED\")"
296
- ]
297
- },
298
- {
299
- "cell_type": "markdown",
300
- "id": "47b04ce9-9653-4e4f-9bd9-1b6ed383f502",
301
- "metadata": {},
302
- "source": [
303
- "### Download a specific file - Wait till DOWNLOAD COMPLETED printed"
304
- ]
305
- },
306
- {
307
- "cell_type": "code",
308
- "execution_count": null,
309
- "id": "download_specific_file",
310
- "metadata": {},
311
- "outputs": [],
312
- "source": [
313
- "# This cell downloads a specific file from a given repo\n",
314
- "\n",
315
- "from huggingface_hub import hf_hub_download\n",
316
- "import os\n",
317
- "\n",
318
- "# Define the repository, file path, and local directory\n",
319
- "repo_id = \"YourUserName/reponame\"\n",
320
- "file_path = \"folder/file_name.extension\"\n",
321
- "local_dir = \"/home/Ubuntu/apps/stable-diffusion-webui/models/Stable-diffusion\"\n",
322
- "\n",
323
- "# Ensure the local directory exists\n",
324
- "os.makedirs(local_dir, exist_ok=True)\n",
325
- "\n",
326
- "# Download the specific file\n",
327
- "file_path_local = hf_hub_download(\n",
328
- " repo_id=repo_id,\n",
329
- " filename=file_path,\n",
330
- " local_dir=local_dir\n",
331
- ")\n",
332
- "\n",
333
- "print(f\"Downloaded {file_path} from {repo_id} to {file_path_local}\")\n",
334
- "print(\".\\n.\\nDOWNLOAD COMPLETED\")"
335
- ]
336
- }
337
- ],
338
- "metadata": {
339
- "kernelspec": {
340
- "display_name": "Python 3",
341
- "language": "python",
342
- "name": "python3"
343
- },
344
- "language_info": {
345
- "codemirror_mode": {
346
- "name": "ipython",
347
- "version": 3
348
- },
349
- "file_extension": ".py",
350
- "mimetype": "text/x-python",
351
- "name": "python",
352
- "nbconvert_exporter": "python",
353
- "pygments_lexer": "ipython3",
354
- "version": "3.9.16"
355
- }
356
- },
357
- "nbformat": 4,
358
- "nbformat_minor": 5
359
- }