arikislam/replicate-php-client
Composer 安装命令:
composer require arikislam/replicate-php-client
包简介
A PHP client for the Replicate API
README 文档
README
A simple PHP client for interacting with the Replicate API, enabling the ability to run machine learning models from your PHP applications. This library provides a clean and reusable way to interact with the API in a Laravel environment but can also be used in other PHP applications.
Features
- Simple API client for Replicate
- Supports Laravel out of the box
- Uses Guzzle HTTP client for HTTP requests
- Allows easy integration with machine learning models using Replicate
Installation
To install the package, simply use Composer:
composer require arikislam/replicate-php-client
Laravel Configuration
For Laravel applications, the package will automatically register the ReplicatePhpClientServiceProvider and the Replicate alias.
You can publish the config file with:
php artisan vendor:publish --provider="Arikislam\ReplicatePhpClient\Providers\ReplicatePhpClientServiceProvider"
Usage
Configuration
To use this package, you must have an API key from Replicate.
Set up the API key in your environment file:
REPLICATE_API_KEY=your_api_key_here
Example Request
To create an inference using the Replicate API, use the Replicate facade in your application as shown below:
use Arikislam\ReplicatePhpClient\Replicate; $response = Replicate::createPrediction([ 'input' => [ 'prompt' => 'Test', 'go_fast' => true, 'num_outputs' => 1, 'aspect_ratio' => '16:9', 'output_format' => 'png', 'output_quality' => 80, 'megapixels' => '.25', ], ]); // Get the prediction results $predictionResult = $response->getResult();
This example demonstrates how to send a request to the Replicate API with the required parameters.
Detailed Breakdown
1. Input
The input parameter specifies the model input options. In this case:
prompt: The text prompt for generating the image.go_fast: A boolean indicating whether the model should prioritize speed over quality.num_outputs: The number of outputs required from the model.aspect_ratio: The aspect ratio of the output (e.g., 16:9).output_format: The format of the generated output (e.g.,png).output_quality: The quality of the output (percentage-based, 80 means 80% quality).megapixels: Controls the megapixel size for the image (e.g., .25).
2. Webhooks
Although webhooks can be used to track the progress of asynchronous tasks, this package allows you to specify a webhook URL and webhook_events_filter to listen for certain events. Currently, the webhook parameter is set to null in the example for simplicity.
3. API Version
The version is an identifier for the version of the Replicate API being used. You can specify which model version you are targeting by using the correct version hash.
Response Handling
The response from the API will contain details of the prediction, including the generated content or the status of the request. You can handle the response as follows:
if ($response->isSuccessful()) { $output = $response->getResult(); // Do something with the output, such as saving the image or displaying it } else { // Handle errors $errorMessage = $response->getErrorMessage(); // Log or display the error message }
Testing
This package includes support for unit testing. You can run tests using PHPUnit:
vendor/bin/phpunit
Advanced Usage
Custom HTTP Client
You can provide your own custom HTTP client if needed by extending the Guzzle client. For example:
use GuzzleHttp\Client; use Arikislam\ReplicatePhpClient\Replicate; $client = new Client([ 'base_uri' => 'https://api.replicate.com', 'timeout' => 10.0, ]); $replicate = new Replicate($client); $response = $replicate->createPrediction([ // input options ]);
Facades in Laravel
For Laravel users, the Replicate facade provides a convenient way to interact with the API. However, if you are not using Laravel, you can directly instantiate the Replicate class and make API requests.
Contributing
Contributions are welcome! Please submit a pull request or open an issue to contribute to the development of this package.
License
This package is licensed under the MIT License. See the LICENSE file for more information.
Author
This package is maintained by S M Ariq Islam.
arikislam/replicate-php-client 适用场景与选型建议
arikislam/replicate-php-client 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 145 次下载、GitHub Stars 达 0, 最近一次更新时间为 2024 年 10 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 arikislam/replicate-php-client 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 arikislam/replicate-php-client 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 145
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-17