Huggingface PHP is a community-maintained PHP API client that allows you to interact with the Hugging Face API.
Requires PHP 8.1+
First, install OpenAI via the Composer package manager:
composer require kambo/huggingface-php
Ensure that the php-http/discovery
composer plugin is allowed to run or install a client manually if your project does not already have a PSR-18 client integrated.
composer require guzzlehttp/guzzle
Then, interact with Hugging Face's API:
use Kambo\Huggingface\Huggingface;
use Kambo\Huggingface\Enums\Type;
$yourApiKey = getenv('HUGGINGFACE_API_KEY');
$client = Huggingface::client($yourApiKey);
$result = $client->inference()->create([
'model' => 'gpt2',
'inputs' => 'The goal of life is?',
'type' => Type::TEXT_GENERATION,
]);
echo $result['generated_text']."\n";
Inference
Resourcecreate
Execute inference using the chosen model.
$response = $client->inference()->create([
'model' => 'gpt2',
'inputs' => 'The goal of life is?',
'type' => Type::TEXT_GENERATION,
]);
$response->toArray(); // ['type' => .., 'generated_text' => ...]
This library was inspired at the source level by the PHP OpenAI client. Portions of the code have been directly copied from this outstanding library.
Huggingface PHP is an open-sourced software licensed under the MIT license.