-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtranslation.go
48 lines (38 loc) · 1.04 KB
/
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
41
42
43
44
45
46
47
48
package hfapigo
import (
"encoding/json"
"errors"
)
const (
RecommendedRussianToEnglishModel = "Helsinki-NLP/opus-mt-ru-en"
)
// Request structure for the Translation endpoint
type TranslationRequest struct {
// (Required) a string to be translated in the original languages
Inputs []string `json:"inputs,omitempty"`
Options Options `json:"options,omitempty"`
}
// Response structure from the Translation endpoint
type TranslationResponse struct {
// The translated Input string
TranslationText string `json:"translation_text,omitempty"`
}
func SendTranslationRequest(model string, request *TranslationRequest) ([]*TranslationResponse, error) {
if request == nil {
return nil, errors.New("nil TranslationRequest")
}
jsonBuf, err := json.Marshal(request)
if err != nil {
return nil, err
}
respBody, err := MakeHFAPIRequest(jsonBuf, model)
if err != nil {
return nil, err
}
tresps := make([]*TranslationResponse, len(request.Inputs))
err = json.Unmarshal(respBody, &tresps)
if err != nil {
return nil, err
}
return tresps, nil
}