Getting error runing Azure Text-to-speech in Google Function

42 views Asked by At

I'm running Azure text-to-speech on a Google function. It runs smoothly locally and function builds successfully but I'm getting error when I'm sending a request to the function via HTTP request.

Here's the code:

import logging
from config import load_api
import azure.cognitiveservices.speech as speechsdk

logger = logging.getLogger()
logger.setLevel(logging.INFO)
config = load_api('config.yaml')

# Azure Speech SDK Configuration

class textToSpeech:
    def __init__(self, config):
        """Intialize configuration
        Args: 
            config: configuration object
        """ 
        self.subscription=config['speech']['api']
        self.region=config['speech']["region"]


    def synthesized_ssml(self, response, language, emotion):
        """
    Args:
        text (String): response of the model
        language: language of the model
        emotion: emotion predicted based on the text
    Returns:
        synthesized SSML
    """
        print(f"response: {response}")
        print(f"language: {language}")
        print(f"emotion: {emotion}")
        language_word = ""
        if (language.upper() == 'ENGLISH'):
            language_word = "en-US-JennyNeural"
        else:
            language_word = "en-US-JennyMultilingualV2Neural"
        ssml_text = f"""
            <speak version="1.0" xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="https://www.w3.org/2001/mstts" xml:lang="en-US">
                <voice name="{language_word}">
                    <mstts:express-as style= "{emotion}" styledegree="2">
                        {response}
                    </mstts:express-as>
                </voice>
            </speak>
        """
        # print(f"ssml_text: {ssml_text}")
        speech_config = speechsdk.SpeechConfig(subscription=self.subscription, region=self.region)
        # print(f"speechsdk.SpeechConfig(subscription={self.subscription}, region={self.region})")
        speech_config.set_speech_synthesis_output_format(speechsdk.SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3)
        speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
        result = speech_synthesizer.speak_text_async(ssml_text).get()
        if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
            print("Speech synthesized to speaker for text [{}]".format(response))
            
            # Convert the result to a binary string
            audio_data = result.audio_data
            # binary_string = f"{bin(audio_data.hex())}"  # Convert to hex if you need a string representation
            
            return audio_data
        elif result.reason == speechsdk.ResultReason.Canceled:
            cancellation_details = result.cancellation_details
            print("Speech synthesis canceled: {}".format(cancellation_details.reason))
            if cancellation_details.reason == speechsdk.CancellationReason.Error:
                if cancellation_details.error_details:
                    print("Error details: {}".format(cancellation_details.error_details))
            return None
    

Here's the error I'm getting:

