0

So, question is on title, I can create speech to text stream to one user and it works nice, but when I try to connect second user, it works bad for all, sometimes it works good for one user, so question is, can I create sppech-to-text stream for two or more users at same time with one GOOGLE_APPLICATION_CREDENTIALS or each user need start his own project?

Bill Johan
  • 83
  • 1
  • 7

1 Answers1

2

You should be able to create multiple streaming threads by using the same StreamingRecognize() client, the ones that can be used to send the requests in parallel. You can take a look on this and this Github posts where it is discussed this topic.

I suggest you to try this alternative and verify if you can perform these streaming calls by creating 2 different objects or clients, such as:

client = speech.SpeechClient()

responses = client.streaming_recognize()
responses2 = client.streaming_recognize()

On the other hand, if you want to make audio recognitions for batch, it is rather recommended to use synchronous or asynchronous methods.

Update. Adding an example with multiple threads in python

import io
import os
import copy

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

import threading

# Instantiates a client
# The name of the audio file to transcribe
file_name = os.path.join(
    os.path.dirname(__file__),
    'resources',
    <Audio_File_Name>)

#Using same client
client = speech.SpeechClient()

def streaming(thread):

    #Using different clients
    #client = speech.SpeechClient()


    with io.open(file_name, 'rb') as audio_file:

        content = audio_file.read()
        content2 = copy.deepcopy(content) 

        # In practice, stream should be a generator yielding chunks of audio data.
        stream = [content]
        requests = (types.StreamingRecognizeRequest(audio_content=chunk)
                                       for chunk in stream)

    config = types.RecognitionConfig(
        encoding=enums.RecognitionConfig.AudioEncoding.LINEAR16,
        sample_rate_hertz=16000,
        language_code='en-US',
        max_alternatives=1,
        enable_automatic_punctuation=True,
        model='video',
        enable_word_time_offsets=True)


    streaming_config = types.StreamingRecognitionConfig(
            config=config,
            interim_results=True)

    # Detects speech in the audio file
    responses = client.streaming_recognize(streaming_config, requests)
    for response in responses:
        for result in response.results:
            print('{}: Transcript: {}'.format(thread, result.alternatives[0].transcript))
            #print('isFinal: {}'.format(result.is_final))

thread1 = threading.Thread(name="thread1", target=streaming, args=('1',))
thread1.start()
thread2 = threading.Thread(name="thread2", target=streaming, args=('2',))
thread2.start()

print(threading.enumerate())
thread1.join()
thread2.join()
exit(0)
DiegoPiket
  • 56
  • 2
  • thx a lot! I'll try, did second speech clients before, maybe this caused problems, never tried second streaming call to same speech client sounds right. – Bill Johan Feb 28 '20 at 07:22
  • I mean tried both methods, new client and new streaming call to existing client, but did them in different threads, not in one function as you show – Bill Johan Feb 28 '20 at 07:51
  • Thanks for sharing! Can you show how did you implemented it to have a better reference? – DiegoPiket Feb 28 '20 at 13:36
  • 1
    I tested it now with other user and all seems working, before I tested from same pc in different tabs, strange, no changes to code, seems it's some kind of a bug on socket-io.js or I don't even know, I'll check your idea with requests in one thread someday when wil have free time – Bill Johan Feb 28 '20 at 16:14