Skip to content

Commit e67b014

Browse files
feat(speech): Add Speech-to-Text On-Prem sample (#4223)
* feat: adds speech-to-text onprem sample * Remove client options and change to f-strings * Move to onprem folder, add resource and README Co-authored-by: Leah E. Cole <[email protected]>
1 parent be30f47 commit e67b014

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed
Binary file not shown.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Cloud Speech-to-Text On-Prem Python Samples
4+
===============================================================================
5+
6+
7+
.. warning:: This product is only available to customers that have been granted access. Please `contact us`_ to request access to the Speech-to-Text On-Prem feature.
8+
9+
This directory contains samples for `Google Cloud Speech-to-Text On-Prem`_. Speech-to-Text On-Prem enables easy integration of Google speech recognition technologies into your on-prem solution.
10+
11+
12+
.. _Google Cloud Speech-to-Text On-Prem: https://cloud.google.com/speech-to-text/on-prem/priv/docs
13+
14+
.. _contact us: https://cloud.google.com/contact
15+
16+
.. _Google Cloud Speech-to-Text On-Prem: https://cloud.google.com/speech-to-text/on-prem/priv/docs
17+
18+
Setup
19+
-------------------------------------------------------------------------------
20+
21+
22+
Prepare and Deploy API
23+
+++++++++++++++++++++++
24+
25+
This sample requires you to have a Kubernetes cluster with the Speech-to-Text On-Prem service deployed. Follow the quickstart steps listed below:
26+
27+
#. `Setup IAM, Kubernetes, Billing`_
28+
29+
#. `Deploy the API using the UI or command line`_
30+
31+
#. `Query the API to ensure it's working`_
32+
33+
34+
.. _Query the API to ensure it's working:
35+
https://cloud.google.com/speech-to-text/on-prem/priv/docs/query
36+
37+
.. _Deploy the API using the UI or command line:
38+
https://cloud.google.com/speech-to-text/on-prem/priv/docs/deploy
39+
40+
.. _Setup IAM, Kubernetes, Billing:
41+
https://cloud.google.com/speech-to-text/on-prem/priv/docs/before-you-begin
42+
43+
Install Dependencies
44+
++++++++++++++++++++
45+
46+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
47+
48+
.. code-block:: bash
49+
50+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
51+
$ cd python-doc-samples/speech/cloud-client
52+
53+
#. Install `pip`_ and `virtualenv`_ if you do not already have them. You may want to refer to the `Python Development Environment Setup Guide`_ for Google Cloud Platform for instructions.
54+
55+
.. _Python Development Environment Setup Guide:
56+
https://cloud.google.com/python/setup
57+
58+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
59+
60+
.. code-block:: bash
61+
62+
$ virtualenv env
63+
$ source env/bin/activate
64+
65+
#. Install the dependencies needed to run the samples.
66+
67+
.. code-block:: bash
68+
69+
$ pip install -r requirements.txt
70+
71+
.. _pip: https://pip.pypa.io/
72+
.. _virtualenv: https://virtualenv.pypa.io/
73+
74+
Samples
75+
-------------------------------------------------------------------------------
76+
77+
transcribe_onprem
78+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
79+
80+
81+
You can run this sample one of two ways, using a **public IP**:
82+
83+
.. code-block:: bash
84+
85+
# Using a Public IP
86+
$ python transcribe_onprem.py --file_path="../resources/two_channel_16k.wav" --api_endpoint=${PUBLIC_IP}:443
87+
88+
or by using a **cluster level IP**:
89+
90+
.. code-block:: bash
91+
92+
# Using a cluster level IP
93+
$ kubectl port-forward -n $NAMESPACE $POD 10000:443
94+
$ python transcribe_onprem.py --file_path="../resources/two_channel_16k.wav" --api_endpoint="0.0.0.0:10000"
95+
96+
The client library
97+
-------------------------------------------------------------------------------
98+
99+
This sample uses the `Google Cloud Client Library for Python`_.
100+
You can read the documentation for more details on API usage and use GitHub
101+
to `browse the source`_ and `report issues`_.
102+
103+
.. _Google Cloud Client Library for Python:
104+
https://googlecloudplatform.github.io/google-cloud-python/
105+
.. _browse the source:
106+
https://github.com/GoogleCloudPlatform/google-cloud-python
107+
.. _report issues:
108+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
109+
110+
111+
.. _Google Cloud SDK: https://cloud.google.com/sdk/
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/usr/bin/env python
2+
3+
# Copyright 2020, Google LLC
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+
import argparse
17+
18+
19+
# [START speech_transcribe_onprem]
20+
def transcribe_onprem(local_file_path, api_endpoint):
21+
"""
22+
Transcribe a short audio file using synchronous speech recognition on-prem
23+
24+
Args:
25+
local_file_path: The path to local audio file, e.g. /path/audio.wav
26+
api_endpoint: Endpoint to call for speech recognition, e.g. 0.0.0.0:10000
27+
"""
28+
from google.cloud import speech_v1p1beta1
29+
from google.cloud.speech_v1p1beta1 import enums
30+
import grpc
31+
import io
32+
33+
# api_endpoint = '0.0.0.0:10000'
34+
# local_file_path = '../resources/two_channel_16k.raw'
35+
36+
# Create a gRPC channel to your server
37+
channel = grpc.insecure_channel(target=api_endpoint)
38+
39+
client = speech_v1p1beta1.SpeechClient(channel=channel)
40+
41+
# The language of the supplied audio
42+
language_code = "en-US"
43+
44+
# Sample rate in Hertz of the audio data sent
45+
sample_rate_hertz = 16000
46+
47+
# Encoding of audio data sent. This sample sets this explicitly.
48+
# This field is optional for FLAC and WAV audio formats.
49+
encoding = enums.RecognitionConfig.AudioEncoding.LINEAR16
50+
config = {
51+
"encoding": encoding,
52+
"language_code": language_code,
53+
"sample_rate_hertz": sample_rate_hertz,
54+
}
55+
with io.open(local_file_path, "rb") as f:
56+
content = f.read()
57+
audio = {"content": content}
58+
59+
response = client.recognize(config, audio)
60+
for result in response.results:
61+
# First alternative is the most probable result
62+
alternative = result.alternatives[0]
63+
print(f"Transcript: {alternative.transcript}")
64+
# [END speech_transcribe_onprem]
65+
66+
67+
if __name__ == "__main__":
68+
parser = argparse.ArgumentParser(
69+
description=__doc__,
70+
formatter_class=argparse.RawDescriptionHelpFormatter
71+
)
72+
parser.add_argument(
73+
"--file_path",
74+
required=True,
75+
help="Path to local audio file to be recognized, e.g. /path/audio.wav",
76+
)
77+
parser.add_argument(
78+
"--api_endpoint",
79+
required=True,
80+
help="Endpoint to call for speech recognition, e.g. 0.0.0.0:10000",
81+
)
82+
83+
args = parser.parse_args()
84+
transcribe_onprem(
85+
local_file_path=args.file_path, api_endpoint=args.api_endpoint
86+
)

0 commit comments

Comments
 (0)