Commit 1856ca9b authored by Sandeep S. Shewalkar's avatar Sandeep S. Shewalkar
Browse files

new module added for google stt and stt

new module added which is wrapper over "google.golang.org/api/speech/v1beta1" for google stt and stt
parent 80b9ed1c
Branches aimdl_shiva
1 merge request!65new module added for google stt and tts
Showing with 229 additions and 0 deletions
package googleapi
import (
"context"
"net/http"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
"golang.org/x/oauth2/jwt"
)
//ClientConfig Google API config from service file
type ClientConfig struct {
Email string
PrivateKey string
TokenURL string `default:"https://accounts.google.com/o/oauth2/token"`
}
//GetClient Get the client configured to send authenticated requests
func GetClient(c ClientConfig) *http.Client {
loggermdl.LogInfo("IN GetClient")
if c.TokenURL == "" {
c.TokenURL = "https://accounts.google.com/o/oauth2/token"
}
conf := &jwt.Config{
Email: c.Email,
// The contents of your RSA private key or your PEM file
// that contains a private key.
// If you have a p12 file instead, you
// can use `openssl` to export the private key into a pem file.
//
// $ openssl pkcs12 -in key.p12 -passin pass:notasecret -out key.pem -nodes
//
// The field only supports PEM containers with no passphrase.
// The openssl command will convert p12 keys to passphrase-less PEM containers.
PrivateKey: []byte(c.PrivateKey),
TokenURL: c.TokenURL,
Scopes: []string{
"https://www.googleapis.com/auth/bigquery",
"https://www.googleapis.com/auth/blogger",
"https://www.googleapis.com/auth/cloud-platform",
},
// If you would like to impersonate a user, you can
// create a transport with a subject. The following GET
// request will be made on the behalf of user@example.com.
// Optional.
}
// Initiate an http.Client, the following GET request will be
// authorized and authenticated on the behalf of user@example.com.
client := conf.Client(context.Background())
loggermdl.LogInfo("Client created")
loggermdl.LogInfo("OUT GetClient")
return client
}
package googleapi
import (
"encoding/base64"
"net/http"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
speech "google.golang.org/api/speech/v1beta1"
"io/ioutil"
)
//AudioConfig Google Speech-to-Text config
type AudioConfig struct {
FilePath string
LanguageCode string
AudioEncoding string
AudioSampleRate int64
}
//SpeechToText get the speech to text
func SpeechToText(client *http.Client, c AudioConfig) (string, error) {
loggermdl.LogInfo("IN SpeechToText")
if c.AudioEncoding == "" {
c.AudioEncoding = "FLAC"
}
if c.AudioSampleRate == 0 {
c.AudioSampleRate = 16000
}
if c.LanguageCode == "" {
c.LanguageCode = "en-US"
}
speechService, err := speech.New(client)
if err != nil {
loggermdl.LogError("speechService Error: ", err)
loggermdl.LogInfo("OUT SpeechToText")
return "", err
}
fileDir := c.FilePath
audioData, err := ioutil.ReadFile(fileDir)
if err != nil {
loggermdl.LogError("ReadFile Error: ", err)
loggermdl.LogInfo("OUT SpeechToText")
return "", err
}
encoded := base64.StdEncoding.EncodeToString(audioData)
speechRecConfig := speech.RecognitionConfig{
SampleRate: c.AudioSampleRate,
Encoding: c.AudioEncoding,
LanguageCode: c.LanguageCode,
}
audio := speech.RecognitionAudio{
Content: encoded,
}
speechRequest := speech.SyncRecognizeRequest{
Audio: &audio,
Config: &speechRecConfig,
}
syncRecCall := speechService.Speech.Syncrecognize(&speechRequest)
syncRecResponse, err := syncRecCall.Do()
if err != nil {
loggermdl.LogError("syncRecCall Error: ", err)
loggermdl.LogInfo("OUT SpeechToText")
return "", err
}
resp, err := syncRecResponse.MarshalJSON()
if err != nil {
loggermdl.LogError("syncRecResponse Error: ", err)
loggermdl.LogInfo("OUT SpeechToText")
return "", err
}
loggermdl.LogInfo("OUT SpeechToText")
return string(resp), nil
}
package googleapi
import (
"net/http"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/loggermdl"
texttospeech "google.golang.org/api/texttospeech/v1beta1"
)
//TextConfig Google Text-to-Speech config
type TextConfig struct {
Text string
LanguageCode string
AudioEncoding string
}
//TextToSpeech get the text to speech
func TextToSpeech(client *http.Client, c TextConfig) (string, error) {
loggermdl.LogInfo("IN TextToSpeech")
texttospeechService, err := texttospeech.New(client)
if c.AudioEncoding == "" {
c.AudioEncoding = "MP3"
}
if c.LanguageCode == "" {
c.LanguageCode = "en-IN"
}
if err != nil {
loggermdl.LogError("texttospeechService Error: ", err)
loggermdl.LogInfo("OUT TextToSpeech")
return "", err
}
ttsConfig := texttospeech.AudioConfig{
AudioEncoding: c.AudioEncoding,
}
inputText := texttospeech.SynthesisInput{
Text: c.Text,
}
voiceParams := texttospeech.VoiceSelectionParams{
LanguageCode: c.LanguageCode,
}
ttsSpeechReq := texttospeech.SynthesizeSpeechRequest{
AudioConfig: &ttsConfig,
Input: &inputText,
Voice: &voiceParams,
}
ttsSpeechCall := texttospeechService.Text.Synthesize(&ttsSpeechReq)
syncResponse, err := ttsSpeechCall.Do()
if err != nil {
loggermdl.LogError("ttsSpeechCall Error", err)
loggermdl.LogInfo("OUT TextToSpeech")
return "", err
}
resp, err := syncResponse.MarshalJSON()
if err != nil {
loggermdl.LogError("syncResponse Error", err)
loggermdl.LogInfo("OUT TextToSpeech")
return "", err
}
loggermdl.LogInfo("OUT TextToSpeech")
return string(resp), nil
}
File added
package main
import (
"fmt"
googleapi "corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/aimdl/googleapi"
)
func main() {
//Google API Configuration
apiConf := googleapi.ClientConfig{
Email: "youremail@yourdomain.iam.gserviceaccount.com",
PrivateKey: "-----BEGIN PRIVATE KEY-----\nYourRSAPrivateKey\n-----END PRIVATE KEY-----\n",
}
//OAuth configured Google client
googleClient := googleapi.GetClient(apiConf)
//AudioConfig for Google Speech-to-Text
audioConf := googleapi.AudioConfig{
FilePath: "brooklyn.flac",
}
audioResponse, err := googleapi.SpeechToText(googleClient, audioConf)
if err != nil {
fmt.Println(err)
}
fmt.Println(audioResponse)
textConf := googleapi.TextConfig{
Text: "Hello, darkness my old friend!",
}
textResponse, err := googleapi.TextToSpeech(googleClient, textConf)
if err != nil {
fmt.Println(err)
}
fmt.Println(textResponse)
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment