PHP code example of halilcosdu / laravel-replicate

1. Go to this page and download the library: Download halilcosdu/laravel-replicate 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/ */

    

halilcosdu / laravel-replicate example snippets


return [
    'api_token' => env('REPLICATE_API_TOKEN'),
    'api_url' => env('REPLICATE_API_URL', 'https://api.replicate.com/v1'),
    'webhook_secret' => env('REPLICATE_WEBHOOK_SECRET'),
    'webhook_tolerance' => (int) env('REPLICATE_WEBHOOK_TOLERANCE', 300),
];

use HalilCosdu\Replicate\Facades\Replicate;

$prediction = Replicate::createOfficialModelPrediction(
    owner: 'black-forest-labs',
    name: 'flux-schnell',
    data: [
        'input' => ['prompt' => 'A quiet library floating above Istanbul'],
    ],
)->throw()->json();

$predictionId = $prediction['id'];

$response = Replicate::createPrediction([
    'version' => 'replicate/hello-world:version-id',
    'input' => ['text' => 'Laravel'],
]);

$prediction = $response->throw()->json();

$response = Replicate::createOfficialModelPrediction(
    'black-forest-labs',
    'flux-schnell',
    ['input' => ['prompt' => 'Minimal geometric poster']],
    ['Prefer' => 'wait=30', 'Cancel-After' => '2m'],
);

$prediction = $response->throw()->json();

$prediction = Replicate::getPrediction($predictionId)->throw()->json();

$page = Replicate::listPredictions([
    'created_after' => now()->subDay()->toIso8601String(),
    'source' => 'web',
    'cursor' => $cursor,
])->throw()->json();

$stream = fopen(storage_path('app/training-images.zip'), 'rb');

throw_if($stream === false, RuntimeException::class, 'Unable to open the upload.');

try {
    $file = Replicate::createFile(
        contents: $stream,
        filename: 'training-images.zip',
        contentType: 'application/zip',
        metadata: ['dataset' => 'product-photography'],
    )->throw()->json();
} finally {
    fclose($stream);
}

$secret = Replicate::defaultSecret()->throw()->json('key');

use HalilCosdu\Replicate\Facades\Replicate;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::post('/webhooks/replicate', function (Request $request) {
    abort_unless(Replicate::verifyWebhook($request), 401);

    $prediction = $request->json()->all();

    // Persist output, dispatch a job, or continue a model pipeline...

    return response()->noContent();
});

$valid = Replicate::verifyWebhook($request, $secret, tolerance: 120);

Replicate::account();
Replicate::search(string $query, ?int $limit = null);
Replicate::listHardware();

Replicate::listCollections(array $query = []);
Replicate::getCollection(string $slug);

Replicate::listDeployments(array $query = []);
Replicate::createDeployment(array $data);
Replicate::getDeployment(string $owner, string $name);
Replicate::updateDeployment(string $owner, string $name, array $data);
Replicate::deleteDeployment(string $owner, string $name);
Replicate::createDeploymentPrediction(string $owner, string $name, array $data, array $headers = []);

Replicate::createFile(mixed $contents, string $filename, string $contentType = 'application/octet-stream', array $metadata = []);
Replicate::listFiles(array $query = []);
Replicate::getFile(string $id);
Replicate::deleteFile(string $id);
Replicate::downloadFile(string $id, string $owner, int $expiry, string $signature);

Replicate::listModels(array $query = []);
Replicate::createModel(array $data);
Replicate::getModel(string $owner, string $name);
Replicate::updateModel(string $owner, string $name, array $data);
Replicate::deleteModel(string $owner, string $name);
Replicate::getModelReadme(string $owner, string $name);
Replicate::searchModels(string $query);
Replicate::listModelExamples(string $owner, string $name, array $query = []);
Replicate::listModelVersions(string $owner, string $name);
Replicate::getModelVersion(string $owner, string $name, string $version);
Replicate::deleteModelVersion(string $owner, string $name, string $version);

Replicate::createPrediction(array $data, array $headers = []);
Replicate::createOfficialModelPrediction(string $owner, string $name, array $data, array $headers = []);
Replicate::getPrediction(string $id);
Replicate::listPredictions(array $query = []);
Replicate::cancelPrediction(string $id);

Replicate::listTrainings(array $query = []);
Replicate::createTraining(string $owner, string $name, string $version, array $data);
Replicate::getTraining(string $id);
Replicate::cancelTraining(string $id);

Replicate::defaultSecret();
Replicate::verifyWebhook(Request $request, ?string $secret = null, ?int $tolerance = null);

$response = Replicate::getPrediction($predictionId);

if ($response->tooManyRequests()) {
    // Release the job using Retry-After or your own backoff policy.
}

$prediction = $response->throw()->json();
bash
php artisan vendor:publish --tag=replicate-config