Replicate Ruby client
This is a Ruby client for Replicate. It lets you run models from your Ruby code and do various other things on Replicate.
Installation
Add this line to your application's Gemfile:
gem 'replicate-ruby'
Usage
Grab your token from replicate.com/account and authenticate by configuring api_token
:
Replicate.configure do |config|
config.api_token = "your_api_token"
end
You can retrieve a model:
model = Replicate.client.retrieve_model("stability-ai/stable-diffusion")
version = model.latest_version
version = Replicate.client.retrieve_model("stability-ai/stable-diffusion", version: :all)
version = Replicate.client.retrieve_model("stability-ai/stable-diffusion", version: "<id>")
And then run predictions on it:
prediction = version.predict(prompt: "a handsome teddy bear")
prediction = prediction.refetch
prediction = prediction.cancel
output = prediction.output
prediction = version.predict(prompt: "a handsome teddy bear", "https://webhook.url/path")
id = prediction.id
prediction = Replicate.client.retrieve_prediction(id)
Dreambooth
There is support for the experimental dreambooth endpoint.
First, upload your training dataset:
upload = Replicate.client.upload_zip('tmp/data.zip')
Then start training a new model using, for instance:
training = Replicate.client.create_training(
input: {
instance_prompt: "zwx style",
class_prompt: "style",
instance_data: upload.serving_url,
max_train_steps: 5000
},
model: 'yourusername/yourmodel'
)
As soon as the model has finished training, you can run predictions on it:
prediction = Replicate.client.create_prediction(
input: {
prompt: 'your prompt, zwx style'
},
version: training.version
)
You can also download the output.zip
file from the dreambooth training prediction, unzip it, and then convert the trained model to a Stable Diffusion checkpoint with convert_diffusers_to_sd.py.
python ./convert_diffusers_to_sd.py --model_path ~/Downloads/output --checkpoint_path ~/Downloads/output.ckpt
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake test
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.