Google Cloud Text to Speech-为什么通过Go客户端库和REST API的请求之间存在延迟差异?

问题描述

我的目标是让我在Base64的Google Cloud文本语音转换中获得audioContent。我当前的程序两次进行相同的精确调用:一次是通过Google的TTS客户端库,另一个是通过身份验证令牌,我必须通过运行命令gcloud auth application-default print-access-token进行复制和粘贴。显然,按照Google Cloud的建议,我应该使用客户端库。正如您在终端中用两种方法对请求进行计时时所看到的那样,问题是客户端库占用了4-5倍的时间!

% go run main.go                                                           
[CLIENT LIBRARY took 586ms]
[REST API took 138ms]

延迟始终高于500毫秒,而对REST API的请求永远不会超过200毫秒。 为什么它的客户端库这么慢,有什么方法可以优化请求?我本来以为用Base64编码是永远需要的,但是当我计时时,编码只花了3毫秒话虽如此,请求audioContent使用Base64 String可以使请求更快吗? TTS api文档没有提及对客户端的支持,所以甚至不可能吗?

我的使用方式有问题吗?如果我必须使用REST API进行速度控制,那么我还遇到了一个问题,即需要在程序中生成授权令牌,并在其过期时不断检查和更新。我一直在手动使用终端生成一个,然后将其复制并粘贴到我的代码中。 Go中是否有一个库,可以为我的Google Cloud Rest API生成访问/身份验证令牌?

package main

import (
    b64 "encoding/base64"
    "fmt"
    "io/IoUtil"
    "net/http"
    "strings"
    "time"

    "context"

    texttospeech "cloud.google.com/go/texttospeech/apiv1"
    texttospeechpb "google.golang.org/genproto/googleapis/cloud/texttospeech/v1"
)

func audioFromClient(text string) {
    ctx := context.Background()

    client,err := texttospeech.NewClient(ctx)
    if err != nil {
        fmt.Println(err)
    }

    req := &texttospeechpb.SynthesizeSpeechRequest{
        Input: &texttospeechpb.SynthesisInput{
            InputSource: &texttospeechpb.SynthesisInput_Text{Text: text},},Voice: &texttospeechpb.VoiceSelectionParams{LanguageCode: "en-US"},AudioConfig: &texttospeechpb.AudioConfig{
            AudioEncoding: texttospeechpb.AudioEncoding_MP3,}

    resp,err := client.SynthesizeSpeech(ctx,req)
    if err != nil {
        fmt.Println(err)
    }

    audioContent := b64.StdEncoding.EncodetoString((resp.AudioContent))
    _ = audioContent
    // GOT THE AUdio IN BASE64
}

func audioFromrest(text string) {
    url := "https://texttospeech.googleapis.com/v1/text:synthesize"
    method := "POST"

    payload := strings.NewReader("{\n  \"input\": {\n    \"text\": \"Hello Gamers!\"\n  },\n  \"voice\": {\n    \"languageCode\": \"en-US\",\n    \"name\": \"en-US-Wavenet-A\",\n    \"ssmlGender\": \"MALE\"\n  },\n  \"audioConfig\": {\n    \"audioEncoding\": \"MP3\"\n  }\n}")

    client := &http.Client{}

    req,err := http.NewRequest(method,url,payload)

    if err != nil {
        fmt.Println(err)
    }
    req.Header.Add("Authorization","Bearer ********* ACCESS TOKEN *******")
    req.Header.Add("Content-Type","application/json")

    res,err := client.Do(req)

    defer res.Body.Close()
    body,err := IoUtil.ReadAll(res.Body)
    _ = body
    // GOT THE AUdio IN BASE64
}

func main() {
    start := time.Now()
    audioFromClient("This a test!")
    fmt.Printf("[CLIENT LIBRARY took %.0fms]\n",float64(time.Since(start))/1e6)

    start = time.Now()
    audioFromrest("This a test!")
    fmt.Printf("[REST API took %.0fms]\n",float64(time.Since(start))/1e6)
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...