-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathsync_hf_to_ms.py
More file actions
63 lines (52 loc) · 2.09 KB
/
Copy pathsync_hf_to_ms.py
File metadata and controls
63 lines (52 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import argparse
import os
from huggingface_hub import snapshot_download
from modelscope.hub.api import HubApi
from modelscope.hub.constants import Licenses, ModelVisibility
def sync_model(repo_name: str, hf_repo: str, ms_repo: str):
print(f"\n🔄 Syncing {hf_repo} -> {ms_repo}")
# Login to ModelScope
MODELSCOPE_TOKEN = os.getenv("MODELSCOPE_TOKEN")
assert MODELSCOPE_TOKEN, "Please set the MODELSCOPE_TOKEN environment variable or hardcode the token."
api = HubApi()
api.login(MODELSCOPE_TOKEN)
# Download the model snapshot from Hugging Face
local_dir = snapshot_download(repo_id=hf_repo, cache_dir=repo_name, local_dir=repo_name)
print(f"📥 Downloaded to: {local_dir}")
# Check if the ModelScope repo already exists
exists = False
try:
api.get_model(ms_repo)
exists = True
print(f"✅ Model already exists on ModelScope: {ms_repo}")
except Exception:
print(f"ℹ️ Model not found on ModelScope: {ms_repo}, creating...")
# Create repo if it doesn't exist
if not exists:
api.create_model(
model_id=ms_repo,
visibility=ModelVisibility.PUBLIC, # Change to "Private" if needed
license=Licenses.APACHE_V2,
)
print(f"✅ Created ModelScope repo: {ms_repo}")
# Upload model files to ModelScope
print(f"⏫ Uploading to ModelScope...")
# api.upload_model(model_dir=local_dir, model_id=ms_repo)
api.upload_folder(
repo_id=ms_repo,
folder_path=local_dir,
commit_message=f"Sync from Hugging Face {hf_repo}",
ignore_patterns=["*nunchaku-tech*", "README.md"],
)
print(f"✅ Sync complete: {hf_repo} -> {ms_repo}")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-n",
"--repo-name",
type=str,
required=True,
help="Name of the HuggingFace repository under nunchaku-tech to sync to (e.g., `nunchaku`)",
)
args = parser.parse_args()
sync_model(args.repo_name, f"nunchaku-tech/{args.repo_name}", f"nunchaku-tech/{args.repo_name}")