SpeechContext has no "boost" field

Viewed 705

I am using google cloud api for speech to text, I wanted to add some context phrases. According to google docs the speech context also has a boost field which I am not able to set.

I have changed the imported speech_v1p1beta1 instead of speech_v1 and coded as follows:

    config = speech.RecognitionConfig(
    encoding=speech.RecognitionConfig.AudioEncoding.LINEAR16,

    sample_rate_hertz=8000,
    language_code=first_lang,
    #alternative_language_codes=[second_lang],
    audio_channel_count=1,
    enable_separate_recognition_per_channel=False,
    #metadata=metadata,
    max_alternatives=5,
    # enable_word_confidence=True,

    speech_contexts=[{'phrases':['sfs'],'boost':15}]


    # speech_contexts=[speech.SpeechContext(
    #         phrases=['life insurance'],boost=15            
    #         )
    #         ]
    )

I am getting following error:

  File "convert-wav-beta.py", line 165, in <module>
    transcript , confidence = google_transcribe(audio_file_name)
  File "convert-wav-beta.py", line 106, in google_transcribe
    speech_contexts=[{'phrases':['sfs'],'boost':15}]
  File "/env/lib/python3.5/site-packages/google/protobuf/internal/python_message.py", line 528, in init
    copy.add(**val)
  File "/env/lib/python3.5/site-packages/google/protobuf/internal/containers.py", line 376, in add
    new_element = self._message_descriptor._concrete_class(**kwargs)
  File "/env/lib/python3.5/site-packages/google/protobuf/internal/python_message.py", line 509, in init
    field = _GetFieldByName(message_descriptor, field_name)
  File "/env/lib/python3.5/site-packages/google/protobuf/internal/python_message.py", line 573, in _GetFieldByName
    (message_descriptor.name, field_name))
ValueError: Protocol message SpeechContext has no "boost" field.```
2 Answers

I solved this issue by updating the beta library in my system. I did this by copying the code from github code by google.

It seems you are using RecognitionConfig of package "speech", and not "speech_v1p1beta1". The types and enums should also be imported from the speech_v1p1beta1 package if using beta functionalities. Your import statements should look like:

from google.cloud import speech_v1p1beta1
from google.cloud.speech_v1p1beta1 import enums
from google.cloud.speech_v1p1beta1 import types
Related