Reputation: 3002
I'm using google speech-to-text api. When I run this code in Google cloud Run.
operation = self.client.long_running_recognize(config=self.config, audio=audio)
I got this error. I searched this error message on google. However I can't fined good answer.
"/code/app/./moji/speech_api.py", line 105, in _long_recognize operation = self.client.long_running_recognize(config=self.config, audio=audio) File "/usr/local/lib/python3.8/site-packages/google/cloud/speech_v1p1beta1/services/speech/client.py", line 457, in long_running_recognize response = rpc(request, retry=retry, timeout=timeout, metadata=metadata,) File "/usr/local/lib/python3.8/site-packages/google/api_core/gapic_v1/method.py", line 145, in __call__ return wrapped_func(*args, **kwargs) File "/usr/local/lib/python3.8/site-packages/google/api_core/grpc_helpers.py", line 69, in error_remapped_callable six.raise_from(exceptions.from_grpc_error(exc), exc) File "<string>", line 3, in raise_from google.api_core.exceptions.ServiceUnavailable: 503 Getting metadata from plugin failed with error: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response')) [pid: 11|app: 0|req: 56/2613] 169.254.8.129 () {44 vars in 746 bytes} [Sat Aug 28 18:16:17 2021] GET / => generated 0 bytes in 19 msecs (HTTP/1.1 302) 4 headers in 141 bytes (1 switches on core 0)
This is my code.
def speech_to_text(request: WSGIRequest):
moji_response = MojiResponse()
threads = []
def transcript_audio_thread(description):
if description.length == 0: description.status = 'done'
if description.status != 'done':
description.status = 'processing'
description.save()
if description.api_name == 'amivoice' and description.lang == 'ja-JP':
description.transcribe_ami()
else:
description.transcribe_gcs()
description.consume_audio_limit()
description.update_at = timezone.now()
description.status = 'done'
description.save()
description.save_words(description.words)
moji_response.append(description.get_result_dict())
send_mail_for_over_10_min(description, description.user)
def transcribe_gcs(self, gcs_uri, is_long_recognize):
audio = speech.RecognitionAudio(uri=gcs_uri)
logger.info(f'gcs_uri: {gcs_uri} speech.config: {self.config}')
try:
if is_long_recognize:
self._long_recognize(audio)
else:
self._recognize(audio)
except Exception as e:
logger.error(f'recognized file gcs_uri: {gcs_uri} {e}')
raise e
# noinspection PyTypeChecker
def set_config(self, language_code, sample_rate_hertz, phrases, channels=1, ):
logger.info(f'language_code: {language_code} sample_rate_hertz: {sample_rate_hertz} channels: {channels}')
encoding = speech.RecognitionConfig.AudioEncoding.FLAC
# encoding = speech.RecognitionConfig.AudioEncoding.LINEAR16
self.config = {
'encoding': encoding,
'sample_rate_hertz': sample_rate_hertz,
'language_code': language_code,
'enable_automatic_punctuation': True,
'enable_word_time_offsets': True,
'audio_channel_count': channels,
}
if len(phrases) > 0: self.config['speech_contexts'] = phrases
@stop_watch
def _long_recognize(self, audio):
operation = self.client.long_running_recognize(config=self.config, audio=audio)
logger.info("Waiting for speech_to_text to complete...")
self.response = operation.result(timeout=60 * 90)
def transcript_audio(file_path: str, user: CustomUser, language_code: str, api_name: str,
is_trial: bool) -> Description
t = threading.Thread(target=transcript_audio_thread, args=(description,))
t.start()
if description.length > 600 and user.plan != 'anonymous':
moji_response.append({'id': description.pk, 'file_name': description.file.name,
'text': MyMessage.OVER_10_MIN})
return
threads.append(t)
This error happen only few files. Most of all files can transcript.
Error file info
When I transcript same file and same code in local development environment, it worked fine.
Authentification
I downloaded auth json file. Then set the path to GOOGLE_APPLICATION_CREDENTIALS
.
from google.oauth2 import service_account
GS_CREDENTIALS = service_account.Credentials.from_service_account_file(
os.environ.get("GOOGLE_APPLICATION_CREDENTIALS")
)
This authentification work fine. Because when I transcribe audio files can do it. The error happen only a few files.
Is there anyone help me, how to solve this error?
Upvotes: 0
Views: 531
Reputation: 3002
I solved this error. This is not text-to-speech error. It is threading
error. I forgot append Thread
before return
.
t = threading.Thread(target=transcript_audio_thread, args=(description,))
t.start()
if description.length > 600 and user.plan != 'anonymous':
moji_response.append({'id': description.pk, 'file_name': description.file.name,
'text': MyMessage.OVER_10_MIN})
return
threads.append(t) <-- point
I changed above code like below.
t = threading.Thread(target=transcript_audio_thread, args=(description,))
t.start()
threads.append(t) <-- point
if description.length > 600 and user.plan != 'anonymous':
moji_response.append({'id': description.pk, 'file_name': description.file.name,
'text': MyMessage.OVER_10_MIN})
On local environment can run Thread
after HttpResponse. However Cloud Run stop server after HttpResponse. So I got this error only in Cloud Run.
GET / => generated 0 bytes in 19 msecs (HTTP/1.1 302) 4 headers in 141 bytes (1 switches on core 0)
Upvotes: 1