-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
61 lines (51 loc) · 1.05 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
package main
import (
"fmt"
"os"
"time"
"github.com/Kardbord/hfapigo/v3"
)
const HuggingFaceTokenEnv = "HUGGING_FACE_TOKEN"
func init() {
key := os.Getenv(HuggingFaceTokenEnv)
if key != "" {
hfapigo.SetAPIKey(key)
}
}
func main() {
const audioFile = "./sample.flac"
const maxRetries = 10
fmt.Printf("Sending speech recongition request (%s)", audioFile)
type ChanRv struct {
resp *hfapigo.SpeechRecognitionResponse
err error
}
ch := make(chan ChanRv)
go func() {
arResp := &hfapigo.SpeechRecognitionResponse{}
var err error
for i := 0; i < maxRetries; i++ {
arResp, err = hfapigo.SendSpeechRecognitionRequest(hfapigo.RecommendedSpeechRecongnitionModelEnglish, audioFile)
if err == nil {
break
} else {
time.Sleep(time.Second * 5)
}
}
ch <- ChanRv{arResp, err}
}()
for {
select {
case chrv := <-ch:
if chrv.err != nil {
fmt.Println(chrv.err)
return
}
fmt.Printf("\nRecognized text: \"%s\"\n", chrv.resp.Text)
return
default:
fmt.Print(".")
time.Sleep(time.Millisecond * 200)
}
}
}