Skip to content

Commit 633f6c2

Browse files
b-loved-dreamerdandhlee
authored andcommitted
samples: adds speech export to gcs sample (#176)
1 parent 5ed969c commit 633f6c2

File tree

6 files changed

+166
-38
lines changed

6 files changed

+166
-38
lines changed

speech/microphone/noxfile.py

+17-16
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,29 @@
3838

3939
TEST_CONFIG = {
4040
# You can opt out from the test for specific Python versions.
41-
'ignored_versions': ["2.7"],
42-
41+
"ignored_versions": ["2.7"],
4342
# Old samples are opted out of enforcing Python type hints
4443
# All new samples should feature them
45-
'enforce_type_hints': False,
46-
44+
"enforce_type_hints": False,
4745
# An envvar key for determining the project id to use. Change it
4846
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
4947
# build specific Cloud project. You can also use your own string
5048
# to use your own Cloud project.
51-
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
49+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
5250
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
5351
# If you need to use a specific version of pip,
5452
# change pip_version_override to the string representation
5553
# of the version number, for example, "20.2.4"
5654
"pip_version_override": None,
5755
# A dictionary you want to inject into your test. Don't put any
5856
# secrets here. These values will override predefined values.
59-
'envs': {},
57+
"envs": {},
6058
}
6159

6260

6361
try:
6462
# Ensure we can import noxfile_config in the project's directory.
65-
sys.path.append('.')
63+
sys.path.append(".")
6664
from noxfile_config import TEST_CONFIG_OVERRIDE
6765
except ImportError as e:
6866
print("No user noxfile_config found: detail: {}".format(e))
@@ -77,12 +75,12 @@ def get_pytest_env_vars() -> Dict[str, str]:
7775
ret = {}
7876

7977
# Override the GCLOUD_PROJECT and the alias.
80-
env_key = TEST_CONFIG['gcloud_project_env']
78+
env_key = TEST_CONFIG["gcloud_project_env"]
8179
# This should error out if not set.
82-
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
80+
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
8381

8482
# Apply user supplied envs.
85-
ret.update(TEST_CONFIG['envs'])
83+
ret.update(TEST_CONFIG["envs"])
8684
return ret
8785

8886

@@ -91,7 +89,7 @@ def get_pytest_env_vars() -> Dict[str, str]:
9189
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8", "3.9"]
9290

9391
# Any default versions that should be ignored.
94-
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
92+
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
9593

9694
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
9795

@@ -140,7 +138,7 @@ def _determine_local_import_names(start_dir: str) -> List[str]:
140138

141139
@nox.session
142140
def lint(session: nox.sessions.Session) -> None:
143-
if not TEST_CONFIG['enforce_type_hints']:
141+
if not TEST_CONFIG["enforce_type_hints"]:
144142
session.install("flake8", "flake8-import-order")
145143
else:
146144
session.install("flake8", "flake8-import-order", "flake8-annotations")
@@ -149,9 +147,11 @@ def lint(session: nox.sessions.Session) -> None:
149147
args = FLAKE8_COMMON_ARGS + [
150148
"--application-import-names",
151149
",".join(local_names),
152-
"."
150+
".",
153151
]
154152
session.run("flake8", *args)
153+
154+
155155
#
156156
# Black
157157
#
@@ -164,6 +164,7 @@ def blacken(session: nox.sessions.Session) -> None:
164164

165165
session.run("black", *python_files)
166166

167+
167168
#
168169
# Sample Tests
169170
#
@@ -212,9 +213,9 @@ def py(session: nox.sessions.Session) -> None:
212213
if session.python in TESTED_VERSIONS:
213214
_session_tests(session)
214215
else:
215-
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
216-
session.python
217-
))
216+
session.skip(
217+
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
218+
)
218219

219220

220221
#

speech/snippets/noxfile.py

+22-19
Original file line numberDiff line numberDiff line change
@@ -38,31 +38,29 @@
3838

3939
TEST_CONFIG = {
4040
# You can opt out from the test for specific Python versions.
41-
'ignored_versions': ["2.7"],
42-
41+
"ignored_versions": ["2.7"],
4342
# Old samples are opted out of enforcing Python type hints
4443
# All new samples should feature them
45-
'enforce_type_hints': False,
46-
44+
"enforce_type_hints": False,
4745
# An envvar key for determining the project id to use. Change it
4846
# to 'BUILD_SPECIFIC_GCLOUD_PROJECT' if you want to opt in using a
4947
# build specific Cloud project. You can also use your own string
5048
# to use your own Cloud project.
51-
'gcloud_project_env': 'GOOGLE_CLOUD_PROJECT',
49+
"gcloud_project_env": "GOOGLE_CLOUD_PROJECT",
5250
# 'gcloud_project_env': 'BUILD_SPECIFIC_GCLOUD_PROJECT',
5351
# If you need to use a specific version of pip,
5452
# change pip_version_override to the string representation
5553
# of the version number, for example, "20.2.4"
5654
"pip_version_override": None,
5755
# A dictionary you want to inject into your test. Don't put any
5856
# secrets here. These values will override predefined values.
59-
'envs': {},
57+
"envs": {},
6058
}
6159

