Neph0s commited on
Commit
8f12b2c
·
verified ·
1 Parent(s): c7a0236

Upload upload.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. upload.py +22 -53
upload.py CHANGED
@@ -1,29 +1,10 @@
1
  import os
2
  from huggingface_hub import HfApi
3
  import time
4
- from concurrent.futures import ThreadPoolExecutor
5
- from tqdm import tqdm
6
 
7
- def upload_single_file(args):
8
- file_path, path_in_repo, repo_id, max_retries = args
9
  api = HfApi()
10
 
11
- for attempt in range(max_retries):
12
- try:
13
- api.upload_file(
14
- path_or_fileobj=file_path,
15
- path_in_repo=path_in_repo,
16
- repo_id=repo_id,
17
- repo_type="model"
18
- )
19
- return True, path_in_repo, None
20
- except Exception as e:
21
- if attempt < max_retries - 1:
22
- time.sleep(10 * (attempt + 1))
23
- else:
24
- return False, path_in_repo, str(e)
25
-
26
- def upload_with_retry(folder_path, repo_id, max_retries=10, max_workers=4):
27
  # 获取所有文件
28
  files = []
29
  for root, _, filenames in os.walk(folder_path):
@@ -35,38 +16,26 @@ def upload_with_retry(folder_path, repo_id, max_retries=10, max_workers=4):
35
 
36
  print(f"Found {len(files)} files to upload")
37
 
38
- # 准备上传参数
39
- upload_args = [
40
- (file_path, path_in_repo, repo_id, max_retries)
41
- for file_path, path_in_repo in files
42
- ]
43
-
44
- # 并行上传
45
- failed_files = []
46
- with ThreadPoolExecutor(max_workers=max_workers) as executor:
47
- futures = list(tqdm(
48
- executor.map(upload_single_file, upload_args),
49
- total=len(files),
50
- desc="Uploading files"
51
- ))
52
-
53
- # 处理结果
54
- for success, file_name, error in futures:
55
- if not success:
56
- failed_files.append((file_name, error))
57
-
58
- # 报告结果
59
- print(f"\nUpload completed!")
60
- print(f"Successfully uploaded: {len(files) - len(failed_files)} files")
61
- if failed_files:
62
- print("\nFailed uploads:")
63
- for file_name, error in failed_files:
64
- print(f"- {file_name}: {error}")
65
 
66
  # 使用
67
- upload_with_retry(
68
- ".",
69
- "Neph0s/CoSER-Llama-3.1-70B",
70
- max_retries=10,
71
- max_workers=10 # 同时上传4个文件
72
- )
 
1
  import os
2
  from huggingface_hub import HfApi
3
  import time
 
 
4
 
5
+ def upload_with_retry(folder_path, repo_id, max_retries=10):
 
6
  api = HfApi()
7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  # 获取所有文件
9
  files = []
10
  for root, _, filenames in os.walk(folder_path):
 
16
 
17
  print(f"Found {len(files)} files to upload")
18
 
19
+ for file_path, path_in_repo in files:
20
+ for attempt in range(max_retries):
21
+ try:
22
+ print(f"Uploading {path_in_repo} (Attempt {attempt + 1} of {max_retries})")
23
+ api.upload_file(
24
+ path_or_fileobj=file_path,
25
+ path_in_repo=path_in_repo,
26
+ repo_id=repo_id,
27
+ repo_type="model"
28
+ )
29
+ print(f"Successfully uploaded {path_in_repo}")
30
+ break
31
+ except Exception as e:
32
+ print(f"Error uploading {path_in_repo}: {e}")
33
+ if attempt < max_retries - 1:
34
+ wait_time = 10 * (attempt + 1)
35
+ print(f"Retrying in {wait_time} seconds...")
36
+ time.sleep(wait_time)
37
+ else:
38
+ print(f"Max retries reached. Failed to upload {path_in_repo}")
 
 
 
 
 
 
 
39
 
40
  # 使用
41
+ upload_with_retry(".", "Neph0s/CoSER-Llama-3.1-70B")