/
example_test.go
75 lines (60 loc) · 2.01 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package replicate_test
import (
"context"
"fmt"
"github.com/replicate/replicate-go"
)
func ExampleClient_Run() {
ctx := context.Background()
// You can also provide a token directly with `replicate.NewClient(replicate.WithToken("r8_..."))`
r8, err := replicate.NewClient(replicate.WithTokenFromEnv())
if err != nil {
return
}
// https://replicate.com/stability-ai/stable-diffusion
version := "ac732df83cea7fff18b8472768c88ad041fa750ff7682a21affe81863cbe77e4"
input := replicate.PredictionInput{
"prompt": "an astronaut riding a horse on mars, hd, dramatic lighting",
}
webhook := replicate.Webhook{
URL: "https://example.com/webhook",
Events: []replicate.WebhookEventType{"start", "completed"},
}
// Run a model and wait for its output
output, err := r8.Run(ctx, version, input, &webhook)
if err != nil {
return
}
fmt.Println("output: ", output)
}
func ExampleClient_CreatePrediction() {
ctx := context.Background()
// You can also provide a token directly with `replicate.NewClient(replicate.WithToken("r8_..."))`
r8, err := replicate.NewClient(replicate.WithTokenFromEnv())
if err != nil {
return
}
// https://replicate.com/stability-ai/stable-diffusion
version := "ac732df83cea7fff18b8472768c88ad041fa750ff7682a21affe81863cbe77e4"
input := replicate.PredictionInput{
"prompt": "an astronaut riding a horse on mars, hd, dramatic lighting",
}
webhook := replicate.Webhook{
URL: "https://example.com/webhook",
Events: []replicate.WebhookEventType{"start", "completed"},
}
// The `Run` method is a convenience method that
// creates a prediction, waits for it to finish, and returns the output.
// If you want a reference to the prediction, you can call `CreatePrediction`,
// call `Wait` on the prediction, and access its `Output` field.
prediction, err := r8.CreatePrediction(ctx, version, input, &webhook, false)
if err != nil {
return
}
// Wait for the prediction to finish
err = r8.Wait(ctx, prediction)
if err != nil {
return
}
fmt.Println("output: ", prediction.Output)
}