marceloeatworld/replicate-php 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

marceloeatworld/replicate-php

最新稳定版本:v1.0.0

Composer 安装命令:

composer require marceloeatworld/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 MarceloEatWorld\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.

Create a prediction for a specific deployment

ou can now create a prediction for a specific deployment using the createForDeployment method. This method takes the full name of the deployment and an array of data as input, sends the HTTP request, and returns the data of the created prediction.

Here's an example of how to use it:

$deploymentName = 'my-deployment';
$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()->createForDeployment($deploymentName, $input);
$data->id; // yfv4cakjzvh2lexxv7o5qzymqy

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 MarceloEatWorld\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 MarceloEatWorld\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 MarceloEatWorld\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.

统计信息

  • 总下载量: 640
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 6
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-11-20