Search code examples
python-3.xgoogle-api-python-clientgoogle-speech-api

Google speech-to-text api fails with multiprocessing python


I'm newbie and trying to use the google-cloud speech-to-text with python and multiprocessing. Here is a simple example to reproduce my issue. I'm running the code on Windows.

When I run the code without multiprocessing, it works fine.

import io
from tqdm import tqdm
from multiprocessing import Pool, freeze_support, cpu_count

from google.cloud import speech
from google.cloud.speech import enums
from google.cloud.speech import types

# Instantiates a client
CLIENT = speech.SpeechClient()

def speech_to_text(file_name, language= "en-US"):

    with io.open(file_name, 'rb') as audio_file:
        content = audio_file.read()
        audio = types.RecognitionAudio(content=content)

    config = types.RecognitionConfig(
        encoding=enums.RecognitionConfig.AudioEncoding.ENCODING_UNSPECIFIED,
        sample_rate_hertz=16000,
        language_code= language)
    # Detects speech in the audio file
    response = CLIENT.recognize(config, audio)
    transcript = ""
    if len(response.results):
        transcript = response.results[0].alternatives[0].transcript
    return transcript

def worker(ix):
    audio_file_name = "audio.mp3"
    transcript = speech_to_text(audio_file_name)

if __name__ == "__main__":

    n_cores = cpu_count() - 1
    freeze_support()  # for Windows support
    with Pool(n_cores) as p:
        max_ = len(range(2))
        with tqdm(total=max_) as pbar:
            for i, result in enumerate(tqdm(p.imap_unordered(worker, range(2)))):
                pbar.update()

Here is the Error message that I get:

Traceback (most recent call last):
  File "C:\Users\me\Anaconda3\lib\multiprocessing\spawn.py", line 114, in _main
    prepare(preparation_data)
  File "C:\Users\me\Anaconda3\lib\multiprocessing\spawn.py", line 225, in prepare
    _fixup_main_from_path(data['init_main_from_path'])
  File "C:\Users\me\Anaconda3\lib\multiprocessing\spawn.py", line 277, in _fixup_main_from_path
    run_name="__mp_main__")
  File "C:\Users\me\Anaconda3\lib\runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "C:\Users\me\Anaconda3\lib\runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "C:\Users\me\Anaconda3\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\me\Desktop\outCaptcha\multiproc.py", line 10, in <module>
    from google.cloud import speech
  File "C:\Users\me\.virtualenvs\outCaptcha\lib\site-packages\google\cloud\speech.py", line 20, in <module>
    from google.cloud.speech_v1 import SpeechClient
  File "C:\Users\me\.virtualenvs\outCaptcha\lib\site-packages\google\cloud\speech_v1\__init__.py", line 17, in <module>
    from google.cloud.speech_v1.gapic import speech_client
  File "C:\Users\me\.virtualenvs\outCaptcha\lib\site-packages\google\cloud\speech_v1\gapic\speech_client.py", line 23, in <module>
    import google.api_core.client_options
  File "C:\Users\me\.virtualenvs\outCaptcha\lib\site-packages\google\api_core\__init__.py", line 23, in <module>
    __version__ = get_distribution("google-api-core").version
  File "C:\Users\me\AppData\Roaming\Python\Python37\site-packages\pkg_resources\__init__.py", line 481, in get_distribution
    dist = get_provider(dist)
  File "C:\Users\me\AppData\Roaming\Python\Python37\site-packages\pkg_resources\__init__.py", line 357, in get_provider
    return working_set.find(moduleOrReq) or require(str(moduleOrReq))[0]
  File "C:\Users\me\AppData\Roaming\Python\Python37\site-packages\pkg_resources\__init__.py", line 900, in require
    needed = self.resolve(parse_requirements(requirements))
  File "C:\Users\me\AppData\Roaming\Python\Python37\site-packages\pkg_resources\__init__.py", line 786, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'google-api-core' distribution was not found and is required by the application

Thanks a lot for your help. Please let me know if you need any details about the issue


Solution

  • In my case this solved the problem.

    • easy_install --upgrade google-api-core
    • easy_install --upgrade google-cloud-speech

    I hope this helps.