├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── SECURITY.md ├── composer.json ├── config └── replicate.php └── src ├── Facades └── Replicate.php ├── Replicate.php ├── ReplicateServiceProvider.php └── Services ├── AuthService.php ├── CollectionService.php ├── DeploymentService.php ├── HardwareService.php ├── ModelService.php ├── PredictionService.php ├── TrainingService.php └── WebhookService.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to `laravel-replicate` will be documented in this file. 4 | 5 | ## v1.0.2 - 2024-04-27 6 | 7 | **Full Changelog**: https://github.com/halilcosdu/laravel-replicate/compare/v1.0.0...v1.0.2 8 | 9 | ## v1.0.0 - 2024-04-27 10 | 11 | ### What's Changed 12 | 13 | * Bump dependabot/fetch-metadata from 1.6.0 to 2.1.0 by @dependabot in https://github.com/halilcosdu/laravel-replicate/pull/1 14 | 15 | ### New Contributors 16 | 17 | * @dependabot made their first contribution in https://github.com/halilcosdu/laravel-replicate/pull/1 18 | 19 | **Full Changelog**: https://github.com/halilcosdu/laravel-replicate/commits/v1.0.0 20 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) HalilCosdu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Replicate Laravel PHP client 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/halilcosdu/laravel-replicate.svg?style=flat-square)](https://packagist.org/packages/halilcosdu/laravel-replicate) 4 | [![GitHub Tests Action Status](https://img.shields.io/github/actions/workflow/status/halilcosdu/laravel-replicate/run-tests.yml?branch=main&label=tests&style=flat-square)](https://github.com/halilcosdu/laravel-replicate/actions?query=workflow%3Arun-tests+branch%3Amain) 5 | [![GitHub Code Style Action Status](https://img.shields.io/github/actions/workflow/status/halilcosdu/laravel-replicate/fix-php-code-style-issues.yml?branch=main&label=code%20style&style=flat-square)](https://github.com/halilcosdu/laravel-replicate/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain) 6 | [![Total Downloads](https://img.shields.io/packagist/dt/halilcosdu/laravel-replicate.svg?style=flat-square)](https://packagist.org/packages/halilcosdu/laravel-replicate) 7 | 8 | The halilcosdu/laravel-replicate package is a Laravel client for the Replicate API. It provides a convenient way to interact with the Replicate API using PHP and Laravel's Facade pattern. The package includes methods for managing and interacting with various aspects of the Replicate service, such as accounts, collections, deployments, hardware, models, predictions, and trainings. For example, you can list all collections, get a specific collection, create a new deployment, get or update an existing deployment, list all hardware, create a new model, get a specific model or its version, list all versions of a model, delete a model version, list all models, create a prediction, get or cancel a prediction, list all predictions, list all trainings, create a new training, get or cancel a training, get the default secret, and create a deployment or model prediction. The package is configurable via Laravel's configuration system, allowing you to set your API token and URL through your environment file. 9 | ## Installation 10 | 11 | You can install the package via composer: 12 | 13 | ```bash 14 | composer require halilcosdu/laravel-replicate 15 | ``` 16 | 17 | You can publish the config file with: 18 | 19 | ```bash 20 | php artisan vendor:publish --tag="replicate-config" 21 | ``` 22 | 23 | This is the contents of the published config file: 24 | 25 | ```php 26 | return [ 27 | 'api_token' => env('REPLICATE_API_TOKEN'), 28 | 'api_url' => env('REPLICATE_API_URL', 'https://api.replicate.com/v1'), 29 | ]; 30 | ``` 31 | 32 | ## Usage 33 | ```php 34 | Replicate::account() 35 | Replicate::getCollection(string $slug) 36 | Replicate::listCollections() 37 | Replicate::listDeployments() 38 | Replicate::createDeployment(array $data) 39 | Replicate::getDeployment(string $owner, string $name) 40 | Replicate::updateDeployment(string $owner, string $name, array $data) 41 | Replicate::listHardware() 42 | Replicate::createModel(array $data) 43 | Replicate::getModel(string $owner, string $name) 44 | Replicate::getModelVersion(string $owner, string $name, string $version) 45 | Replicate::listModelVersions(string $owner, string $name) 46 | Replicate::deleteModelVersion(string $owner, string $name, string $version) 47 | Replicate::listModels() 48 | Replicate::createPrediction(array $data) 49 | Replicate::getPrediction(string $id) 50 | Replicate::cancelPrediction($id) 51 | Replicate::listPredictions() 52 | Replicate::listTrainings() 53 | Replicate::createTraining(string $owner, string $name, string $version, array $data) 54 | Replicate::getTraining(string $id) 55 | Replicate::cancelTraining($id) 56 | Replicate::defaultSecret() 57 | Replicate::createDeploymentPrediction(string $owner, string $name, array $data) 58 | Replicate::createModelPrediction(string $owner, string $name, string $version, array $data) 59 | ``` 60 | #### Reference: https://replicate.com/docs/reference/http 61 | 62 | ## Example 63 | 64 | ```php 65 | use HalilCosdu\Replicate\Facades\Replicate; 66 | 67 | $response = Replicate::account(); 68 | ``` 69 | 70 | ```bash 71 | $response->body() : string; 72 | $response->json($key = null, $default = null) : mixed; 73 | $response->object() : object; 74 | $response->collect($key = null) : Illuminate\Support\Collection; 75 | $response->status() : int; 76 | $response->successful() : bool; 77 | $response->redirect(): bool; 78 | $response->failed() : bool; 79 | $response->clientError() : bool; 80 | $response->header($header) : string; 81 | $response->headers() : array; 82 | ``` 83 | In addition to the response methods listed above, the following methods may be used to determine if the response has a given status code: 84 | ```bash 85 | $response->ok() : bool; // 200 OK 86 | $response->created() : bool; // 201 Created 87 | $response->accepted() : bool; // 202 Accepted 88 | $response->noContent() : bool; // 204 No Content 89 | $response->movedPermanently() : bool; // 301 Moved Permanently 90 | $response->found() : bool; // 302 Found 91 | $response->badRequest() : bool; // 400 Bad Request 92 | $response->unauthorized() : bool; // 401 Unauthorized 93 | $response->paymentRequired() : bool; // 402 Payment Required 94 | $response->forbidden() : bool; // 403 Forbidden 95 | $response->notFound() : bool; // 404 Not Found 96 | $response->requestTimeout() : bool; // 408 Request Timeout 97 | $response->conflict() : bool; // 409 Conflict 98 | $response->unprocessableEntity() : bool; // 422 Unprocessable Entity 99 | $response->tooManyRequests() : bool; // 429 Too Many Requests 100 | $response->serverError() : bool; // 500 Internal Server Error 101 | 102 | ``` 103 | 104 | ## Testing 105 | 106 | ```bash 107 | composer test 108 | ``` 109 | 110 | ## Changelog 111 | 112 | Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. 113 | 114 | ## Contributing 115 | 116 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details. 117 | 118 | ## Security Vulnerabilities 119 | 120 | Please review [our security policy](../../security/policy) on how to report security vulnerabilities. 121 | 122 | ## Credits 123 | 124 | - [Halil Cosdu](https://github.com/halilcosdu) 125 | - [All Contributors](../../contributors) 126 | 127 | ## License 128 | 129 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 130 | -------------------------------------------------------------------------------- /SECURITY.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | ## Supported Versions 4 | 5 | Use this section to tell people about which versions of your project are currently being supported with security updates. 6 | 7 | | Version | Supported | 8 | |---------| ------------------ | 9 | | 1.0.x | :white_check_mark: | 10 | 11 | ## Reporting a Vulnerability 12 | 13 | Use this section to tell people how to report a vulnerability. 14 | 15 | If you discover a security vulnerability within this project, please send an email to halilcosdu@gmail.com. All security vulnerabilities will be promptly addressed. 16 | 17 | Please do not publicly disclose the issue until it has been addressed by the team. 18 | 19 | ## Security Updates 20 | 21 | Use this section to tell people where and how they will receive updates about security. 22 | 23 | We will push the updates for security vulnerabilities to the main branch of this repository. We recommend always using the latest stable version of the project. 24 | 25 | ## More Information 26 | 27 | If you have any other questions, please contact us at halilcosdu@gmail.com. 28 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "halilcosdu/laravel-replicate", 3 | "description": "Replicate Laravel PHP client", 4 | "keywords": [ 5 | "HalilCosdu", 6 | "laravel", 7 | "laravel-replicate" 8 | ], 9 | "homepage": "https://github.com/halilcosdu/laravel-replicate", 10 | "license": "MIT", 11 | "authors": [ 12 | { 13 | "name": "Halil Cosdu", 14 | "email": "halilcosdu@gmail.com", 15 | "role": "Developer" 16 | } 17 | ], 18 | "require": { 19 | "php": "^8.2", 20 | "spatie/laravel-package-tools": "^1.16", 21 | "illuminate/contracts": "^10.0||^11.0" 22 | }, 23 | "require-dev": { 24 | "laravel/pint": "^1.14", 25 | "nunomaduro/collision": "^8.1.1||^7.10.0", 26 | "larastan/larastan": "^2.9", 27 | "orchestra/testbench": "^9.0.0||^8.22.0", 28 | "pestphp/pest": "^2.34", 29 | "pestphp/pest-plugin-arch": "^2.7", 30 | "pestphp/pest-plugin-laravel": "^2.3", 31 | "phpstan/extension-installer": "^1.3", 32 | "phpstan/phpstan-deprecation-rules": "^1.1", 33 | "phpstan/phpstan-phpunit": "^1.3", 34 | "spatie/laravel-ray": "^1.35" 35 | }, 36 | "autoload": { 37 | "psr-4": { 38 | "HalilCosdu\\Replicate\\": "src/", 39 | "HalilCosdu\\Replicate\\Database\\Factories\\": "database/factories/" 40 | } 41 | }, 42 | "autoload-dev": { 43 | "psr-4": { 44 | "HalilCosdu\\Replicate\\Tests\\": "tests/", 45 | "Workbench\\App\\": "workbench/app/" 46 | } 47 | }, 48 | "scripts": { 49 | "post-autoload-dump": "@composer run prepare", 50 | "clear": "@php vendor/bin/testbench package:purge-laravel-replicate --ansi", 51 | "prepare": "@php vendor/bin/testbench package:discover --ansi", 52 | "build": [ 53 | "@composer run prepare", 54 | "@php vendor/bin/testbench workbench:build --ansi" 55 | ], 56 | "start": [ 57 | "Composer\\Config::disableProcessTimeout", 58 | "@composer run build", 59 | "@php vendor/bin/testbench serve" 60 | ], 61 | "analyse": "vendor/bin/phpstan analyse", 62 | "test": "vendor/bin/pest", 63 | "test-coverage": "vendor/bin/pest --coverage", 64 | "format": "vendor/bin/pint" 65 | }, 66 | "config": { 67 | "sort-packages": true, 68 | "allow-plugins": { 69 | "pestphp/pest-plugin": true, 70 | "phpstan/extension-installer": true 71 | } 72 | }, 73 | "extra": { 74 | "laravel": { 75 | "providers": [ 76 | "HalilCosdu\\Replicate\\ReplicateServiceProvider" 77 | ], 78 | "aliases": { 79 | "Replicate": "HalilCosdu\\Replicate\\Facades\\Replicate" 80 | } 81 | } 82 | }, 83 | "minimum-stability": "dev", 84 | "prefer-stable": true 85 | } 86 | -------------------------------------------------------------------------------- /config/replicate.php: -------------------------------------------------------------------------------- 1 | env('REPLICATE_API_TOKEN'), 6 | 'api_url' => env('REPLICATE_API_URL', 'https://api.replicate.com/v1'), 7 | ]; 8 | -------------------------------------------------------------------------------- /src/Facades/Replicate.php: -------------------------------------------------------------------------------- 1 | authService->account(); 32 | } 33 | 34 | public function getCollection(string $slug) 35 | { 36 | return $this->collectionService->get($slug); 37 | } 38 | 39 | public function listCollections() 40 | { 41 | return $this->collectionService->list(); 42 | } 43 | 44 | public function listDeployments() 45 | { 46 | return $this->deploymentService->list(); 47 | } 48 | 49 | public function createDeployment(array $data) 50 | { 51 | return $this->deploymentService->create($data); 52 | } 53 | 54 | public function getDeployment(string $owner, string $name) 55 | { 56 | return $this->deploymentService->get($owner, $name); 57 | } 58 | 59 | public function updateDeployment(string $owner, string $name, array $data) 60 | { 61 | return $this->deploymentService->update($owner, $name, $data); 62 | } 63 | 64 | public function listHardware() 65 | { 66 | return $this->hardwareService->list(); 67 | } 68 | 69 | public function createModel(array $data) 70 | { 71 | return $this->modelService->create($data); 72 | } 73 | 74 | public function getModel(string $owner, string $name) 75 | { 76 | return $this->modelService->get($owner, $name); 77 | } 78 | 79 | public function getModelVersion(string $owner, string $name, string $version) 80 | { 81 | return $this->modelService->getVersion($owner, $name, $version); 82 | } 83 | 84 | public function listModelVersions(string $owner, string $name) 85 | { 86 | return $this->modelService->listVersions($owner, $name); 87 | } 88 | 89 | public function deleteModelVersion(string $owner, string $name, string $version) 90 | { 91 | return $this->modelService->deleteVersion($owner, $name, $version); 92 | } 93 | 94 | public function deleteModel(string $owner, string $name) 95 | { 96 | return $this->modelService->delete($owner, $name); 97 | } 98 | 99 | public function listModels() 100 | { 101 | return $this->modelService->list(); 102 | } 103 | 104 | public function createPrediction(array $data) 105 | { 106 | return $this->predictionService->create($data); 107 | } 108 | 109 | public function getPrediction(string $id) 110 | { 111 | return $this->predictionService->get($id); 112 | } 113 | 114 | public function cancelPrediction($id) 115 | { 116 | return $this->predictionService->cancel($id); 117 | } 118 | 119 | public function listPredictions() 120 | { 121 | return $this->predictionService->list(); 122 | } 123 | 124 | public function listTrainings() 125 | { 126 | return $this->trainingService->list(); 127 | } 128 | 129 | public function createTraining(string $owner, string $name, string $version, array $data) 130 | { 131 | return $this->trainingService->create($owner, $name, $version, $data); 132 | } 133 | 134 | public function getTraining(string $id) 135 | { 136 | return $this->trainingService->get($id); 137 | } 138 | 139 | public function cancelTraining($id) 140 | { 141 | return $this->trainingService->cancel($id); 142 | } 143 | 144 | public function defaultSecret() 145 | { 146 | return $this->webhookService->defaultSecret(); 147 | } 148 | 149 | public function createDeploymentPrediction(string $owner, string $name, array $data) 150 | { 151 | return $this->predictionService->createDeploymentPrediction($owner, $name, $data); 152 | } 153 | 154 | public function createModelPrediction(string $owner, string $name, string $version, array $data) 155 | { 156 | return $this->predictionService->createModelPrediction($owner, $name, $version, $data); 157 | } 158 | } 159 | -------------------------------------------------------------------------------- /src/ReplicateServiceProvider.php: -------------------------------------------------------------------------------- 1 | name('laravel-replicate') 20 | ->hasConfigFile(); 21 | } 22 | 23 | public function packageRegistered(): void 24 | { 25 | Http::macro('replicate', function () { 26 | return Http::withHeaders([ 27 | 'Accept' => 'application/json', 28 | ])->withToken(config('replicate.api_token')) 29 | ->baseUrl(config('replicate.api_url')); 30 | }); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/Services/AuthService.php: -------------------------------------------------------------------------------- 1 | get('/account'); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Services/CollectionService.php: -------------------------------------------------------------------------------- 1 | get("/collections/$slug"); 15 | } 16 | 17 | /* 18 | * https://replicate.com/docs/reference/http#collections.list 19 | */ 20 | public function list() 21 | { 22 | return Http::replicate()->get('/collections'); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/Services/DeploymentService.php: -------------------------------------------------------------------------------- 1 | get('/deployments'); 15 | } 16 | 17 | /* 18 | * https://replicate.com/docs/reference/http#deployments.create 19 | */ 20 | public function create(array $data) 21 | { 22 | return Http::replicate()->post('/deployments', $data); 23 | } 24 | 25 | /* 26 | * https://replicate.com/docs/reference/http#deployments.get 27 | */ 28 | public function get(string $owner, string $name) 29 | { 30 | return Http::replicate()->get("/deployments/$owner/$name"); 31 | } 32 | 33 | /* 34 | * https://replicate.com/docs/reference/http#deployments.update 35 | */ 36 | public function update(string $owner, string $name, array $data) 37 | { 38 | return Http::replicate()->patch("/deployments/$owner/$name", $data); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Services/HardwareService.php: -------------------------------------------------------------------------------- 1 | get('/hardware'); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/Services/ModelService.php: -------------------------------------------------------------------------------- 1 | post('/models', $data); 15 | } 16 | 17 | /* 18 | * https://replicate.com/docs/reference/http#models.get 19 | */ 20 | public function get(string $owner, string $name) 21 | { 22 | return Http::replicate()->get("/models/$owner/$name"); 23 | } 24 | 25 | /* 26 | * https://replicate.com/docs/reference/http#models.versions.get 27 | */ 28 | public function getVersion(string $owner, string $name, string $version) 29 | { 30 | return Http::replicate()->get("/models/$owner/$name/versions/$version"); 31 | } 32 | 33 | /* 34 | * https://replicate.com/docs/reference/http#models.versions.list 35 | */ 36 | public function listVersions(string $owner, string $name) 37 | { 38 | return Http::replicate()->get("/models/$owner/$name/versions"); 39 | } 40 | 41 | /* 42 | * https://replicate.com/docs/reference/http#models.versions.delete 43 | */ 44 | public function deleteVersion(string $owner, string $name, string $version) 45 | { 46 | return Http::replicate()->delete("/models/$owner/$name/versions/$version"); 47 | } 48 | 49 | /* 50 | * https://replicate.com/docs/reference/http#models.delete 51 | */ 52 | public function delete(string $owner, string $name) 53 | { 54 | return Http::replicate()->delete("/models/$owner/$name"); 55 | } 56 | 57 | /* 58 | * https://replicate.com/docs/reference/http#models.list 59 | */ 60 | public function list() 61 | { 62 | return Http::replicate()->get('/models'); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Services/PredictionService.php: -------------------------------------------------------------------------------- 1 | post('/predictions', $data); 15 | } 16 | 17 | /* 18 | * https://replicate.com/docs/reference/http#predictions.get 19 | */ 20 | public function get(string $id) 21 | { 22 | return Http::replicate()->get("/predictions/$id"); 23 | } 24 | 25 | /* 26 | * https://replicate.com/docs/reference/http#predictions.list 27 | */ 28 | public function list() 29 | { 30 | return Http::replicate()->get('/predictions'); 31 | } 32 | 33 | /* 34 | * https://replicate.com/docs/reference/http#predictions.cancel 35 | */ 36 | public function cancel($id) 37 | { 38 | return Http::replicate()->post("/predictions/$id/cancel"); 39 | } 40 | 41 | /* 42 | * https://replicate.com/docs/reference/http#deployments.predictions.create 43 | */ 44 | public function createDeploymentPrediction(string $owner, string $name, array $data) 45 | { 46 | return Http::replicate()->post("/deployments/$owner/$name/predictions", $data); 47 | } 48 | 49 | /* 50 | * https://replicate.com/docs/reference/http#models.predictions.create 51 | */ 52 | public function createModelPrediction(string $owner, string $name, string $version, array $data) 53 | { 54 | return Http::replicate()->post("/models/$owner/$name/predictions", $data); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/Services/TrainingService.php: -------------------------------------------------------------------------------- 1 | post("/models/$owner/$name/versions/$version/trainings", $data); 15 | } 16 | 17 | /* 18 | * https://replicate.com/docs/reference/http#trainings.get 19 | */ 20 | public function get(string $id) 21 | { 22 | return Http::replicate()->get("/trainings/$id"); 23 | } 24 | 25 | /* 26 | * https://replicate.com/docs/reference/http#trainings.list 27 | */ 28 | public function list() 29 | { 30 | return Http::replicate()->get('/trainings'); 31 | } 32 | 33 | /* 34 | * https://replicate.com/docs/reference/http#trainings.cancel 35 | */ 36 | public function cancel($id) 37 | { 38 | return Http::replicate()->post("/trainings/$id/cancel"); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Services/WebhookService.php: -------------------------------------------------------------------------------- 1 | get('/webhooks/default/secret'); 15 | } 16 | } 17 | --------------------------------------------------------------------------------