Do you want to do Automatic Speech Recognition, or ASR, in Go, and are wondering how this can be done?
This article shows the different available options and how Speech Recognition can be integrated into your Go application in 60 seconds.
Speech-to-Text APIs For Go
Since open source options in Go are still limited, the best option is to use a Speech-to-Text API, which you can access with our AssemblyAI SDK for Go.
Choosing the best Speech-to-Text API for your project can be challenging. One of the most easy-to-use APIs to integrate is AssemblyAI. So let's learn how this can be integrated into a Go application with only a few lines of code.
Speech Recognition in Go With AssemblyAI
Before we start writing code, we need to get a working API key. You can get one here and get started for free:
Get a free API KeyGet started with the AssemblyAI SDK
To install the AssemblyAI SDK for Go, use go get
in your terminal:
go get github.com/AssemblyAI/assemblyai-go-sdk
Next, import the module and create a new authenticated client using your API key:
package main
import (
aai "github.com/AssemblyAI/assemblyai-go-sdk"
)
func main() {
client := aai.NewClient("YOUR_API_KEY")
// ...
}
Transcribe a local file
If you have a file locally on your computer, you can use the TranscribeFromReader
method to first upload the file to AssemblyAI's servers. The file will be deleted as soon as the transcription finishes.
package main
import (
"context"
"fmt"
"log"
"os"
aai "github.com/AssemblyAI/assemblyai-go-sdk"
)
func main() {
client := aai.NewClient("YOUR_API_KEY")
f, err := os.Open("my_audio.mp3")
if err != nil {
log.Fatal(err)
}
defer f.Close()
transcript, err := client.Transcripts.TranscribeFromReader(context.TODO(), f, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(*transcript.Text)
}
Transcribe audio from a URL
If you instead want to transcribe audio that is already available from a URL, use the TranscribeFromURL
method.
package main
import (
"context"
"fmt"
"log"
aai "github.com/AssemblyAI/assemblyai-go-sdk"
)
func main() {
client := aai.NewClient("YOUR_API_KEY")
transcript, err := client.Transcripts.TranscribeFromURL(context.TODO(), "YOUR_AUDIO_URL", nil)
if err != nil {
log.Fatal(err)
}
fmt.Println(*transcript.Text)
}
Open Source Speech Recognition Libraries in Golang
Unfortunately, the options in Go are still limited. The go-to programming language with the most open source libraries for offline Speech Recognition is Python.
If you still want to use an open source library, you can try PocketSphinx for Go. It's a lightweight Speech Recognition engine, specifically tuned for handheld and mobile devices. However, this project hasn't been updated in over four years.