6260

6361
try:
6462
# Ensure we can import noxfile_config in the project's directory.
65-
sys.path.append('.')
63+
sys.path.append(".")
6664
from noxfile_config import TEST_CONFIG_OVERRIDE
6765
except ImportError as e:
6866
print("No user noxfile_config found: detail: {}".format(e))
@@ -77,12 +75,12 @@ def get_pytest_env_vars() -> Dict[str, str]:
7775
ret = {}
7876

7977
# Override the GCLOUD_PROJECT and the alias.
80-
env_key = TEST_CONFIG['gcloud_project_env']
78+
env_key = TEST_CONFIG["gcloud_project_env"]
8179
# This should error out if not set.
82-
ret['GOOGLE_CLOUD_PROJECT'] = os.environ[env_key]
80+
ret["GOOGLE_CLOUD_PROJECT"] = os.environ[env_key]
8381

8482
# Apply user supplied envs.
85-
ret.update(TEST_CONFIG['envs'])
83+
ret.update(TEST_CONFIG["envs"])
8684
return ret
8785

8886

@@ -91,7 +89,7 @@ def get_pytest_env_vars() -> Dict[str, str]:
9189
ALL_VERSIONS = ["2.7", "3.6", "3.7", "3.8", "3.9"]
9290

9391
# Any default versions that should be ignored.
94-
IGNORED_VERSIONS = TEST_CONFIG['ignored_versions']
92+
IGNORED_VERSIONS = TEST_CONFIG["ignored_versions"]
9593

9694
TESTED_VERSIONS = sorted([v for v in ALL_VERSIONS if v not in IGNORED_VERSIONS])
9795

@@ -140,7 +138,7 @@ def _determine_local_import_names(start_dir: str) -> List[str]:
140138

141139
@nox.session
142140
def lint(session: nox.sessions.Session) -> None:
143-
if not TEST_CONFIG['enforce_type_hints']:
141+
if not TEST_CONFIG["enforce_type_hints"]:
144142
session.install("flake8", "flake8-import-order")
145143
else:
146144
session.install("flake8", "flake8-import-order", "flake8-annotations")
@@ -149,9 +147,11 @@ def lint(session: nox.sessions.Session) -> None:
149147
args = FLAKE8_COMMON_ARGS + [
150148
"--application-import-names",
151149
",".join(local_names),
152-
"."
150+
".",
153151
]
154152
session.run("flake8", *args)
153+
154+
155155
#
156156
# Black
157157
#
@@ -164,6 +164,7 @@ def blacken(session: nox.sessions.Session) -> None:
164164

165165
session.run("black", *python_files)
166166

167+
167168
#
168169
# Sample Tests
169170
#
@@ -172,7 +173,9 @@ def blacken(session: nox.sessions.Session) -> None:
172173
PYTEST_COMMON_ARGS = ["--junitxml=sponge_log.xml"]
173174

174175

175-
def _session_tests(session: nox.sessions.Session, post_install: Callable = None) -> None:
176+
def _session_tests(
177+
session: nox.sessions.Session, post_install: Callable = None
178+
) -> None:
176179
if TEST_CONFIG["pip_version_override"]:
177180
pip_version = TEST_CONFIG["pip_version_override"]
178181
session.install(f"pip=={pip_version}")
@@ -202,7 +205,7 @@ def _session_tests(session: nox.sessions.Session, post_install: Callable = None)
202205
# on travis where slow and flaky tests are excluded.
203206
# See http://doc.pytest.org/en/latest/_modules/_pytest/main.html
204207
success_codes=[0, 5],
205-
env=get_pytest_env_vars()
208+
env=get_pytest_env_vars(),
206209
)
207210

208211

@@ -212,9 +215,9 @@ def py(session: nox.sessions.Session) -> None:
212215
if session.python in TESTED_VERSIONS:
213216
_session_tests(session)
214217
else:
215-
session.skip("SKIPPED: {} tests are disabled for this sample.".format(
216-
session.python
217-
))
218+
session.skip(
219+
"SKIPPED: {} tests are disabled for this sample.".format(session.python)
220+
)
218221

219222

220223
#
@@ -223,7 +226,7 @@ def py(session: nox.sessions.Session) -> None:
223226

224227

