TransformersPHP Documentation

repository·main·Indexed 20 days ago

https://github.com/codewithkyrian/transformers-php

A PHP library that brings transformer-based machine learning capabilities to the PHP ecosystem. It provides a pipeline API for tasks such as sentiment analysis, audio classification, and automatic speech recognition (ASR), along with a CLI tool for managing models. The library supports custom model identifiers, quantization, and configurable storage and remote hosting via a fluent setup interface.

Tokens
34.1K
Snippets
119
Records
144
Agent score
72%

What's inside TransformersPHP

  1. What is TransformersPHP

    main

    TransformersPHP is a toolkit for PHP developers to integrate machine learning capabilities directly into PHP applications. It allows you to run pre-trained models locally on your server without relying on external APIs or services.

    Key features:

    • Local Inference: Models run on your own hardware/server.
    • Pre-trained Model Support: Uses models already trained on massive datasets for tasks like text summarization, translation, and sentiment analysis.
    • ONNX Runtime Integration: Uses the ONNX (Open Neural Network Exchange) format, allowing models from PyTorch, TensorFlow, JAX, and others to run efficiently in PHP.
    • Compatibility: Most models prepared for Xenova/transformers (JavaScript) are compatible with TransformersPHP.
  2. What is a Tensor in TransformersPHP?

    main

    A tensor is a multidimensional array used for numerical computing and machine learning. In TransformersPHP, the Tensor class uses a C-based buffer for high-performance element-wise and mathematical operations.

    Performance and Backends

    • OpenBLAS: Automatically included in the package for fast mathematical operations. No separate installation is required.
    • OpenMP: Can be installed separately to enable parallel computation across multiple CPU cores.
    • Fallback: If OpenBLAS is unavailable, the library falls back to a PHP-based buffer, which is slower but remains functional.
  3. What are Pipelines in TransformersPHP

    main

    Pipelines are a high-level abstraction designed to simplify machine learning tasks. They encapsulate the entire workflow required for Natural Language Processing (NLP), including:

    1. Input Preprocessing: Converting raw text into a format the model understands.
    2. Model Execution: Running the model inference.
    3. Post-processing: Converting model outputs back into human-readable formats (e.g., labels, scores, or translated text).

    This allows developers to integrate advanced NLP capabilities without manually managing model weights or tokenization logic.

  4. How aggregation strategies work in Token Classification

    main

    Because tokenizers often split words into subwords (e.g., "Onitsha" $\rightarrow$ "On", "##it", "##sha"), aggregationStrategy allows you to group these back into meaningful entities. When an aggregation strategy is used, the output key entity changes to entity_group.

    Available strategies:

    • AggregationStrategy::NONE: No grouping. Output contains individual subword tokens and their labels.
    • AggregationStrategy::FIRST: Groups subwords and tokens with similar entities. The resulting score is the score of the first token in the group.
    • AggregationStrategy::AVERAGE: Groups subwords and tokens with similar entities. The resulting score is the average of the scores in the group.
    • AggregationStrategy::MAX: Groups subwords and tokens with similar entities. The resulting score is the maximum score found in the group.
    // Example of using MAX aggregation to get grouped entities
    $output = $ner('My name is Kyrian and I live in Onitsha', aggregationStrategy: 'max');
    
    // Output format with aggregation:
    // [
    //   ["entity_group" => "PER", "score" => 0.99431570686513, "word" => "Kyrian"],
    //   ["entity_group" => "LOC", "score" => 0.9980088367015, "word" => "Onitsha"]
    // ]
  5. Use task-specific AutoModel classes for typed outputs

    main

    While AutoModel is general-purpose, task-specific classes (e.g., AutoModelForSequenceClassification) provide two main advantages:

    1. Validation: They ensure the loaded model is compatible with the intended task.
    2. Typed Outputs: Instead of returning a generic array, they return objects with named properties (e.g., $output->logits), making code cleaner and more robust.

    Available task-specific classes include:

    • AutoModelForCasualLM
    • AutoModelForImageClassification
    • AutoModelForImageFeatureExtraction
    • AutoModelForImageToImage
    • AutoModelForMaskedLM
    • AutoModelForObjectDetection
    • AutoModelForQuestionAnswering
    • AutoModelForSeq2SeqLM
    • AutoModelForSequenceClassification
    • AutoModelForTokenClassification
    • AutoModelForVision2Seq
    • AutoModelForZeroShotObjectDetection
    use Codewithkyrian\Transformers\Models\Auto\AutoModelForSequenceClassification;
    
    // Using the task-specific class provides an object with a 'logits' property
    $model = AutoModelForSequenceClassification::fromPretrained('Xenova/toxic-bert');
    $output = $model($encodedInput);
    
    $probabilities = $output->logits[0]->softmax();
  6. Handle text-generation pipeline outputs

    main

    The output of a text-generation pipeline is an array of associative arrays. Each element corresponds to an input and contains a generated_text key.

    If you are building a chat interface, you should append the generated content back to your message history using the assistant role to maintain context for subsequent turns.

    Output Format:

    [
        ["generated_text" => "...generated content..."]
    ]
    $output = $generator($input, maxNewTokens: 256, returnFullText: false);
    
    // Extract the generated string
    $generatedMessage = $output[0]['generated_text'];
    
    // Append to conversation history
    $messages[] = ['role' => 'assistant', 'content' => $generatedMessage];
  7. Understand Automatic Speech Recognition pipeline outputs

    main

    The ASR pipeline returns an array. The structure depends on whether timestamps were requested via returnTimestamps.

    Default Output

    If no timestamps are requested, the output contains only the transcribed text:

    [
      "text" => "Transcribed text here..."
    ]

    Chunk-Level Timestamps

    If returnTimestamps is set to true, the output includes a chunks array with timestamps for segments of text:

    [
      "text" => "Transcribed text...",
      "chunks" => [
        [
          "timestamp" => [0.0, 5.12],
          "text" => "First chunk of text"
        ],
        // ...
      ]
    ]

    Word-Level Timestamps

    If returnTimestamps is set to 'word', the chunks array contains individual words and their specific timestamps:

    [
      "text" => "...",
      "chunks" => [
        ["text" => "We,", "timestamp" => [0.6, 0.94]],
        ["text" => "the", "timestamp" => [0.94, 1.3]],
        // ...
      ]
    ]
  8. Understand Audio Classification pipeline outputs

    main

    The output of the audio classification pipeline is an array of classification results. Each result contains:

    • label: The classification label (the specific names depend on the model used).
    • score: The confidence score, a value between 0 and 1 (where 1 is the highest confidence).

    If topK is greater than 1, the output is an array of these associative arrays. If processing multiple audio files, the structure may vary depending on the specific implementation context, but typically returns the labels and scores for the inputs.

    // Example output for topK: 4
    [
        ['label' => 'Cat Meow',  'score' => 0.8456],
        ['label' => 'Domestic Animal',  'score' => 0.1234],
        ['label' => 'Pet',  'score' => 0.0987],
        ['label' => 'Mammal',  'score' => 0.0567]
    ]
  9. Understand text generation in TransformersPHP

    main

    Text generation is a task where models produce sequences of text based on an input prompt. Unlike classification (assigning labels) or Named Entity Recognition (identifying entities), generation involves creating novel, coherent, and contextually relevant content such as stories, answers, or summaries.

    Models often include a generation_config.json file in their repository which provides default settings optimized for that specific model. These defaults are applied automatically when the model instance is created, but can be overridden during invocation for dynamic control.

  10. Understand translation pipeline output format

    main

    The translation pipeline returns an array of arrays. Each inner array corresponds to one input text and contains a single key: translated_text.

    If you pass a single string, the result will be an array containing one element. If you pass an array of strings, the output array will have the same number of elements as the input.

    // Example output structure for a single input
    [
      [
        'translated_text' => 'The United Nations chief says there is no military solution in Syria'
      ]
    ]
  11. Understand Image Feature Extraction Output Shapes

    main

    The output of the image-feature-extraction pipeline is a feature vector. The shape of this vector depends on whether pooling is enabled:

    Without Pooling (pool: false)

    The output shape is typically [X, Y, Z]:

    • X: Batch size (e.g., 1 for a single image).
    • Y: Sequence length or number of patches/tokens.
    • Z: The size of the feature vector per patch. Example (ViT architecture): [1, 197, 768]

    With Pooling (pool: true)

    The output shape is typically [X, Z]:

    • X: Batch size.
    • Z: The size of the single pooled feature vector representing the entire image. Example: [1, 768]