【问题标题】:Google Speech-to-Text JupyterLab notebook script run locally using Google Cloud SDK使用 Google Cloud SDK 在本地运行的 Google Speech-to-Text JupyterLab 笔记本脚本
【发布时间】:2020-10-09 21:01:34
【问题描述】:

我有以下 Python 脚本,该脚本在 Google JupyterLab 笔记本上运行良好,但在本地使用 Google Cloud SDK 时无法运行:

from google.cloud import speech_v1p1beta1

def speech_to_text(audio_file):

    client = speech_v1p1beta1.SpeechClient()

    enable_word_time_offsets = True
    enable_word_confidence = True
    enable_automatic_punctuation = True

    language_code = 'en-US'
    config = {
        'enable_word_confidence': enable_word_confidence,
        'enable_word_time_offsets': enable_word_time_offsets,
        'enable_automatic_punctuation': enable_automatic_punctuation,
        'language_code': language_code
    }
    audio = {'uri': audio_file}
    operation = client.long_running_recognize (config, audio)
    response = client.recognize(config, audio)
    result = response.results[0]
    alternative = result.alternatives[0]

    print(alternative)
    
speech_to_text('gs://my-bucket/my-folder/my-subfolder/my-audio-file.flac')

但是,当我尝试使用 Google Cloud SDK 在虚拟环境中本地运行此脚本(WIN10、Python 3.8)时,我收到以下错误消息:

Traceback (most recent call last):
  File "my-speech-to-text-script.py", line 32, in <module>
    speech_to_text('gs://my-bucket/my-folder/my-subfolder/my-audio-file.flac')
  File "my-speech-to-text-script.py", line 25, in speech_to_text
    operation = client.long_running_recognize (config, audio)
TypeError: long_running_recognize() takes from 1 to 2 positional arguments but 3 were given

我按照本教程设置虚拟环境https://cloud.google.com/python/setup#windows,然后运行pip install google-cloud-speech

我做错了什么?

【问题讨论】:

    标签: python google-cloud-platform jupyter-notebook sdk speech-to-text


    【解决方案1】:

    这解决了我的问题,非常感谢。这是现在工作的代码:

    from google.cloud import speech_v1p1beta1
    
    def speech_to_text(audio_file):
    
        client = speech_v1p1beta1.SpeechClient()
    
        enable_word_time_offsets = True
        enable_word_confidence = True
        enable_automatic_punctuation = True
    
        language_code = "en-US"
        config = {
            "enable_word_confidence": enable_word_confidence,
            "enable_word_time_offsets": enable_word_time_offsets,
            "enable_automatic_punctuation": enable_automatic_punctuation,
            "language_code": language_code
        }
        audio = {"uri": audio_file}
        operation = client.long_running_recognize(request={"config":config, "audio":audio})
        response = client.recognize(request={"config":config, "audio":audio})
        result = response.results[0]
        alternative = result.alternatives[0]
    
        print(alternative)
        
    speech_to_text('gs://my-bucket/my-folder/my-subfolder/my-audio-file.flac')
    

    【讨论】:

      【解决方案2】:

      我通过更新我的代码找到了答案,我的代码和你的一样,可能是基于旧版本的 Speech-to-Text 库。

      重要的变化:

      operation = client.long_running_recognize(request={"config":config, "audio":audio})
      

      【讨论】:

        猜你喜欢
        • 2020-08-11
        • 1970-01-01
        • 2019-04-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-21
        • 1970-01-01
        相关资源
        最近更新 更多