View Source Cheatsheet

This document is a cheatsheet on how to use the Replicate Elixir client.

models

Models

run-a-model

Run a model

Run a model with:

iex> Replicate.run("stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf", prompt: "a 19th century portrait of a wombat gentleman")

["https://replicate.com/api/models/stability-ai/stable-diffusion/files/50fcac81-865d-499e-81ac-49de0cb79264/out-0.png"]

run-a-model-in-the-background

Run a model in the background

You can start a model and run it in the background with Replicate.Predictions.create/5:

  iex> model = Replicate.Models.get!("stability-ai/stable-diffusion")
  iex> version = Replicate.Models.get_version!(model, "db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf")
  iex> {:ok, prediction} = Replicate.Predictions.create(version, %{prompt: "a 19th century portrait of a wombat gentleman"})
  iex> prediction
  %Replicate.Predictions.Prediction{
    id: "krdjxq6rw5bx3dopem52ohezca",
    error: nil,
    input: %{"prompt" => "a 19th century portrait of a wombat gentleman"},
    logs: "",
    output: nil,
    status: "starting",
    version: "db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf",
    started_at: nil,
    created_at: "2023-06-02T20:43:55.720751299Z",
    completed_at: nil
  }
  iex> {:ok, prediction} = Replicate.Predictions.get(prediction.id)

run-a-model-in-the-background-and-get-a-webhook

Run a model in the background and get a webhook

You can run a model and get a webhook when it completes, instead of waiting for it to finish:

Replicate.Predictions.create(version, %{prompt: "a 19th century portrait of a wombat gentleman"}, "https://example.com/webhook", ["completed"])

create-a-model

Create a model

You can create a model for a user or organization with a given name, visibility, and hardware SKU. To see a list of available hardware SKUs, run Replicate.Hardware.list/0.

iex> {:ok, model} =
        Replicate.Models.create(
        owner: "your-username",
        name: "my-model",
        visibility: "public",
        hardware: "gpu-a40-large"
        )

list-versions-of-a-model

List versions of a model

You can list all the versions of a model:

  iex> model = Replicate.Models.get!("stability-ai/stable-diffusion")
  iex> versions = Replicate.Models.list_versions(model)
  iex> Replicate.Models.list_versions(model) |> Enum.map(& &1.id) |> Enum.slice(0..5)
    ["db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf",
    "328bd9692d29d6781034e3acab8cf3fcb122161e6f5afb896a4ca9fd57090577",
    "f178fa7a1ae43a9a9af01b833b9d2ecf97b1bcb0acfd2dc5dd04895e042863f1",
    "0827b64897df7b6e8c04625167bbb275b9db0f14ab09e2454b9824141963c966",
    "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
    "8abccf52e7cba9f6e82317253f4a3549082e966db5584e92c808ece132037776"]

get-latest-version-of-a-model

Get latest version of a model

ELIXIR CLIENT EXCLUSIVE

Gets the latest version of a model. Raises an error if the version doesn't exist.

iex> model = Replicate.Models.get!("stability-ai/stable-diffusion")
iex> version = Replicate.Models.get_latest_version!(model)
iex> version.id
"db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf"
iex> version.cog_version
"0.6.0"

predictions

Predictions

cancel-a-prediction

Cancel a prediction

You can cancel a running prediction by passing an id or %Replicate.Predictions.Prediction{} to Replicate.Predictions.cancel/1:

  iex> model = Replicate.Models.get!("stability-ai/stable-diffusion")
  iex> version = Replicate.Models.get_version!(model, "db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf")
  iex> {:ok, prediction} = Replicate.Predictions.create(version, %{prompt: "Watercolor painting of the Babadook"})
  iex> prediction.status
  "starting"
  iex> {:ok, prediction} = Replicate.Predictions.cancel(prediction.id)
  iex> prediction.status
  "canceled"

  iex> model = Replicate.Models.get!("stability-ai/stable-diffusion")
  iex> version = Replicate.Models.get_version!(model, "db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf")
  iex> {:ok, prediction} = Replicate.Predictions.create(version, %{prompt: "a 19th century portrait of a wombat gentleman"})
  iex> {:ok, prediction} = Replicate.Predictions.cancel(prediction)
  iex> prediction.status
  "canceled"

list-predictions

List predictions

You can list all the predictions you've run:

iex> Replicate.Predictions.list()
[%Prediction{
id: "1234",
status: "starting",
input: %{"prompt" => "a 19th century portrait of a wombat gentleman"},
version: "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
output: ["https://replicate.com/api/models/stability-ai/stable-diffusion/files/50fcac81-865d-499e-81ac-49de0cb79264/out-0.png"]
},
%Prediction{
id: "1235",
status: "starting",
input: %{"prompt" => "a 19th century portrait of a wombat gentleman"},
version: "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
output: ["https://replicate.com/api/models/stability-ai/stable-diffusion/files/50fcac81-865d-499e-81ac-49de0cb79264/out-0.png"]}
]