PHP code example of marceloeatworld / replicate-php
1. Go to this page and download the library: Download marceloeatworld/replicate-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
marceloeatworld / replicate-php example snippets
use MarceloEatWorld\Replicate\Replicate;
$replicate = new Replicate(
apiToken: $_ENV['REPLICATE_API_TOKEN'],
);
$prediction = $replicate->predictions()->create(
version: 'stability-ai/sdxl:c221b2b8ef527988fb59bf24a8b97c4561f1c671f73bd389f866bfb27c061316',
input: ['prompt' => 'a photo of an astronaut riding a horse on mars'],
);
$prediction->id; // "xyz123"
$prediction->status; // "starting"
$prediction = $replicate->models()->createPrediction(
owner: 'meta',
name: 'meta-llama-3-70b-instruct',
input: ['prompt' => 'Write a haiku about PHP'],
);
$prediction = $replicate->predictions()->create(
version: 'stability-ai/sdxl:c221b2b8ef527988fb59bf24a8b97c4561f1c671f73bd389f866bfb27c061316',
input: ['prompt' => 'a painting of a cat'],
wait: 60, // wait up to 60 seconds for completion
);
if ($prediction->status === 'succeeded') {
$prediction->output; // result is ready
}
$prediction = $replicate->predictions()->create(
version: 'owner/model:version',
input: ['prompt' => 'hello'],
stream: true,
);
// If the model supports streaming, use the stream URL
$prediction->urls['stream']; // SSE endpoint URL
// List public models
$models = $replicate->models()->list();
// Get a model
$model = $replicate->models()->get('stability-ai', 'sdxl');
// Create a model
$model = $replicate->models()->create(
owner: 'your-username',
name: 'my-model',
hardware: 'gpu-a40-large',
visibility: 'private',
);
// Update a model
$model = $replicate->models()->update('your-username', 'my-model', [
'description' => 'Updated description',
]);
// Delete a model (must be private, no versions)
$replicate->models()->delete('your-username', 'my-model');
// List deployments
$deployments = $replicate->deployments()->list();
// Get a deployment
$deployment = $replicate->deployments()->get('your-username', 'my-deployment');
// Create a deployment
$deployment = $replicate->deployments()->create(
name: 'my-deployment',
model: 'your-username/my-model',
version: 'abc123...',
hardware: 'gpu-a40-large',
minInstances: 1,
maxInstances: 3,
);
// Update a deployment
$deployment = $replicate->deployments()->update('your-username', 'my-deployment', [
'min_instances' => 2,
'max_instances' => 5,
]);
// Create prediction on a deployment
$prediction = $replicate->deployments()->createPrediction(
owner: 'your-username',
name: 'my-deployment',
input: ['prompt' => 'hello world'],
);
// Delete a deployment
$replicate->deployments()->delete('your-username', 'my-deployment');
// Create a training
$training = $replicate->trainings()->create(
owner: 'stability-ai',
name: 'sdxl',
versionId: 'abc123...',
destination: 'your-username/my-trained-model',
input: ['train_data' => 'https://example.com/data.zip'],
webhook: 'https://example.com/training-done',
);
// Get training status
$training = $replicate->trainings()->get($training->id);
// List trainings
$trainings = $replicate->trainings()->list();
// Cancel a training
$replicate->trainings()->cancel($training->id);
// Upload a file
$file = $replicate->files()->upload(
content: file_get_contents('/path/to/image.jpg'),
filename: 'image.jpg',
contentType: 'image/jpeg',
);
// Get file metadata
$file = $replicate->files()->get($file->id);
// List files
$files = $replicate->files()->list();
// Delete a file
$replicate->files()->delete($file->id);
// List collections
$collections = $replicate->collections()->list();
// Get a collection with its models
$collection = $replicate->collections()->get('text-to-image');
$collection->models; // array of ModelData
// List available hardware
$hardware = $replicate->hardware()->list();
// Returns array of HardwareData with name and sku
$account = $replicate->account()->get();
$account->username;
$account->type; // "user" or "organization"