定制 benbjurstrom/replicate-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

benbjurstrom/replicate-php

最新稳定版本:v0.3.0

Composer 安装命令:

composer require benbjurstrom/replicate-php

包简介

A PHP client for the Replicate API

README 文档

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.

Latest Version on Packagist GitHub Tests Action Status

Table of contents

???? Quick start

Install with composer.

composer require benbjurstrom/replicate-php

Create a new api instance.

use BenBjurstrom\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 BenBjurstrom\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 BenBjurstrom\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 BenBjurstrom\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.

统计信息

  • 总下载量: 18.66k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 39
  • 点击次数: 1
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 39
  • Watchers: 1
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 未知

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固