Unlock AI Power in PHP: A Hands‑On Guide to TransformersPHP

TransformersPHP brings Hugging Face’s Transformer models to PHP, enabling developers to run thousands of pre‑trained NLP models locally for tasks like text generation, summarisation, and translation, with simple installation, ONNX‑based execution, and a Python‑like pipeline API.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Unlock AI Power in PHP: A Hands‑On Guide to TransformersPHP

Overview

TransformersPHP is a PHP toolkit that brings the capabilities of Hugging Face’s Transformers library to the PHP ecosystem, allowing developers to run thousands of pre‑trained models for tasks such as text generation, summarisation, translation, and more, all locally.

Using Pre‑trained Models

The core idea is to let you run already‑trained models directly in a PHP application without external APIs; the models are ready‑to‑use out of the box.

What is ONNX?

ONNX (Open Neural Network Exchange) is a high‑performance runtime format that enables models trained in PyTorch, TensorFlow, JAX, scikit‑learn, LightGBM, XGBoost, etc., to be executed efficiently across platforms, including PHP.

Inspiration

The project is inspired by the JavaScript Xenova/transformers library, which also uses the ONNX runtime, making most models compatible between the two ecosystems.

Quick Look

Python
from transformers import pipeline

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

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

Features

Local model execution . Deploy large NLP models in PHP without external APIs.

Easy integration . Add advanced AI functions such as text classification and entity recognition to your PHP apps.

Performance‑optimized . Fast processing and efficient resource usage for scalable AI solutions.

Installation

Install via Composer: composer require codewithkyrian/transformers If the automatic installation of shared libraries fails, run:

./vendor/bin/transformers install
Note: Shared libraries are platform‑specific; run the composer require or transformers install command on the target machine or Docker container.

Pre‑download Models

When a pipeline is first used, TransformersPHP downloads the ONNX weights from Hugging Face. To speed up later runs, you can pre‑download models:

./vendor/bin/transformers download <model_name_or_path> [<task>] [options]

Example:

./vendor/bin/transformers download Xenova/bert-base-uncased

Basic Usage

The simplest way to start is the pipeline API, which mirrors the Python library.

Create a Pipeline

use function Codewithkyrian\Transformers\Pipelines\pipeline;

$classifier = pipeline('sentiment-analysis');

The first run downloads and caches the default model.

Use a Different Model

$classifier = pipeline('sentiment-analysis', 'Xenova/bert-base-multilingual-uncased-sentiment');

You can also disable quantisation:

$classifier = pipeline('sentiment-analysis', quantized: false);

Run the Pipeline

$result = $classifier('I love TransformersPHP!');

Result example:

['label' => 'POSITIVE', 'score' => 0.9995358059835]

You can pass an array of texts to analyse multiple inputs at once:

$results = $classifier([
    'I love TransformersPHP!',
    'I hate TransformersPHP!',
]);

Sample output:

[
    ['label' => 'POSITIVE', 'score' => 0.99980061678407],
    ['label' => 'NEGATIVE', 'score' => 0.99842234422764],
];
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

machine learningAIPHPNLPTransformersONNX
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.