/
sa_service_whisper.go
executable file
·283 lines (234 loc) · 6.99 KB
/
sa_service_whisper.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
Copyright 2023 Milan Suk
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this db except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"os"
"os/exec"
"strconv"
"sync"
"time"
)
type SAServiceWhisperCppProps struct {
Offset_t int
Offset_n int
Duration int
Max_context int
Max_len int
Best_of int
Beam_size int
Word_thold float64
Entropy_thold float64
Logprob_thold float64
Translate bool
Diarize bool
Tinydiarize bool
Split_on_word bool
No_timestamps bool
Language string
Detect_language bool
Temperature float64
Temperature_inc float64
Response_format string
}
func (p *SAServiceWhisperCppProps) Hash() (OsHash, error) {
js, err := json.Marshal(p)
if err != nil {
return OsHash{}, err
}
return InitOsHash(js)
}
func (p *SAServiceWhisperCppProps) Write(w *multipart.Writer) {
w.WriteField("offset_t", strconv.Itoa(p.Offset_t))
w.WriteField("offset_n", strconv.Itoa(p.Offset_n))
w.WriteField("duration", strconv.Itoa(p.Duration))
w.WriteField("max_context", strconv.Itoa(p.Max_context))
w.WriteField("max_len", strconv.Itoa(p.Max_len))
w.WriteField("best_of", strconv.Itoa(p.Best_of))
w.WriteField("beam_size", strconv.Itoa(p.Beam_size))
w.WriteField("word_thold", strconv.FormatFloat(p.Word_thold, 'f', -1, 64))
w.WriteField("entropy_thold", strconv.FormatFloat(p.Entropy_thold, 'f', -1, 64))
w.WriteField("logprob_thold", strconv.FormatFloat(p.Logprob_thold, 'f', -1, 64))
w.WriteField("translate", OsTrnString(p.Translate, "1", "0"))
w.WriteField("diarize", OsTrnString(p.Diarize, "1", "0"))
w.WriteField("tinydiarize", OsTrnString(p.Tinydiarize, "1", "0"))
w.WriteField("split_on_word", OsTrnString(p.Split_on_word, "1", "0"))
w.WriteField("no_timestamps", OsTrnString(p.No_timestamps, "1", "0"))
w.WriteField("language", p.Language)
w.WriteField("detect_language", OsTrnString(p.Detect_language, "1", "0"))
w.WriteField("temperature", strconv.FormatFloat(p.Temperature, 'f', -1, 64))
w.WriteField("temperature_inc", strconv.FormatFloat(p.Temperature_inc, 'f', -1, 64))
w.WriteField("response_format", p.Response_format)
}
type SAServiceWhisperCpp struct {
jobs *SAJobs
cmd *exec.Cmd
addr string //http://127.0.0.1:8080/
cache map[string][]byte //results
lock sync.Mutex
last_setModel string
}
func SAServiceWhisperCpp_cachePath() string {
return "services/whisper.cpp.json"
}
func NewSAServiceWhisperCpp(jobs *SAJobs, addr string, port string, init_model string) (*SAServiceWhisperCpp, error) {
wh := &SAServiceWhisperCpp{jobs: jobs}
wh.addr = addr + ":" + port + "/"
wh.cache = make(map[string][]byte)
//load cache
{
js, _ := os.ReadFile(SAServiceWhisperCpp_cachePath())
if len(js) > 0 {
err := json.Unmarshal(js, &wh.cache)
if err != nil {
return nil, fmt.Errorf("NewSAServiceWhisperCpp() failed: %w", err)
}
}
}
//run process
modelPath := "models/" + init_model + ".bin"
{
wh.cmd = exec.Command("./server", "--port", port, "--convert", "-m", modelPath)
wh.cmd.Dir = "services/whisper.cpp/"
wh.cmd.Stdout = os.Stdout
wh.cmd.Stderr = os.Stderr
err := wh.cmd.Start()
if err != nil {
return nil, fmt.Errorf("Command() failed: %w", err)
}
}
//wait until it's running
{
err := errors.New("err")
st := OsTicks()
for err != nil && OsIsTicksIn(st, 10000) { //max 10sec to start
err = wh.setModel(init_model)
time.Sleep(100 * time.Millisecond)
}
if err != nil {
return nil, err
}
wh.last_setModel = init_model
}
return wh, nil
}
func (wh *SAServiceWhisperCpp) Destroy() {
//save cache
js, err := json.Marshal(wh.cache)
if err == nil {
os.WriteFile(SAServiceWhisperCpp_cachePath(), js, 0644)
}
//py.cmd.Process.Signal(syscall.SIGQUIT)
err = wh.cmd.Process.Kill()
if err != nil {
fmt.Println(err)
}
}
func (wh *SAServiceWhisperCpp) findCache(model string, blob OsBlob, propsHash OsHash) ([]byte, bool) {
str, found := wh.cache[model+blob.hash.Hex()+propsHash.Hex()]
return str, found
}
func (wh *SAServiceWhisperCpp) addCache(model string, blob OsBlob, propsHash OsHash, value []byte) {
wh.cache[model+blob.hash.Hex()+propsHash.Hex()] = value
}
func (wh *SAServiceWhisperCpp) Transcribe(model string, blob OsBlob, props *SAServiceWhisperCppProps) ([]byte, error) {
wh.lock.Lock()
defer wh.lock.Unlock()
//find
propsHash, err := props.Hash()
if err != nil {
return nil, fmt.Errorf("Hash() failed: %w", err)
}
str, found := wh.findCache(model, blob, propsHash)
if found {
return str, nil
}
//set model
if model != wh.last_setModel {
err := wh.setModel(model)
if err != nil {
return nil, fmt.Errorf("setModel() failed: %w", err)
}
}
//translate
out, err := wh.transcribe(blob, props)
if err != nil {
return nil, fmt.Errorf("transcribe() failed: %w", err)
}
wh.addCache(model, blob, propsHash, out)
return out, nil
}
func (wh *SAServiceWhisperCpp) transcribe(blob OsBlob, props *SAServiceWhisperCppProps) ([]byte, error) {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
//set parameters
{
part, err := writer.CreateFormFile("file", "audio.wav")
if err != nil {
return nil, fmt.Errorf("CreateFormFile() failed: %w", err)
}
part.Write(blob.data)
props.Write(writer)
}
writer.Close()
req, err := http.NewRequest(http.MethodPost, wh.addr+"inference", body)
if err != nil {
return nil, fmt.Errorf("NewRequest() failed: %w", err)
}
req.Header.Add("Content-Type", writer.FormDataContentType())
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("Do() failed: %w", err)
}
resBody, err := io.ReadAll(res.Body) //job.close .......
if err != nil {
return nil, fmt.Errorf("ReadAll() failed: %w", err)
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("statusCode != 200, response: %s", resBody)
}
return resBody, nil
}
func (wh *SAServiceWhisperCpp) setModel(model string) error {
body := &bytes.Buffer{}
writer := multipart.NewWriter(body)
writer.WriteField("model", "models/"+model+".bin")
writer.Close()
req, err := http.NewRequest(http.MethodPost, wh.addr+"load", body)
if err != nil {
return fmt.Errorf("NewRequest() failed: %w", err)
}
req.Header.Add("Content-Type", writer.FormDataContentType())
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
return fmt.Errorf("Do() failed: %w", err)
}
defer res.Body.Close()
resBody, err := io.ReadAll(res.Body)
if err != nil {
return fmt.Errorf("ReadAll() failed: %w", err)
}
if res.StatusCode != 200 {
return fmt.Errorf("statusCode != 200, response: %s", resBody)
}
wh.last_setModel = model
return nil
}