-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinput.go
178 lines (134 loc) · 3.79 KB
/
input.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
package clarifai
import "net/http"
type Input struct {
Data *Image `json:"data,omitempty"`
ID string `json:"id,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Status *ServiceStatus `json:"status,omitempty"`
}
type Inputs struct {
Inputs []*Input `json:"inputs"`
modelID string `json:"-"`
}
// InitInputs returns a default inputs object.
func InitInputs() *Inputs {
return &Inputs{
modelID: PublicModelGeneral,
}
}
// AddInput adds an image input to a request.
func (i *Inputs) AddInput(im *Image, id string) error {
if len(i.Inputs) >= InputLimit {
return ErrInputLimitReached
}
in := &Input{
Data: im,
}
// Add custom ID if provided.
if id != "" {
in.ID = id
}
i.Inputs = append(i.Inputs, in)
return nil
}
// SetModel is an optional model setter for predict calls.
func (i *Inputs) SetModel(m string) {
i.modelID = m
}
// AddConcept adds concepts to input.
func (i *Input) AddConcept(id string, value interface{}) {
if i.Data == nil {
i.Data = &Image{}
}
i.Data.Concepts = append(i.Data.Concepts, map[string]interface{}{
"name": id,
"value": value,
})
}
// SetMetadata adds metadata to a query input item ("input" -> "data" -> "metadata").
func (q *Input) SetMetadata(i interface{}) {
if q.Data == nil {
q.Data = &Image{}
}
q.Data.Metadata = i
}
// AddInputs builds a request to add inputs to the API.
func (s *Session) AddInputs(p *Inputs) *Request {
r := NewRequest(s, http.MethodPost, "inputs")
r.SetPayload(p)
return r
}
// GetAllInputs fetches a list of all inputs.
func (s *Session) GetAllInputs() *Request {
return NewRequest(s, http.MethodGet, "inputs")
}
// GetInput fetches one input.
func (s *Session) GetInput(id string) *Request {
return NewRequest(s, http.MethodGet, "inputs/"+id)
}
// GetInputStatuses fetches statuses of all inputs.
func (s *Session) GetInputStatuses() *Request {
return NewRequest(s, http.MethodGet, "inputs/status")
}
// DeleteInputConcepts remove concepts that were already added to an input.
func (s *Session) DeleteInputConcepts(id string, concepts []string) *Request {
// 1. Build a request.
r := NewRequest(s, http.MethodPatch, "inputs/"+id+"/data/concepts")
// 2. Add payload.
p := struct {
Concepts []*OutputConcept `json:"concepts,omitempty"`
Action string `json:"action"`
}{}
for _, v := range concepts {
oc := &OutputConcept{
ID: v,
}
p.Concepts = append(p.Concepts, oc)
}
p.Action = "delete_concepts"
r.SetPayload(p)
return r
}
// UpdateInputConcepts updates existing and/or adds new concepts to an input by its ID.
func (s *Session) UpdateInputConcepts(id string, userConcepts map[string]bool) *Request {
// 1. Build a request.
r := NewRequest(s, http.MethodPatch, "inputs/"+id+"/data/concepts")
// 2. Add payload.
// Convert an input map into a map of concepts.
var reqConcepts []map[string]interface{}
for id, value := range userConcepts {
reqConcepts = append(reqConcepts, map[string]interface{}{
"id": id,
"value": value,
})
}
p := struct {
Concepts []map[string]interface{} `json:"concepts"`
Action string `json:"action"`
}{
Concepts: reqConcepts,
Action: "merge_concepts",
}
r.SetPayload(p)
return r
}
// DeleteInput deletes a single input by its ID.
func (s *Session) DeleteInput(id string) *Request {
return NewRequest(s, http.MethodDelete, "inputs/"+id)
}
// DeleteInputs deletes multiple inputs by their IDs.
func (s *Session) DeleteInputs(ids []string) *Request {
// 1. Build a request.
r := NewRequest(s, http.MethodDelete, "inputs")
// 2. Add a payload.
r.SetPayload(struct {
Inputs []string `json:"ids"`
}{
Inputs: ids,
})
return r
}
// DeleteAllInputs deletes all inputs.
func (s *Session) DeleteAllInputs() *Request {
return NewRequest(s, http.MethodDelete, "inputs")
}