-
Notifications
You must be signed in to change notification settings - Fork 5
/
globals.go
138 lines (116 loc) · 2.8 KB
/
globals.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
package hfapigo
import (
"bytes"
"encoding/json"
"errors"
"io"
"net/http"
"os"
)
const APIBaseURL = "https://api-inference.huggingface.co/models/"
var APIKey = func() string { return "" }
func SetAPIKey(key string) {
APIKey = func() string { return key }
}
const (
AuthHeaderKey = "Authorization"
AuthHeaderPrefix = "Bearer "
)
func setAuthorizationHeader(req *http.Request) *http.Request {
if req == nil {
return req
}
if APIKey() != "" {
req.Header.Set(AuthHeaderKey, AuthHeaderPrefix+APIKey())
}
return req
}
// MakeHFAPIRequest builds and sends an HTTP POST request to the given model
// using the provided JSON body. If the request is successful, returns the
// response JSON and a nil error. If the request fails, returns an empty slice
// and an error describing the failure.
func MakeHFAPIRequest(jsonBody []byte, model string) ([]byte, error) {
req, err := http.NewRequest(http.MethodPost, APIBaseURL+model, bytes.NewBuffer(jsonBody))
if err != nil {
return nil, err
}
if req == nil {
return nil, errors.New("nil request created")
}
req.Header.Set("Content-Type", "application/json")
setAuthorizationHeader(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = checkRespForError(respBody)
if err != nil {
return nil, err
}
return respBody, nil
}
type apiError struct {
Error string `json:"error,omitempty"`
}
type apiErrors struct {
Errors []string `json:"error,omitempty"`
}
// Checks for errors in the API response and returns them if
// found.
func checkRespForError(respJSON []byte) error {
// Check for single error
{
buf := make([]byte, len(respJSON))
copy(buf, respJSON)
apiErr := apiError{}
json.Unmarshal(buf, &apiErr)
if apiErr.Error != "" {
return errors.New(string(respJSON))
}
}
// Check for multiple errors
{
buf := make([]byte, len(respJSON))
copy(buf, respJSON)
apiErrs := apiErrors{}
json.Unmarshal(buf, &apiErrs)
if apiErrs.Errors != nil {
return errors.New(string(respJSON))
}
}
return nil
}
func MakeHFAPIRequestWithMedia(model, mediaFile string) ([]byte, error) {
buf, err := os.ReadFile(mediaFile)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, APIBaseURL+model, bytes.NewReader(buf))
if err != nil {
return nil, err
}
if req == nil {
return nil, errors.New("nil request created")
}
req.Header.Set("Content-Type", "application/octet-stream")
setAuthorizationHeader(req)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
err = checkRespForError(respBody)
if err != nil {
return nil, err
}
return respBody, nil
}