-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmodel.go
183 lines (156 loc) · 5.92 KB
/
model.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package replicate
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strings"
)
type Model struct {
URL string `json:"url"`
Owner string `json:"owner"`
Name string `json:"name"`
Description string `json:"description"`
Visibility string `json:"visibility"`
GithubURL string `json:"github_url"`
PaperURL string `json:"paper_url"`
LicenseURL string `json:"license_url"`
RunCount int `json:"run_count"`
CoverImageURL string `json:"cover_image_url"`
DefaultExample *Prediction `json:"default_example"`
LatestVersion *ModelVersion `json:"latest_version"`
rawJSON json.RawMessage `json:"-"`
}
func (m *Model) RawJSON() json.RawMessage {
return m.rawJSON
}
var _ json.Unmarshaler = (*Model)(nil)
func (m *Model) UnmarshalJSON(data []byte) error {
m.rawJSON = data
type Alias Model
alias := &struct{ *Alias }{Alias: (*Alias)(m)}
return json.Unmarshal(data, alias)
}
type CreateModelOptions struct {
Visibility string `json:"visibility"`
Hardware string `json:"hardware"`
Description *string `json:"description,omitempty"`
GithubURL *string `json:"github_url,omitempty"`
PaperURL *string `json:"paper_url,omitempty"`
LicenseURL *string `json:"license_url,omitempty"`
CoverImageURL *string `json:"cover_image_url,omitempty"`
}
type ModelVersion struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"`
CogVersion string `json:"cog_version"`
OpenAPISchema interface{} `json:"openapi_schema"`
rawJSON json.RawMessage `json:"-"`
}
func (m *ModelVersion) RawJSON() json.RawMessage {
return m.rawJSON
}
var _ json.Unmarshaler = (*ModelVersion)(nil)
func (m *ModelVersion) UnmarshalJSON(data []byte) error {
m.rawJSON = data
type Alias ModelVersion
alias := &struct{ *Alias }{Alias: (*Alias)(m)}
return json.Unmarshal(data, alias)
}
// ListModels lists public models.
func (r *Client) ListModels(ctx context.Context) (*Page[Model], error) {
response := &Page[Model]{}
err := r.fetch(ctx, http.MethodGet, "/models", nil, response)
if err != nil {
return nil, fmt.Errorf("failed to list models: %w", err)
}
return response, nil
}
// SearchModels searches for public models.
func (r *Client) SearchModels(ctx context.Context, query string) (*Page[Model], error) {
response := &Page[Model]{}
request, err := r.newRequest(ctx, "QUERY", "/models", strings.NewReader(query))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
request.Header.Set("Content-Type", "text/plain")
err = r.do(request, response)
if err != nil {
return nil, fmt.Errorf("failed to search models: %w", err)
}
return response, nil
}
// GetModel retrieves information about a model.
func (r *Client) GetModel(ctx context.Context, modelOwner string, modelName string) (*Model, error) {
model := &Model{}
err := r.fetch(ctx, http.MethodGet, fmt.Sprintf("/models/%s/%s", modelOwner, modelName), nil, model)
if err != nil {
return nil, fmt.Errorf("failed to get model: %w", err)
}
return model, nil
}
// CreateModel creates a new model.
func (r *Client) CreateModel(ctx context.Context, modelOwner string, modelName string, options CreateModelOptions) (*Model, error) {
model := &Model{}
body := struct {
Owner string `json:"owner"`
Name string `json:"name"`
CreateModelOptions
}{
Owner: modelOwner,
Name: modelName,
CreateModelOptions: options,
}
err := r.fetch(ctx, http.MethodPost, "/models", body, model)
if err != nil {
return nil, fmt.Errorf("failed to create model: %w", err)
}
return model, nil
}
// DeleteModel deletes a model with no associated versions.
func (r *Client) DeleteModel(ctx context.Context, modelOwner string, modelName string) error {
err := r.fetch(ctx, http.MethodDelete, fmt.Sprintf("/models/%s/%s", modelOwner, modelName), nil, nil)
if err != nil {
return fmt.Errorf("failed to delete model: %w", err)
}
return nil
}
// ListModelVersions lists the versions of a model.
func (r *Client) ListModelVersions(ctx context.Context, modelOwner string, modelName string) (*Page[ModelVersion], error) {
response := &Page[ModelVersion]{}
err := r.fetch(ctx, http.MethodGet, fmt.Sprintf("/models/%s/%s/versions", modelOwner, modelName), nil, response)
if err != nil {
return nil, fmt.Errorf("failed to list model versions: %w", err)
}
return response, nil
}
// GetModelVersion retrieves a specific version of a model.
func (r *Client) GetModelVersion(ctx context.Context, modelOwner string, modelName string, versionID string) (*ModelVersion, error) {
version := &ModelVersion{}
err := r.fetch(ctx, http.MethodGet, fmt.Sprintf("/models/%s/%s/versions/%s", modelOwner, modelName, versionID), nil, version)
if err != nil {
return nil, fmt.Errorf("failed to get model version: %w", err)
}
return version, nil
}
// DeleteModelVersion deletes a model version and all associated predictions, including all output files.
func (r *Client) DeleteModelVersion(ctx context.Context, modelOwner string, modelName string, versionID string) error {
err := r.fetch(ctx, http.MethodDelete, fmt.Sprintf("/models/%s/%s/versions/%s", modelOwner, modelName, versionID), nil, nil)
if err != nil {
return fmt.Errorf("failed to delete model version: %w", err)
}
return nil
}
// CreatePredictionWithModel sends a request to the Replicate API to create a prediction for a model.
func (r *Client) CreatePredictionWithModel(ctx context.Context, modelOwner string, modelName string, input PredictionInput, webhook *Webhook, stream bool) (*Prediction, error) {
path := fmt.Sprintf("/models/%s/%s/predictions", modelOwner, modelName)
req, err := r.createPredictionRequest(ctx, path, nil, input, webhook, stream)
if err != nil {
return nil, err
}
prediction := &Prediction{}
if err := r.do(req, prediction); err != nil {
return nil, fmt.Errorf("failed to create prediction with model: %w", err)
}
return prediction, nil
}