ERROR 2024-03-22T02:56:05.843451Z Traceback (most recent call last): File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 2529, in wsgi_app response = self.full_dispatch_request()
DEFAULT 2024-03-22T02:56:05.843459Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DEFAULT 2024-03-22T02:56:05.843468Z File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 1825, in full_dispatch_request
DEFAULT 2024-03-22T02:56:05.843475Z rv = self.handle_user_exception(e)
DEFAULT 2024-03-22T02:56:05.843481Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DEFAULT 2024-03-22T02:56:05.843488Z File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 1823, in full_dispatch_request
DEFAULT 2024-03-22T02:56:05.843494Z rv = self.dispatch_request()
DEFAULT 2024-03-22T02:56:05.843499Z ^^^^^^^^^^^^^^^^^^^^^^^
DEFAULT 2024-03-22T02:56:05.843505Z File "/layers/google.python.pip/pip/lib/python3.11/site-packages/flask/app.py", line 1799, in dispatch_request
DEFAULT 2024-03-22T02:56:05.843512Z return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
DEFAULT 2024-03-22T02:56:05.843518Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DEFAULT 2024-03-22T02:56:05.843527Z File "/layers/google.python.pip/pip/lib/python3.11/site-packages/functions_framework/__init__.py", line 99, in view_func
DEFAULT 2024-03-22T02:56:05.843531Z return function(request._get_current_object())
DEFAULT 2024-03-22T02:56:05.843536Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DEFAULT 2024-03-22T02:56:05.843540Z File "/workspace/main.py", line 32, in text_to_speech
DEFAULT 2024-03-22T02:56:05.843545Z synthesized_ssml = (gcpClass.synthesized_ssml(response,language,emotion))
DEFAULT 2024-03-22T02:56:05.843549Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DEFAULT 2024-03-22T02:56:05.843554Z File "/workspace/AudioHandler.py", line 51, in synthesized_ssml
DEFAULT 2024-03-22T02:56:05.843558Z speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config)
DEFAULT 2024-03-22T02:56:05.843563Z ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DEFAULT 2024-03-22T02:56:05.843568Z File "/layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/speech.py", line 2149, in __init__
DEFAULT 2024-03-22T02:56:05.843576Z _call_hr_fn(fn=_sdk_lib.synthesizer_create_speech_synthesizer_from_config, *[
DEFAULT 2024-03-22T02:56:05.843582Z File "/layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/interop.py", line 62, in _call_hr_fn
DEFAULT 2024-03-22T02:56:05.843586Z _raise_if_failed(hr)
DEFAULT 2024-03-22T02:56:05.843591Z File "/layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/interop.py", line 55, in _raise_if_failed
DEFAULT 2024-03-22T02:56:05.843595Z __try_get_error(_spx_handle(hr))
DEFAULT 2024-03-22T02:56:05.843606Z File "/layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/interop.py", line 50, in __try_get_error
DEFAULT 2024-03-22T02:56:05.843610Z raise RuntimeError(message)
DEFAULT 2024-03-22T02:56:05.843620Z RuntimeError: Exception with error code:
DEFAULT 2024-03-22T02:56:05.843624Z [CALL STACK BEGIN]
DEFAULT 2024-03-22T02:56:05.843634Z /layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1b883f) [0x3ee8443b883f]
DEFAULT 2024-03-22T02:56:05.843641Z /layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1ea2af) [0x3ee8443ea2af]
DEFAULT 2024-03-22T02:56:05.843647Z /lib/x86_64-linux-gnu/libc.so.6(+0x99ee8) [0x3ee846699ee8]
DEFAULT 2024-03-22T02:56:05.843654Z /layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1eb46c) [0x3ee8443eb46c]
DEFAULT 2024-03-22T02:56:05.843659Z /layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x19a15f) [0x3ee84439a15f]
DEFAULT 2024-03-22T02:56:05.843664Z /layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x198311) [0x3ee844398311]
DEFAULT 2024-03-22T02:56:05.843669Z /layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1ba0ef) [0x3ee8443ba0ef]
DEFAULT 2024-03-22T02:56:05.843675Z /layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1b06aa) [0x3ee8443b06aa]
DEFAULT 2024-03-22T02:56:05.843681Z /layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1e04a6) [0x3ee8443e04a6]
DEFAULT 2024-03-22T02:56:05.843688Z /layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0xe78e5) [0x3ee8442e78e5]
DEFAULT 2024-03-22T02:56:05.843696Z /layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1ba0ef) [0x3ee8443ba0ef]
DEFAULT 2024-03-22T02:56:05.843702Z /layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x197e23) [0x3ee844397e23]
DEFAULT 2024-03-22T02:56:05.843707Z /layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(+0x1f7f0e) [0x3ee8443f7f0e]
DEFAULT 2024-03-22T02:56:05.843713Z /layers/google.python.pip/pip/lib/python3.11/site-packages/azure/cognitiveservices/speech/libMicrosoft.CognitiveServices.Speech.core.so(synthesizer_create_speech_synthesizer_from_config+0xf3) [0x3ee8442cfd0e]
DEFAULT 2024-03-22T02:56:05.843718Z /lib/x86_64-linux-gnu/libffi.so.8(+0x7e2e) [0x3ee846830e2e]
DEFAULT 2024-03-22T02:56:05.843722Z /lib/x86_64-linux-gnu/libffi.so.8(+0x4493) [0x3ee84682d493]
DEFAULT 2024-03-22T02:56:05.843727Z /layers/google.python.runtime/python/lib/python3.11/lib-dynload/_ctypes.cpython-311-x86_64-linux-gnu.so(+0xe6d0) [0x3ee844d436d0]
DEFAULT 2024-03-22T02:56:05.843732Z [CALL STACK END]
DEFAULT 2024-03-22T02:56:05.843741Z Runtime error: Failed to initialize platform (azure-c-shared). Error: 2153

I've already tried using azure.cognitiveservices.speech==1.27.0,1.28.0,1.30.0,1.36.0 Python 3.11

0

There are 0 answers