xjyplayer commited on
Commit
43cb1e4
·
verified ·
1 Parent(s): e6bcd02

xjyplayer uploaded at 2024-03-24 16:35:39

Browse files
Files changed (1) hide show
  1. test +421 -1
test CHANGED
@@ -1 +1,421 @@
1
- {"last_folder": "F:\\projects\\scripts\\huggingface_io"}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import subprocess
2
+ import json
3
+ from huggingface_hub import HfApi, list_models
4
+ from huggingface_hub.utils import HfHubHTTPError, RepositoryNotFoundError, RevisionNotFoundError
5
+ from huggingface_hub.hf_api import RepoFile, RepoFolder
6
+ import os
7
+ os.environ['NO_PROXY'] = 'localhost, 127.0.0.1, ::1, ip.cn, chinaz.com, 192.168.0.0/16, mirrors.cloud.aliyuncs.com, hf-mirror.com'
8
+
9
+ import re
10
+ import json
11
+ from datetime import datetime
12
+
13
+ # Specify the mirror URL
14
+ mirror_url = "https://hf-mirror.com"
15
+ raw_url = "https://huggingface.co"
16
+ user=""
17
+ user_id=""
18
+ api=None
19
+ model_list = {}
20
+ commit_message = ""
21
+
22
+ def save_access_token(access_token):
23
+ """保存 AccessToken 到文件中."""
24
+ with open("access_token.json", "w") as f:
25
+ json.dump({"access_token": access_token}, f)
26
+
27
+ def load_access_token():
28
+ """加载保存的 AccessToken."""
29
+ try:
30
+ with open("access_token.json", "r") as f:
31
+ return json.load(f)["access_token"]
32
+ except FileNotFoundError:
33
+ return None
34
+
35
+ def login(init=False):
36
+ """登录到 Hugging Face 账户. 只有初始化的时候才可以略过登录,直接读取本地文件,其他情况代表手动调用"""
37
+ access_token = load_access_token()
38
+ if not (access_token and init):
39
+ access_token = input("请输入您的 AccessToken: ")
40
+ save_access_token(access_token)
41
+ subprocess.run(["huggingface-cli", "login", "--token", access_token], capture_output=True, text=True)
42
+ return access_token
43
+
44
+ def makesure_login(init=False):
45
+ global user, api, user_id
46
+ if init:
47
+ login(init)
48
+ """确保登录成功."""
49
+ while True:
50
+ status, res = check_login()
51
+ if status:
52
+ print(f"登录成功,用户名为: {res}")
53
+ if api == None:
54
+ api = HfApi(endpoint=mirror_url, token=load_access_token())
55
+ user=res
56
+ user_id = user
57
+ break
58
+ else:
59
+ print(f"登录失败,请重新登录, {res}")
60
+ login(init)
61
+
62
+ def check_login():
63
+ result = subprocess.run(["huggingface-cli", "whoami"], capture_output=True, text=True)
64
+ if result.returncode == 0:
65
+ return True, result.stdout.strip("\n")
66
+ else:
67
+ return False, result.stderr
68
+
69
+ def list_projects():
70
+ global user, api, model_list
71
+ """列出用户的项目列表."""
72
+ makesure_login()
73
+ # List models
74
+ models = api.list_models(author=user)
75
+ # Print table header
76
+ print("\nProjects:")
77
+ print("| {:<5} | {:<40} | {:<30} | {:<10} |".format("ID", "Model ID", "Created At", "Private"))
78
+ print("| {:<5} | {:<40} | {:<30} | {:<10} |".format("-"*5, "-"*40, "-"*30, "-"*10))
79
+ start_num = 99
80
+ end_num = 99
81
+ for i, model in enumerate(models, start=100):
82
+ end_num += 1
83
+ model_list[i] = model.id
84
+ print(f"| {i:<5} | {model.id.ljust(40)} | {str(model.created_at).ljust(30)} | {str(model.private).ljust(10)} |")
85
+ # # List datasets
86
+ # print("\nDatasets:")
87
+ # datasets = api.list_datasets()
88
+ # # List spaces
89
+ # print("\nSpaces:")
90
+ # spaces = api.list_spaces()
91
+
92
+ while True:
93
+ print("\n请选择操作:")
94
+ print("1. 添加一个新项目")
95
+ print("2. 下载某个项目")
96
+ print("3. 选择一个项目为当前项目")
97
+ print("4. 返回上一级")
98
+ choice = input("请输入操作编号: ")
99
+ if choice == "1":
100
+ repo_name = input("请输入存储库名称, 直接回车取消: ")
101
+ if repo_name == "":
102
+ continue
103
+ private = input("是否为私有库, 请输入 y 或 n: ") == "y"
104
+ repo_id = create_repository(repo_name, private= private)
105
+
106
+ elif choice == "2":
107
+ download_project()
108
+ elif choice == "3":
109
+ select_current_project(start_num + 1, end_num)
110
+ elif choice == "4":
111
+ return # Return to the previous level
112
+ else:
113
+ print("无效的操作编号,请重新输入")
114
+
115
+
116
+ def select_current_project(start_num, end_num):
117
+ """选择一个项目为当前项目."""
118
+ global current_project, model_list
119
+ while True:
120
+ selected_id = input(f"请在 {str(start_num)} - {str(end_num)} 之中选择一个项目编号,或直接输入repo_id作为当前选择的项目: ")
121
+ if selected_id.isdigit() and start_num <= int(selected_id) <= end_num:
122
+ current_project = model_list[int(selected_id)]
123
+ print(f"当前项目: {current_project}")
124
+ show_project_details()
125
+ break
126
+ elif "/" in selected_id:
127
+ current_project = selected_id
128
+ print(f"当前项目: {current_project}")
129
+ show_project_details()
130
+ break
131
+ else:
132
+ print("无效的项目编号,请重新选择。")
133
+
134
+ def show_project_details():
135
+ """显示项目的具体内容,并提供下载和上传文件的选项."""
136
+ global current_project, api
137
+ makesure_login()
138
+ if current_project:
139
+ try:
140
+ # Get project details
141
+ info = api.repo_info(current_project)
142
+ print(f"项目信息:")
143
+ print(f"ID: {info.id}")
144
+ print(f"Author: {info.author}")
145
+ print(f"Created At: {info.created_at}")
146
+ print(f"Last Modified: {info.last_modified}")
147
+ print(f"Private: {info.private}")
148
+
149
+ print_tree(current_project)
150
+ # Display project content using tree structure
151
+ print("\n项目内容:")
152
+
153
+ # Provide options for downloading or uploading files
154
+ while True:
155
+ print("\n请选择操作:")
156
+ print("1. 下载一个或多个项目文件")
157
+ print("2. 上传一个或多个项目文件")
158
+ print("3. 返回上一级")
159
+ choice = input("请输入操作编号: ")
160
+ if choice == "1":
161
+ download_files(current_project)
162
+ elif choice == "2":
163
+ upload_files(current_project)
164
+ elif choice == "3":
165
+ return # Return to the previous level
166
+ else:
167
+ print("无效的操作编号,请重新输入。")
168
+ except HfHubHTTPError as e:
169
+ print(f"获取项目详情失败: {e}")
170
+ else:
171
+ print("未选择任何项目。")
172
+
173
+
174
+
175
+ def format_size(size):
176
+ """将文件大小格式化为更易读的形式."""
177
+ power = 2**10
178
+ n = 0
179
+ power_labels = {0: '', 1: 'K', 2: 'M', 3: 'G', 4: 'T'}
180
+ while size > power:
181
+ size /= power
182
+ n += 1
183
+ return f"{size:.2f} {power_labels[n]}B"
184
+
185
+ def print_tree(repo_id, path=None, expand=False, revision=None, repo_type=None, indent=""):
186
+ """以树状形式打印项目文件结构."""
187
+ global api
188
+ if path is None:
189
+ path = ''
190
+ repo_tree = api.list_repo_tree(repo_id, path)
191
+ for item in repo_tree:
192
+ if isinstance(item, RepoFile):
193
+ file_size = format_size(item.size) if item.size else "0 B"
194
+ print(indent + f"📄 {item.path} (Size: {file_size})")
195
+ elif isinstance(item, RepoFolder):
196
+ print(indent + f"📁 {item.path}/")
197
+ print_tree(repo_id, path=f"{item.path}", expand=expand, indent=indent + " ")
198
+
199
+ def save_last_folder(folder_path):
200
+ """
201
+ Save the last used folder path to a configuration file.
202
+ If the folder does not exist, create it.
203
+
204
+ Args:
205
+ - folder_path (str): The folder path to save.
206
+ """
207
+ if not os.path.exists(folder_path):
208
+ os.makedirs(folder_path, exist_ok=True)
209
+ print(f"文件夹 '{folder_path}' 不存在,已创建。")
210
+
211
+ config = {"last_folder": folder_path}
212
+ with open("config.json", "w") as f:
213
+ json.dump(config, f)
214
+
215
+ def get_default_folder():
216
+ default_folder = os.getcwd() # 默认文件夹为当前工作目录
217
+ config_file = "config.json"
218
+ if os.path.exists(config_file):
219
+ with open(config_file, "r") as f:
220
+ config = json.load(f)
221
+ if "last_folder" in config and os.path.exists(config["last_folder"]):
222
+ default_folder = config["last_folder"]
223
+ return default_folder
224
+
225
+ def download_files(repo_id):
226
+ total_files = 0
227
+ successful_downloads = 0
228
+ failed_downloads = 0
229
+
230
+ default_folder = get_default_folder()
231
+
232
+ while True:
233
+ print("\n请输入要下载的文件路径,多个文件请使用逗号分隔, 回车返回上一级")
234
+ file_paths_input = input("请输入文件路径: ")
235
+ if len(file_paths_input) == 0:
236
+ return
237
+
238
+ folder_path = input(f"请输入文件夹路径,按回车使用{default_folder}:")
239
+
240
+ if folder_path == "":
241
+ folder_path = default_folder
242
+
243
+ save_last_folder(folder_path)
244
+
245
+ file_paths = [file_path.strip() for file_path in file_paths_input.split(",")]
246
+ total_files += len(file_paths)
247
+
248
+ # Prompt user to place downloaded files in corresponding folders
249
+ user_choice = input("是否把下载的文件放到对应的文件夹?(Y/n):")
250
+ if user_choice.lower() != 'n':
251
+ dir_obj = True
252
+
253
+ for file_path in file_paths:
254
+ # Extract filename and subfolder from file_path
255
+ if "/" in file_path:
256
+ parts = file_path.rsplit("/", 1)
257
+ filename = parts[-1]
258
+ subfolder = parts[0]
259
+ else:
260
+ filename = file_path
261
+ subfolder = ""
262
+
263
+ try:
264
+ path_input = folder_path
265
+ if dir_obj:
266
+ path_input = folder_path + "/" + subfolder
267
+ print(f'downloading {filename} to {path_input}...')
268
+ api.hf_hub_download(repo_id=repo_id, filename=filename, subfolder=subfolder, local_dir=path_input, local_dir_use_symlinks=False)
269
+ successful_downloads += 1
270
+ except Exception as e:
271
+ print(f"下载文件 '{filename}' 时出错:{e}")
272
+ failed_downloads += 1
273
+
274
+ print(f"\n总共下载文件数: {total_files}")
275
+ print(f"成功下载文件数: {successful_downloads}")
276
+ print(f"失败下载文件数: {failed_downloads}")
277
+
278
+ import os
279
+
280
+ def upload_files(repo_id):
281
+ """上传一个或多个项目文件."""
282
+ global api
283
+ makesure_login()
284
+ if not is_own_project(repo_id):
285
+ print("检测到当前项目不是本人的项目,故而没有上传权限")
286
+ return
287
+ while True:
288
+ file_paths_input = input("请输入要上传的文件路径(多个文件请用逗号分隔), 回车返回上一级: ")
289
+ if len(file_paths_input) == 0:
290
+ return
291
+ file_paths = [path.strip() for path in file_paths_input.split(",")]
292
+
293
+ relative_paths_input = input("请输入要上传的文件相对路径, 没有会自动创建,不填全部上传到根目录(多个路径请用逗号分隔): ")
294
+ if len(relative_paths_input) == 0:
295
+ relative_paths = [""]
296
+ else:
297
+ relative_paths = [path.strip() for path in relative_paths_input.split(",")]
298
+
299
+ if len(relative_paths) < len(file_paths):
300
+ fill_choice = input("文件路径数目大于相对路径数目,请选择填充方式:\n1. 使用根目录填充\n2. 根据最后一个路径填充\n请选择(1/2): ")
301
+ if fill_choice == "1":
302
+ relative_paths += ["" for _ in range(len(file_paths) - len(relative_paths))]
303
+ elif fill_choice == "2":
304
+ last_path = relative_paths[-1] if relative_paths else ""
305
+ relative_paths += [last_path for _ in range(len(file_paths) - len(relative_paths))]
306
+ elif len(relative_paths) > len(file_paths):
307
+ ignored_paths = relative_paths[len(file_paths):]
308
+ print(f"文件路径数目小于相对路径数目,以下相对路径将被忽略: {', '.join(ignored_paths)}")
309
+ relative_paths = relative_paths[:len(file_paths)]
310
+
311
+ # 获取当前时间
312
+ now = datetime.now()
313
+ current_time = now.strftime("%Y-%m-%d %H:%M:%S")
314
+ default_message = f"{user_id} uploaded at {current_time}"
315
+ commit_message = input(f"请输入本次提交的目的,(回车默认填写:{default_message}):")
316
+
317
+ for file_path, relative_path in zip(file_paths, relative_paths):
318
+ # Convert relative path to absolute path
319
+ absolute_path = os.path.abspath(file_path)
320
+ if not os.path.exists(file_path):
321
+ print(f"文件 '{file_path}' 不存在,跳过上传。")
322
+ continue
323
+
324
+ try:
325
+ print(f'uploading {absolute_path} to {repo_id}/{relative_path}...')
326
+
327
+ # 构造 commit_message
328
+ if len(commit_message) == 0:
329
+ commit_message = default_message
330
+ else:
331
+ commit_message = f"{user_id}: " + commit_message
332
+ file_name = os.path.basename(file_path)
333
+
334
+ api.upload_file(repo_id=repo_id, path_or_fileobj=absolute_path, path_in_repo=relative_path + "/" + file_name, commit_message=commit_message)
335
+ print(f"文件 {file_path} 上传成功。")
336
+ except HfHubHTTPError as e:
337
+ print(f"文件 {file_path} 上传失败: {e}")
338
+ except ValueError as e:
339
+ print(f"文件 {file_path} 上传失败: {e}")
340
+ except RepositoryNotFoundError as e:
341
+ print(f"文件 {file_path} 上传失败: {e}")
342
+ except RevisionNotFoundError as e:
343
+ print(f"文件 {file_path} 上传失败: {e}")
344
+
345
+ break
346
+
347
+ def is_own_project(repo_id):
348
+ """检查给定的 repo_id 是否为当前用户的项目."""
349
+ return repo_id in model_list.values()
350
+
351
+ def create_repository(repo_name, private = False):
352
+ global user, api
353
+ makesure_login()
354
+ try:
355
+ # 创建存储库
356
+ api.create_repo(repo_name, private=private)
357
+ except HfHubHTTPError as e:
358
+ error_code = extract_first_three_digits(str(e))
359
+ if error_code == 403:
360
+ print("HTTPError 403: You don't have write permissions to the repository.")
361
+ elif error_code == 404:
362
+ print("HTTPError 404: The repository or project doesn't exist on the Hub.")
363
+ elif error_code == 409:
364
+ print("HTTPError 409: The project already exists in the collection and _ok=False.")
365
+ else:
366
+ print(f"HTTPError {e.server_message}")
367
+
368
+ except ValueError as e:
369
+ """bug"""
370
+ print("warning:" + str(e))
371
+
372
+ repo_id = user + '/' + repo_name
373
+ if not private:
374
+ url = mirror_url + "/" + repo_id
375
+ else:
376
+ url = "https://huggingface.co" + + "/" + repo_id
377
+ # 检查是否成功创建存储库
378
+ if url:
379
+ print(f"存储库 {repo_id} 创建成功!地址为:" + url)
380
+ else:
381
+ print("存储库创建失败,请检查��限或存储库是否已存在。")
382
+ return repo_id
383
+
384
+ def extract_first_three_digits(s):
385
+ match = re.search(r'\d{3}', s)
386
+ if match:
387
+ return int(match.group())
388
+ return 0
389
+
390
+ def getrepoid_from_url(url):
391
+ parts = url.split('/')
392
+ repo_id = parts[-2] + "/" + parts[-1]
393
+ return repo_id
394
+
395
+ def download_project():
396
+ """下载某个项目."""
397
+ # Your code to download a project goes here
398
+ pass
399
+
400
+ def switch_account():
401
+ """切换账户."""
402
+ access_token = input("请输入您的新 AccessToken: ")
403
+ save_access_token(access_token)
404
+ makesure_login() # 切换账户后立即登录
405
+
406
+ def main():
407
+ makesure_login(True)
408
+ while True:
409
+ print("\n请选择操作:")
410
+ print("1. 查看项目列表")
411
+ print("2. 切换账户")
412
+ choice = input("请输入操作编号: ")
413
+ if choice == "1":
414
+ list_projects()
415
+ elif choice == "2":
416
+ switch_account()
417
+ else:
418
+ print("无效的操作编号,请重新输入")
419
+
420
+ if __name__ == "__main__":
421
+ main()