-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.go
116 lines (98 loc) · 2.83 KB
/
main.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"fmt"
"os"
"strings"
"time"
"github.com/Kardbord/hfapigo/v3"
)
const HuggingFaceTokenEnv = "HUGGING_FACE_TOKEN"
func init() {
key := os.Getenv(HuggingFaceTokenEnv)
if key != "" {
hfapigo.SetAPIKey(key)
}
}
func main() {
inputs := []string{
"My name is Sarah Jessica Parker but you can call me Jessica",
"My name is Clara and I live in Berkeley, California.",
}
classifyNoAggregation(inputs)
fmt.Println("===========================================================")
classifyWithAggregation(inputs)
}
func classifyNoAggregation(inputs []string) {
fmt.Printf("Inputs: [\"%s\"]\n", strings.Join(inputs, `", "`))
fmt.Printf("\nSending request (AggregationStrategy=%s)", hfapigo.AggregationStrategyNone)
type ChanRv struct {
resps []*hfapigo.TokenClassificationResponse
err error
}
ch := make(chan ChanRv)
go func() {
tcresps, err := hfapigo.SendTokenClassificationRequest(hfapigo.RecommendedTokenClassificationModel, &hfapigo.TokenClassificationRequest{
Inputs: inputs,
Parameters: *hfapigo.NewTokenClassificationParameters().SetAggregationStrategy(hfapigo.AggregationStrategyNone),
Options: *hfapigo.NewOptions().SetWaitForModel(true),
})
ch <- ChanRv{resps: tcresps, err: err}
}()
for {
select {
case chrv := <-ch:
fmt.Println()
if chrv.err != nil {
fmt.Println(chrv.err)
return
}
for i, input := range inputs {
fmt.Printf("\nInput %d: %s\n", i, input)
for _, entity := range chrv.resps[i].Entities {
fmt.Printf("Entity: \"%s\" Label: \"%s\" Score: %f\n", entity.Entity, entity.Label, entity.Score)
}
}
return
default:
fmt.Print(".")
time.Sleep(time.Millisecond * 100)
}
}
}
func classifyWithAggregation(inputs []string) {
fmt.Printf("Inputs: [\"%s\"]\n", strings.Join(inputs, `", "`))
fmt.Printf("\nSending request (AggregationStrategy=%s)", hfapigo.AggregationStrategySimple)
type ChanRv struct {
resps []*hfapigo.TokenClassificationResponse
err error
}
ch := make(chan ChanRv)
go func() {
tcresps, err := hfapigo.SendTokenClassificationRequest(hfapigo.RecommendedTokenClassificationModel, &hfapigo.TokenClassificationRequest{
Inputs: inputs,
Parameters: *hfapigo.NewTokenClassificationParameters().SetAggregationStrategy(hfapigo.AggregationStrategySimple),
Options: *hfapigo.NewOptions().SetWaitForModel(true),
})
ch <- ChanRv{resps: tcresps, err: err}
}()
for {
select {
case chrv := <-ch:
fmt.Println()
if chrv.err != nil {
fmt.Println(chrv.err)
return
}
for i, input := range inputs {
fmt.Printf("\nInput %d: %s\n", i, input)
for _, entity := range chrv.resps[i].Entities {
fmt.Printf("Entity: \"%s\" Label: \"%s\" Score: %f\n", entity.Entity, entity.Label, entity.Score)
}
}
return
default:
fmt.Print(".")
time.Sleep(time.Millisecond * 100)
}
}
}