Upload upload.py with huggingface_hub
Browse files
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
|
| 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 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 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")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|