kambo/huggingface-php

v0.1.0 2023-05-08 07:50 UTC

This package is auto-updated.

Last update: 2024-04-08 10:03:56 UTC


README

GitHub Workflow Status (main) Total Downloads Latest Version License

Huggingface PHP is a community-maintained PHP API client that allows you to interact with the Hugging Face API.

Table of Contents

Get Started

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";

Usage

Inference Resource

create

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' => ...]

Acknowledge

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.