forked from boocmp/go-stt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
265 lines (217 loc) · 5.39 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package main
import (
"net/http"
"strconv"
"os"
"sync"
"time"
"reflect"
"encoding/binary"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"azul3d.org/engine/audio"
_ "azul3d.org/engine/audio/flac"
"github.com/brave-experiments/go-stt/whisper"
"github.com/brave-experiments/go-stt/google_streaming_api"
)
// Version set at compile-time
var (
Version string
)
func main() {
zerolog.SetGlobalLevel(zerolog.InfoLevel)
log.Logger = log.Output(
zerolog.ConsoleWriter{
Out: os.Stderr,
NoColor: true,
},
)
zerolog.CallerMarshalFunc = func(pc uintptr, file string, line int) string {
short := file
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
short = file[i+1:]
break
}
}
file = short
return file + ":" + strconv.Itoa(line)
}
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Logger = log.With().Caller().Logger()
app := cli.NewApp()
app.Name = "Speech-to-Text Using Whisper API"
app.Usage = "Speech-to-Text."
app.Action = run
app.Version = Version
if err := app.Run(os.Args); err != nil {
log.Fatal().Err(err).Msg("can't run app")
}
}
var model *whisper.WhisperModel
type Handler struct {
Results chan whisper.TextResult
Done chan bool
Transcriber *whisper.Transcriber
Paired chan bool
}
func (h *Handler)IsPaired()(bool) {
return h.Results != nil && h.Transcriber != nil
}
type Handlers struct {
sync.Mutex
handlers map[string]*Handler
}
func (hs *Handlers)IsLocked() bool {
const mutexLocked = 1
state := reflect.ValueOf(hs).Elem().FieldByName("state")
return state.Int()&mutexLocked == mutexLocked
return true
}
func (hs *Handlers)CreateHandlerForRequest(req *http.Request) (*Handler, string) {
if !hs.IsLocked() {
panic("Lock before calling this function")
}
is_upstream_request := req.URL.Path == "/up"
is_downstream_request := req.URL.Path == "/down"
log.Info().Msgf("%s", req.URL.Path)
if !is_upstream_request && !is_downstream_request {
return nil, ""
}
if req.ParseForm() != nil {
return nil, ""
}
pair := req.FormValue("pair")
if pair == "" {
return nil, ""
}
handler, ok := hs.handlers[pair]
if !ok {
handler = new(Handler)
hs.handlers[pair] = handler
}
if is_upstream_request {
lang := req.FormValue("lang")
if len(lang) > 2 {
lang = lang[:2]
}
transcriber, err := whisper.NewTranscriber(model, lang)
if err != nil {
if hs.handlers[pair] != nil {
delete(hs.handlers, pair)
}
return nil, ""
}
handler.Transcriber = transcriber
if handler.Paired == nil {
handler.Paired = make(chan bool, 1)
}
}
if is_downstream_request {
handler.Results = make(chan whisper.TextResult, 1)
handler.Done = make(chan bool, 2)
if handler.Paired == nil {
handler.Paired = make(chan bool)
}
}
if handler.IsPaired() {
if hs.handlers[pair] != nil {
hs.handlers[pair].Paired <- true
delete(hs.handlers, pair)
}
}
return handler, pair
}
var handlers Handlers
func run(c* cli.Context) error {
if m, err := whisper.LoadWhisperModel("/app/models/ggml-model.bin"); err != nil {
return err
} else {
model = m
}
handlers = Handlers{ handlers: make(map[string]*Handler) }
http.HandleFunc("/up", handleUpstream)
http.HandleFunc("/down", handleDownstream)
http.ListenAndServe(":8090", nil)
return nil
}
func handleUpstream(w http.ResponseWriter, req *http.Request) {
handlers.Lock()
handler, pair := handlers.CreateHandlerForRequest(req)
if handler == nil {
// Invalid request, just leave
handlers.Unlock()
return
}
handlers.Unlock()
go func() {
handler.Transcriber.Process()
}()
log.Info().Msgf("[UPSTREAM] Start with pair %s", pair)
dec, _, _ := audio.NewDecoder(req.Body)
for quit := false; !quit; {
select {
case <-handler.Done:
quit = true
default:
samples := make(audio.Float32, 16000)
n, err := dec.Read(samples)
if n <= 0 || err != nil {
quit = true
handler.Transcriber.Quit <- true
<- handler.Done
} else {
log.Info().Msgf("Passing = %d to pair=%s",len(samples), pair)
handler.Transcriber.Audio <- samples
}
}
}
handler.Transcriber.Quit <- true
log.Info().Msgf("[UPSTREAM] Done with pair %s", pair)
}
func handleDownstream(w http.ResponseWriter, req *http.Request) {
handlers.Lock()
handler, pair := handlers.CreateHandlerForRequest(req)
if handler == nil {
// Invalid request, just leave
handlers.Unlock()
return
}
handlers.Unlock()
log.Info().Msgf("[DOWNSTREAM] Start with pair %s", pair)
select {
case <-handler.Paired:
break
case <-time.After(time.Second * 5):
log.Info().Msgf("[DOWNSTREAM] Timeout upstream connection with pair %s", pair)
handler.Done <- true
return
}
for running := true; running; {
select {
case <-handler.Done:
running = false
case <-handler.Transcriber.Done:
running = false
case <-time.After(time.Second * 60):
log.Info().Msgf("Timeout pair=%s", pair)
running = false
case result := <-handler.Transcriber.Results:
message := google_streaming_api.NewRecognitionMessage()
for _, r := range(result) {
message.Add(r.Text, r.Final)
}
bytes, err := message.Serialize()
if err == nil {
binary.Write(w, binary.BigEndian, uint32(len(bytes)))
w.Write(bytes)
if fl, ok := w.(http.Flusher); ok {
fl.Flush()
}
}
}
}
handler.Done <- true
log.Info().Msgf("[DOWNSTREAM] Done with pair %s", pair)
}