225228
def _get_repo_root() -> Optional[str]:
226-
""" Returns the root folder of the project. """
229+
"""Returns the root folder of the project."""
227230
# Get root of this repository. Assume we don't have directories nested deeper than 10 items.
228231
p = Path(os.getcwd())
229232
for i in range(10):

speech/snippets/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
google-cloud-speech==2.4.0
2+
google-cloud-storage==1.38.0

speech/snippets/speech_model_adaptation_beta.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,7 @@ def transcribe_with_model_adaptation(
6464
# class and phrase set to send a transcription request with speech adaptation
6565

6666
# Speech adaptation configuration
67-
speech_adaptation = speech.SpeechAdaptation(
68-
phrase_set_references=[phrase_set_name]
69-
)
67+
speech_adaptation = speech.SpeechAdaptation(phrase_set_references=[phrase_set_name])
7068

7169
# speech configuration object
7270
config = speech.RecognitionConfig(
+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
16+
# [START speech_transcribe_with_speech_to_storage_beta]
17+
18+
from google.cloud import speech_v1p1beta1 as speech
19+
20+
21+
def export_transcript_to_storage_beta(
22+
input_storage_uri, output_storage_uri, encoding, sample_rate_hertz, language_code
23+
):
24+
25+
# input_uri URI for audio file in Cloud Storage, e.g. gs://[BUCKET]/[FILE]
26+
audio = speech.RecognitionAudio(uri=input_storage_uri)
27+
28+
# Pass in the URI of the Cloud Storage bucket to hold the transcription
29+
output_config = speech.TranscriptOutputConfig(gcs_uri=output_storage_uri)
30+
31+
# Speech configuration object
32+
config = speech.RecognitionConfig(
33+
encoding=encoding,
34+
sample_rate_hertz=sample_rate_hertz,
35+
language_code=language_code,
36+
)
37+
38+
# Compose the long-running request
39+
request = speech.LongRunningRecognizeRequest(
40+
audio=audio, config=config, output_config=output_config
41+
)
42+
43+
# Create the speech client
44+
speech_client = speech.SpeechClient()
45+
46+
operation = speech_client.long_running_recognize(request=request)
47+
48+
print("Waiting for operation to complete...")
49+
response = operation.result(timeout=90)
50+
51+
# Each result is for a consecutive portion of the audio. Iterate through
52+
# them to get the transcripts for the entire audio file.
53+
for result in response.results:
54+
# The first alternative is the most likely one for this portion.
55+
print("Transcript: {}".format(result.alternatives[0].transcript))
56+
print("Confidence: {}".format(result.alternatives[0].confidence))
57+
58+
# [END speech_transcribe_with_speech_to_storage_beta]
59+
return response.results[0].alternatives[0].transcript
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Copyright 2021 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import uuid
16+
17+
from google.cloud import speech_v1p1beta1 as speech
18+
from google.cloud import storage
19+
import pytest
20+
21+
import speech_to_storage_beta
22+
23+
STORAGE_URI = "gs://cloud-samples-data/speech/brooklyn_bridge.raw"
24+
25+
26+
storage_client = storage.Client()
27+
28+
BUCKET_UUID = str(uuid.uuid4())[:8]
29+
BUCKET_NAME = f"speech-{BUCKET_UUID}"
30+
BUCKET_PREFIX = "export-transcript-output-test"
31+
DELIMETER = None
32+
33+
INPUT_STORAGE_URI = "gs://cloud-samples-data/speech/commercial_mono.wav"
34+
OUTPUT_STORAGE_URI = f"gs://{BUCKET_NAME}/{BUCKET_PREFIX}"
35+
encoding = speech.RecognitionConfig.AudioEncoding.LINEAR16
36+
sample_rate_hertz = 8000
37+
language_code = "en-US"
38+
39+
40+
def test_export_transcript_to_storage_beta(bucket, capsys):
41+
transcript = speech_to_storage_beta.export_transcript_to_storage_beta(
42+
INPUT_STORAGE_URI,
43+
OUTPUT_STORAGE_URI,
44+
encoding,
45+
sample_rate_hertz,
46+
language_code,
47+
)
48+
assert "I'm here" in transcript
49+
50+
51+
@pytest.fixture
52+
def bucket():
53+
"""Yields a bucket that is deleted after the test completes."""
54+
bucket = None
55+
while bucket is None or bucket.exists():
56+
bucket = storage_client.bucket(BUCKET_NAME)
57+
bucket.storage_class = "COLDLINE"
58+
storage_client.create_bucket(bucket, location="us")
59+
yield bucket
60+
61+
blobs = storage_client.list_blobs(BUCKET_NAME, prefix=BUCKET_PREFIX)
62+
63+
for blob in blobs:
64+
blob.delete()
65+
66+
bucket.delete(force=True)

0 commit comments

Comments
 (0)