soiposervices / replicate-php
A PHP client for the Replicate API
Fund package maintenance!
benbjurstrom
Requires
- php: ^8.1.0
- saloonphp/saloon: ^3.0
Requires (Dev)
- laravel/pint: ^1.4
- pestphp/pest: ^2.0.0
- pestphp/pest-plugin-arch: 2.x-dev
- phpstan/phpstan: ^1.9.11
- symfony/var-dumper: ^6.2.3
README
This is a framework-agnostic PHP client for Replicate.com built on the amazing Saloon v3 🤠library. Use it to easily interact with machine learning models such as Stable Diffusion right from your PHP application.
Table of contents
🚀 Quick start
Install with composer.
composer require soiposervices/replicate-php
Create a new api instance.
use SoipoServices\Replicate\Replicate; ... $api = new Replicate( apiToken: $_ENV['REPLICATE_API_TOKEN'], );
Then use it to invoke your model (or in replicate terms "create a prediction").
$version = 'db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf'; $input = [ 'model' => 'stable-diffusion-2-1', 'prompt' => 'a photo of an astronaut riding a horse on mars', 'negative_prompt' => 'moon, alien, spaceship', 'width' => 768, 'height' => 768, 'num_inference_steps' => 50, 'guidance_scale' => 7.5, 'scheduler' => 'DPMSolverMultistep', 'seed' => null, ]; $data = $api->predictions()->create($version, $input); $data->id; // yfv4cakjzvh2lexxv7o5qzymqy
Note that the input parameters will vary depending on what version (model) you're using. In this example version db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf is a Stable Diffusion 2.1 model optimized for speed.
Using with Laravel
Begin by adding your credentials to your services config file.
// config/services.php 'replicate' => [ 'api_token' => env('REPLICATE_API_TOKEN'), ],
Bind the Replicate
class in a service provider.
// app/Providers/AppServiceProvider.php public function register() { $this->app->bind(Replicate::class, function () { return new Replicate( apiToken: config('services.replicate.api_token'), ); }); }
And use anywhere in your application.
$data = app(Replicate::class)->predictions()->get($id);
Test your integration using Saloon's amazing response recording.
use Saloon\Laravel\Saloon; // composer require sammyjo20/saloon-laravel "^2.0" ... Saloon::fake([ MockResponse::fixture('getPrediction'), ]); $id = 'yfv4cakjzvh2lexxv7o5qzymqy'; // The initial request will check if a fixture called "getPrediction" // exists. Because it doesn't exist yet, the real request will be // sent and the response will be recorded to tests/Fixtures/Saloon/getPrediction.json. $data = app(Replicate::class)->predictions()->get($id); // However, the next time the request is made, the fixture will // exist, and Saloon will not make the request again. $data = app(Replicate::class)->predictions()->get($id);
Response Data
All responses are returned as data objects. Detailed information can be found by inspecting the following class properties:
Webhooks
Replicate allows you to configure a webhook to be called when your prediction is complete. To do so chain withWebhook($url)
onto your api instance before calling the create
method. For example:
$api->predictions()->withWebhook('https://www.example.com/webhook')->create($version, $input); $data->id; // la5xlbbrfzg57ip5jlx6obmm5y
Available Prediction Methods
get()
Use to get details about an existing prediction. If the prediction has completed the results will be under the output property.
use SoipoServices\Replicate\Data\PredictionData; ... $id = 'la5xlbbrfzg57ip5jlx6obmm5y' /* @var PredictionData $data */ $data = $api->predictions()->get($id); $data->output[0]; // https://replicate.delivery/pbxt/6UFOVtl1xCJPAFFiTB2tfveYBNRLhLmJz8yMQAYCOeZSFhOhA/out-0.png
list()
Use to get a cursor paginated list of predictions. Returns an PredictionsData object.
use SoipoServices\Replicate\Data\PredictionsData ... /* @var PredictionsData $data */ $data = $api->predictions()->list( cursor: '123', // optional ); $data->results[0]->id; // la5xlbbrfzg57ip5jlx6obmm5y
create()
Use to create a new prediction (invoke a model). Returns an PredictionData object.
use SoipoServices\Replicate\Data\PredictionData; ... $version = '5c7d5dc6dd8bf75c1acaa8565735e7986bc5b66206b55cca93cb72c9bf15ccaa'; $input = [ 'text' => 'Alice' ]; /* @var PredictionData $data */ $data = $api->predictions() ->withWebhook('https://www.example.com/webhook') // optional ->create($version, $input); $data->id; // la5xlbbrfzg57ip5jlx6obmm5y
Credits
License
The MIT License (MIT). Please see License File for more information.
If you enjoy this, please consider supporting SoipoServices: