You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Hugging Face模型多版本存储:分支与标签的实现方法

当然可以通过Git分支或Git标签来存储模型的多个版本,两种方式各有适用场景:分支适合迭代中的版本更新,标签适合标记稳定的发布版本。以下是通过Hugging Face工具实现的具体方法:

一、使用Git分支存储不同版本

分支用来区分不同开发阶段的模型版本(比如迭代中的v1、v2),操作方式如下:

1. 推送模型到指定分支

直接在push_to_hub方法中指定branch参数,将模型推送到新分支:

from transformers import CLIPProcessor, CLIPModel

# 加载本地新的模型版本
checkpoint = "./checkpoints-new/checkpoint-120"
model = CLIPModel.from_pretrained(checkpoint)
processor = CLIPProcessor.from_pretrained(checkpoint)

repo = "vincentclaes/emoji-predictor"
# 推送到名为v1的分支
model.push_to_hub(repo, use_temp_dir=True, branch="v1")
processor.push_to_hub(repo, use_temp_dir=True, branch="v1")

2. 提前创建分支(可选)

如果需要先创建分支再推送,可以用huggingface_hub的HfApi:

from huggingface_hub import HfApi

api = HfApi()
# 创建名为v2的新分支
api.create_branch(repo_id="vincentclaes/emoji-predictor", branch="v2")

3. 加载指定分支的模型

后续加载对应分支的模型时,指定revision参数:

model = CLIPModel.from_pretrained("vincentclaes/emoji-predictor", revision="v1")
processor = CLIPProcessor.from_pretrained("vincentclaes/emoji-predictor", revision="v1")
二、使用Git标签标记稳定版本

标签适合标记正式发布的稳定版本(比如v1.0、v2.0),操作方式如下:

1. 推送模型后打标签

先将模型推送到主分支或指定分支,再用HfApi为特定提交创建标签:

from huggingface_hub import HfApi

api = HfApi()
# 为main分支的最新提交创建v1.0标签,并添加说明
api.create_tag(
    repo_id="vincentclaes/emoji-predictor",
    tag="v1.0",
    commit_hash="main",  # 也可以替换为具体的提交哈希值
    tag_message="First stable release of emoji predictor"
)

2. 加载指定标签的模型

加载时同样通过revision参数指定标签名:

model = CLIPModel.from_pretrained("vincentclaes/emoji-predictor", revision="v1.0")
processor = CLIPProcessor.from_pretrained("vincentclaes/emoji-predictor", revision="v1.0")

内容的提问来源于stack exchange,提问作者Vincent Claes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 00:10:32