Skip to main content

PHP can run AI models - no need Python

 Objective:

    Run AI models only using php. Now this is possible with https://github.com/CodeWithKyrian/transformers-php this library. I have tried object detection and question answering using this library.

Technology:

     1. PHP 8.2

Initialization:

    Guide - https://transformers.codewithkyrian.com/getting-started

1. Initialize project by composer init cmd.

2. Install transformer library by composer require codewithkyrian/transformers cmd.

3. Enable ffi extention in php.ini

    extension = ffi

    ffi.enable = true

4. Enable opcache

    opcache.jit = tracing

5. Increase memory limit

    memory_limit = 512M

I. Object detection: (https://transformers.codewithkyrian.com/object-detection)

    Object detection can improve web application perspective . Example: Retail applications can speed up checkout by automatically recognizing purchased items. Similarly, inventory management can be automated by detecting stock levels from images or video feeds.


 require __dir__.'\..\vendor\autoload.php';
 use Codewithkyrian\Transformers\Transformers;
 use function Codewithkyrian\Transformers\Pipelines\pipeline;
 use Codewithkyrian\Transformers\Utils\ImageDriver;
 use Codewithkyrian\Transformers\Utils\Image;


 Transformers::setup()
  ->setImageDriver(ImageDriver::GD)
  ->apply();

 $detector = pipeline('object-detection');
 $file = 'path\image\image.jpg';
 $result = $detector($file,threshold: 0.6);
 $image = Image::read(__dir__.'\images\\'.$file);


fororeach ($result as $object) {
  $box = $object['box'];
  $image = $image->drawRectangle($box['xmin'], $box['ymin'], $box['xmax'], $box['ymax'], '000000', thickness: 2);

  $image = $image->drawText($object['label'], $box['xmin'], max($box['ymin'] - 14, 0),'\fonts\used\Roboto-Medium.ttf', 12, '000000');

 }

 $image->save('path\image\output.jpg');

Result image: 

PHP object detection output image

Input image:

PHP object detection input image

II. Question answering - (https://transformers.codewithkyrian.com/question-answering)

    The full product description, features, FAQs, or any relevant text are fed into the AI model as the "context" or "knowledge base.". The user gets a specific and informative answer without needing to read the entire description.



require __dir__.'\..\vendor\autoload.php';

use function Codewithkyrian\Transformers\Pipelines\pipeline;

$question = 'Which Gmail feature do you find most helpful for staying organized and why?';

$context = 'Gmail is a powerful, user-friendly email service that seamlessly integrates with Google Workspace to enhance your productivity. In 2025, Gmail offers advanced AI-driven features like smart email summarization that lets you preview the essential points of long emails instantly, saving you time. With improved spam detection powered by machine learning, your inbox stays clean and secure from phishing or junk mail. Gmail’s integrated task panel enables you to convert emails into actionable tasks with due dates and calendar sync, streamlining your workflow without leaving your inbox. Other highlights include voice typing for hands-free email composition, real-time translation for multilingual communication, dynamic smart labels and filters for better organization, and offline mode improvements so you can work anywhere. Whether on web or mobile, Gmail keeps you connected with intuitive features that reduce clutter and help you focus on what matters most.';

$pipeline = pipeline('question-answering', 'Xenova/distilbert-base-cased-distilled-squad');
$result = $pipeline($question, $context, topK: 3);
print_r($result);

output:

    



Comments

Popular posts from this blog

Don't assign value to reference variable.

Objective:          I just tried to create linked list in php.  Challenges:     In php array is sufficient for most data types (simple array, multi-dimensional array and key-value associative array). But, i just tried using  stdClass Object ( pre-defined interfaces and class ). Here, I created one stdClass with reference key and it reference to another class up to final and that final class  reference key is null. Here I used reference key as reference variable. Once i started assigning reference value to this key it assigning new value to it and old one erased. $a = new stdClass(); $a->name = "Trial"; $a->ref = null; for($i=1;$i <= 10;$i++){  $b = new stdClass();  $b->name = 'Demo mj '.$i;  $b->ref = null;  if($a->ref === null ){   $a->ref = &$b;  }else{   $current = $a->ref;   wh...

bootstrap-popper not allowing html table

  Objective :       Creating popover with html using bootstrap-popover.js Technology:  Bootstrap version - 4.6.1 Popper - 1.16.1 Jquery - 3.5.1 Challenge:     1. basic syntax for popover <button type= "button" class= "btn btn-lg btn-success" data-toggle= "popover" title= "Popover title" data-content= "And here's some amazing content. It's very engaging. Right?" > Click to toggle popover </button>      $ ( function () {           $ ( '[data-toggle="popover"]' ). popover ();      });    2. Put html to that popover syntax <button type= "button" class= "btn btn-lg btn-success" data-toggle= "popover" title= "Popover title" data-content= "And here's some amazing content. It's very engaging. Right?" > Click to toggle popover </button>      $(function () { $('button[data-toggle="popover"]').popover(...