You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.0 KiB
101 lines
2.0 KiB
2 years ago
|
package whisper
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"os/exec"
|
||
|
"path/filepath"
|
||
|
|
||
|
"github.com/ggerganov/whisper.cpp/bindings/go/pkg/whisper"
|
||
|
wav "github.com/go-audio/wav"
|
||
1 year ago
|
"github.com/go-skynet/LocalAI/pkg/grpc/whisper/api"
|
||
2 years ago
|
)
|
||
|
|
||
|
func sh(c string) (string, error) {
|
||
|
cmd := exec.Command("/bin/sh", "-c", c)
|
||
|
cmd.Env = os.Environ()
|
||
|
o, err := cmd.CombinedOutput()
|
||
|
return string(o), err
|
||
|
}
|
||
|
|
||
|
// AudioToWav converts audio to wav for transcribe. It bashes out to ffmpeg
|
||
|
// TODO: use https://github.com/mccoyst/ogg?
|
||
|
func audioToWav(src, dst string) error {
|
||
|
out, err := sh(fmt.Sprintf("ffmpeg -i %s -format s16le -ar 16000 -ac 1 -acodec pcm_s16le %s", src, dst))
|
||
|
if err != nil {
|
||
|
return fmt.Errorf("error: %w out: %s", err, out)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
1 year ago
|
func Transcript(model whisper.Model, audiopath, language string, threads uint) (api.Result, error) {
|
||
|
res := api.Result{}
|
||
2 years ago
|
|
||
|
dir, err := os.MkdirTemp("", "whisper")
|
||
|
if err != nil {
|
||
2 years ago
|
return res, err
|
||
2 years ago
|
}
|
||
|
defer os.RemoveAll(dir)
|
||
|
|
||
|
convertedPath := filepath.Join(dir, "converted.wav")
|
||
|
|
||
|
if err := audioToWav(audiopath, convertedPath); err != nil {
|
||
2 years ago
|
return res, err
|
||
2 years ago
|
}
|
||
|
|
||
|
// Open samples
|
||
|
fh, err := os.Open(convertedPath)
|
||
|
if err != nil {
|
||
2 years ago
|
return res, err
|
||
2 years ago
|
}
|
||
|
defer fh.Close()
|
||
|
|
||
|
// Read samples
|
||
|
d := wav.NewDecoder(fh)
|
||
|
buf, err := d.FullPCMBuffer()
|
||
|
if err != nil {
|
||
2 years ago
|
return res, err
|
||
2 years ago
|
}
|
||
|
|
||
|
data := buf.AsFloat32Buffer().Data
|
||
|
|
||
|
// Process samples
|
||
|
context, err := model.NewContext()
|
||
|
if err != nil {
|
||
2 years ago
|
return res, err
|
||
2 years ago
|
|
||
|
}
|
||
|
|
||
2 years ago
|
context.SetThreads(threads)
|
||
|
|
||
2 years ago
|
if language != "" {
|
||
|
context.SetLanguage(language)
|
||
2 years ago
|
} else {
|
||
|
context.SetLanguage("auto")
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
if err := context.Process(data, nil, nil); err != nil {
|
||
2 years ago
|
return res, err
|
||
2 years ago
|
}
|
||
|
|
||
|
for {
|
||
2 years ago
|
s, err := context.NextSegment()
|
||
2 years ago
|
if err != nil {
|
||
|
break
|
||
|
}
|
||
2 years ago
|
|
||
|
var tokens []int
|
||
1 year ago
|
for _, t := range s.Tokens {
|
||
2 years ago
|
tokens = append(tokens, t.Id)
|
||
|
}
|
||
|
|
||
1 year ago
|
segment := api.Segment{Id: s.Num, Text: s.Text, Start: s.Start, End: s.End, Tokens: tokens}
|
||
2 years ago
|
res.Segments = append(res.Segments, segment)
|
||
|
|
||
|
res.Text += s.Text
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
return res, nil
|
||
2 years ago
|
}
|