Transformers 是一个模型定义框架,为涉及文本、计算机视觉、音频、视频和多模态模型的最先进机器学习提供推理和训练能力。
它集中了模型定义,使整个生态系统的定义保持一致。`transformers` 是跨框架的枢纽:如果一个模型定义得到支持,它将兼容大多数训练框架(Axolotl、Unsloth、DeepSpeed、FSDP、PyTorch-Lightning……)、推理引擎(vLLM、SGLang、TGI……)以及利用 `transformers` 模型定义的周边建模库(llama.cpp、mlx……)。
我们致力于支持新的最先进模型,并通过使其模型定义简单、可定制且高效,来普及这些模型的使用。
[Hugging Face Hub](https://huggingface.co/models) 上有超过 100 万个 Transformers [模型检查点](https://huggingface.co/models?library=transformers&sort=trending) 可供使用。
立即探索 [Hub](https://huggingface.co/),找到模型并使用 Transformers 快速上手。
## 安装
Transformers 支持 Python 3.10+ 和 [PyTorch](https://pytorch.org/get-started/locally/) 2.4+。
使用 [venv](https://docs.python.org/3/library/venv.html) 或 [uv](https://docs.astral.sh/uv/),(一个基于 Rust 的快速 Python 包和项目管理器)创建并激活虚拟环境。
```py
# venv
python -m venv .my-env
source .my-env/bin/activate
# uv
uv venv .my-env
source .my-env/bin/activate
```
在虚拟环境中安装 Transformers。
```py
# pip
pip install "transformers[torch]"
# uv
uv pip install "transformers[torch]"
```
如果你希望使用库中的最新更改或有意贡献代码,可以从源码安装 Transformers。不过,*最新*版本可能不稳定。如果遇到错误,欢迎提交 [issue](https://github.com/huggingface/transformers/issues)。
```shell
git clone https://github.com/huggingface/transformers.git
cd transformers
# pip
pip install '.[torch]'
# uv
uv pip install '.[torch]'
```
## 快速入门
使用 [Pipeline](https://huggingface.co/docs/transformers/pipeline_tutorial) API 快速开始使用 Transformers。`Pipeline` 是一个高级推理类,支持文本、音频、视觉和多模态任务。它负责预处理输入并返回相应的输出。
实例化一个 pipeline 并指定用于文本生成的模型。模型会被下载并缓存,方便你重复使用。最后,传入一些文本作为提示输入到模型中。
```py
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` 的内容)。
> [!TIP]
> 只要 [`transformers serve` 正在运行](https://huggingface.co/docs/transformers/main/en/serving).),您也可以直接从命令行与模型对话。
> ```shell
> transformers chat Qwen/Qwen2.5-0.5B-Instruct
> ```
```py
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` 在不同模态和任务中的工作方式。
自动语音识别(Automatic speech recognition)
```py
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.'}
```
图像分类(Image classification)