-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
55 lines (46 loc) · 951 Bytes
/
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
package main
import (
"fmt"
"math/rand"
"os"
"time"
"github.com/Kardbord/hfapigo/v3"
)
const HuggingFaceTokenEnv = "HUGGING_FACE_TOKEN"
func init() {
rand.Seed(time.Now().UnixNano())
key := os.Getenv(HuggingFaceTokenEnv)
if key != "" {
hfapigo.SetAPIKey(key)
}
}
const inputImg = "./test-image.png"
func main() {
fmt.Printf("Sending image to text request for image %s", inputImg)
type ChanRv struct {
resps []*hfapigo.ImageToTextResponse
err error
}
ch := make(chan ChanRv)
go func() {
resps, err := hfapigo.SendImageToTextRequest(hfapigo.RecommendedImageToTextModel, inputImg)
ch <- ChanRv{resps: resps, err: err}
}()
for {
select {
case chrv := <-ch:
fmt.Println()
if chrv.err != nil {
fmt.Println(chrv.err)
return
}
for _, r := range chrv.resps {
fmt.Printf("Caption: %s\n", r.GeneratedText)
}
return
default:
fmt.Print(".")
time.Sleep(time.Millisecond * 400)
}
}
}