-
Notifications
You must be signed in to change notification settings - Fork 6
/
text_classification.go
45 lines (38 loc) · 1.41 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
package huggingface
import (
"context"
"encoding/json"
"errors"
)
// TextClassificationRequest represents a request for text classification.
type TextClassificationRequest struct {
// Inputs is the string to be generated from.
Inputs string `json:"inputs"`
// Options represents optional settings for the classification.
Options Options `json:"options,omitempty"`
// Model is the name of the model to use for classification.
Model string `json:"-"`
}
// TextClassificationResponse represents a response for text classification.
type TextClassificationResponse [][]struct {
// Label is the label for the class (model-specific).
Label string `json:"label,omitempty"`
// Score is a float that represents how likely it is that the text belongs to this class.
Score float32 `json:"score,omitempty"`
}
// TextClassification performs text classification using the provided request.
func (ic *InferenceClient) TextClassification(ctx context.Context, req *TextClassificationRequest) (TextClassificationResponse, error) {
// Check if inputs are provided.
if len(req.Inputs) == 0 {
return nil, errors.New("inputs are required")
}
body, err := ic.post(ctx, req.Model, "text-classification", req)
if err != nil {
return nil, err
}
textClassificationResponse := TextClassificationResponse{}
if err := json.Unmarshal(body, &textClassificationResponse); err != nil {
return nil, err
}
return textClassificationResponse, nil
}