diff --git a/aimdl/googleapi/googleclient.go b/aimdl/googleapi/googleclient.go new file mode 100644 index 0000000000000000000000000000000000000000..5e87975f1c10fd5dcb73f714ef7428ac7f420b90 --- /dev/null +++ b/aimdl/googleapi/googleclient.go @@ -0,0 +1,53 @@ +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 +} diff --git a/aimdl/googleapi/speechtotext.go b/aimdl/googleapi/speechtotext.go new file mode 100644 index 0000000000000000000000000000000000000000..85b1e2affdbb4a6434785c20327be88a9199b39c --- /dev/null +++ b/aimdl/googleapi/speechtotext.go @@ -0,0 +1,76 @@ +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 +} diff --git a/aimdl/googleapi/texttospeech.go b/aimdl/googleapi/texttospeech.go new file mode 100644 index 0000000000000000000000000000000000000000..7d4296e3402db50f07cc5c4ab972a05a4c686e01 --- /dev/null +++ b/aimdl/googleapi/texttospeech.go @@ -0,0 +1,61 @@ +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 +} diff --git a/aimdl/sample/brooklyn.flac b/aimdl/sample/brooklyn.flac new file mode 100644 index 0000000000000000000000000000000000000000..44d6f9ecd2a297da58fd28faa164d78fba639167 Binary files /dev/null and b/aimdl/sample/brooklyn.flac differ diff --git a/aimdl/sample/example.go b/aimdl/sample/example.go new file mode 100644 index 0000000000000000000000000000000000000000..b64bb8bda69aeaef8f325bc82afefaf1bedefa18 --- /dev/null +++ b/aimdl/sample/example.go @@ -0,0 +1,39 @@ +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) +}