-
Notifications
You must be signed in to change notification settings - Fork 6
/
translation.go
40 lines (31 loc) · 1007 Bytes
/
translation.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
package huggingface
import (
"context"
"encoding/json"
"errors"
)
// TranslationRequest represents a request for translation.
type TranslationRequest struct {
Inputs []string `json:"inputs"`
Options Options `json:"options,omitempty"`
Model string `json:"-"`
}
// TranslationResponse represents the response for a translation request.
type TranslationResponse []struct {
TranslationText string `json:"translation_text"`
}
// Translation sends a translation request to the InferenceClient and returns the translation response.
func (ic *InferenceClient) Translation(ctx context.Context, req *TranslationRequest) (TranslationResponse, error) {
if len(req.Inputs) == 0 {
return nil, errors.New("inputs are required")
}
body, err := ic.post(ctx, req.Model, "translation", req)
if err != nil {
return nil, err
}
translationResponse := TranslationResponse{}
if err := json.Unmarshal(body, &translationResponse); err != nil {
return nil, err
}
return translationResponse, nil
}