大模型底座 · 中文选型解读

transformers

已解读GitHubApache-2.0Python

🤗 Transformers是Hugging Face推出的模型定义框架,支持文本、视觉、音频等多模态大模型的训练与推理,兼容主流AI开发工具链。

164.7kstar今天仍有更新维护状态Apache-2.0 · 可评估商用商用提醒
在 GitHub 查看官方项目
适合解决作为问答、生成和智能体应用的基础模型
更适合正在比较模型能力、成本与部署方式的团队
投入判断上手门槛:较高。需要评测真实业务数据与许可边界
一分钟看懂

这个项目值得继续研究吗?

AI 依据上游资料解读 · 2026/8/28

🤗 Transformers是Hugging Face推出的模型定义框架,支持文本、视觉、音频等多模态大模型的训练与推理,兼容主流AI开发工具链。

解决什么问题
企业落地AI应用时,常面临不同模态大模型适配成本高、跨训练/推理工具链兼容性差、从零训练模型算力投入大、多场景模型复用难等问题,大幅拉长AI应用上线周期。
适合什么团队
需要开发文本生成、语音识别、图像识别、多模态交互等AI应用的企业技术团队,以及有大模型训练、调优需求的算法团队。
使用前注意
部署需满足Python 3.10+、PyTorch 2.5+的环境要求;采用Apache-2.0许可证可自由商用;安装源码版本获取最新功能时,需注意其可能存在不稳定问题。

本页用于缩短初步筛选时间,不构成技术、采购或法律结论。 正式使用前请在真实业务数据上验证,并以官方说明与许可证为准。

项目导读

从官方资料看清能力、部署与采用边界

AI 翻译整理 · 保留官方来源

以下内容依据项目公开 README 或模型卡翻译整理,代码、命令和产品名保持原样。

项目定位

Transformers是Hugging Face推出的大模型底座类框架,作为生态内统一的模型定义标准,支撑文本、计算机视觉、音频、视频、多模态等各类前沿机器学习模型的训练与推理。其作为AI开发全链路的枢纽,只要是被Transformers支持的模型定义,即可兼容主流训练框架、推理引擎及周边建模工具,无需额外做适配改造。目前Hugging Face Hub上已有超100万适配Transformers的预训练模型权重,用户可直接调用,无需从零训练模型。

核心能力

  1. 全模态任务覆盖:支持自然语言理解/生成、语音识别、图像分类、视觉问答等各类单模态、多模态AI任务,覆盖绝大多数企业AI应用落地需求。
  2. 低门槛统一API:仅需学习3个核心类即可上手,所有预训练模型均采用统一调用逻辑,高阶Pipeline API自动完成输入预处理、输出后处理,大幅降低开发成本。
  3. 全链路生态兼容:兼容Axolotl、Unsloth、DeepSpeed、PyTorch-Lightning等主流训练框架,vLLM、SGLang、TGI等推理引擎,以及llama.cpp、mlx等周边建模库,用户可自由选择适合自身业务的工具组合。
  4. 降低算力成本:无需从零训练模型,直接复用生态内已公开的预训练权重,可大幅减少算力投入、缩短开发周期。
  5. 灵活可定制:官方提供每个模型架构的原版效果复现示例,模型内部逻辑对外一致暴露,模型文件可脱离框架单独使用,方便用户根据自身业务需求做定制化调整。

典型使用方式

Transformers最常用的入口是Pipeline高阶API,仅需3步即可完成AI能力调用:

目前支持的典型场景包括:

  1. 导入pipeline类,指定任务类型与要使用的模型ID;
  2. 传入对应格式的输入内容(文本、音频链接、图片链接等);
  3. 直接获取模型输出结果。
  • 文本生成:仅需指定text-generation任务与对应大模型,即可实现内容生成、多轮对话等能力,也可通过transformers chat命令直接在命令行与模型对话。
  • 自动语音识别:指定automatic-speech-recognition任务与对应语音模型,传入音频链接即可得到转写文本。
  • 图像分类:指定image-classification任务与对应视觉模型,传入图片链接即可得到分类结果与置信度。
  • 视觉问答:指定visual-question-answering任务与对应多模态模型,同时传入图片与问题,即可得到对应答案。

部署要求

Transformers运行需满足以下基础环境要求:

安装方式分为两种:

  • Python版本≥3.10
  • PyTorch版本≥2.5

安装前建议先通过venv或uv创建独立的虚拟环境,避免依赖冲突。

  1. 稳定版安装:通过pip或uv工具直接安装官方发布的稳定版本,适合生产环境使用。
  2. 源码安装:直接拉取GitHub仓库源码安装,可获得最新功能,但版本可能不稳定,遇到问题可提交官方Issue反馈。

许可证与采用建议

Transformers采用Apache-2.0许可证,用户可自由使用、修改、分发代码,包括商用场景,无额外授权限制。 对于企业用户的选型建议:

  • 如果是快速落地AI应用场景,建议优先使用稳定版Transformers,配合Hugging Face Hub上的公开预训练模型,可在数天内完成基础AI能力上线。
  • 如果有大模型定制训练需求,可基于Transformers提供的模型定义做调整,配合兼容的训练框架完成微调,无需从零搭建模型结构。
  • 官方提供简体中文文档,国内用户可直接查阅降低学习成本。

