Skip to content

Commit

Permalink
support inference endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
batmac authored and batmac committed Apr 29, 2023
1 parent bf0f776 commit 18dc85b
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions pkg/mutators/single/huggingface.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ type HuggingFaceRequest struct {

func init() {
singleRegister("huggingface", huggingface,
withDescription("ask HuggingFace for simple tasks, optional arg is the model (needs a valid key in $HUGGING_FACE_HUB_TOKEN)"),
withDescription("ask HuggingFace for simple tasks, optional arg is the model (needs a valid key in $HUGGING_FACE_HUB_TOKEN, set HUGGING_FACE_ENDPOINT to use an Inference API endpoint)"),
withConfigBuilder(stdConfigStrings(0, 1)),
withAliases("hf"),
withCategory("external APIs"),
Expand All @@ -70,6 +70,7 @@ func init() {

func huggingface(w io.WriteCloser, r io.ReadCloser, conf any) (int64, error) {
arg := conf.([]string)

baseURL := "https://api-inference.huggingface.co/models/"

token, source, err := getHuggingFaceToken()
Expand All @@ -86,8 +87,17 @@ func huggingface(w io.WriteCloser, r io.ReadCloser, conf any) (int64, error) {
model = m
}

url := baseURL + model
if os.Getenv("HUGGING_FACE_ENDPOINT") != "" {
url = os.Getenv("HUGGING_FACE_ENDPOINT")
if len(arg) >= 1 && arg[0] != "" {
log.Println("warning: HUGGING_FACE_ENDPOINT is set, ignoring model argument")
}
}

log.Debugln("token: from ", source)
log.Debugln("model: ", model)
log.Debugln("url: ", url)

input, err := io.ReadAll(r)
if err != nil {
Expand All @@ -100,7 +110,7 @@ func huggingface(w io.WriteCloser, r io.ReadCloser, conf any) (int64, error) {

log.Debugf("request: %s\n", request)

req, err := http.NewRequest(http.MethodPost, baseURL+model, bytes.NewReader(request))
req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(request))
if err != nil {
return 0, err
}
Expand Down

0 comments on commit 18dc85b

Please sign in to comment.