Azure speech to text not working in AWS Lambda

MH 5 Reputation points
2023-04-13T21:44:03.3833333+00:00

Azure STT Python SDK returns "Reason.Cancelled" automatically after starting the transcription.
I am using it in AWS Lambda environment.

Here is my code:

def speech_recognize_continuous_from_file():
        results = []
        print(datetime.datetime.now())
        """performs continuous speech recognition with input from an audio file"""
        # <SpeechContinuousRecognitionWithFile>
        speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
        # Ask for detailed recognition result
        speech_config.output_format = speechsdk.OutputFormat.Detailed

        # If you also want word-level timing in the detailed recognition results, set the following.
        # Note that if you set the following, you can omit the previous line
        #   "speech_config.output_format = speechsdk.OutputFormat.Detailed",
        # since word-level timing implies detailed recognition results.
        speech_config.request_word_level_timestamps()
        audio_config = speechsdk.audio.AudioConfig(filename=file_name)

        speech_recognizer = speechsdk.SpeechRecognizer(speech_config=speech_config, audio_config=audio_config)

        def new_func(evt):

            results.append(json.loads(evt.result.json))


        #CustomPhrases = ["villusectomy", "Villa","abranchi","ataabranchi","ateabrasio"]
        CustomPhrases = event["CustomPhrases"]

        phrase_list_grammar = speechsdk.PhraseListGrammar.from_recognizer(speech_recognizer)

        for phr in CustomPhrases:
            phrase_list_grammar.addPhrase(phr)

       
        done = False

        def stop_cb(evt: speechsdk.SessionEventArgs):
            print(evt)

            nonlocal done
            done = True

        # Connect callbacks to the events fired by the speech recognizer

        speech_recognizer.recognized.connect(lambda evt: new_func(evt))

        # stop continuous recognition on either session stopped or canceled events
        speech_recognizer.session_stopped.connect(stop_cb)
        speech_recognizer.canceled.connect(stop_cb)

        # Start continuous speech recognition
        speech_recognizer.start_continuous_recognition()
        while not done:
            
            time.sleep(.5)
            


        speech_recognizer.stop_continuous_recognition()
        # </SpeechContinuousRecognitionWithFile>

        phrase_list_grammar.clear()

        print(datetime.datetime.now())

        return results
Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,444 questions
{count} vote

1 answer

Sort by: Most helpful
  1. YutongTie-MSFT 46,996 Reputation points
    2023-04-15T05:35:53.76+00:00

    Hello @MH Thanks for reaching out to us. If you are having issues with Azure Speech-to-Text not working in AWS Lambda, there could be a few possible reasons:

    1. Network connectivity: Lambda functions run inside a VPC (Virtual Private Cloud) by default, which means they may not have internet access unless you explicitly configure the VPC to allow outbound internet traffic. If Azure Speech-to-Text is not accessible from within your Lambda function, this could be due to network connectivity issues.
    2. Authentication: Azure Speech-to-Text requires an authentication token to access the service. If you are not passing the correct credentials or if the token has expired, the service will not work. Make sure you have provided the correct subscription key and region for your Azure Speech-to-Text service.
    3. Runtime dependencies: The Azure Speech-to-Text SDK has some runtime dependencies, such as the pyaudio library, that may not be available in the AWS Lambda runtime environment. Make sure all required dependencies are included in your deployment package.
    4. Resource limitations: AWS Lambda functions have limits on the amount of memory and CPU time they can use. If your function is running out of resources while trying to transcribe speech, it may not work correctly.

    To troubleshoot the issue, you can start by checking the CloudWatch logs for your Lambda function to see if there are any error messages or exceptions being thrown. You can also try running the same code outside of Lambda to see if it works correctly, and make sure all dependencies are included in your deployment package.

    Please have a try and let me know is you still see more issue. If you are still blocked by this issue, we would like to recommend you raise a support ticket for this issue. Let us know if you have no support plan and we are happy to enable you a free ticket for this issue.

    Regards, Yutong

    -Please kindly accept the answer if you feel helpful to support the community, thanks a lot.

    0 comments No comments