From 853fba0198777fc28cf923067e99f919cca1213c Mon Sep 17 00:00:00 2001 From: Suvigya Jain <42478893+suvigyajain0101@users.noreply.github.com> Date: Mon, 14 Apr 2025 13:31:39 +0100 Subject: [PATCH 1/2] Enforce 25Kb limit for infinite transcription Current implementation breaks when a new stream is created, even under 5 min limit. This is due to the missing logic to handle 25KB stream size limit [1] Updated the 'generator' function to yield data as soon as API limit is reached. [1] - https://github.com/GoogleCloudPlatform/python-docs-samples/issues/12053 --- .../transcribe_streaming_infinite_v2.py | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/speech/microphone/transcribe_streaming_infinite_v2.py b/speech/microphone/transcribe_streaming_infinite_v2.py index 54b48439131..2c9098d36e3 100644 --- a/speech/microphone/transcribe_streaming_infinite_v2.py +++ b/speech/microphone/transcribe_streaming_infinite_v2.py @@ -40,6 +40,8 @@ STREAMING_LIMIT = 240000 # 4 minutes SAMPLE_RATE = 16000 CHUNK_SIZE = int(SAMPLE_RATE / 10) # 100ms +# 25KB API limit. Increasing this will throw error +MAX_STREAMING_CHUNK = 25 * 1024 RED = "\033[0;31m" GREEN = "\033[0;32m" @@ -213,7 +215,23 @@ def generator(self: object) -> object: except queue.Empty: break - yield b"".join(data) + # Enforce max streaming chunk size supported by the API + combined_size = sum(len(chunk) for chunk in data) + if combined_size <= MAX_STREAMING_CHUNK: + yield b"".join(data) + else: + run_chunks = [] + run_size = 0 + for chunk in data: + if len(chunk) + run_size > MAX_STREAMING_CHUNK: + yield b"".join(run_chunks) + run_chunks = [chunk] + run_size = len(chunk) + else: + run_chunks.append(chunk) + run_size += len(chunk) + if run_chunks: + yield b"".join(run_chunks) def listen_print_loop(responses: object, stream: object) -> None: From 5301bd7e94f779475b1fb43aacc9651dba860fb9 Mon Sep 17 00:00:00 2001 From: Jennifer Davis Date: Mon, 14 Apr 2025 09:58:08 -0700 Subject: [PATCH 2/2] nit: Update speech/microphone/transcribe_streaming_infinite_v2.py Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> --- speech/microphone/transcribe_streaming_infinite_v2.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/speech/microphone/transcribe_streaming_infinite_v2.py b/speech/microphone/transcribe_streaming_infinite_v2.py index 2c9098d36e3..4ad6527a56b 100644 --- a/speech/microphone/transcribe_streaming_infinite_v2.py +++ b/speech/microphone/transcribe_streaming_infinite_v2.py @@ -40,8 +40,8 @@ STREAMING_LIMIT = 240000 # 4 minutes SAMPLE_RATE = 16000 CHUNK_SIZE = int(SAMPLE_RATE / 10) # 100ms -# 25KB API limit. Increasing this will throw error -MAX_STREAMING_CHUNK = 25 * 1024 +# 25KB API limit for streaming requests. Exceeding this limit will result in an error. +MAX_STREAMING_CHUNK = 25 * 1024 RED = "\033[0;31m" GREEN = "\033[0;32m"