维护状态

项目自2018年创建至今仍处于活跃维护状态,最近代码更新时间为2026年8月,官方会持续跟进支持最新的前沿大模型,推动前沿AI能力的普惠应用。

可核对的事实层

官方资料与来源

查看来源 →
  • audio
  • deep-learning
  • deepseek
  • gemma
  • glm
  • hacktoberfest
  • llm
  • machine-learning
  • model-hub
  • natural-language-processing
  • nlp
  • pretrained-models
默认分支main
关注仓库1.2k
复刻次数34.4k
开放议题2.4k
近期更新2026/8/26
仓库状态未标记归档
上游部署线索
## Installation

Transformers works with Python 3.10+, and [PyTorch](https://pytorch.org/get-started/locally/) 2.5+.

Create and activate a virtual environment with [venv](https://docs.python.org/3/library/venv.html) or [uv](https://docs.astral.sh/uv/), a fast Rust-based Python package and project manager.

```py

该片段来自项目 README,仅用于初步判断;实际部署请以官方文档为准。

核对上游原始说明节选

🤗 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.

Copyright 2020 The HuggingFace Team. All rights reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -->

English | 简体中文 | 繁體中文 | 한국어 | Español | 日本語 | हिन्दी | Русский | Português | తెలుగు | Français | Deutsch | Italiano | Tiếng Việt | العربية | اردو | বাংলা | فارسی | Română | Türkçe

State-of-the-art pretrained models for inference and training

Transformers acts as the model-definition framework for state-of-the-art machine learning with text, computer vision, audio, video, and multimodal models, for both inference and training.

It centralizes the model definition so that this definition is agreed upon across the ecosystem. transformers is the pivot across frameworks: if a model definition is supported, it will be compatible with the majority of training frameworks (Axolotl, Unsloth, DeepSpeed, FSDP, PyTorch-Lightning, ...), inference engines (vLLM, SGLang, TGI, ...), and adjacent modeling libraries (llama.cpp, mlx, ...) which leverage the model definition from transformers.

We pledge to help support new state-of-the-art models and democratize their usage by having their model definition be simple, customizable, and efficient.

There are over 1M+ Transformers model checkpoints on the Hugging Face Hub you can use.

Explore the Hub today to find a model and use Transformers to help you get started right away.

Installation

Transformers works with Python 3.10+, and PyTorch 2.5+.

Create and activate a virtual environment with venv or uv, a fast Rust-based Python package and project manager.

# venv
python -m venv .my-env
source .my-env/bin/activate
# uv
uv venv .my-env
source .my-env/bin/activate

Install Transformers in your virtual environment.

# pip
pip install "transformers[torch]"

# uv
uv pip install "transformers[torch]"

Install Transformers from source if you want the latest changes in the library or are interested in contributing. However, the latest version may not be stable. Feel free to open an issue if you encounter an error.

git clone https://github.com/huggingface/transformers.git
cd transformers

# pip
pip install '.[torch]'

# uv
uv pip install '.[torch]'

Quickstart

Get started with Transformers right away with the Pipeline API. The Pipeline is a high-level inference class that supports text, audio, vision, and multimodal tasks. It handles preprocessing the input and returns the appropriate output.

Instantiate a pipeline and specify model to use for text generation. The model is downloaded and cached so you can easily reuse it again. Finally, pass some text to prompt the model.

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.'}]

To chat with a model, the usage pattern is the same. The only difference is you need to construct a chat history (the input to Pipeline) between you and the system.

[!TIP]
You can also chat with a model directly from the command line, as long as transformers serve is running.
```shell
transformers chat Qwen/Qwen2.5-0.5B-Instruct
```
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"])

Expand the examples below to see how Pipeline works for different modalities and tasks.

Automatic speech recognition

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

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}]

Visual question answering

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'}]

Why should I use Transformers?

  1. Easy-to-use state-of-the-art models:
  • High performance on natural language understanding & generation, computer vision, audio, video, and multimodal tasks.
  • Low barrier to entry for researchers, engineers, and developers.
  • Few user-facing abstractions with just three classes to learn.
  • A unified API for using all our pretrained models.
  1. Lower compute costs, smaller carbon footprint:
  • Share trained models instead of training from scratch.
  • Reduce compute time and production costs.
  • Hundreds of model architectures with 1M+ pretrained checkpoints across all modalities.
  1. Choose the right framework for every part of a model's lifetime:
  • Train state-of-the-art models in 3 lines of code.
  • Move a single model between PyTorch/JAX/TF2.0 frameworks at will.
  • Pick the right framework for training, evaluation, and production.
  1. Easily customize a model or an example to your needs:
  • We provide examples for each architecture to reproduce the results published by its original authors.
  • Model internals are exposed as consistently as possible.
  • Model files can be used independently of the library for quick experiments.

When shouldn't I use Transformers?

  • This library is not a modular tool