ACCC1380 commited on
Commit
6be1cbd
1 Parent(s): 97b0173

Upload lora-scripts/sd-scripts/library/huggingface_util.py with huggingface_hub

Browse files
lora-scripts/sd-scripts/library/huggingface_util.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Union, BinaryIO
2
+ from huggingface_hub import HfApi
3
+ from pathlib import Path
4
+ import argparse
5
+ import os
6
+ from library.utils import fire_in_thread
7
+ from library.utils import setup_logging
8
+ setup_logging()
9
+ import logging
10
+ logger = logging.getLogger(__name__)
11
+
12
+ def exists_repo(repo_id: str, repo_type: str, revision: str = "main", token: str = None):
13
+ api = HfApi(
14
+ token=token,
15
+ )
16
+ try:
17
+ api.repo_info(repo_id=repo_id, revision=revision, repo_type=repo_type)
18
+ return True
19
+ except:
20
+ return False
21
+
22
+
23
+ def upload(
24
+ args: argparse.Namespace,
25
+ src: Union[str, Path, bytes, BinaryIO],
26
+ dest_suffix: str = "",
27
+ force_sync_upload: bool = False,
28
+ ):
29
+ repo_id = args.huggingface_repo_id
30
+ repo_type = args.huggingface_repo_type
31
+ token = args.huggingface_token
32
+ path_in_repo = args.huggingface_path_in_repo + dest_suffix if args.huggingface_path_in_repo is not None else None
33
+ private = args.huggingface_repo_visibility is None or args.huggingface_repo_visibility != "public"
34
+ api = HfApi(token=token)
35
+ if not exists_repo(repo_id=repo_id, repo_type=repo_type, token=token):
36
+ try:
37
+ api.create_repo(repo_id=repo_id, repo_type=repo_type, private=private)
38
+ except Exception as e: # とりあえずRepositoryNotFoundErrorは確認したが他にあると困るので
39
+ logger.error("===========================================")
40
+ logger.error(f"failed to create HuggingFace repo / HuggingFaceのリポジトリの作成に失敗しました : {e}")
41
+ logger.error("===========================================")
42
+
43
+ is_folder = (type(src) == str and os.path.isdir(src)) or (isinstance(src, Path) and src.is_dir())
44
+
45
+ def uploader():
46
+ try:
47
+ if is_folder:
48
+ api.upload_folder(
49
+ repo_id=repo_id,
50
+ repo_type=repo_type,
51
+ folder_path=src,
52
+ path_in_repo=path_in_repo,
53
+ )
54
+ else:
55
+ api.upload_file(
56
+ repo_id=repo_id,
57
+ repo_type=repo_type,
58
+ path_or_fileobj=src,
59
+ path_in_repo=path_in_repo,
60
+ )
61
+ except Exception as e: # RuntimeErrorを確認済みだが他にあると困るので
62
+ logger.error("===========================================")
63
+ logger.error(f"failed to upload to HuggingFace / HuggingFaceへのアップロードに失敗しました : {e}")
64
+ logger.error("===========================================")
65
+
66
+ if args.async_upload and not force_sync_upload:
67
+ fire_in_thread(uploader)
68
+ else:
69
+ uploader()
70
+
71
+
72
+ def list_dir(
73
+ repo_id: str,
74
+ subfolder: str,
75
+ repo_type: str,
76
+ revision: str = "main",
77
+ token: str = None,
78
+ ):
79
+ api = HfApi(
80
+ token=token,
81
+ )
82
+ repo_info = api.repo_info(repo_id=repo_id, revision=revision, repo_type=repo_type)
83
+ file_list = [file for file in repo_info.siblings if file.rfilename.startswith(subfolder)]
84
+ return file_list