Chris Thompson
Chris Thompson

Reputation: 31

Difficulty accessing Google Text-to-Speech API from within Flask app

I am having difficulties accessing Google's texttospeech Python API from within a Flask app. The app is running in a container with Debian 10, Nginx/Gunicorn, and Python 3.7 installed. In the code below, the client connects successfully, but the synthesize_speech request hangs indefinitely (without any error message). When I run the same code from a Python script in the same container without Flask, the speech synthesis request is successful. However, I can call other external APIs, such as those at AWS, from my Flask app without any problems.

What could be causing this or how I could go about diagnosing the problem? I have tried switching to version 1.0.1 of the texttospeech library, but without success. Presumably the problem isn't my credentials, which I believe I have set up correctly, as otherwise the connection request wouldn't be successful.

from google.cloud import texttospeech

# Connect (this is successful)
client = texttospeech.TextToSpeechClient()
input_text = texttospeech.SynthesisInput(text="God dag")
voice_parameters = texttospeech.VoiceSelectionParams(
    language_code="sv-SE",
    name="sv-SE-Wavenet-A"
)
audio_config = texttospeech.AudioConfig(
    audio_encoding=texttospeech.AudioEncoding.MP3
)

# Synthesize speech (this never completes)
response = client.synthesize_speech(
    request={
        "input": input_text,
        "voice": voice_parameters,
        "audio_config": audio_config
    }
)

pip freeze

google-api-core==1.22.1
google-auth==1.20.1
google-cloud-texttospeech==2.2.0
googleapis-common-protos==1.52.0
grpcio==1.31.0
...

Upvotes: 2

Views: 336

Answers (1)

Chris Thompson
Chris Thompson

Reputation: 31

It turns out the issue was caused by monkey_patching in gunicorn's gevent worker class. I have managed to resolve the issue by changing the worker_class in gunicorn to "sync", as suggested on this page for a similar issue:

https://github.com/googleapis/google-cloud-python/issues/5848

Upvotes: 1

Related Questions