Tech News#PHP#TransformersPHP#ML

Announcing TransformersPHP: Bring Machine Learning Magic to Your PHP Projects

I introduced TransformersPHP, a PHP toolkit for running pre-trained transformer models for text generation, classification, summarization, and translation.

By Kyrian Obikwelu·4 min read·published March 20, 2024·updated March 20, 2024
Announcing TransformersPHP: Bring Machine Learning Magic to Your PHP Projects

Introduction

I built TransformersPHP because I wanted PHP applications to run useful transformer models without handing the whole job to a Python service. This post is the original introduction to the library and the problem it was meant to solve.

What is TransformersPHP?

TransformersPHP brings the Hugging Face Transformers workflow to PHP. It supports text generation, classification, summarization, translation, and other model-backed tasks inside a PHP application.

It uses ONNX Runtime to run ONNX models, which gives PHP developers access to pre-trained models across more than 100 languages.

Key Features

Getting Started with TransformersPHP

Prerequisites

Before using TransformersPHP, ensure your system meets the following requirements:

  • PHP 8.1 or above
  • Composer (obviously)
  • PHP FFI extension
  • JIT compilation (optional, but recommended for performance improvement)
  • Increased memory limit (for advanced tasks like text generation)

Installation

Installation is straightforward with Composer:

composer require codewithkyrian/transformers

After installation, initialize the package to download the necessary shared libraries for ONNX models:

./vendor/bin/transformers install

Remember, the shared libraries are platform-specific so make sure to run the install command on the target platform where your code will be executed (eg inside the docker container)

Pre-Download Models

To avoid downloading the model on-the-fly when using it, pre-download the ONNX model weights from the Hugging Face model hub. Use the command-line tool included with the package:

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

For example:

./vendor/bin/transformers download Xenova/mobilebert-uncased-mnli zero-shot-classification

Example Usage

Here’s a simple example of how to use TransformersPHP for zero-shot classification:

use function Codewithkyrian\Transformers\Pipelines\pipeline;

$classifier = pipeline('zero-shot-classification', 'Xenova/mobilebert-uncased-mnli');

$text = 'I have a problem with my iphone that needs to be resolved asap!';

$labels = ['urgent', 'not urgent', 'phone', 'tablet', 'computer'];

$result = $classifier($text, $labels, multiLabel: true);

And the output will be this:

[
  "sequence" => "I have a problem with my iphone that needs to be resolved asap!",
  "labels" => ["urgent", "phone", "computer", "tablet", "not urgent"],
  "scores" => [0.99588709563603, 0.9923963400697,  0.0023335396113424, 0.0015134149376, 0.0010699384208377]
]

Example 2

Here’s another example for another task - token classification

use function Codewithkyrian\Transformers\Pipelines\pipeline;

$ner = pipeline('token-classification', 'codewithkyrian/bert-english-uncased-finetuned-pos');

$output = $ner('My name is Kyrian and I live in Onitsha', aggregationStrategy: 'max');

And the output will be:

[
    ["entity_group" => "PRON", "word" => "my", "score" => 0.99482086393966],
    ["entity_group" => "NOUN", "word" => "name", "score" => 0.95769686675798],
    ["entity_group" => "AUX", "word" => "is", "score" => 0.97602109098715],
    ["entity_group" => "PROPN", "word" => "kyrian", "score" => 0.96583783664597],
    ["entity_group" => "CCONJ", "word" => "and", "score" => 0.98444884455349],
    ["entity_group" => "PRON", "word" => "i", "score" => 0.99566682068677],
    ["entity_group" => "VERB", "word" => "live", "score" => 0.98391136480035],
    ["entity_group" => "ADP", "word" => "in", "score" => 0.99580186695928],
    ["entity_group" => "PROPN", "word" => "onitsha", "score" => 0.91250281394515],
]

Learn More

For detailed information on installation, model conversion, and usage of TransformersPHP, head over to the comprehensive documentation. You can also check out the package GitHub repository and leave some stars ⭐️

I’m excited to see how the PHP community uses TransformersPHP to push the boundaries of what’s possible in web development and beyond.