Skip to content

Commit

Permalink
Initial import
Browse files Browse the repository at this point in the history
  • Loading branch information
mudler committed Mar 18, 2023
1 parent 26de6d4 commit c1a1994
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 0 deletions.
13 changes: 13 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module github.com/go-skynet/whisper

go 1.19

require (
github.com/ggerganov/whisper.cpp/bindings/go v0.0.0-20230307193630-09e90680072d
github.com/go-audio/wav v1.1.0
)

require (
github.com/go-audio/audio v1.0.0 // indirect
github.com/go-audio/riff v1.0.0 // indirect
)
12 changes: 12 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/ggerganov/whisper.cpp/bindings/go v0.0.0-20230307193630-09e90680072d h1:a+wrVgrr8EGpMmauel61hLJsM7h3LGXwoB2oUdPyr7Y=
github.com/ggerganov/whisper.cpp/bindings/go v0.0.0-20230307193630-09e90680072d/go.mod h1:QIjZ9OktHFG7p+/m3sMvrAJKKdWrr1fZIK0rM6HZlyo=
github.com/go-audio/audio v1.0.0 h1:zS9vebldgbQqktK4H0lUqWrG8P0NxCJVqcj7ZpNnwd4=
github.com/go-audio/audio v1.0.0/go.mod h1:6uAu0+H2lHkwdGsAY+j2wHPNPpPoeg5AaEFh9FlA+Zs=
github.com/go-audio/riff v1.0.0 h1:d8iCGbDvox9BfLagY94fBynxSPHO80LmZCaOsmKxokA=
github.com/go-audio/riff v1.0.0/go.mod h1:l3cQwc85y79NQFCRB7TiPoNiaijp6q8Z0Uv38rVG498=
github.com/go-audio/wav v1.1.0 h1:jQgLtbqBzY7G+BM8fXF7AHUk1uHUviWS4X39d5rsL2g=
github.com/go-audio/wav v1.1.0/go.mod h1:mpe9qfwbScEbkd8uybLuIpTgHyrISw/OTuvjUW2iGtE=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
79 changes: 79 additions & 0 deletions whisper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package whisper

import (
"fmt"
"os"
"os/exec"

"github.com/ggerganov/whisper.cpp/bindings/go/pkg/whisper"
wav "github.com/go-audio/wav"
)

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
}

func Transcribe(modelpath, audiopath, language string) (string, error) {
// Open samples
fh, err := os.Open(audiopath)
if err != nil {
return "", err
}
defer fh.Close()

// Read samples
d := wav.NewDecoder(fh)
buf, err := d.FullPCMBuffer()
if err != nil {
return "", err
}

data := buf.AsFloat32Buffer().Data

// Load the model
model, err := whisper.New(modelpath)
if err != nil {
return "", err
}
defer model.Close()

// Process samples
context, err := model.NewContext()
if err != nil {
return "", err

}

if language != "" {
context.SetLanguage(language)
}

if err := context.Process(data, nil); err != nil {
return "", err
}

text := ""
for {
segment, err := context.NextSegment()
if err != nil {
break
}
text += segment.Text
}

return text, nil
}

0 comments on commit c1a1994

Please sign in to comment.