Skip to content

Commit 919dda5

Browse files
samples: add multi region transcribe sample (#394)
shows how to change default endpoint
1 parent ce83132 commit 919dda5

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2020 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 speech_transcribe_with_multi_region_gcs]
20+
21+
import com.google.cloud.speech.v1.RecognitionAudio;
22+
import com.google.cloud.speech.v1.RecognitionConfig;
23+
import com.google.cloud.speech.v1.RecognitionConfig.AudioEncoding;
24+
import com.google.cloud.speech.v1.RecognizeResponse;
25+
import com.google.cloud.speech.v1.SpeechClient;
26+
import com.google.cloud.speech.v1.SpeechRecognitionAlternative;
27+
import com.google.cloud.speech.v1.SpeechRecognitionResult;
28+
import com.google.cloud.speech.v1.SpeechSettings;
29+
import java.io.IOException;
30+
import java.util.List;
31+
32+
public class SpeechTranscribeMultiRegion {
33+
34+
public void speechTranscribeMultiRegion() throws Exception {
35+
String uriPath = "gs://cloud-samples-tests/speech/brooklyn.flac";
36+
speechTranscribeMultiRegion(uriPath);
37+
}
38+
39+
/**
40+
* Transcribe a remote audio file with multi-channel recognition
41+
*
42+
* @param gcsUri the path to the audio file
43+
*/
44+
public static void speechTranscribeMultiRegion(String gcsUri) throws Exception {
45+
// Use the SpeechSettings to initialize the SpeechClient with the new endpoint.
46+
String endPoint = "eu-speech.googleapis.com:443";
47+
SpeechSettings speechSettings =
48+
SpeechSettings.newBuilder()
49+
.setEndpoint(endPoint)
50+
.build();
51+
52+
// Instantiates a client with GOOGLE_APPLICATION_CREDENTIALS
53+
try (SpeechClient speech = SpeechClient.create(speechSettings)) {
54+
55+
// Configure remote file request
56+
RecognitionConfig config =
57+
RecognitionConfig.newBuilder()
58+
.setEncoding(AudioEncoding.FLAC)
59+
.setLanguageCode("en-US")
60+
.setSampleRateHertz(16000)
61+
.build();
62+
63+
// Set the remote path for the audio file
64+
RecognitionAudio audio = RecognitionAudio.newBuilder().setUri(gcsUri).build();
65+
66+
// Use blocking call to get audio transcript
67+
RecognizeResponse response = speech.recognize(config, audio);
68+
List<SpeechRecognitionResult> results = response.getResultsList();
69+
70+
for (SpeechRecognitionResult result : results) {
71+
// There can be several alternative transcripts for a given chunk of speech. Just use the
72+
// first (most likely) one here.
73+
SpeechRecognitionAlternative alternative = result.getAlternativesList().get(0);
74+
System.out.printf("Transcription: %s\n", alternative.getTranscript());
75+
}
76+
}
77+
}
78+
}
79+
// [END speech_transcribe_with_multi_region_gcs]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2020 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+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import java.io.ByteArrayOutputStream;
22+
import java.io.IOException;
23+
import java.io.PrintStream;
24+
import org.junit.After;
25+
import org.junit.Before;
26+
import org.junit.Test;
27+
import org.junit.runner.RunWith;
28+
import org.junit.runners.JUnit4;
29+
30+
@RunWith(JUnit4.class)
31+
@SuppressWarnings("checkstyle:abbreviationaswordinname")
32+
public class SpeechTranscribeMultiRegionTest {
33+
private static final String AUDIO_FILE = "gs://cloud-samples-tests/speech/brooklyn.flac";
34+
private ByteArrayOutputStream bout;
35+
private PrintStream stdout;
36+
private PrintStream out;
37+
38+
@Before
39+
public void setUp() {
40+
bout = new ByteArrayOutputStream();
41+
out = new PrintStream(bout);
42+
stdout = System.out;
43+
System.setOut(out);
44+
}
45+
46+
@After
47+
public void tearDown() {
48+
System.setOut(stdout);
49+
}
50+
51+
@Test
52+
public void testSpeechTranscribeMultiRegion() throws Exception {
53+
SpeechTranscribeMultiRegion.speechTranscribeMultiRegion(AUDIO_FILE);
54+
String got = bout.toString();
55+
assertThat(got).contains("how old is the Brooklyn Bridge");
56+
}
57+
}

0 commit comments

Comments
 (0)