8000 added streaming for V2 · amirx-cloud/java-docs-samples@0ab846e · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ab846e

Browse files
committed
added streaming for V2
1 parent c076851 commit 0ab846e

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed

speech/resources/brooklyn_bridge.wav

56.7 KB
Binary file not shown.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.speech;
18+
19+
// [START TranscribeStreamingV2]
20+
21+
import com.google.api.gax.rpc.BidiStream;
22+
import com.google.cloud.speech.v2.*;
23+
import com.google.protobuf.ByteString;
24+
25+
import java.io.IOException;
26+
import java.nio.file.Files;
27+
import java.nio.file.Path;
28+
import java.nio.file.Paths;
29+
import java.util.ArrayList;
30+
import java.util.Iterator;
31+
import java.util.List;
32+
33+
public class TranscribeStreamingV2 {
34+
private static final String PROJECT_ID = System.getenv("GOOGLE_CLOUD_PROJECT");
35+
36+
// Transcribes audio from an audio file stream using Google Cloud Speech-to-Text API.
37+
// Args:
38+
// streamFile (String): Path to the local audio file to be transcribed.
39+
// Example: "resources/audio.wav"
40+
// Returns:
41+
// List<StreamingRecognizeResponse>: A list of objects.
42+
// Each response includes the transcription results for the corresponding audio segment.
43+
//
44+
public static List<StreamingRecognizeResponse> transcribeStreamingV2(String streamFile) throws IOException {
45+
List<StreamingRecognizeResponse> responses = new ArrayList<>();
46+
// Instantiates a client
47+
try (SpeechClient client = SpeechClient.create()) {
48+
49+
Path path = Paths.get(streamFile);
50+
byte[] audioContent = Files.readAllBytes(path);
51+
52+
// In practice, stream should be a generator yielding chunks of audio data
53+
int chunkLength = audioContent.length / 5;
54+
List<byte[]> stream = new ArrayList<>();
55+
for (int i = 0; i < audioContent.length; i += chunkLength) {
56+
int end = Math.min(i + chunkLength, audioContent.length);
57+
byte[] chunk = new byte[end - i];
58+
System.arraycopy(audioContent, i, chunk, 0, end - i);
59+
stream.add(chunk);
60+
}
61+
62+
63+
List<StreamingRecognizeRequest> audioRequests = new ArrayList<>();
64+
for (byte[] audio : stream) {
65+
audioRequests.add(StreamingRecognizeRequest.newBuilder().setAudio(ByteString.copyFrom(audio)).build());
66+
}
67+
68+
RecognitionConfig recognitionConfig = RecognitionConfig.newBuilder()
69+
.setAutoDecodingConfig(AutoDetectDecodingConfig.getDefaultInstance())
70+
.addLanguageCodes("en-US")
71+
.setModel("long")
72+
.build();
73+
74+
StreamingRecognitionConfig streamingConfig = StreamingRecognitionConfig.newBuilder()
75+
.setConfig(recognitionConfig)
76+
.build();
77+
78+
StreamingRecognizeRequest configRequest = StreamingRecognizeRequest.newBuilder()
79+
.setRecognizer(String.format("projects/%s/locations/global/recognizers/_", PROJECT_ID))
80+
.setStreamingConfig(streamingConfig)
81+
.build();
82+
83+
84+
List<StreamingRecognizeRequest> requests = new ArrayList<>();
85+
requests.add(configRequest);
86+
requests.addAll(audioRequests);
87+
88+
BidiStream<StreamingRecognizeRequest, StreamingRecognizeResponse> stream1 = client.streamingRecognizeCallable().call();
89+
for (StreamingRecognizeRequest request : requests) {
90+
stream1.send(request);
91+
}
92+
for (int i = 0; i < requests.size(); i++) {
93+
StreamingRecognizeRequest request = requests.get(i);
94+
System.out.println("Request " + (i + 1) + ": " + request);
95+
// You might want to print specific parts of the request, like the audio or config, rather than the entire object
96+
if (request.hasAudio()) {
97+
System.out.println("Audio: " + request.getAudio().size() + " bytes");
98+
}
99+
if (request.hasStreamingConfig()) {
100+
System.out.println("Streaming Config: " + request.getStreamingConfig());
101+
}
102+
}
103+
stream1.closeSend();
104+
105+
Iterator<StreamingRecognizeResponse> responseIterator = stream1.iterator();
106+
while (responseIterator.hasNext()) {
107+
StreamingRecognizeResponse response = responseIterator.next();
108+
System.out.println(response);
109+
// Process the response and extract the transcript
110+
System.out.println("Transcript: " + response.getResultsList().get(0).getAlternativesList().get(0).getTranscript());
111+
responses.add(response);
112+
}
113+
114+
115+
}
116+
return responses;
117+
}
118+
119+
public static void main(String[] args) throws IOException {
120+
List<StreamingRecognizeResponse> responses = transcribeStreamingV2("./resources/brooklyn_bridge.wav");
121+
}
122+
}
123+
// [END TranscribeStreamingV2]
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2025 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.speech;
18+
import com.google.cloud.speech.v2.StreamingRecognizeResponse;
19+
import com.google.common.truth.Truth;
20+
import org.junit.Test;
21+
import java.io.IOException;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
25+
import java.util.List;
26+
import java.util.regex.Pattern;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
@RunWith(JUnit4.class)
31+
public class TranscribeStreamingV2IT {
32+
33+
@Test
34+
public void testTranscribeStreamingV2_Success() throws IOException {
35+
// Create a dummy audio file for testing
36+
String testFilePath = "/Users/amirx/IdeaProjects/java-docs-samples/speech/resources/speech_snippets_resources_audio.wav";
37+
38+
// Call the method to test
39+
List<StreamingRecognizeResponse> responses = TranscribeStreamingV2.transcribeStreamingV2(testFilePath);
40+
41+
// Assert the transcript
42+
String transcript = "";
43+
for (StreamingRecognizeResponse response : responses) {
44+
if (response.getResultsCount() > 0) {
45+
transcript += response.getResults(0).getAlternatives(0).getTranscript();
46+
}
47+
}
48+
// Use a regex to match the expected transcript
49+
Pattern pattern = Pattern.compile("how old is the Brooklyn Bridge", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
50+
Truth.assertThat(pattern.matcher(transcript).find()).isTrue();
51+
52+
}
53+
}
54+

0 commit comments

Comments
 (0)
0