github.com/hupe1980/go-huggingface@v0.0.15/translation.go (about) 1 package huggingface 2 3 import ( 4 "context" 5 "encoding/json" 6 "errors" 7 ) 8 9 // TranslationRequest represents a request for translation. 10 type TranslationRequest struct { 11 Inputs []string `json:"inputs"` 12 Options Options `json:"options,omitempty"` 13 Model string `json:"-"` 14 } 15 16 // TranslationResponse represents the response for a translation request. 17 type TranslationResponse []struct { 18 TranslationText string `json:"translation_text"` 19 } 20 21 // Translation sends a translation request to the InferenceClient and returns the translation response. 22 func (ic *InferenceClient) Translation(ctx context.Context, req *TranslationRequest) (TranslationResponse, error) { 23 if len(req.Inputs) == 0 { 24 return nil, errors.New("inputs are required") 25 } 26 27 body, err := ic.post(ctx, req.Model, "translation", req) 28 29 if err != nil { 30 return nil, err 31 } 32 33 translationResponse := TranslationResponse{} 34 35 if err := json.Unmarshal(body, &translationResponse); err != nil { 36 return nil, err 37 } 38 39 return translationResponse, nil 40 }