PHP code example of demonyka / eden-ai-sdk

1. Go to this page and download the library: Download demonyka/eden-ai-sdk 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/ */

    

demonyka / eden-ai-sdk example snippets


use EdenAI\Api;

// Method 1: Provide token directly
$api = new Api('your-api-token-here');

// Method 2: Use environment variable
// Set the EDEN_AI_TOKEN environment variable
// $api = new Api();

use EdenAI\Api;

// Initialize with API token
$api = new Api('your-api-token-here');

// Check for explicit content in an image
$result = $api->checkExplicitContent([
    'file_url' => 'https://example.com/image.jpg',
    'providers' => 'google,clarifai'
]);

// Check if the image is NSFW
if ($result->isNSFW()) {
    echo "This image contains explicit content";
} else {
    echo "This image is safe";
}

$result = $api->checkExplicitContent([
    'file_url' => 'https://example.com/image.jpg',
    'providers' => 'clarifai,google' // Optional, defaults to config
]);

// Get the average NSFW score across providers
$score = $result->getAverageScore();

// Check if the image is considered NSFW
$isNSFW = $result->isNSFW();

$result = $api->detectDeepfake([
    'file_url' => 'https://example.com/image.jpg',
    'providers' => 'sightengine' // Optional, defaults to config
]);

// Get the average deepfake score
$score = $result->getAverageScore();

// Check if the image is likely a deepfake
$isDeepfake = $result->isDeepfake();

$result = $api->detectObject([
    'file_url' => 'https://example.com/image.jpg',
    'providers' => 'google,amazon' // Optional, defaults to config
]);

// Get all detected items with confidence scores
$items = $result->getItems();

// Check for a specific object
$hasCar = $result->hasItem('car');

// Get confidence score for a specific object
$carConfidence = $result->item('car');

$result = $api->compareFace([
    'file1_url' => 'https://example.com/face1.jpg',
    'file2_url' => 'https://example.com/face2.jpg',
    'providers' => 'amazon' // Optional, defaults to config
]);

// Get average confidence of face matching
$confidence = $result->getAverageConfidence();

// Check if faces are considered the same person
$isMatched = $result->isCompared();

$result = $api->moderateText([
    'text' => 'Text to be moderated',
    'language' => 'en', // Optional, defaults to config
    'providers' => 'google' // Optional, defaults to config
]);

// Similar to explicit content detection for images
$score = $result->getAverageScore();
$isInappropriate = $result->isNSFW();

$result = $api->generateCode([
    'instruction' => 'Create a function that calculates the factorial of a number',
    'temperature' => 0.1, // Optional, defaults to config
    'max_tokens' => 500, // Optional, defaults to config
    'providers' => 'openai' // Optional, defaults to config
]);

// Get generated code from a specific provider
$code = $result->getResult('openai');

// Or get code from all providers
$allCode = $result->getResult();

$result = $api->textToSpeech([
    'text' => 'Hello, this is a test of text to speech',
    'language' => 'en', // Optional, defaults to config
    'option' => 'MALE', // Optional, defaults to config (MALE/FEMALE)
    'providers' => 'openai' // Optional, defaults to config
]);

// Get audio URL from a specific provider
$audioUrl = $result->getAudioResourceUrl('openai');

// Or get audio URLs from all providers
$allAudioUrls = $result->getAudioResourceUrl();

use EdenAI\Laravel\Facades\EdenAI;

// Use the facade
$result = EdenAI::checkExplicitContent([
    'file_url' => 'https://example.com/image.jpg'
]);

// config/edenai.php example
return [
    'token' => env('EDEN_AI_TOKEN', ''),
    'exclipt_content' => [
        'providers' => 'clarifai,google',
        'threshold' => 0.8
    ],
    'deepfake_detection' => [
        'providers' => 'sightengine',
        'threshold' => 0.8
    ],
    // Other configurations...
];

// Using multiple providers
$result = $api->detectObject([
    'file_url' => 'https://example.com/image.jpg',
    'providers' => 'google,amazon,microsoft'
]);

// Access individual provider results
$providers = $result->getProviders();
foreach ($providers as $providerName => $provider) {
    echo "Provider: $providerName\n";
    echo "Status: " . $provider['status'] . "\n";
    
    if ($provider['status'] === 'success') {
        // Access provider-specific data
        print_r($provider->getRawResponse());
    } else {
        // Handle error
        echo "Error: " . $provider['error']['message'] . "\n";
    }
}

// Get total cost of the operation
$cost = $result->getCost();

use EdenAI\Exceptions\EdenAIException;

try {
    $result = $api->checkExplicitContent([
        'file_url' => 'https://example.com/image.jpg'
    ]);
} catch (EdenAIException $e) {
    echo "Error: " . $e->getMessage() . "\n";
    echo "Code: " . $e->getCode() . "\n";
}

use EdenAI\Api;
use EdenAI\HttpClients\GuzzleHttpClient;
use GuzzleHttp\Client;

// Create custom Guzzle client with specific options
$guzzleClient = new Client([
    'proxy' => 'http://proxy.example.com:8125',
    'verify' => false,
]);

$httpClient = new GuzzleHttpClient($guzzleClient);

// Pass to API
$api = new Api('your-token', $httpClient);

// Or set after instantiation
$api->setHttpClientHandler($httpClient);

// Configure timeouts
$api->setTimeOut(120); // in seconds
$api->setConnectTimeOut(30); // in seconds

public function __construct(
    ?string $token = null, 
    ?HttpClientInterface $httpClientHandler = null, 
    ?string $baseUrl = null
)

public function setTimeOut(int $timeOut): self
public function setConnectTimeOut(int $connectTimeOut): self
public function getLastResponse(): ?EdenAIResponse

public function getCost(): float
public function getProviders(): array
public function getRawResponse(): mixed
bash
php artisan vendor:publish --tag="edenai-config"