forked from Kardbord/hfapigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_classification.go
64 lines (52 loc) · 1.59 KB
/
text_classification.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
package hfapigo
import (
"encoding/json"
"errors"
)
const (
RecommendedTextClassificationModel = "distilbert-base-uncased-finetuned-sst-2-english"
)
// Request structure for the Text classification endpoint
type TextClassificationRequest struct {
// (Required) strings to be classified
Inputs []string `json:"inputs,omitempty"`
Options Options `json:"options,omitempty"`
}
// Response structure for the Text classification endpoint
type TextClassificationResponse struct {
// HFAPI returns a list of labels and their associated scores for
// each input.
Labels []*TextClassificationResponseLabel
}
// Used in TextClassificationResponse
type TextClassificationResponseLabel struct {
// The label for the class (model specific)
Name string `json:"label,omitempty"`
// A float that represents how likely is that the text belongs in this class.
Score float64 `json:"score,omitempty"`
}
func SendTextClassificationRequest(model string, request *TextClassificationRequest) ([]*TextClassificationResponse, error) {
if request == nil {
return nil, errors.New("nil TextClassificationRequest")
}
jsonBuf, err := json.Marshal(request)
if err != nil {
return nil, err
}
respBody, err := MakeHFAPIRequest(jsonBuf, model)
if err != nil {
return nil, err
}
tclabels := make([][]*TextClassificationResponseLabel, len(request.Inputs))
err = json.Unmarshal(respBody, &tclabels)
if err != nil {
return nil, err
}
tcresps := make([]*TextClassificationResponse, len(tclabels))
for i := range tclabels {
tcresps[i] = &TextClassificationResponse{
Labels: tclabels[i],
}
}
return tcresps, nil
}