question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Python SDK in python:3.11 - Failed to initialize platform (azure-c-shared)

See original GitHub issue

Describe the bug When python sdk is used in default python:3.X container it is not working. Library is correctly installed, everything is correctly started, but stream is canceled with " Failed to initialize platform (azure-c-shared) ".

Tested with python:3.8 and python 3.11 containers

But it is working with -buster suffix: python:3.11-buster

To Reproduce

working Dockerfile

FROM python:3.11-buster
WORKDIR /app
RUN pip install azure-cognitiveservices-speech
RUN apt update && apt install -y tcpdump
COPY azure_test.py /app/azure_test.py
COPY test.wav /app/test.wav
CMD ["/bin/bash"]

not working Dockerfile

FROM python:3.11
WORKDIR /app
RUN pip install azure-cognitiveservices-speech
RUN apt update && apt install -y tcpdump
COPY azure_test.py /app/azure_test.py
COPY test.wav /app/test.wav
CMD ["/bin/bash"]

code (1:1 from your samples)

import wave

import azure.cognitiveservices.speech as speechsdk
import time

speech_key = # ADD YOURS SPEECH KEY
service_region = # ADD YOURS SPEECH REGION

def main():
    class WavFileReaderCallback(speechsdk.audio.PullAudioInputStreamCallback):
        """Example class that implements the Pull Audio Stream interface to recognize speech from
        an audio file"""

        def __init__(self, filename: str):
            super().__init__()
            self._file_h = wave.open(filename, mode=None)

            self.sample_width = self._file_h.getsampwidth()

            assert self._file_h.getnchannels() == 1
            assert self._file_h.getsampwidth() == 2
            assert self._file_h.getframerate() == 16000
            assert self._file_h.getcomptype() == 'NONE'

        def read(self, buffer: memoryview) -> int:
            """read callback function"""
            size = buffer.nbytes
            frames = self._file_h.readframes(size // self.sample_width)

            buffer[:len(frames)] = frames

            print('sending data', len(frames))

            return len(frames)

        def close(self):
            """close callback function"""
            self._file_h.close()

    speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)

    # specify the audio format
    wave_format = speechsdk.audio.AudioStreamFormat(samples_per_second=16000, bits_per_sample=16,
                                                    channels=1)

    # setup the audio stream
    callback = WavFileReaderCallback("test.wav")
    stream = speechsdk.audio.PullAudioInputStream(callback, wave_format)
    audio_config = speechsdk.audio.AudioConfig(stream=stream)

    # instantiate the speech recognizer with pull stream input
    speech_recognizer = speechsdk.SpeechRecognizer(
        speech_config=speech_config,
        audio_config=audio_config,
        language="cs-CZ"
    )

    done = False

    def stop_cb(evt: speechsdk.SessionEventArgs):
        nonlocal done
        done = True
        print('CLOSING on {}'.format(evt))

    def canceled_cb(evt: speechsdk.SpeechRecognitionCanceledEventArgs):
        nonlocal done
        done = True
        print('CANCELED on {}'.format(evt.cancellation_details))

    # Connect callbacks to the events fired by the speech recognizer
    speech_recognizer.recognizing.connect(lambda evt: print('RECOGNIZING: {}'.format(evt)))
    speech_recognizer.recognized.connect(lambda evt: print('RECOGNIZED: {}'.format(evt)))
    speech_recognizer.session_started.connect(lambda evt: print('SESSION STARTED: {}'.format(evt)))
    speech_recognizer.session_stopped.connect(lambda evt: print('SESSION STOPPED {}'.format(evt)))
    # speech_recognizer.canceled.connect(lambda evt: print('CANCELED {} {]'.format(evt, evt.reason)))
    # stop continuous recognition on either session stopped or canceled events
    speech_recognizer.session_stopped.connect(stop_cb)
    speech_recognizer.canceled.connect(canceled_cb)

    # Start continuous speech recognition
    speech_recognizer.start_continuous_recognition()

    while not done:
        time.sleep(.5)

    speech_recognizer.stop_continuous_recognition()


main()

create some test.wav with speech

Expected behavior Installation should not be allowed if it will have errors like “Failed to initialize platform”

Unfortunately I don’t know how to investigate difference between python:3.11-buster and python:3.11 and find solution but I hope this helps.

Issue Analytics

  • State:closed
  • Created 3 months ago
  • Reactions:2
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

5reactions
pankoponcommented, Jun 19, 2023

We don’t yet support OpenSSL 3.0 which is the default in Debian 12, the compatibility is still in the works. However, it is fairly straightforward to install the latest OpenSSL 1.x from sources and use the Speech SDK with that. I’ve verified the following setup on Debian 12 from scratch:

sudo apt-get install build-essential ca-certificates libasound2 wget
wget -O - https://www.openssl.org/source/openssl-1.1.1u.tar.gz | tar zxf -
cd openssl-1.1.1u
./config --prefix=/usr/local
make -j $(nproc)
sudo make install_sw install_ssldirs
sudo ldconfig -v
export SSL_CERT_DIR=/etc/ssl/certs

The last line to set SSL_CERT_DIR is critical (it must be in effect systemwide or at least in the console where you run your app), otherwise OpenSSL 1.x installed in /usr/local won’t find certificates.

1reaction
pankoponcommented, Jul 10, 2023

Closing the issue - we have updated Speech SDK installation instructions with details about OpenSSL 1.x setup: https://learn.microsoft.com/azure/cognitive-services/speech-service/quickstarts/setup-platform?tabs=linux&pivots=programming-language-python

Read more comments on GitHub >

github_iconTop Results From Across the Web

ErrorDetails=Runtime error: Failed to initialize platform ...
Describe the bug We are deploying containers via IOT hub and we use speech Cognitive Service. Our containers must interact with Linux host ......
Read more >
How to fix Microsoft Cognitive Speech error "Failed ...
How to fix Microsoft Cognitive Speech error "Failed to initialize platform (azure-c-shared)"? ... I am using Microsoft.CognitiveServices.Speech ( ...
Read more >
Runtime error: Failed to initialize platform (azure-c-shared) ...
I have installed this SDK in AWS lambda through the lambda layer. Please sign in to rate this answer.
Read more >
Error with Microsoft.CognitiveServices.Speech - Python
Code: speech_synthesizer = SpeechSynthesizer(speech_config=speech_config). RuntimeError: Exception with error code: [CALL STACK BEGIN].
Read more >
Question regarding deploy azure speech sdk on streamlit
Question regarding deploy azure speech sdk on streamlit ... Runtime error: Failed to initialize platform (azure-c-shared). Error: 2153.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found