Add an official API binding for PHP alongside PHP-ML support (complete with examples and setup guide)

I desperately need access to a model from Hugging Face for my website. The whole thing is written in PHP; I even tried getting it to interoperate with a Python script (using Hugging Face’s transformer library), but that just didn’t work at all (note that the Python script itself DID work, but I just had trouble getting the results through PHP). As I last resort, I downloaded a third-party binding for PHP, but it was unmaintained and no version of it I could find worked. This is the ONLY PHP library I could find that supports the Hugging Face API.

I wish there was a version of the Hugging Face API that worked as well as the JS or Python version. I also wish there was a way to download and run the models locally through the transformer library on my server like is possible through PyTorch (supposedly using the PHP-ML library in place of PyTorch).

2 Likes

Hello. I wrote a PHP library just for that. It’s still in it’s early stages but you can check to see if the task you want is currently supported. The API is similar to the python and Javascript counterparts.

Check it out here: GitHub - CodeWithKyrian/transformers-php: Transformers PHP is a toolkit for PHP developers to add machine learning magic to their projects easily.

1 Like

Just here to say a big thanks to Kyrian for making this library available!

I’m not in production with it yet, but in my tests it’s workable for creating text embeddings with local models. As per the docs, the Xenova models mostly work.

Doing a single embedding (for a query, say) is RAPID, although the FFI startup does add an overhead. However, that matters less for batch processing, and it’s a LOT quicker than using remote APIs. Albeit, only as good as the model you’re using.

If it helps anyone, I had no problem doing composer update on my local Mac, and deploying compser.lock to Linux. Running composer install pulls the right ONNX library.