Skip to content

Commit ff25b07

Browse files
amygdalaJerjou Cheng
authored and
Jerjou Cheng
committed
initial checkin of java grpc speech API examples.
Change-Id: Ifc48ab25bfea7ff5ce9ae11caf5c6c483febe705
1 parent 847bcc5 commit ff25b07

32 files changed

+3022
-0
lines changed

speech/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Cloud Pub/Sub samples for Java
2+
3+
This directory contains several samples for the [Cloud Speech API](https://cloud.google.com/speech/)
4+
with Java.
5+
6+
- [grpc](grpc)
7+
8+
A sample for accessing Cloud Speech streaming and non streaming apis with [gRPC](http://www.grpc.io/).

speech/grpc/README.md

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Cloud Speech API gRPC samples for Java
2+
3+
This is a sample repo for accessing the [Google Cloud Speech API](http://cloud.google.com/speech) with
4+
[gRPC](http://www.grpc.io/) client library.
5+
6+
7+
## Prerequisites
8+
9+
### Enable the Speech API
10+
11+
If you have not already done so, [enable the Google Cloud Speech API for your project](https://console.developers.google.com/apis/api/speech.googleapis.com/overview).
12+
You must be whitelisted to do this.
13+
14+
15+
### Download and install Java and Maven
16+
17+
Install [Java7 or
18+
higher](http://www.oracle.com/technetwork/java/javase/downloads/jre7-downloads-1880261.html).
19+
20+
This sample uses the [Apache Maven][maven] build system. Before getting started, be
21+
sure to [download][maven-download] and [install][maven-install] it. When you use
22+
Maven as described here, it will automatically download the needed client
23+
libraries.
24+
25+
[maven]: https://maven.apache.org
26+
[maven-download]: https://maven.apache.org/download.cgi
27+
[maven-install]: https://maven.apache.org/install.html
28+
29+
30+
### Set Up to Authenticate With Your Project's Credentials
31+
32+
The example uses a service account for OAuth2 authentication.
33+
So next, set up to authenticate with the Speech API using your project's
34+
service account credentials.
35+
36+
Visit the [Cloud Console](https://console.developers.google.com), and navigate to:
37+
`API Manager > Credentials > Create credentials >
38+
Service account key > New service account`.
39+
Create a new service account, and download the json credentials file.
40+
41+
Then, set
42+
the `GOOGLE_APPLICATION_CREDENTIALS` environment variable to point to your
43+
downloaded service account credentials before running this example:
44+
45+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/your/credentials-key.json
46+
47+
If you do not do this, you will see an error that looks something like this when
48+
you run the example scripts:
49+
`WARNING: RPC failed: Status{code=PERMISSION_DENIED, description=Request had insufficient authentication scopes., cause=null}`.
50+
See the
51+
[Cloud Platform Auth Guide](https://cloud.google.com/docs/authentication#developer_workflow)
52+
for more information.
53+
54+
## Build the application
55+
56+
Then, build the program:
57+
58+
```sh
59+
$ mvn package
60+
```
61+
62+
or
63+
64+
```sh
65+
$ mvn compile
66+
$ mvn assembly:single
67+
```
68+
69+
## Run the clients
70+
71+
These programs return the transcription of the audio file you provided. Please
72+
note that the audio file must be in RAW format. You can use `sox`
73+
(available, e.g. via [http://sox.sourceforge.net/](http://sox.sourceforge.net/)
74+
or [homebrew](http://brew.sh/)) to convert audio files to raw format.
75+
76+
### Run the non-streaming client
77+
78+
You can run the batch client like this:
79+
80+
```sh
81+
$ bin/speech-sample-nonstreaming.sh --host=speech.googleapis.com --port=443 \
82+
--file=<audio file path> --sampling=<sample rate>
83+
```
84+
85+
Try a streaming rate of 16000 and the included sample audio file, as follows:
86+
87+
```sh
88+
$ bin/speech-sample-nonstreaming.sh --host=speech.googleapis.com --port=443 \
89+
--file=resources/audio.raw --sampling=16000
90+
```
91+
92+
### Run the streaming client
93+
94+
You can run the streaming client as follows:
95+
96+
```sh
97+
$ bin/speech-sample-streaming.sh --host=speech.googleapis.com --port=443 \
98+
--file=resources/audio.raw --sampling=16000
99+
```
100+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
# Copyright 2016 Google Inc. All Rights Reserved.
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+
export SRC_DIR=$(cd "$(dirname "$0")/.."; pwd)
17+
java -cp ${SRC_DIR}/target/grpc-sample-1.0-jar-with-dependencies.jar \
18+
com.google.cloud.speech.grpc.demos.NonStreamingRecognizeClient "$@"
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
# Copyright 2016 Google Inc. All Rights Reserved.
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+
export SRC_DIR=$(cd "$(dirname "$0")/.."; pwd)
17+
java -cp ${SRC_DIR}/target/grpc-sample-1.0-jar-with-dependencies.jar \
18+
com.google.cloud.speech.grpc.demos.RecognizeClient "$@"

speech/grpc/pom.xml

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>com.google.cloud.speech</groupId>
7+
<artifactId>grpc-sample</artifactId>
8+
<version>1.0</version>
9+
<packaging>jar</packaging>
10+
11+
<name>speech-grpc-sample</name>
12+
<url>https://cloud.google.com/speech/</url>
13+
<inceptionYear>2016</inceptionYear>
14+
15+
<licenses>
16+
<license>
17+
<name>Apache 2</name>
18+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
19+
<distribution>repo</distribution>
20+
</license>
21+
</licenses>
22+
23+
<organization>
24+
<name>Google</name>
25+
<url>http://www.google.com</url>
26+
</organization>
27+
28+
<developers>
29+
<developer>
30+
<id>ejonte@google.com</id>
31+
<name>Erik Jonte</name>
32+
<email>ejonte@google.com</email>
33+
<url>http://www.google.com</url>
34+
<organization>Google</organization>
35+
<organizationUrl>http://www.google.com</organizationUrl>
36+
<roles>
37+
<role>developer</role>
38+
</roles>
39+
<timezone>-8</timezone>
40+
</developer>
41+
</developers>
42+
43+
<properties>
44+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
45+
</properties>
46+
47+
<profiles>
48+
<profile>
49+
<id>staged</id>
50+
<repositories>
51+
<repository>
52+
<id>snapshots-repo</id>
53+
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
54+
<releases>
55+
<enabled>false</enabled>
56+
</releases>
57+
<snapshots>
58+
<enabled>true</enabled>
59+
</snapshots>
60+
</repository>
61+
</repositories>
62+
</profile>
63+
<profile>
64+
<id>jdk7</id>
65+
<activation>
66+
<jdk>1.7</jdk>
67+
</activation>
68+
<properties>
69+
<jdk.version>1.7</jdk.version>
70+
</properties>
71+
</profile>
72+
<profile>
73+
<id>jdk8</id>
74+
<activation>
75+
<jdk>1.8</jdk>
76+
</activation>
77+
<properties>
78+
<jdk.version>1.8</jdk.version>
79+
</properties>
80+
</profile>
81+
<profile>
82+
<id>fedora</id>
83+
<activation>
84+
<property>
85+
<name>os.detected.classifier</name>
86+
<value>os.detected.release.fedora</value>
87+
</property>
88+
</activation>
89+
<properties>
90+
<tcnative.classifier>${os.detected.classifier}-fedora</tcnative.classifier>
91+
</properties>
92+
</profile>
93+
<profile>
94+
<id>non-fedora</id>
95+
<activation>
96+
<property>
97+
<name>os.detected.classifier</name>
98+
<value>!os.detected.release.fedora</value>
99+
</property>
100+
</activation>
101+
<properties>
102+
<tcnative.classifier>${os.detected.classifier}</tcnative.classifier>
103+
</properties>
104+
</profile>
105+
</profiles>
106+
107+
<!-- // [START dependency] -->
108+
<dependencies>
109+
<dependency>
110+
<groupId>commons-cli</groupId>
111+
<artifactId>commons-cli</artifactId>
112+
<version>1.3.1</version>
113+
</dependency>
114+
<dependency>
115+
<groupId>com.google.guava</groupId>
116+
<artifactId>guava</artifactId>
117+
<version>19.0</version>
118+
</dependency>
119+
<dependency>
120+
<groupId>io.grpc</groupId>
121+
<artifactId>grpc-all</artifactId>
122+
<version>0.13.2</version>
123+
</dependency>
124+
<dependency>
125+
<groupId>com.google.oauth-client</groupId>
126+
<artifactId>google-oauth-client</artifactId>
127+
<version>1.21.0</version>
128+
</dependency>
129+
<dependency>
130+
<!--
131+
It is recommended to use OpenSSL: Statically Linked Netty
132+
for transport security. These steps do not use the TLS that
133+
comes with JDK (Jetty APLN/NPN), which is not
134+
recommended. See
135+
https://github.com/grpc/grpc-java/blob/master/SECURITY.md
136+
for details.
137+
-->
138+
<groupId>io.netty</groupId>
139+
<artifactId>netty-tcnative-boringssl-static</artifactId>
140+
<version>1.1.33.Fork14</version>
141+
<classifier>${tcnative.classifier}</classifier>
142+
</dependency>
143+
</dependencies>
144+
<!-- // [END dependency] -->
145+
146+
<pluginRepositories>
147+
<pluginRepository>
148+
<releases>
149+
<updatePolicy>never</updatePolicy>
150+
</releases>
151+
<snapshots>
152+
<enabled>false</enabled>
153+
</snapshots>
154+
<id>central</id>
155+
<name>Central Repository</name>
156+
<url>https://repo.maven.apache.org/maven2</url>
157+
</pluginRepository>
158+
<pluginRepository>
159+
<id>protoc-plugin</id>
160+
<url>https://dl.bintray.com/sergei-ivanov/maven/</url>
161+
</pluginRepository>
162+
</pluginRepositories>
163+
<!-- // [START os-maven-plugin] -->
164+
<build>
165+
<extensions>
166+
<extension>
167+
<groupId>kr.motd.maven</groupId>
168+
<artifactId>os-maven-plugin</artifactId>
169+
<version>1.4.1.Final</version>
170+
</extension>
171+
</extensions>
172+
<!-- // [END os-maven-plugin] -->
173+
<plugins>
174+
<plugin>
175+
<groupId>org.xolstice.maven.plugins</groupId>
176+
<artifactId>protobuf-maven-plugin</artifactId>
177+
<version>0.5.0</version>
178+
<configuration>
179+
<!--
180+
The version of protoc must match protobuf-java. If you
181+
don't depend on protobuf-java directly, you will be
182+
transitively depending on the protobuf-java version that
183+
grpc depends on.
184+
-->
185+
<protocArtifact>com.google.protobuf:protoc:3.0.0-beta-2:exe:${os.detected.classifier}</protocArtifact>
186+
<pluginId>grpc-java</pluginId>
187+
<pluginArtifact>io.grpc:protoc-gen-grpc-java:0.13.2:exe:${os.detected.classifier}</pluginArtifact>
188+
</configuration>
189+
<executions>
190+
<execution>
191+
<goals>
192+
<goal>compile</goal>
193+
<goal>compile-custom</goal>
194+
</goals>
195+
</execution>
196+
</executions>
197+
</plugin>
198+
<plugin>
199+
<groupId>org.codehaus.mojo</groupId>
200+
<artifactId>versions-maven-plugin</artifactId>
201+
<version>2.1</version>
202+
<executions>
203+
<execution>
204+
<phase>compile</phase>
205+
<goals>
206+
<goal>display-dependency-updates</goal>
207+
</goals>
208+
</execution>
209+
</executions>
210+
</plugin>
211+
<plugin>
212+
<artifactId>maven-compiler-plugin</artifactId>
213+
<version>3.1</version>
214+
<configuration>
215+
<source>${jdk.version}</source>
216+
<target>${jdk.version}</target>
217+
<showWarnings>true</showWarnings>
218+
<showDeprecation>false</showDeprecation>
219+
<compilerArgument>-Xlint:-options</compilerArgument>
220+
</configuration>
221+
</plugin>
222+
<plugin>
223+
<artifactId>maven-assembly-plugin</artifactId>
224+
<configuration>
225+
<descriptorRefs>
226+
<descriptorRef>jar-with-dependencies</descriptorRef>
227+
</descriptorRefs>
228+
</configuration>
229+
<executions>
230+
<execution>
231+
<id>simple-command</id>
232+
<phase>package</phase>
233+
<goals>
234+
<goal>attached</goal>
235+
</goals>
236+
</execution>
237+
</executions>
238+
</plugin>
239+
</plugins>
240+
<!-- // [START footer] -->
241+
</build>
242+
</project>
243+
<!-- // [END footer] -->

speech/grpc/resources/audio.raw

56.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)