Add Pascal/Go/C#/Dart API for NeMo Canary ASR models (#2367)
Add support for the new NeMo Canary ASR model across multiple language bindings by introducing a Canary model configuration and setter method on the offline recognizer. - Define Canary model config in Pascal, Go, C#, Dart and update converter functions - Add SetConfig API for offline recognizer (Pascal, Go, C#, Dart) - Extend CI/workflows and example scripts to test non-streaming Canary decoding
This commit is contained in:
17
go-api-examples/non-streaming-canary-decode-files/go.mod
Normal file
17
go-api-examples/non-streaming-canary-decode-files/go.mod
Normal file
@@ -0,0 +1,17 @@
|
||||
module non-streaming-canary-decode-files
|
||||
|
||||
go 1.17
|
||||
|
||||
require (
|
||||
github.com/k2-fsa/sherpa-onnx-go v1.12.4
|
||||
github.com/spf13/pflag v1.0.6
|
||||
github.com/youpy/go-wav v0.3.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/k2-fsa/sherpa-onnx-go-linux v1.12.4 // indirect
|
||||
github.com/k2-fsa/sherpa-onnx-go-macos v1.12.4 // indirect
|
||||
github.com/k2-fsa/sherpa-onnx-go-windows v1.12.4 // indirect
|
||||
github.com/youpy/go-riff v0.1.0 // indirect
|
||||
github.com/zaf/g711 v0.0.0-20190814101024-76a4a538f52b // indirect
|
||||
)
|
||||
113
go-api-examples/non-streaming-canary-decode-files/main.go
Normal file
113
go-api-examples/non-streaming-canary-decode-files/main.go
Normal file
@@ -0,0 +1,113 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
sherpa "github.com/k2-fsa/sherpa-onnx-go/sherpa_onnx"
|
||||
"github.com/youpy/go-wav"
|
||||
)
|
||||
|
||||
func main() {
|
||||
log.SetFlags(log.LstdFlags | log.Lmicroseconds)
|
||||
|
||||
config := sherpa.OfflineRecognizerConfig{}
|
||||
|
||||
config.ModelConfig.Canary.Encoder = "./sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/encoder.int8.onnx"
|
||||
config.ModelConfig.Canary.Decoder = "./sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/decoder.int8.onnx"
|
||||
config.ModelConfig.Canary.SrcLang = "en"
|
||||
config.ModelConfig.Canary.TgtLang = "en"
|
||||
config.ModelConfig.Canary.UsePnc = 1
|
||||
config.ModelConfig.Tokens = "./sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/tokens.txt"
|
||||
|
||||
waveFilename := "./sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/test_wavs/en.wav"
|
||||
|
||||
samples, sampleRate := readWave(waveFilename)
|
||||
|
||||
log.Println("Initializing recognizer (may take several seconds)")
|
||||
recognizer := sherpa.NewOfflineRecognizer(&config)
|
||||
log.Println("Recognizer created!")
|
||||
defer sherpa.DeleteOfflineRecognizer(recognizer)
|
||||
|
||||
log.Println("Start decoding!")
|
||||
stream := sherpa.NewOfflineStream(recognizer)
|
||||
defer sherpa.DeleteOfflineStream(stream)
|
||||
|
||||
stream.AcceptWaveform(sampleRate, samples)
|
||||
|
||||
recognizer.Decode(stream)
|
||||
log.Println("Decoding done!")
|
||||
result := stream.GetResult()
|
||||
|
||||
log.Println("Text in English: " + strings.ToLower(result.Text))
|
||||
|
||||
s := sherpa.NewOfflineStream(recognizer)
|
||||
defer sherpa.DeleteOfflineStream(s)
|
||||
|
||||
s.AcceptWaveform(sampleRate, samples)
|
||||
|
||||
config.ModelConfig.Canary.TgtLang = "de"
|
||||
recognizer.SetConfig(&config)
|
||||
recognizer.Decode(s)
|
||||
result = s.GetResult()
|
||||
|
||||
log.Println("Text in German: " + strings.ToLower(result.Text))
|
||||
}
|
||||
|
||||
func readWave(filename string) (samples []float32, sampleRate int) {
|
||||
file, _ := os.Open(filename)
|
||||
defer file.Close()
|
||||
|
||||
reader := wav.NewReader(file)
|
||||
format, err := reader.Format()
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to read wave format")
|
||||
}
|
||||
|
||||
if format.AudioFormat != 1 {
|
||||
log.Fatalf("Support only PCM format. Given: %v\n", format.AudioFormat)
|
||||
}
|
||||
|
||||
if format.NumChannels != 1 {
|
||||
log.Fatalf("Support only 1 channel wave file. Given: %v\n", format.NumChannels)
|
||||
}
|
||||
|
||||
if format.BitsPerSample != 16 {
|
||||
log.Fatalf("Support only 16-bit per sample. Given: %v\n", format.BitsPerSample)
|
||||
}
|
||||
|
||||
reader.Duration() // so that it initializes reader.Size
|
||||
|
||||
buf := make([]byte, reader.Size)
|
||||
n, err := reader.Read(buf)
|
||||
if n != int(reader.Size) {
|
||||
log.Fatalf("Failed to read %v bytes. Returned %v bytes\n", reader.Size, n)
|
||||
}
|
||||
|
||||
samples = samplesInt16ToFloat(buf)
|
||||
sampleRate = int(format.SampleRate)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func samplesInt16ToFloat(inSamples []byte) []float32 {
|
||||
numSamples := len(inSamples) / 2
|
||||
outSamples := make([]float32, numSamples)
|
||||
|
||||
for i := 0; i != numSamples; i++ {
|
||||
s := inSamples[i*2 : (i+1)*2]
|
||||
|
||||
var s16 int16
|
||||
buf := bytes.NewReader(s)
|
||||
err := binary.Read(buf, binary.LittleEndian, &s16)
|
||||
if err != nil {
|
||||
log.Fatal("Failed to parse 16-bit sample")
|
||||
}
|
||||
outSamples[i] = float32(s16) / 32768
|
||||
}
|
||||
|
||||
return outSamples
|
||||
}
|
||||
13
go-api-examples/non-streaming-canary-decode-files/run.sh
Executable file
13
go-api-examples/non-streaming-canary-decode-files/run.sh
Executable file
@@ -0,0 +1,13 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
set -ex
|
||||
|
||||
if [ ! -f sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8/encoder.int8.onnx ]; then
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8.tar.bz2
|
||||
tar xvf sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8.tar.bz2
|
||||
rm sherpa-onnx-nemo-canary-180m-flash-en-es-de-fr-int8.tar.bz2
|
||||
fi
|
||||
|
||||
go mod tidy
|
||||
go build
|
||||
./non-streaming-canary-decode-files
|
||||
Reference in New Issue
Block a user