transformers

2025-12-10 0 221

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

Transformers acts as the modeldefinition framework for state-of-the-art machine learning models in text, computer
vision, audio, video, and multimodal model, 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.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.

# venvpython -m venv .my-envsource .my-env/bin/activate# uvuv venv .my-envsource .my-env/bin/activate

Install Transformers in your virtual environment.

# pippip install \"transformers[torch]\"# uvuv 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://*gi*thub.c*om/huggingface/transformers.gitcd transformers# pippip install .[torch]# uvuv 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 pipelinepipeline = 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.

transformers chat Qwen/Qwen2.5-0.5B-Instruct
import torchfrom transformers import pipelinechat = [
    {\"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\", torch_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 pipelinepipeline = pipeline(task=\"automatic-speech-recognition\", model=\"openai/whisper-large-v3\")pipeline(\"https://h**ugging*face.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 pipelinepipeline = pipeline(task=\"image-classification\", model=\"facebook/dinov2-small-imagenet1k-1-layer\")pipeline(\"https://h*uggingf*ac*e.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 pipelinepipeline = pipeline(task=\"visual-question-answering\", model=\"Salesforce/blip-vqa-base\")pipeline(image=\"https://hu**gg*ingface.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.

  • 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 models 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.

    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
    • Audio classification with Whisper

    • Automatic speech recognition with Moonshine

    • Keyword spotting with Wav2Vec2

    • Speech to speech generation with Moshi

    • Text to audio with MusicGen

    • Text to speech with Bark

    Computer vision
    • Automatic mask generation with SAM

    • Depth estimation with DepthPro

    • Image classification with DINO v2

    • Keypoint detection with SuperGlue

    • Keypoint matching with SuperGlue

    • Object detection with RT-DETRv2

    • Pose Estimation with VitPose

    • Universal segmentation with OneFormer

    • Video classification with VideoMAE

    Multimodal
    • Audio or text to text with Qwen2-Audio

    • Document question answering with LayoutLMv3

    • Image or text to text with Qwen-VL

    • Image captioning BLIP-2

    • OCR-based document understanding with GOT-OCR2

    • Table question answering with TAPAS

    • Unified multimodal understanding and generation with Emu3

    • Vision to text with Llava-OneVision

    • Visual question answering with Llava

    • Visual referring expression segmentation with Kosmos-2

    NLP
    • Masked word completion with ModernBERT

    • Named entity recognition with Gemma

    • Question answering with Mixtral

    • Summarization with BART

    • Translation with T5

    • Text generation with Llama

    • Text classification with Qwen

    Citation

    We now have a paper you can cite for the ? Transformers library:

    @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://www.acl***web.org/anthology/2020.emnlp-demos.6\",pages = \"38--45\"}

    下载源码

    通过命令行克隆项目:

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

    收藏 (0) 打赏

    感谢您的支持,我会继续努力的!

    打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
    点赞 (0)

    申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

    左子网 编程相关 transformers https://www.zuozi.net/33112.html

    petals
    上一篇: petals
    常见问题
    • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
    查看详情
    • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
    查看详情
    • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
    查看详情
    • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
    查看详情

    相关文章

    猜你喜欢
    发表评论
    暂无评论
    官方客服团队

    为您解决烦忧 - 24小时在线 专业服务