🤗 Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and multimodal models, for both inference and training.
English | 简体中文 | 繁體中文 | 한국어 | Español | 日本語 | हिन्दी | Русский | Português | తెలుగు | Français | Deutsch | Italiano | Tiếng Việt | العربية | اردو | বাংলা | فارسی |
用于推理和训练的最先进预训练模型

Transformers 是一个模型定义框架,适用于文本、计算机视觉、音频、视频和多模态模型的最先进机器学习,支持推理和训练。
它集中管理模型定义,确保整个生态系统对该定义达成一致。transformers 是各框架之间的枢纽:如果支持某个模型定义,它将与大多数训练框架(Axolotl、Unsloth、DeepSpeed、FSDP、PyTorch-Lightning 等)、推理引擎(vLLM、SGLang、TGI 等)以及利用 transformers 模型定义的相关建模库(llama.cpp、mlx 等)兼容。
我们承诺通过提供简单、可定制且高效的模型定义,帮助支持新的最先进模型并普及它们的使用。
在 Hugging Face Hub 上有超过 100 万个 Transformers 模型检查点 可供您使用。
立即探索 Hub 以找到合适的模型,并使用 Transformers 立即开始您的项目。
安装
Transformers 适用于 Python 3.10+ 以及 PyTorch 2.4+。
使用 venv 或 uv(一个快速的基于 Rust 的 Python 包和项目管理器)创建并激活虚拟环境。
# venv
python -m venv .my-env
source .my-env/bin/activate
# uv
uv venv .my-env
source .my-env/bin/activate
在您的虚拟环境中安装 Transformers。
# pip
pip install "transformers[torch]"
# uv
uv pip install "transformers[torch]"
如果您希望获取库中的最新变更,或者有意参与贡献,可从源代码安装Transformers。不过,最新版本可能不够稳定。如果遇到错误,欢迎提交issue。
git clone https://github.com/huggingface/transformers.git
cd transformers
# pip
pip install '.[torch]'
# uv
uv pip install '.[torch]'
快速入门
借助 Pipeline API 立即开始使用 Transformers。Pipeline 是一个高级推理类,支持文本、音频、视觉和多模态任务。它负责对输入进行预处理并返回相应的输出。
实例化一个 pipeline 并指定用于文本生成的模型。模型会被下载并缓存,以便您可以轻松地再次重用它。最后,传入一些文本以提示模型。
from transformers import pipeline
pipeline = pipeline(task="text-generation", model="Qwen/Qwen2.5-1.5B")
pipeline("the secret to baking a really good cake is ")
[{'generated_text': 'the secret to baking a really good cake is 1) to use the right ingredients and 2) to follow the recipe exactly. the recipe for the cake is as follows: 1 cup of sugar, 1 cup of flour, 1 cup of milk, 1 cup of butter, 1 cup of eggs, 1 cup of chocolate chips. if you want to make 2 cakes, how much sugar do you need? To make 2 cakes, you will need 2 cups of sugar.'}]
要与模型对话,使用方式是相同的。唯一的区别在于,你需要构建一个你与系统之间的对话历史(作为 Pipeline 的输入)。
import torch
from transformers import pipeline
chat = [
{"role": "system", "content": "You are a sassy, wise-cracking robot as imagined by Hollywood circa 1986."},
{"role": "user", "content": "Hey, can you tell me any fun things to do in New York?"}
]
pipeline = pipeline(task="text-generation", model="meta-llama/Meta-Llama-3-8B-Instruct", dtype=torch.bfloat16, device_map="auto")
response = pipeline(chat, max_new_tokens=512)
print(response[0]["generated_text"][-1]["content"])
展开以下示例,了解 Pipeline 在不同模态和任务中的工作方式。
语音自动识别
from transformers import pipeline
pipeline = pipeline(task="automatic-speech-recognition", model="openai/whisper-large-v3")
pipeline("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac")
{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'}
from transformers import pipeline
pipeline = pipeline(task="image-classification", model="facebook/dinov2-small-imagenet1k-1-layer")
pipeline("https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png")
[{'label': 'macaw', 'score': 0.997848391532898},
{'label': 'sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita',
'score': 0.0016551691805943847},
{'label': 'lorikeet', 'score': 0.00018523589824326336},
{'label': 'African grey, African gray, Psittacus erithacus',
'score': 7.85409429227002e-05},
{'label': 'quail', 'score': 5.502637941390276e-05}]
from transformers import pipeline
pipeline = pipeline(task="visual-question-answering", model="Salesforce/blip-vqa-base")
pipeline(
image="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/idefics-few-shot.jpg",
question="What is in the image?",
)
[{'answer': 'statue of liberty'}]
为什么选择 Transformers?
-
易用的最先进模型:
- 在自然语言理解与生成、计算机视觉、音频、视频及多模态任务上表现卓越。
- 为研究人员、工程师和开发者降低入门门槛。
- 用户只需学习三个核心类,抽象概念少。
- 统一 API,可便捷使用所有预训练模型。
-
降低计算成本,减少碳足迹:
- 共享已训练模型,无需从零开始训练。
- 缩短计算时间,降低生产成本。
- 涵盖数百种模型架构,各模态下预训练 checkpoint 超 100 万个。
-
为模型生命周期各阶段灵活选择框架:
- 仅需 3 行代码即可训练最先进模型。
- 模型可在 PyTorch/JAX/TF2.0 框架间随意切换。
- 为训练、评估和生产环节选择最适合的框架。
-
轻松根据需求定制模型或示例:
- 为每种架构提供示例,可复现原作者发表的结果。
- 模型内部结构尽可能保持一致且开放。
- 模型文件可独立于库使用,便于快速实验。
何时不适合使用 Transformers?
- 本库并非神经网络的模块化构建工具集。模型文件中的代码未进行额外抽象重构,目的是让研究人员无需深入额外抽象/文件即可快速迭代各模型。
- 训练 API 针对 Transformers 提供的 PyTorch 模型优化。对于通用机器学习循环,建议使用 Accelerate 等其他库。
- 示例脚本 仅为 示例。它们未必能直接适用于您的特定用例,可能需要调整代码才能运行。
100 个使用 Transformers 的项目
Transformers 不仅仅是一个使用预训练模型的工具包,更是一个围绕它和 Hugging Face Hub 构建的项目社区。我们希望 Transformers 能够让开发者、研究人员、学生、教授、工程师以及其他任何人都能构建他们的梦想项目。
为了庆祝 Transformers 获得 100,000 颗星,我们希望通过 awesome-transformers 页面聚焦社区,该页面列出了 100 个使用 Transformers 构建的出色项目。
如果您拥有或使用的项目您认为应该加入该列表,请提交 PR 进行添加!
示例模型
您可以直接在 Hub 模型页面 上测试我们的大多数模型。
展开下面的每个模态,查看针对各种用例的一些示例模型。
音频
计算机视觉
多模态
- 使用 Voxtral、Audio Flamingo 进行音频或文本到文本转换
- 使用 LayoutLMv3 进行文档问答
- 使用 Qwen-VL 进行图像或文本到文本转换
- 使用 BLIP-2 进行图像 captioning
- 使用 GOT-OCR2 进行基于 OCR 的文档理解
- 使用 TAPAS 进行表格问答
- 使用 Emu3 进行统一的多模态理解与生成
- 使用 Llava-OneVision 进行视觉到文本转换
- 使用 Llava 进行视觉问答
- 使用 Kosmos-2 进行视觉指代表达分割
自然语言处理(NLP)
引用
现在您可以引用一篇关于 🤗 Transformers 库的论文:
@inproceedings{wolf-etal-2020-transformers,
title = "Transformers: State-of-the-Art Natural Language Processing",
author = "Thomas Wolf and Lysandre Debut and Victor Sanh and Julien Chaumond and Clement Delangue and Anthony Moi and Pierric Cistac and Tim Rault and Rémi Louf and Morgan Funtowicz and Joe Davison and Sam Shleifer and Patrick von Platen and Clara Ma and Yacine Jernite and Julien Plu and Canwen Xu and Teven Le Scao and Sylvain Gugger and Mariama Drame and Quentin Lhoest and Alexander M. Rush",
booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing: System Demonstrations",
month = oct,
year = "2020",
address = "Online",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2020.emnlp-demos.6/",
pages = "38--45"
}
项目介绍
🤗 Transformers: the model-definition framework for state-of-the-art machine learning models in text, vision, audio, and multimodal models, for both inference and training.
下载使用量
项目总下载次数(含Clone、Pull、 zip 包及 release 下载),每日凌晨更新

