-
Notifications
You must be signed in to change notification settings - Fork 12
/
training.go
67 lines (56 loc) · 2.01 KB
/
training.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
package replicate
import (
"context"
"fmt"
"net/http"
)
type Training Prediction
type TrainingInput PredictionInput
// CreateTraining sends a request to the Replicate API to create a new training.
func (r *Client) CreateTraining(ctx context.Context, modelOwner string, modelName string, version string, destination string, input TrainingInput, webhook *Webhook) (*Training, error) {
data := map[string]interface{}{
"version": version,
"destination": destination,
"input": input,
}
if webhook != nil {
data["webhook"] = webhook.URL
if len(webhook.Events) > 0 {
data["webhook_events_filter"] = webhook.Events
}
}
training := &Training{}
path := fmt.Sprintf("/models/%s/%s/versions/%s/trainings", modelOwner, modelName, version)
err := r.fetch(ctx, http.MethodPost, path, data, training)
if err != nil {
return nil, fmt.Errorf("failed to create training: %w", err)
}
return training, nil
}
// ListTrainings returns a list of trainings.
func (r *Client) ListTrainings(ctx context.Context) (*Page[Training], error) {
response := &Page[Training]{}
err := r.fetch(ctx, http.MethodGet, "/trainings", nil, response)
if err != nil {
return nil, fmt.Errorf("failed to list trainings: %w", err)
}
return response, nil
}
// GetTraining sends a request to the Replicate API to get a training.
func (r *Client) GetTraining(ctx context.Context, trainingID string) (*Training, error) {
training := &Training{}
err := r.fetch(ctx, http.MethodGet, fmt.Sprintf("/trainings/%s", trainingID), nil, training)
if err != nil {
return nil, fmt.Errorf("failed to get training: %w", err)
}
return training, nil
}
// CancelTraining sends a request to the Replicate API to cancel a training.
func (r *Client) CancelTraining(ctx context.Context, trainingID string) (*Training, error) {
training := &Training{}
err := r.fetch(ctx, http.MethodPost, fmt.Sprintf("/trainings/%s/cancel", trainingID), nil, training)
if err != nil {
return nil, fmt.Errorf("failed to cancel training: %w", err)
}
return training, nil
}