-
Notifications
You must be signed in to change notification settings - Fork 15
/
requests.go
133 lines (111 loc) · 3.86 KB
/
requests.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
package clarifai
import (
"encoding/json"
"errors"
"math/big"
)
// InfoResp represents the expected JSON response from /info/
type InfoResp struct {
StatusCode string `json:"status_code"`
StatusMessage string `json:"status_msg"`
Results struct {
MaxImageSize int `json:"max_image_size"`
DefaultLanguage string `json:"default_language"`
MaxVideoSize int `json:"max_video_size"`
MaxImageBytes int `json:"max_image_bytes"`
DefaultModel string `json:"default_model"`
MaxVideoBytes int `json:"max_video_bytes"`
MaxVideoDuration int `json:"max_video_duration"`
MaxVideoBatchSize int `json:"max_video_batch_size"`
MinVideoSize int `json:"min_video_size"`
MinImageSize int `json:"min_image_size"`
MaxBatchSize int `json:"max_batch_size"`
APIVersion float32 `json:"api_version"`
}
}
// TagRequest represents a JSON request for /tag/
type TagRequest struct {
URLs []string `json:"url"`
LocalIDs []string `json:"local_ids,omitempty"`
Model string `json:"model,omitempty"`
}
// TagResp represents the expected JSON response from /tag/
type TagResp struct {
StatusCode string `json:"status_code"`
StatusMessage string `json:"status_msg"`
Meta struct {
Tag struct {
Timestamp json.Number `json:"timestamp"`
Model string `json:"model"`
Config string `json:"config"`
}
}
Results []TagResult
}
// TagResult represents the expected data for a single tag result
type TagResult struct {
DocID *big.Int `json:"docid"`
URL string `json:"url"`
StatusCode string `json:"status_code"`
StatusMessage string `json:"status_msg"`
LocalID string `json:"local_id"`
Result struct {
Tag struct {
Classes []string `json:"classes"`
CatIDs []string `json:"catids"`
Probs []float32 `json:"probs"`
}
}
DocIDString string `json:"docid_str"`
}
// FeedbackForm is used to send feedback back to Clarifai
type FeedbackForm struct {
DocIDs []string `json:"docids,omitempty"`
URLs []string `json:"url,omitempty"`
AddTags []string `json:"add_tags,omitempty"`
RemoveTags []string `json:"remove_tags,omitempty"`
DissimilarDocIDs []string `json:"dissimilar_docids,omitempty"`
SimilarDocIDs []string `json:"similar_docids,omitempty"`
SearchClick []string `json:"search_click,omitempty"`
}
// FeedbackResp is the expected response from /feedback/
type FeedbackResp struct {
StatusCode string `json:"status_code"`
StatusMessage string `json:"status_msg"`
}
// Info will return the current status info for the given client
func (client *Client) Info() (*InfoResp, error) {
res, err := client.commonHTTPRequest(nil, "info", "GET", false)
if err != nil {
return nil, err
}
info := new(InfoResp)
err = json.Unmarshal(res, info)
return info, err
}
// Tag allows the client to request tag data on a single, or multiple photos
func (client *Client) Tag(req TagRequest) (*TagResp, error) {
if len(req.URLs) < 1 {
return nil, errors.New("Requires at least one url")
}
res, err := client.commonHTTPRequest(req, "tag", "POST", false)
if err != nil {
return nil, err
}
tagres := new(TagResp)
err = json.Unmarshal(res, tagres)
return tagres, err
}
// Feedback allows the user to provide contextual feedback to Clarifai in order to improve their results
func (client *Client) Feedback(form FeedbackForm) (*FeedbackResp, error) {
if form.DocIDs == nil && form.URLs == nil {
return nil, errors.New("Requires at least one docid or url")
}
if form.DocIDs != nil && form.URLs != nil {
return nil, errors.New("Request must provide exactly one of the following fields: {'DocIDs', 'URLs'}")
}
res, err := client.commonHTTPRequest(form, "feedback", "POST", false)
feedbackres := new(FeedbackResp)
err = json.Unmarshal(res, feedbackres)
return feedbackres, err
}