codewithkyrian/transformers
A Laravel-friendly transformers package for turning models, arrays, and API responses into consistent, reusable output. Define transformer classes, map fields, nest relations, and format data cleanly for JSON APIs, with minimal boilerplate and flexible customization.
Pipelines are a core feature of TransformersPHP, designed to simplify the use of machine learning models for various natural language processing (NLP) tasks. They encapsulate the entire process of running a model, from input preprocessing to post-processing the output, making it easy to integrate advanced NLP capabilities into your applications.
To create a pipeline, you use the pipeline function, specifying the task you want to perform. Here's a basic example for sentiment analysis:
use function Codewithkyrian\Transformers\Pipelines\pipeline;
$classifier = pipeline('sentiment-analysis');
This initializes a pipeline for sentiment analysis, automatically handling model downloading, caching, input processing, and output interpretation.
Besides passing the task you want to perform, you can also customize the pipeline instance creation with some additional options. For instance, you can specify different model to use instead of the default model:
$classifier = pipeline('sentiment-analysis', 'nlptown/bert-base-multilingual-uncased-sentiment');
Beyond the task and model name, you can further tailor your pipeline with additional named arguments. Here's a breakdown of these options for better clarity:
taskSpecifies the task you wish the pipeline to execute. Refer to the list of supported tasks for available options.
modelNameSpecifies the model to be used by the pipeline. You can use any ONNX model from the Hugging Face model repository that is compatible with the specified task. You can also use your custom models, provided you've prepared them as instructed. If not provided, the default model for the task will be used. Eg
$generator = pipeline('text-generation', 'Xenova/codegen-350M-mono');
quantizedA boolean value indicating whether to use a quantized version of the model. Quantization reduces the model size and speeds up inference but may slightly decrease accuracy. This option defaults to false.
configAllows you to pass a custom configuration for the pipeline. This could include specific model parameters or preprocessing options. Providing a custom config can help tailor the pipeline's behavior to better fit your application' s requirements.
cacheDirWhile it's typically recommended to set the cache directory globally, this allows you to modify the cache directory to save and look for models for this pipelie instance.
revisionThis specified model version to use. It can be a branch name, a tag name, or a commit id. Since HuggingFace uses a
git-based system for storing models and other artifacts, so revision can be any identifier allowed by git.
modelFilenameThis specified the filename of the model in the repository. It's particularly used for decoder only models. It defaults
to decoder_model_merged but you can set it to use another if the repository doesn't use that nomenclature.
Once you've created a pipeline, running it is straightforward. All pipelines are designed to accept input text as their primary argument. Here's how to run a pipeline for some common NLP tasks.
For tasks like sentiment analysis, text generation, or named entity recognition (NER), you typically provide a string or an array of strings as input. Here's an example using the sentiment analysis pipeline created earlier:
$result = $classifier("TransformersPHP makes NLP easy and accessible.");
Most pipelines can also process multiple inputs in a single call, which is especially useful for batch processing. Provide an array of strings to analyze multiple texts at once:
$results = $classifier([
"I love using TransformersPHP for my projects.",
"The weather today is dreadful."
]);
Additional arguments can be passed to the pipeline function to customize it's behavior, but they are hugely dependent on the task you're using the pipelines for. For example, for translation, you can specify the source and target languages:
$translator = pipeline('translation', 'Xenova/m2m100_418M');
$result = $translator('I love TransformersPHP!', srcLang: 'en', tgtLang: 'fr');
Details on the specific options available for each pipeline task are provided within the documentation for that task.
The output generated by a pipeline varies based on the task it's performing and the nature of the input provided. For example:
For the classifier with one input, the output can be:
['label' => 'POSITIVE', 'score' => 0.9995358059835]
and for the multiple input classifier:
[
['label' => 'POSITIVE', 'score' => 0.99980061678407],
['label' => 'NEGATIVE', 'score' => 0.99842234422764],
]
and for the translation task:
['translation_text' => 'J\'aime 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. | ✅ |
| Zero-Shot Image Classification | zero-shot-image |
Classifying images into classes that are unseen during training. | ✅ |
| 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. | ✅ |
| Zero-Shot Object Detection | zero-shot-object |
Detecting objects in images that are unseen during training. | ✅ |
| 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? |
|---|---|---|---|
| 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. | ✅ |
| [Ima |
ge-to-Text](/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. | ✅ |
TransformersPHP supports a wide range of model architectures for various NLP tasks. If the specific model you're interested in isn't listed here, you can open an issue on the repository so we can add support for it. Here's a list of currently tested and supported model architectures:
How can I help you explore Laravel packages today?