PHP code example of swapinvidya / laravel-huggingface-client

1. Go to this page and download the library: Download swapinvidya/laravel-huggingface-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

swapinvidya / laravel-huggingface-client example snippets


return [
    'api_key' => env('HUGGINGFACE_API_KEY'),
    'base_uri' => 'https://api-inference.huggingface.co/',
];

use Swapinvidya\HuggingFaceClient\HuggingFaceClient;

public function generateText(HuggingFaceClient $huggingFaceClient)
{
    $response = $huggingFaceClient->generateText('gpt2', 'Write a short story about a hero.');
    return response()->json($response);
}

public function generateImage(HuggingFaceClient $huggingFaceClient)
{
    $response = $huggingFaceClient->generateImage('stable-diffusion-v1', 'A futuristic city with flying cars.');
    return response()->json($response);
}

public function generateCompletion(HuggingFaceClient $huggingFaceClient)
{
    $params = [
        'inputs' => 'Explain the theory of relativity in simple terms.',
        'parameters' => [
            'temperature' => 0.7,
            'max_new_tokens' => 200,
        ],
    ];
    $response = $huggingFaceClient->generateCompletion('gpt-neo', $params);
    return response()->json($response);
}

$response = $huggingFaceClient->generateText('gpt2', 'Hello world');

if (isset($response['error'])) {
    // Handle the error
    return response()->json(['error' => $response['error']], 500);
}

return response()->json($response);
bash
php artisan vendor:publish --provider="Swapinvidya\HuggingFaceClient\HuggingFaceServiceProvider"