如何使用 Hugging Face 部署模型:分步指南
我使用 Hugging Face 部署机器学习模型已有两年多,可以自信地说,这是将模型投入生产最流畅的平台之一。无论你是部署用于情感分析的微调 BERT,还是用于语音识别的自定义 Whisper 模型,Hugging Face 的 Inference Endpoints 和 Spaces 都能让过程异常顺畅。在本教程中,我将逐步讲解部署模型的具体步骤——从环境搭建到处理生产流量。
前置条件
在开始之前,请确保你已具备:
- Hugging Face 账户(免费版可用于测试)
- 已安装 Python 3.8+ 版本
- 已安装
huggingface_hub和transformers库(pip install huggingface_hub transformers) - 准备好训练或微调好的模型(我将以 DistilBERT 情感模型为例)
第一步:准备模型以供部署
第一步是确保你的模型与 Hugging Face 的部署基础设施兼容。我始终从以 transformers 格式保存模型开始——这能保证模型与 Inference Endpoints 无缝协作。
from transformers import AutoModelForSequenceClassification, AutoTokenizer
# 加载微调好的模型(请替换为你自己的模型)
model = AutoModelForSequenceClassification.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
# 本地保存
model.save_pretrained("./my-sentiment-model")
tokenizer.save_pretrained("./my-sentiment-model")
专业提示: 在上传之前,务必先在本地测试模型。我曾浪费数小时调试部署问题,结果发现只是模型加载错误。运行一次快速推理:
inputs = tokenizer("这部电影太棒了!", return_tensors="pt")
outputs = model(**inputs)
print(outputs.logits.argmax().item()) # 应输出 1(正面)
第二步:将模型上传到 Hugging Face Hub
现在,将模型推送到 Hugging Face Hub。这是奇迹发生的地方——Hub 既充当注册中心,又作为分发渠道。
from huggingface_hub import HfApi
api = HfApi()
api.create_repo(repo_id="your-username/my-sentiment-model", exist_ok=True)
api.upload_folder(
folder_path="./my-sentiment-model",
repo_id="your-username/my-sentiment-model",
repo_type="model"
)
常见陷阱: 如果遇到 401 错误,说明你尚未登录。运行 huggingface-cli login 并粘贴来自 huggingface.co/settings/tokens 的访问令牌。

第三步:创建模型卡片(可选但推荐)
好的模型卡片有助于他人(以及未来的你)理解模型的功能。我始终包含:
- 模型描述
- 预期用例
- 训练数据摘要
- 评估指标
你可以直接在 Hub UI 上创建,也可以通过编程方式创建:
from huggingface_hub import ModelCard
card = ModelCard.from_template(
card_data={
"license": "mit",
"language": "en",
"tags": ["sentiment-analysis", "distilbert"]
},
template_path="path/to/custom_template.md" # 可选
)
card.push_to_hub("your-username/my-sentiment-model")