1. 準備
- 注冊一個modelscope賬號(國內的)
- 拿到對應的訪問令牌SDK/API令牌
- 注冊一個google賬號, 登錄colab
2. 開始干!
打開一個ipynb
- 安裝依賴包
!pip install -qqq modelscope huggingface-hub -U
- 選擇安裝git lfs
!curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash!apt-get install git-lfs && git lfs install
- 轉移模型
## 從huggingface下載模型/數據集/spaceimport json
from modelscope.hub.api import HubApi
from huggingface_hub import snapshot_download## huggingface
repo_id = "openai/clip-vit-large-patch14" # hf repo_id
repo_type = "model" # model/dataset/space
local_dir = "clip-vit-large-patch14" # ## modelscope
framework = "Pytorch"
task = "image-generation"
username = "xxx" # modeoscope名字
ACCESS_TOKEN = "xxx-xxx-xxx-xxx-xxx" # modelscope訪問令牌## 下載model
snapshot_download(repo_id,repo_type=repo_type,local_dir=local_dir,# allow_patterns=["*.bin", "*.json", "*.yaml", "*.txt", "*.ckpt"], # 選擇哪些文件# ignore_patterns=[] # 跳過哪些文件
)# 創建 configuration.json
with open(local_dir + "/configuration.json", "w", encoding="utf-8") as f:json.dump({"framework": framework, "task": task}, f, ensure_ascii=False, indent=4)## 開始上傳文件
api = HubApi()
api.login(ACCESS_TOKEN)
model_id = username + "/" + "-".join(repo_id.split("/"))
api.push_model(model_id=model_id, # 如果model_id對應的模型庫不存在,將會自動創建model_dir=local_dir, # 指定本地模型目錄,目錄中必須包含configuration.json文件
)
print(f'>>> model_id: {model_id}')
print(f'>>> 下載命令\nmodelscope download {model_id} --local_dir ./model_dir')
恭喜!直接去modelscope空間下載吧!