Upload upload.py with huggingface_hub
Browse files
upload.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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):
|
| 30 |
+
for filename in filenames:
|
| 31 |
+
if not any(pattern in filename for pattern in [".git"]):
|
| 32 |
+
full_path = os.path.join(root, filename)
|
| 33 |
+
relative_path = os.path.relpath(full_path, folder_path)
|
| 34 |
+
files.append((full_path, relative_path))
|
| 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 |
+
)
|