Skip to content

Commit 94dffa2

Browse files
committed
add cheatsheet
1 parent a11c142 commit 94dffa2

File tree

3 files changed

+158
-2
lines changed

3 files changed

+158
-2
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,11 @@ config :replicate,
5353

5454
```
5555

56+
Now you can use `Replicate` to do cool machine learny stuff.
5657

5758
## Run a model
5859

59-
Now you can use `Replicate` to do cool machine learny stuff.
60+
You can run a model:
6061

6162
```elixir
6263
iex> Replicate.run("stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf", prompt: "a 19th century portrait of a wombat gentleman")
@@ -123,6 +124,7 @@ You can run a model and get a webhook when it completes, instead of waiting for
123124
Replicate.Predictions.create(version, %{prompt: "a 19th century portrait of a wombat gentleman"}, "https://example.com/webhook", ["completed"])
124125
```
125126

127+
126128
## Cancel a prediction
127129

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

cheatsheet.cheatmd

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
# Cheatsheet
2+
3+
This document is a cheatsheet on how to use the [Replicate Elixir client](https://hexdocs.pm/replicate/readme.html).
4+
5+
## Models
6+
{: .col-2}
7+
8+
9+
### Run a model
10+
11+
Run a model with:
12+
13+
```elixir
14+
iex> Replicate.run("stability-ai/stable-diffusion:db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf", prompt: "a 19th century portrait of a wombat gentleman")
15+
16+
["https://replicate.com/api/models/stability-ai/stable-diffusion/files/50fcac81-865d-499e-81ac-49de0cb79264/out-0.png"]
17+
```
18+
19+
### Run a model in the background
20+
21+
You can start a model and run it in the background with `Replicate.Predictions.create/5`:
22+
```elixir
23+
iex> model = Replicate.Models.get!("stability-ai/stable-diffusion")
24+
iex> version = Replicate.Models.get_version!(model, "db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf")
25+
iex> {:ok, prediction} = Replicate.Predictions.create(version, %{prompt: "a 19th century portrait of a wombat gentleman"})
26+
iex> prediction.status
27+
"starting"
28+
iex> prediction
29+
%Replicate.Predictions.Prediction{
30+
id: "krdjxq6rw5bx3dopem52ohezca",
31+
error: nil,
32+
input: %{"prompt" => "a 19th century portrait of a wombat gentleman"},
33+
logs: "",
34+
output: nil,
35+
status: "starting",
36+
version: "db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf",
37+
started_at: nil,
38+
created_at: "2023-06-02T20:43:55.720751299Z",
39+
completed_at: nil
40+
}
41+
iex> {:ok, prediction} = Replicate.Predictions.get(prediction.id)
42+
iex> prediction.logs |> String.split("\n")
43+
["Using seed: 54144", "input_shape: torch.Size([1, 77])",
44+
" 0%| | 0/50 [00:00<?, ?it/s]",
45+
" 6%|▌ | 3/50 [00:00<00:02, 21.14it/s]",
46+
" 12%|█▏ | 6/50 [00:00<00:02, 21.18it/s]",
47+
" 18%|█▊ | 9/50 [00:00<00:01, 21.20it/s]",
48+
" 24%|██▍ | 12/50 [00:00<00:01, 21.25it/s]",
49+
" 30%|███ | 15/50 [00:00<00:01, 21.21it/s]",
50+
" 36%|███▌ | 18/50 [00:00<00:01, 21.24it/s]",
51+
" 42%|████▏ | 21/50 [00:00<00:01, 21.23it/s]",
52+
" 48%|████▊ | 24/50 [00:01<00:01, 21.19it/s]",
53+
" 54%|█████▍ | 27/50 [00:01<00:01, 21.16it/s]",
54+
" 60%|██████ | 30/50 [00:01<00:00, 21.17it/s]",
55+
" 66%|██████▌ | 33/50 [00:01<00:00, 21.15it/s]",
56+
" 72%|███████▏ | 36/50 [00:01<00:00, 21.17it/s]",
57+
" 78%|███████▊ | 39/50 [00:01<00:00, 21.16it/s]",
58+
" 84%|████████▍ | 42/50 [00:01<00:00, 21.16it/s]",
59+
" 90%|█████████ | 45/50 [00:02<00:00, 21.16it/s]",
60+
" 96%|█████████▌| 48/50 [00:02<00:00, 20.99it/s]",
61+
"100%|██████████| 50/50 [00:02<00:00, 21.14it/s]",
62+
""]
63+
iex> {:ok, prediction} = Replicate.Predictions.wait(prediction)
64+
iex> prediction.status
65+
"succeeded"
66+
iex> prediction.output
67+
["https://replicate.delivery/pbxt/xbT582euzFwqCiupR5PVyMUFemfgZHbPyAm5kenezBS3RDQIC/out-0.png"]
68+
```
69+
70+
### Run a model in the background and get a webhook
71+
72+
You can run a model and get a webhook when it completes, instead of waiting for it to finish:
73+
74+
```
75+
Replicate.Predictions.create(version, %{prompt: "a 19th century portrait of a wombat gentleman"}, "https://example.com/webhook", ["completed"])
76+
```
77+
78+
### List versions of a model
79+
80+
You can list all the versions of a model:
81+
82+
```elixir
83+
iex> model = Replicate.Models.get!("stability-ai/stable-diffusion")
84+
iex> versions = Replicate.Models.list_versions(model)
85+
iex> Replicate.Models.list_versions(model) |> Enum.map(& &1.id) |> Enum.slice(0..5)
86+
["db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf",
87+
"328bd9692d29d6781034e3acab8cf3fcb122161e6f5afb896a4ca9fd57090577",
88+
"f178fa7a1ae43a9a9af01b833b9d2ecf97b1bcb0acfd2dc5dd04895e042863f1",
89+
"0827b64897df7b6e8c04625167bbb275b9db0f14ab09e2454b9824141963c966",
90+
"27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
91+
"8abccf52e7cba9f6e82317253f4a3549082e966db5584e92c808ece132037776"]
92+
```
93+
94+
### Get latest version of a model
95+
96+
*ELIXIR CLIENT EXCLUSIVE*
97+
98+
Gets the latest version of a model. Raises an error if the version doesn't exist.
99+
100+
```
101+
iex> model = Replicate.Models.get!("stability-ai/stable-diffusion")
102+
iex> version = Replicate.Models.get_latest_version!(model)
103+
iex> version.id
104+
"db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf"
105+
iex> version.cog_version
106+
"0.6.0"
107+
```
108+
109+
## Predictions
110+
{: .col-2}
111+
112+
### Cancel a prediction
113+
114+
You can cancel a running prediction by passing an id or `%Replicate.Predictions.Prediction{}` to `Replicate.Predictions.cancel/1`:
115+
116+
```
117+
iex> model = Replicate.Models.get!("stability-ai/stable-diffusion")
118+
iex> version = Replicate.Models.get_version!(model, "db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf")
119+
iex> {:ok, prediction} = Replicate.Predictions.create(version, %{prompt: "Watercolor painting of the Babadook"})
120+
iex> prediction.status
121+
"starting"
122+
iex> {:ok, prediction} = Replicate.Predictions.cancel(prediction.id)
123+
iex> prediction.status
124+
"canceled"
125+
126+
iex> model = Replicate.Models.get!("stability-ai/stable-diffusion")
127+
iex> version = Replicate.Models.get_version!(model, "db21e45d3f7023abc2a46ee38a23973f6dce16bb082a930b0c49861f96d1e5bf")
128+
iex> {:ok, prediction} = Replicate.Predictions.create(version, %{prompt: "a 19th century portrait of a wombat gentleman"})
129+
iex> {:ok, prediction} = Replicate.Predictions.cancel(prediction)
130+
iex> prediction.status
131+
"canceled"
132+
```
133+
134+
### List predictions
135+
136+
You can list all the predictions you've run:
137+
138+
```elixir
139+
iex> Replicate.Predictions.list()
140+
[%Prediction{
141+
id: "1234",
142+
status: "starting",
143+
input: %{"prompt" => "a 19th century portrait of a wombat gentleman"},
144+
version: "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
145+
output: ["https://replicate.com/api/models/stability-ai/stable-diffusion/files/50fcac81-865d-499e-81ac-49de0cb79264/out-0.png"]
146+
},
147+
%Prediction{
148+
id: "1235",
149+
status: "starting",
150+
input: %{"prompt" => "a 19th century portrait of a wombat gentleman"},
151+
version: "27b93a2413e7f36cd83da926f3656280b2931564ff050bf9575f1fdf9bcd7478",
152+
output: ["https://replicate.com/api/models/stability-ai/stable-diffusion/files/50fcac81-865d-499e-81ac-49de0cb79264/out-0.png"]}
153+
]
154+
```

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmodule Replicate.MixProject do
1717
homepage_url: "https://hexdocs.pm/replicate/readme.html",
1818
docs: [
1919
main: "readme",
20-
extras: ["README.md", "CHANGELOG.md"],
20+
extras: ["README.md", "cheatsheet.cheatmd", "CHANGELOG.md"],
2121
logo: "logo.png"
2222
]
2323
]

0 commit comments

Comments
 (0)