简体   繁体   中英

How make Google Speech to Text API Async

I am using Google Speech to Text API to convert audio files. The issue is that in Google Speech to text, we have to wait for the operation to complete even thou we made it async as its documentation says by using long_running_recognize() .

Is there a way we can make it completely async? So that when we upload an audio file, it gives us an operation_id that we can use to download the transcription any time Asynchronously.

This is quite curious and the answer is Yes but No directly.

When sending an async request from any Client library you will receive an Operation object which contains two important elements:

  • Name. This will be the identifier for the request that has been sent.

  • Done. This is the boolean that tells us if the request has finished or not.

On your implementation you can send the request with long_running_recognize , get the name and go back to query that name with:

 curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) \
 -H "Content-Type: application/json; charset=utf-8" \
 "https://speech.googleapis.com/v1/operations/your-operation-name"

This one is from the docs on how to transcript long audios. You should receive an answer from it even if it has not finished.

Take a look at this Github issue where the code of the user reached a timeout from the code itself and it made them think that the request was not finished but they were able to retrieve data after reaching the timeout.

What does the Github issue tell us? Even if a script is reaching a timeout waiting for a response, the request is still handled by the Speech to text service. I am not sure if stopping the script would keep the actual request to Speech to Text running, but I can think of the following:

  • Have your script running on a background process or in a thread-like implementation.

  • You can remove the transcript print from it since you want to take a look at this data later on.

  • Make sure to print the Name of your operation and save it for later.

You can use the aforementioned method to retrieve the data or use another script to read it just by passing the Operation Name whenever you want. With these steps we could emulate an asynchronous call to the service.

Hope this is helpful: :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM