Search code examples
pythongoogle-cloud-platformjupyter-notebooksdkspeech-to-text

Google Speech-to-Text JupyterLab notebook script run locally using Google Cloud SDK


I have the following Python script which runs fine on a Google JupyterLab notebook but not locally using 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')

However, when I try to run this script locally (WIN10, Python 3.8) in a virtual environment using the Google Cloud SDK I get the following error message:

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

I followed this tutorial for setting up the virtual environment https://cloud.google.com/python/setup#windows and then I ran pip install google-cloud-speech

What am I doing wrong?


Solution

  • I figured it out by updating my code, which like yours, may have been based on an older version of the Speech-to-Text library.

    The important change:

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