ML Ecosystem in PHP
Learning examples
These examples will help you understand how you can use ML in PHP. They are not full-fledged applications, but they will help you understand the basics of working with ML in PHP.
Example with TransformersPHP
TransformersPHP gives you a simple pipeline API for tasks like sentiment analysis, text classification or semantic comparison. The snippet below runs a sentiment model on two short phrases and shows the resulting label and score.
Example of use
<?php
use function Codewithkyrian\Transformers\Pipelines\pipeline;
// Allocate a pipeline for sentiment analysis
$classifier = pipeline('sentiment-analysis');
$out = $classifier(['I love transformers!']);
echo print_r($out, true) . PHP_EOL;
$out = $classifier(['I hate transformers!']);
echo print_r($out, true);
Result:
Memory: 0.001 Mb
Time running: < 0.001 sec.
Array
(
[label] => POSITIVE
[score] => 0.99978870153427
)
Array
(
[label] => NEGATIVE
[score] => 0.99863630533218
)