Azure cognitive services are not working with PyInstaller build when launching from .app

Hector Haffenden 0 Reputation points
2023-05-07T09:27:27.48+00:00

Originally posted to stack overflow

Building a app in Python, Mac OS, PyInstaller to package app, with speech to text from Azure's Python speechsdk (https://learn.microsoft.com/en-us/python/api/overview/azure/cognitive-services?view=azure-python). I've spent the past few weeks attempting to get an app I can distribute, but am running into pyinstaller + Azure cognitive services errors.

I can successfully build an app when I use the COLLECT in the .spec file. However, this produces just a .app file with the terminal inside (rather than a full .app file with resources, frameworks etc...). This full app runs as normal. I think, it's important to get the full .app framework right, to get the application fully notarized. I've successfully signed, notarized and stapled the .app produced from COLLECT, but it fails to run when downloaded from another source e.g. google drive, dropbox etc...

However, when I build an application with BUNDLE, I get the full app structure, and everything runs - even the Azure cognitive services functions seem to build, however, when I run the app as a application (double click the .app file) no speech is transcribed.

But when I double click the Unix executable, everything runs as normal.

Others with similar pyinstaller problems (.app not working, unix executable works), it's normally to do with the filepath.

When a .app is run, it runs from /

When a unix executable is run, it runs from /Users/username/

It's possible this is causing the error, but displaying all of these filepaths inside the speechsdk, it seems like they are working as expected.

In the .spec file I add the Azure package in the datas, and have tried adding the dylib as a binary libMicrosoft.CognitiveServices.Speech.core.dylib

I've learnt most of the above by building a minimal example, but am stuck on what the right next steps are to getting this working with BUNDLE.

Azure AI Speech
Azure AI Speech
An Azure service that integrates speech processing into apps and services.
1,471 questions
{count} votes

1 answer

Sort by: Most helpful
  1. VasimTamboli 4,780 Reputation points
    2023-05-07T10:44:57.91+00:00

    It seems that the issue may be related to the file paths used by the Azure speech SDK. Since the SDK is unable to find the required files when running from the .app file, but can find them when running from the Unix executable, it is possible that the file paths are not being set correctly.

    One potential solution would be to explicitly set the file paths used by the SDK in your Python code. You can try using the os module to get the current working directory, and then append the required file paths to this directory. For example:

    makefileCopy code
    import os
    from azure.cognitiveservices.speech import SpeechConfig
    
    # Get the current working directory
    cwd = os.getcwd()
    
    # Set the file paths for the speech SDK
    speech_sdk_path = os.path.join(cwd, "path/to/speech/sdk")
    dylib_path = os.path.join(cwd, "path/to/dylib/libMicrosoft.CognitiveServices.Speech.core.dylib")
    
    # Set the speech config with the file paths
    speech_config = SpeechConfig(subscription="YOUR_SUBSCRIPTION_KEY", region="YOUR_REGION", speech_recognition_language="en-US")
    speech_config.set_property_by_name("speechsdk.imports.path", speech_sdk_path)
    speech_config.set_property_by_name("speechsdk.imports.dylib_path", dylib_path)
    

    Replace the "path/to/speech/sdk" and "path/to/dylib/libMicrosoft.CognitiveServices.Speech.core.dylib" with the actual file paths used by the SDK in your code.

    Another potential solution would be to modify the .spec file to explicitly include the required files and directories used by the SDK. You can try adding the following lines to your .spec file:

    cssCopy code
    binaries = [('path/to/dylib/libMicrosoft.CognitiveServices.Speech.core.dylib', '.')]
    
    datas = [('path/to/speech/sdk/*', 'path/to/speech/sdk/')]
    

    Again, replace "path/to/speech/sdk" and "path/to/dylib/libMicrosoft.CognitiveServices.Speech.core.dylib" with the actual file paths used by the SDK in your code.