Sumit Surana
Sumit Surana

Reputation: 1584

Google Speech - googleapiclient.errors.UnknownApiNameOrVersion: name: speech version: v1beta1

I have the below code

import speech_recognition as sr

filename = 'audio.flac'

r = sr.Recognizer()

with sr.AudioFile(filename) as source:
    print('Recording started....')
    audio_data = r.record(source)
    print('Recording completed....')
    with open(service_auth_file) as f:
        text = r.recognize_google_cloud(audio_data)
        print('completed the recognition')
        print(text)

It requires an environment variable by name GOOGLE_APPLICATION_CREDENTIAL. Ref: https://cloud.google.com/speech-to-text/docs/reference/libraries. So I added the location of a file which contains the following data (only mentioning keys within the JSON file as other information is confidential)

{
    "type": "service_account",
    "project_id": "PROJECT_NAME",
    "private_key_id": "PROJECT_KEY",
    "private_key": "PRIVATE_KEY",
    "client_email": "CLIENT_EMAIL",
    "client_id": "CLIENT_ID",
    "auth_uri": "AUTH_URI",
    "token_uri": "TOKEN_URI",
    "auth_provider_x509_cert_url": "AUTH_CERT_URL",
    "client_x509_cert_url": "CLIENT_CERT_URL"
}

But when I am running the above code I am getting the below error

Traceback (most recent call last):
File "./speech_recognizer.py", line 23, in <module>
    text = r.recognize_google_cloud(audio_data)
File "/Users/sumitsurana/miniconda3/envs/gsp/lib/python3.8/site-packages/speech_recognition/__init__.py", line 800, in recognize_google_cloud
    speech_service = build("speech", "v1beta1", credentials=api_credentials)
File "/Users/sumitsurana/miniconda3/envs/gsp/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
    return wrapped(*args, **kwargs)
File "/Users/sumitsurana/miniconda3/envs/gsp/lib/python3.8/site-packages/googleapiclient/discovery.py", line 233, in build
    raise UnknownApiNameOrVersion(
googleapiclient.errors.UnknownApiNameOrVersion: name: speech  version: v1beta1

When I searched about the error, I came across a package called gapic-google-cloud-speech-v1beta1. So tried to run the file after it installing it too. But still getting the same error.

Upvotes: 2

Views: 4148

Answers (3)

Cornerfield
Cornerfield

Reputation: 21

According to the google documentation "v1beta1" is deprecated since April 2017, https://cloud.google.com/speech-to-text/docs/release-notes

Another way to fix the problem is to fix the speech_recognition package just like the traceback suggests. To do this open the index file that is at

/Users/sumitsurana/miniconda3/envs/gsp/lib/python3.8/site-packages/speech_recognition/__init__.py

and in line 800 change

speech_service = build("speech", "v1beta1", credentials=api_credentials)

to

speech_service = build("speech", "v1", credentials=api_credentials)

That way you comply already with this change from the link above

The v1beta1 release of Cloud Speech-to-Text has been deprecated. The v1beta1 endpoint continues to be available for a period of time as defined in the terms of service. To avoid being impacted when the v1beta1 is discontinued, replace references to v1beta1 in your code with v1 and update your code with valid v1 API names and values.


However this will cause some new errors caused by these changes.

SyncRecognize is renamed to Recognize. v1beta1/speech:syncrecognize is renamed to v1/speech:recognize. The behavior is unchanged.

The sample_rate field has been renamed to sample_rate_hertz. The behavior is unchanged.

In the same file, change their usage there as well, the code below are the changes I made,

    if preferred_phrases is None:
        speech_config = {"encoding": "FLAC", "sampleRateHertz": audio_data.sample_rate, "languageCode": language}
    else:
        speech_config = {"encoding": "FLAC", "sampleRateHertz": audio_data.sample_rate, "languageCode": language, "speechContext": {"phrases": preferred_phrases}}
    request = speech_service.speech().recognize(body={"audio": {"content": base64.b64encode(flac_data).decode("utf8")}, "config": speech_config})

These steps helped me fix the issue. Optimally these changes will be applied to the package itself in the future, but I don't know if and when that happens.

Upvotes: 2

Pubudu Sitinamaluwa
Pubudu Sitinamaluwa

Reputation: 978

I'm not sure about the origin of that error. It seems like you have configured GOOGLE_APPLICATION_CREDENTIALS correctly. But there are a few things to check when working with Google APIs.

  1. Check if the service account you are using has the necessary permissions to interact with speech API. (Typically this should return an authentication error. So unlikely in this case.)
  2. Check if you have enabled the API.
  3. If you are using delegated permissions for a Google service account, check if the delegated account has the necessary permissions and has enabled the API.

Upvotes: 2

Vimal CK
Vimal CK

Reputation: 3563

The issue is mostly because the google speech library is not able to read the GOOGLE_APPLICATION_CREDENTIALS environment variable. Set the environment variable like below in your code before instantiating the speech recognizer.

import os
import speech_recognition as sr

os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = "your_path"

filename = 'audio.flac'

r = sr.Recognizer()

with sr.AudioFile(filename) as source:
print('Recording started....')
audio_data = r.record(source)
print('Recording completed....')
with open(service_auth_file) as f:
    text = r.recognize_google_cloud(audio_data)
    print('completed the recognition')
    print(text)

Upvotes: 1

Related Questions