Transformers

posted 3 min read

Transformers Library

Transformers is a library of pretrained text, computer vision, audio, video, and multimodal models for inference and training. Use Transformers to fine-tune models on your data, build inference applications, and for generative AI use cases across multiple modalities.

There are over 500K+ 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.9+, PyTorch 2.1+, TensorFlow 2.6+, and Flax 0.4.1+.

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


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 the 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 ")



Supported Tasks:
Automatic speech recognition

Image classification

Visual question answering

Why should I use Transformers?
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.

Lower compute costs, smaller carbon footprint:
Share trained models instead of training from scratch.

Reduce compute time and production costs.

Dozens of model architectures with 1M+ pretrained checkpoints across all modalities.

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.

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.

Hugging Face Enterprise Hub
Why shouldn't I use Transformers?
This library is not a modular toolbox of building blocks for neural nets. The code in the model files is not refactored with additional abstractions on purpose, so that researchers can quickly iterate on each of the models without diving into additional abstractions/files.

The training API is optimized to work with PyTorch models provided by Transformers. For generic machine learning loops, you should use another library like Accelerate.

The example scripts are only examples. They may not necessarily work out-of-the-box on your specific use case and you'll need to adapt the code for it to work.

100 Projects Using Transformers
Transformers is more than a toolkit to use pretrained models, it's a community of projects built around it and the Hugging Face Hub. We want Transformers to enable developers, researchers, students, professors, engineers, and anyone else to build their dream projects.

In order to celebrate Transformers 100,000 stars, we wanted to put the spotlight on the community with the awesome-transformers page which lists 100 incredible projects built with Transformers.

If you own or use a project that you believe should be part of the list, please open a PR to add it!

Example Models
You can test most of our models directly on their Hub model pages.

Expand each modality below to see a few example models for various use cases:

Audio

Computer vision

Multimodal

NLP

If you read this far, tweet to the author to show them you care. Tweet a Thanks

Thanks for sharing! Can Transformers also be used in other programming languages besides Python?

There is Transformers.js in Javascript

More Posts

Kilo Code for VS Code

kodwings - Apr 28

MCP - Add AI Logic and Security

Tyler Band 1 - May 18

Amazon Q CLI for Code Review: Focus on What Changed, Not Everything

ifanzalukhu97 - May 16

Fine-Tuning AI Models: A Guide

Michael Liang - May 16

Integrating a Retrieval-Augmented Generation (RAG) System Using Python and OpenAI

Michael Liang - May 14
chevron_left