Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
T

transformers-php

> 数据库
Open source

Transformers PHP is a toolkit for PHP developers to add machine learning magic to their projects easily.

759 stars0 likes0 views
WebsiteGitHub

About

Transformers PHP is a toolkit for PHP developers to add machine learning magic to their projects easily.

TransformersPHP

State-of-the-art Machine Learning for PHP

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.

Quick tour

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)

python
from transformers import pipeline

# Allocate a pipeline for sentiment-analysis
pipe = pipeline('sentiment-analysis')

out = pipe('I love transformers!')
# [{'label': 'POSITIVE', 'score': 0.999806941}]
php
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}]
javascript
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:

php
use function Codewithkyrian\Transformers\Pipelines\pipeline;

// Allocate a pipeline for translation
$pipe = pipeline('translation', 'Xenova/distilbert-base-uncased-finetuned-sst-2-english');

Installation

You can install the library via Composer. This is the recommended way to install the library.

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

PHP FFI Extension

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:

ini
extension = ffi

Also, you need to set the ffi.enable directive to true in your php.ini file:

ini
ffi.enable = true

After making these changes, restart your web server or PHP-FPM service, and you should be good to go.

Documentation

For more detailed information on how to use the library, check out the documentation : https://codewithkyrian.github.io/transformers-php

Usage

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.

Configuration

You can configure the behaviour of the TransformersPHP library as follows:

…

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.

Convert your models to ONNX

TransformersPHP only works with ONNX models, therefore, you must convert your PyTorch, TensorFlow or JAX models to ONNX. We recommend using the conversion script from Transformers.js, which uses the Optimum behind the scenes to perform the conversion and quantization of your model.

python -m convert --quantize --model_id 

Pre-Download Models

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:

bash
./vendor/bin/transformers download  [] [options]

Explanation of Arguments:

  • ****: 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).
  • []: (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=**: Specify a directory to store downloaded models (defaults to the configured cache). You can use -c as a shortcut in the command.
    • **--quantized=**: 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.

Supported tasks/models

This package is a WIP, but here's a list of tasks and architectures currently tested and supported by TransformersPHP.

Tasks

Natural Language Processing

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

Vision

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-Imag

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

PHPhuggingfacemlphptransformers

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库