-
Notifications
You must be signed in to change notification settings - Fork 6
/
feature_extraction.go
63 lines (51 loc) · 2.11 KB
/
feature_extraction.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
package huggingface
import (
"context"
"encoding/json"
"errors"
)
// Request structure for the feature extraction endpoint
type FeatureExtractionRequest struct {
// String to get the features from
Inputs []string `json:"inputs"`
Options Options `json:"options,omitempty"`
Model string `json:"-"`
}
// Response structure for the feature extraction endpoint
type FeatureExtractionResponse [][][][]float32
// Response structure for the feature extraction endpoint
type FeatureExtractionWithAutomaticReductionResponse [][]float32
// FeatureExtraction performs feature extraction using the specified model.
// It sends a POST request to the Hugging Face inference endpoint with the provided input data.
// The response contains the extracted features or an error if the request fails.
func (ic *InferenceClient) FeatureExtraction(ctx context.Context, req *FeatureExtractionRequest) (FeatureExtractionResponse, error) {
if len(req.Inputs) == 0 {
return nil, errors.New("inputs are required")
}
body, err := ic.post(ctx, req.Model, "feature-extraction", req)
if err != nil {
return nil, err
}
featureExtractionResponse := FeatureExtractionResponse{}
if err := json.Unmarshal(body, &featureExtractionResponse); err != nil {
return nil, err
}
return featureExtractionResponse, nil
}
// FeatureExtractionWithAutomaticReduction performs feature extraction using the specified model.
// It sends a POST request to the Hugging Face inference endpoint with the provided input data.
// The response contains the extracted features or an error if the request fails.
func (ic *InferenceClient) FeatureExtractionWithAutomaticReduction(ctx context.Context, req *FeatureExtractionRequest) (FeatureExtractionWithAutomaticReductionResponse, error) {
if len(req.Inputs) == 0 {
return nil, errors.New("inputs are required")
}
body, err := ic.post(ctx, req.Model, "feature-extraction", req)
if err != nil {
return nil, err
}
featureExtractionResponse := FeatureExtractionWithAutomaticReductionResponse{}
if err := json.Unmarshal(body, &featureExtractionResponse); err != nil {
return nil, err
}
return featureExtractionResponse, nil
}