定制 benbjurstrom/replicate-php 二次开发

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

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

benbjurstrom/replicate-php

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.

benbjurstrom/replicate-php 适用场景与选型建议

benbjurstrom/replicate-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 25.08k 次下载、GitHub Stars 达 39, 最近一次更新时间为 2023 年 03 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「package」 「php」 「replicate」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 benbjurstrom/replicate-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 benbjurstrom/replicate-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 benbjurstrom/replicate-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-03-13