Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
hupe1980 committed Jun 25, 2023
1 parent e1b89b1 commit b9ee4da
Show file tree
Hide file tree
Showing 15 changed files with 836 additions and 1 deletion.
42 changes: 42 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: build

on:
push:
branches: [ main ]
tags: [ v* ]
pull_request: {}

permissions:
contents: write
pull-requests: write

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Check out code
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set up Go ${{ matrix.node-version }}
uses: actions/setup-go@v4
with:
go-version: 1.20.x

- name: Run Linter
uses: golangci/golangci-lint-action@v3

- name: Run Tests
run: make test

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
if: startsWith(github.ref, 'refs/tags/')
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@

# Go workspace file
go.work

.env
57 changes: 57 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
linters-settings:
errcheck:
check-type-assertions: true
goconst:
min-len: 2
min-occurrences: 3
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style
govet:
check-shadowing: true
nolintlint:
require-explanation: true
require-specific: true

linters:
disable-all: true
enable:
- bodyclose
#- depguard
- dogsled
#- dupl
- errcheck
- exportloopref
- exhaustive
#- goconst TODO
- gofmt
- goimports
#- gomnd
- gocyclo
- gosec
- gosimple
- govet
- ineffassign
- misspell
#- nolintlint
- nakedret
- prealloc
- predeclared
#- revive TODO
- staticcheck
- stylecheck
- thelper
- tparallel
- typecheck
- unconvert
- unparam
- unused
- whitespace
- wsl

run:
issues-exit-code: 1
2 changes: 2 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
builds:
- skip: true
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
PROJECTNAME=$(shell basename "$(PWD)")

# Go related variables.
# Make is verbose in Linux. Make it silent.
MAKEFLAGS += --silent

.PHONY: setup
## setup: Setup installes dependencies
setup:
@go mod tidy

.PHONY: lint
## test: Runs the linter
lint:
golangci-lint run --color=always --sort-results ./...

.PHONY: test
## test: Runs go test with default values
test:
@go test -v -race -count=1 ./...

.PHONY: help
## help: Prints this help message
help: Makefile
@echo
@echo " Choose a command run in "$(PROJECTNAME)":"
@echo
@sed -n 's/^##//p' $< | column -t -s ':' | sed -e 's/^/ /'
@echo
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
# go-huggingface
# go-huggingface
> The Hugging Face Inference Client in Golang is a modul designed to interact with the Hugging Face model repository and perform inference tasks using state-of-the-art natural language processing models. Developed in Golang, it provides a seamless and efficient way to integrate Hugging Face models into your Golang applications.
## Installation
```
go get github.com/hupe1980/golc
```

## How to use
```golang
package main

import (
"context"
"fmt"
"log"
"os"

huggingface "github.com/hupe1980/go-huggingface"
)

func main() {
ic := huggingface.NewInferenceClient(os.Getenv("HUGGINGFACEHUB_API_TOKEN"))

res, err := ic.ZeroShotClassification(context.Background(), &huggingface.ZeroShotRequest{
Inputs: []string{"Hi, I recently bought a device from your company but it is not working as advertised and I would like to get reimbursed!"},
Parameters: huggingface.ZeroShotParameters{
CandidateLabels: []string{"refund", "faq", "legal"},
},
})
if err != nil {
log.Fatal(err)
}

fmt.Println(res[0].Sequence)
fmt.Println("Labels:", res[0].Labels)
fmt.Println("Scores:", res[0].Scores)
}
```
Output:
```text
Hi, I recently bought a device from your company but it is not working as advertised and I would like to get reimbursed!
Labels: [refund faq legal]
Scores: [0.8777876496315002 0.10522633790969849 0.016985949128866196]
```

For more example usage, see [_examples](./_examples).

## License
[MIT](LICENCE)
23 changes: 23 additions & 0 deletions _examples/summarization/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"context"
"fmt"
"log"
"os"

huggingface "github.com/hupe1980/go-huggingface"
)

func main() {
ic := huggingface.NewInferenceClient(os.Getenv("HUGGINGFACEHUB_API_TOKEN"))

res, err := ic.Summarization(context.Background(), &huggingface.SummarizationRequest{
Inputs: []string{"The tower is 324 metres (1,063 ft) tall, about the same height as an 81-storey building, and the tallest structure in Paris. Its base is square, measuring 125 metres (410 ft) on each side. During its construction, the Eiffel Tower surpassed the Washington Monument to become the tallest man-made structure in the world, a title it held for 41 years until the Chrysler Building in New York City was finished in 1930. It was the first structure to reach a height of 300 metres. Due to the addition of a broadcasting aerial at the top of the tower in 1957, it is now taller than the Chrysler Building by 5.2 metres (17 ft). Excluding transmitters, the Eiffel Tower is the second tallest free-standing structure in France after the Millau Viaduct."},
})
if err != nil {
log.Fatal(err)
}

fmt.Println(res[0].SummaryText)
}
24 changes: 24 additions & 0 deletions _examples/text2text_generation/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"context"
"fmt"
"log"
"os"

huggingface "github.com/hupe1980/go-huggingface"
)

func main() {
ic := huggingface.NewInferenceClient(os.Getenv("HUGGINGFACEHUB_API_TOKEN"))

res, err := ic.Text2TextGeneration(context.Background(), &huggingface.Text2TextGenerationRequest{
Inputs: "The answer to the universe is",
Model: "gpt2", // overwrite recommended model
})
if err != nil {
log.Fatal(err)
}

fmt.Println(res[0].GeneratedText)
}
24 changes: 24 additions & 0 deletions _examples/text_generation/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"context"
"fmt"
"log"
"os"

huggingface "github.com/hupe1980/go-huggingface"
)

func main() {
ic := huggingface.NewInferenceClient(os.Getenv("HUGGINGFACEHUB_API_TOKEN"))

res, err := ic.TextGeneration(context.Background(), &huggingface.TextGenerationRequest{
Inputs: "The answer to the universe is",
Model: "gpt2", // overwrite recommended model
})
if err != nil {
log.Fatal(err)
}

fmt.Println(res[0].GeneratedText)
}
28 changes: 28 additions & 0 deletions _examples/zero_shot_classification/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"context"
"fmt"
"log"
"os"

huggingface "github.com/hupe1980/go-huggingface"
)

func main() {
ic := huggingface.NewInferenceClient(os.Getenv("HUGGINGFACEHUB_API_TOKEN"))

res, err := ic.ZeroShotClassification(context.Background(), &huggingface.ZeroShotRequest{
Inputs: []string{"Hi, I recently bought a device from your company but it is not working as advertised and I would like to get reimbursed!"},
Parameters: huggingface.ZeroShotParameters{
CandidateLabels: []string{"refund", "faq", "legal"},
},
})
if err != nil {
log.Fatal(err)
}

fmt.Println(res[0].Sequence)
fmt.Println("Labels:", res[0].Labels)
fmt.Println("Scores:", res[0].Scores)
}
11 changes: 11 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module github.com/hupe1980/go-huggingface

go 1.20

require github.com/stretchr/testify v1.8.4

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading

0 comments on commit b9ee4da

Please sign in to comment.