TransformersPHP is designed to be functionally equivalent to the Python library, while still maintaining the same level of performance and ease of use. This library is built on top of the Hugging Face's Transformers library, which provides thousands of pre-trained models in 100+ languages. It is designed to be a simple and easy-to-use library for PHP developers using a similar API to the Python library. These models can be used for a variety of tasks, including text generation, summarization, translation, and more.
TransformersPHP uses ONNX Runtime to run the models, which is a high-performance scoring engine for Open Neural Network Exchange (ONNX) models. You can easily convert any PyTorch or TensorFlow model to ONNX and use it with TransformersPHP using 🤗 Optimum.
TO learn more about the library and how it works, head over to our extensive documentation.
Because TransformersPHP is designed to be functionally equivalent to the Python library, it's super easy to learn from
existing Python or Javascript code. We provide the pipeline
API, which is a high-level, easy-to-use API that groups
together a model with its necessary preprocessing and postprocessing steps.
Python (original) | PHP (ours) | Javascript (Xenova) |
---|---|---|
from transformers import pipeline
# Allocate a pipeline for sentiment-analysis
pipe = pipeline('sentiment-analysis')
out = pipe('I love transformers!')
# [{'label': 'POSITIVE', 'score': 0.999806941}] |
use function Codewithkyrian\Transformers\Pipelines\pipeline;
// Allocate a pipeline for sentiment-analysis
$pipe = pipeline('sentiment-analysis');
$out = $pipe('I love transformers!');
// [{'label': 'POSITIVE', 'score': 0.999808732}] |
import {pipeline} from '@xenova/transformers';
// Allocate a pipeline for sentiment-analysis
let pipe = await pipeline('sentiment-analysis');
let out = await pipe('I love transformers!');
// [{'label': 'POSITIVE', 'score': 0.999817686}] |
You can also use a different model by specifying the model id or path as the second argument to the pipeline
function.
For example:
use function Codewithkyrian\Transformers\Pipelines\pipeline;
// Allocate a pipeline for translation
$pipe = pipeline('translation', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english');
You can install the library via Composer. This is the recommended way to install the library.
composer require codewithkyrian/transformers
Caution
The ONNX library is platform-specific, so it's important to run the composer require command on the target platform
where the code will be executed. In most cases, this will be your development machine or a server where you deploy
your application, but if you're using a Docker container, run the composer require
command inside that container.
TransformersPHP uses the PHP FFI extension to interact with the ONNX runtime. The FFI extension is included by default
in PHP 7.4 and later, but it may not be enabled by default. If the FFI extension is not enabled, you can enable it by
uncommenting(remove the ;
from the beginning of the line) the
following line in your php.ini
file:
extension = ffi
Also, you need to set the ffi.enable
directive to true
in your php.ini
file:
ffi.enable = true
After making these changes, restart your web server or PHP-FPM service, and you should be good to go.
For more detailed information on how to use the library, check out the documentation : https://codewithkyrian.github.io/transformers-php
By default, TransformersPHP uses hosted pretrained ONNX models. For supported tasks, models that have been converted to work with Xenova's Transformers.js on HuggingFace should work out of the box with TransformersPHP.
You can configure the behaviour of the TransformersPHP library as follows:
use Codewithkyrian\Transformers\Transformers;
Transformers::setup()
->setCacheDir('...') // Set the default cache directory for transformers models. Defaults to `.transformers-cache/models`
->setRemoteHost('...') // Set the remote host for downloading models. Defaults to `https://huggingface.co`
->setRemotePathTemplate('...') // Set the remote path template for downloading models. Defaults to `{model}/resolve/{revision}/{file}`
->setAuthToken('...') // Set the auth token for downloading models. Defaults to `null`
->setUserAgent('...') // Set the user agent for downloading models. Defaults to `transformers-php/{version}`
->setImageDriver('...') // Set the image driver for processing images. Defaults to `IMAGICK'
->apply(); // Apply the configuration
You can call the set
methods in any order, or leave any out entirely, in which case, it uses the default values. For
more information on the configuration options and what they mean, checkout
the documentation.
TransformersPHP only works with ONNX models, therefore, you must convert your PyTorch, TensorFlow or JAX models to ONNX. It is recommended to use 🤗 Optimum to perform the conversion and quantization of your model.
By default, TransformersPHP automatically retrieves model weights (ONNX format) from the Hugging Face model hub when you first use a pipeline or pretrained model. This can lead to a slight delay during the initial use. To improve the user experience, it's recommended to pre-download the models you intend to use before running them in your PHP application, especially for larger models. One way to do that is run the request once manually, but TransformersPHP also comes with a command line tool to help you do just that:
./vendor/bin/transformers download <model_identifier> [<task>] [options]
Explanation of Arguments:
- <model_identifier>: This specifies the model you want to download. You can find model identifiers by browsing the Hugging Face model hub (https://huggingface.co/models?library=transformers.js).
- [<task>]: (Optional) This parameter allows for downloading task-specific configurations and weights. This can be helpful if you know the specific task you'll be using the model for (e.g., "text2text-generation").
- [options]: (Optional) You can further customize the download process with additional options:
- --cache_dir=<directory>: Specify a directory to store downloaded models (defaults to the configured cache). You can use -c as a shortcut in the command.
- --quantized=<true|false>: Download the quantized model version if available (defaults to true). Quantized models are smaller and faster, but may have slightly lower accuracy. Use -q as a shortcut in the command.
Caution
Remember to add your cache directory to your .gitignore
file to avoid committing the downloaded models to your git
repository.
This package is a WIP, but here's a list of tasks and architectures currently tested and supported by TransformersPHP.
Task | ID | Description | Supported? |
---|---|---|---|
Fill-Mask | fill-mask |
Masking some of the words in a sentence and predicting which words should replace those masks. | ✅ |
Question Answering | question-answering |
Retrieve the answer to a question from a given text. | ✅ |
Sentence Similarity | sentence-similarity |
Determining how similar two texts are. | ✅ |
Summarization | summarization |
Producing a shorter version of a document while preserving its important information. | ✅ |
Table Question Answering | table-question-answering |
Answering a question about information from a given table. | ❌ |
Text Classification | text-classification or sentiment-analysis |
Assigning a label or class to a given text. | ✅ |
Text Generation | text-generation |
Producing new text by predicting the next word in a sequence. | ✅ |
Text-to-text Generation | text2text-generation |
Converting one text sequence into another text sequence. | ✅ |
Token Classification | token-classification or ner |
Assigning a label to each token in a text. | ✅ |
Translation | translation |
Converting text from one language to another. | ✅ |
Zero-Shot Classification | zero-shot-classification |
Classifying text into classes that are unseen during training. | ✅ |
Task | ID | Description | Supported? |
---|---|---|---|
Depth Estimation | depth-estimation |
Predicting the depth of objects present in an image. | ❌ |
Image Classification | image-classification |
Assigning a label or class to an entire image. | ✅ |
Image Segmentation | image-segmentation |
Divides an image into segments where each pixel is mapped to an object. This task has multiple variants such as instance segmentation, panoptic segmentation and semantic segmentation. | ❌ |
Image-to-Image | image-to-image |
Transforming a source image to match the characteristics of a target image or a target image domain. | ✅ |
Mask Generation | mask-generation |
Generate masks for the objects in an image. | ❌ |
Object Detection | object-detection |
Identify objects of certain defined classes within an image. | ✅ |
Task | ID | Description | Supported? |
---|---|---|---|
Audio Classification | audio-classification |
Assigning a label or class to a given audio. | ❌ |
Audio-to-Audio | N/A | Generating audio from an input audio source. | ❌ |
Automatic Speech Recognition | automatic-speech-recognition |
Transcribing a given audio into text. | ❌ |
Text-to-Speech | text-to-speech or text-to-audio |
Generating natural-sounding speech given text input. | ❌ |
Task | ID | Description | Supported? |
---|---|---|---|
Tabular Classification | N/A | Classifying a target category (a group) based on set of attributes. | ❌ |
Tabular Regression | N/A | Predicting a numerical value given a set of attributes. | ❌ |
Task | ID | Description | Supported? |
---|---|---|---|
Document Question Answering | document-question-answering |
Answering questions on document images. | ❌ |
Feature Extraction | feature-extraction |
Transforming raw data into numerical features that can be processed while preserving the information in the original dataset. | ✅ |
Image Feature Extraction | image-feature-extraction |
Extracting features from images. | ✅ |
Image-to-Text | image-to-text |
Output text from a given image. | ✅ |
Text-to-Image | text-to-image |
Generates images from input text. | ❌ |
Visual Question Answering | visual-question-answering |
Answering open-ended questions based on an image. | ❌ |
Zero-Shot Audio Classification | zero-shot-audio-classification |
Classifying audios into classes that are unseen during training. | ❌ |
Zero-Shot Image Classification | zero-shot-image-classification |
Classifying images into classes that are unseen during training. | ✅ |
Zero-Shot Object Detection | zero-shot-object-detection |
Identify objects of classes that are unseen during training. | ✅ |
Task | ID | Description | Supported? |
---|---|---|---|
Reinforcement Learning | N/A | Learning from actions by interacting with an environment through trial and error and receiving rewards (negative or positive) as feedback. | ❌ |
- ALBERT (from Google Research and the Toyota Technological Institute at Chicago) released with the paper ALBERT: A Lite BERT for Self-supervised Learning of Language Representations, by Zhenzhong Lan, Mingda Chen, Sebastian Goodman, Kevin Gimpel, Piyush Sharma, Radu Soricut.
- BART (from Facebook) released with the paper BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension by Mike Lewis, Yinhan Liu, Naman Goyal, Marjan Ghazvininejad, Abdelrahman Mohamed, Omer Levy, Ves Stoyanov and Luke Zettlemoyer.
- BERT (from Google) released with the paper BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding by Jacob Devlin, Ming-Wei Chang, Kenton Lee, and Kristina Toutanova.
- BERT For Sequence Generation (from Google) released with the paper Leveraging Pre-trained Checkpoints for Sequence Generation Tasks by Sascha Rothe, Shashi Narayan, Aliaksei Severyn.
- BERTweet (from VinAI Research) released with the paper BERTweet: A pre-trained language model for English Tweets by Dat Quoc Nguyen, Thanh Vu and Anh Tuan Nguyen.
- BigBird-Pegasus (from Google Research) released with the paper Big Bird: Transformers for Longer Sequences by Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed.
- BigBird-RoBERTa (from Google Research) released with the paper Big Bird: Transformers for Longer Sequences by Manzil Zaheer, Guru Guruganesh, Avinava Dubey, Joshua Ainslie, Chris Alberti, Santiago Ontanon, Philip Pham, Anirudh Ravula, Qifan Wang, Li Yang, Amr Ahmed.
- CLIP (from OpenAI) released with the paper Learning Transferable Visual Models From Natural Language Supervision by Alec Radford, Jong Wook Kim, Chris Hallacy, Aditya Ramesh, Gabriel Goh, Sandhini Agarwal, Girish Sastry, Amanda Askell, Pamela Mishkin, Jack Clark, Gretchen Krueger, Ilya Sutskever.
- CodeGen (from Salesforce) released with the paper A Conversational Paradigm for Program Synthesis by Erik Nijkamp, Bo Pang, Hiroaki Hayashi, Lifu Tu, Huan Wang, Yingbo Zhou, Silvio Savarese, Caiming Xiong.
- ConvBERT (from YituTech) released with the paper ConvBERT: Improving BERT with Span-based Dynamic Convolution by Zihang Jiang, Weihao Yu, Daquan Zhou, Yunpeng Chen, Jiashi Feng, Shuicheng Yan.
- DeBERTa (from Microsoft) released with the paper DeBERTa: Decoding-enhanced BERT with Disentangled Attention by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen.
- DeBERTa-v2 (from Microsoft) released with the paper DeBERTa: Decoding-enhanced BERT with Disentangled Attention by Pengcheng He, Xiaodong Liu, Jianfeng Gao, Weizhu Chen.
- DETR (from Facebook) released with the paper End-to-End Object Detection with Transformers by Nicolas Carion, Francisco Massa, Gabriel Synnaeve, Nicolas Usunier, Alexander Kirillov, Sergey Zagoruyko.
- DistilBERT (from HuggingFace), released together with the paper DistilBERT, a distilled version of BERT: smaller, faster, cheaper and lighter by Victor Sanh, Lysandre Debut and Thomas Wolf. The same method has been applied to compress GPT2 into DistilGPT2, RoBERTa into DistilRoBERTa, Multilingual BERT into DistilmBERT and a German version of DistilBERT.
- Donut (from NAVER), released together with the paper OCR-free Document Understanding Transformer by Geewook Kim, Teakgyu Hong, Moonbin Yim, Jeongyeon Nam, Jinyoung Park, Jinyeong Yim, Wonseok Hwang, Sangdoo Yun, Dongyoon Han, Seunghyun Park.
- ELECTRA (from Google Research/Stanford University) released with the paper ELECTRA: Pre-training text encoders as discriminators rather than generators by Kevin Clark, Minh-Thang Luong, Quoc V. Le, Christopher D. Manning.
- FLAN-T5 (from Google AI) released in the repository google-research/t5x by Hyung Won Chung, Le Hou, Shayne Longpre, Barret Zoph, Yi Tay, William Fedus, Eric Li, Xuezhi Wang, Mostafa Dehghani, Siddhartha Brahma, Albert Webson, Shixiang Shane Gu, Zhuyun Dai, Mirac Suzgun, Xinyun Chen, Aakanksha Chowdhery, Sharan Narang, Gaurav Mishra, Adams Yu, Vincent Zhao, Yanping Huang, Andrew Dai, Hongkun Yu, Slav Petrov, Ed H. Chi, Jeff Dean, Jacob Devlin, Adam Roberts, Denny Zhou, Quoc V. Le, and Jason Wei
- GPT-2 (from OpenAI) released with the paper Language Models are Unsupervised Multitask Learners by Alec Radford*, Jeffrey Wu*, Rewon Child, David Luan, Dario Amodei** and Ilya Sutskever**.
- GPT-J (from EleutherAI) released in the repository kingoflolz/mesh-transformer-jax by Ben Wang and Aran Komatsuzaki.
- GPTBigCode (from BigCode) released with the paper SantaCoder: don't reach for the stars! by Loubna Ben Allal, Raymond Li, Denis Kocetkov, Chenghao Mou, Christopher Akiki, Carlos Munoz Ferrandis, Niklas Muennighoff, Mayank Mishra, Alex Gu, Manan Dey, Logesh Kumar Umapathi, Carolyn Jane Anderson, Yangtian Zi, Joel Lamy Poirier, Hailey Schoelkopf, Sergey Troshin, Dmitry Abulkhanov, Manuel Romero, Michael Lappert, Francesco De Toni, Bernardo García del Río, Qian Liu, Shamik Bose, Urvashi Bhattacharyya, Terry Yue Zhuo, Ian Yu, Paulo Villegas, Marco Zocca, Sourab Mangrulkar, David Lansky, Huu Nguyen, Danish Contractor, Luis Villa, Jia Li, Dzmitry Bahdanau, Yacine Jernite, Sean Hughes, Daniel Fried, Arjun Guha, Harm de Vries, Leandro von Werra.
- M2M100 (from Facebook) released with the paper Beyond English-Centric Multilingual Machine Translation by Angela Fan, Shruti Bhosale, Holger Schwenk, Zhiyi Ma, Ahmed El-Kishky, Siddharth Goyal, Mandeep Baines, Onur Celebi, Guillaume Wenzek, Vishrav Chaudhary, Naman Goyal, Tom Birch, Vitaliy Liptchinsky, Sergey Edunov, Edouard Grave, Michael Auli, Armand Joulin.
- MobileBERT (from CMU/Google Brain) released with the paper MobileBERT: a Compact Task-Agnostic BERT for Resource-Limited Devices by Zhiqing Sun, Hongkun Yu, Xiaodan Song, Renjie Liu, Yiming Yang, and Denny Zhou.
- OWL-ViT (from Google AI) released with the paper Simple Open-Vocabulary Object Detection with Vision Transformers by Matthias Minderer, Alexey Gritsenko, Austin Stone, Maxim Neumann, Dirk Weissenborn, Alexey Dosovitskiy, Aravindh Mahendran, Anurag Arnab, Mostafa Dehghani, Zhuoran Shen, Xiao Wang, Xiaohua Zhai, Thomas Kipf, and Neil Houlsby.
- OWLv2 (from Google AI) released with the paper Scaling Open-Vocabulary Object Detection by Matthias Minderer, Alexey Gritsenko, Neil Houlsby.
- RoBERTa (from Facebook), released together with the paper RoBERTa: A Robustly Optimized BERT Pretraining Approach by Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer, Veselin Stoyanov.
- RoBERTa-PreLayerNorm (from Facebook) released with the paper fairseq: A Fast, Extensible Toolkit for Sequence Modeling by Myle Ott, Sergey Edunov, Alexei Baevski, Angela Fan, Sam Gross, Nathan Ng, David Grangier, Michael Auli.
- RoFormer (from ZhuiyiTechnology), released together with the paper RoFormer: Enhanced Transformer with Rotary Position Embedding by Jianlin Su and Yu Lu and Shengfeng Pan and Bo Wen and Yunfeng Liu.
- SigLIP (from Google AI) released with the paper Sigmoid Loss for Language Image Pre-Training by Xiaohua Zhai, Basil Mustafa, Alexander Kolesnikov, Lucas Beyer.
- Swin2SR (from University of Würzburg) released with the paper Swin2SR: SwinV2 Transformer for Compressed Image Super-Resolution and Restoration by Marcos V. Conde, Ui-Jin Choi, Maxime Burchi, Radu Timofte.
- T5 (from Google AI) released with the paper Exploring the Limits of Transfer Learning with a Unified Text-to-Text Transformer by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
- T5v1.1 (from Google AI) released in the repository google-research/text-to-text-transfer-transformer by Colin Raffel and Noam Shazeer and Adam Roberts and Katherine Lee and Sharan Narang and Michael Matena and Yanqi Zhou and Wei Li and Peter J. Liu.
- TrOCR (from Microsoft), released together with the paper TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models by Minghao Li, Tengchao Lv, Lei Cui, Yijuan Lu, Dinei Florencio, Cha Zhang, Zhoujun Li, Furu Wei.
- Vision Transformer (ViT) (from Google AI) released with the paper An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale by Alexey Dosovitskiy, Lucas Beyer, Alexander Kolesnikov, Dirk Weissenborn, Xiaohua Zhai, Thomas Unterthiner, Mostafa Dehghani, Matthias Minderer, Georg Heigold, Sylvain Gelly, Jakob Uszkoreit, Neil Houlsby.
- YOLOS (from Huazhong University of Science & Technology) released with the paper You Only Look at One Sequence: Rethinking Transformer in Vision through Object Detection by Yuxin Fang, Bencheng Liao, Xinggang Wang, Jiemin Fang, Jiyang Qi, Rui Wu, Jianwei Niu, Wenyu Liu.