Skip to content

Commit 7bc4783

Browse files
authored
feat: adds media translation samples (#3117)
* feat: adds media translation samples * fix: changes library version requirement
1 parent 597965c commit 7bc4783

File tree

7 files changed

+399
-0
lines changed

7 files changed

+399
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Cloud Media Translation Python Samples
4+
===============================================================================
5+
6+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
7+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=media-translation/cloud-client/README.rst
8+
9+
10+
This directory contains samples for Cloud Media Translation. With `Cloud Media Translation`, you can dynamically translate text between thousands of language pairs.
11+
12+
13+
14+
15+
.. _Cloud Media Translation: https://cloud.google.com/translate/media/docs
16+
17+
Setup
18+
-------------------------------------------------------------------------------
19+
20+
21+
Authentication
22+
++++++++++++++
23+
24+
This sample requires you to have authentication setup. Refer to the
25+
`Authentication Getting Started Guide`_ for instructions on setting up
26+
credentials for applications.
27+
28+
.. _Authentication Getting Started Guide:
29+
https://cloud.google.com/docs/authentication/getting-started
30+
31+
Install Dependencies
32+
++++++++++++++++++++
33+
34+
#. Clone python-docs-samples and change directory to the sample directory you want to use.
35+
36+
.. code-block:: bash
37+
38+
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git
39+
40+
#. 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.
41+
42+
.. _Python Development Environment Setup Guide:
43+
https://cloud.google.com/python/setup
44+
45+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
46+
47+
.. code-block:: bash
48+
49+
$ virtualenv env
50+
$ source env/bin/activate
51+
52+
#. Install the dependencies needed to run the samples.
53+
54+
.. code-block:: bash
55+
56+
$ pip install -r requirements.txt
57+
58+
.. _pip: https://pip.pypa.io/
59+
.. _virtualenv: https://virtualenv.pypa.io/
60+
61+
Samples
62+
-------------------------------------------------------------------------------
63+
64+
Translate from file
65+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
66+
67+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
68+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=media-translation/cloud-client/translate_from_file.py,media-translation/cloud-client/README.rst
69+
70+
71+
72+
73+
To run this sample:
74+
75+
.. code-block:: bash
76+
77+
$ python translate_from_file.py
78+
79+
80+
81+
82+
Translate from mic
83+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
84+
85+
.. image:: https://gstatic.com/cloudssh/images/open-btn.png
86+
:target: https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/python-docs-samples&page=editor&open_in_editor=media-translation/cloud-client/translate_from_mic.py,media-translation/cloud-client/README.rst
87+
88+
89+
90+
91+
To run this sample:
92+
93+
.. code-block:: bash
94+
95+
$ python translate_from_mic.py
96+
97+
98+
99+
100+
The client library
101+
-------------------------------------------------------------------------------
102+
103+
This sample uses the `Google Cloud Client Library for Python`_.
104+
You can read the documentation for more details on API usage and use GitHub
105+
to `browse the source`_ and `report issues`_.
106+
107+
.. _Google Cloud Client Library for Python:
108+
https://googlecloudplatform.github.io/google-cloud-python/
109+
.. _browse the source:
110+
https://github.com/GoogleCloudPlatform/google-cloud-python
111+
.. _report issues:
112+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
113+
114+
115+
.. _Google Cloud SDK: https://cloud.google.com/sdk/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Cloud Media Translation
5+
short_name: Media Translation
6+
url: https://cloud.google.com/translate/media/docs
7+
description: >
8+
With `Cloud Media Translation`, you can dynamically translate text between
9+
thousands of language pairs.
10+
11+
setup:
12+
- auth
13+
- install_deps
14+
15+
samples:
16+
- name: Translate from file
17+
file: translate_from_file.py
18+
show_help: true
19+
- name: Translate from mic
20+
file: translate_from_mic.py
21+
22+
cloud_client_library: true
23+
24+
folder: media-translation/cloud-client
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
google-cloud-media-translation==0.1.1
2+
pyaudio==0.2.11
3+
six==1.14.0
Binary file not shown.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright 2020 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+
# http://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+
"""Cloud Media Translation sample application.
16+
17+
Example usage:
18+
python translate_from_file.py resources/audio.raw
19+
"""
20+
21+
# [START media_translation_translate_from_file]
22+
from google.cloud import mediatranslation
23+
24+
25+
def translate_from_file(file_path='path/to/your/file'):
26+
27+
client = mediatranslation.SpeechTranslationServiceClient()
28+
29+
# The `sample_rate_hertz` field is not required for FLAC and WAV (Linear16)
30+
# encoded data. Other audio encodings must provide the sampling rate.
31+
audio_config = mediatranslation.TranslateSpeechConfig(
32+
audio_encoding='linear16',
33+
source_language_code='en-US',
34+
target_language_code='fr-FR')
35+
36+
streaming_config = mediatranslation.StreamingTranslateSpeechConfig(
37+
audio_config=audio_config)
38+
39+
def request_generator(config, audio_file_path):
40+
41+
# The first request contains the configuration.
42+
# Note that audio_content is explicitly set to None.
43+
yield mediatranslation.StreamingTranslateSpeechRequest(
44+
streaming_config=config, audio_content=None)
45+
46+
with open(audio_file_path, 'rb') as audio:
47+
while True:
48+
chunk = audio.read(4096)
49+
if not chunk:
50+
break
51+
yield mediatranslation.StreamingTranslateSpeechRequest(
52+
audio_content=chunk,
53+
streaming_config=config)
54+
55+
requests = request_generator(streaming_config, file_path)
56+
responses = client.streaming_translate_speech(requests)
57+
58+
for response in responses:
59+
# Once the transcription settles, the response contains the
60+
# is_final result. The other results will be for subsequent portions of
61+
# the audio.
62+
result = response.result
63+
translation = result.text_translation_result.translation
64+
source = result.recognition_result
65+
66+
if result.text_translation_result.is_final:
67+
print(u'\nFinal translation: {0}'.format(translation))
68+
print(u'Final recognition result: {0}'.format(source))
69+
break
70+
71+
print(u'\nPartial translation: {0}'.format(translation))
72+
print(u'Partial recognition result: {0}'.format(source))
73+
# [END media_translation_translate_from_file]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Copyright 2020 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+
# http://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 os
16+
import re
17+
18+
import translate_from_file
19+
20+
RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
21+
22+
23+
def test_translate_streaming(capsys):
24+
translate_from_file.translate_from_file(
25+
os.path.join(RESOURCES, 'audio.raw'))
26+
out, err = capsys.readouterr()
27+
28+
assert re.search(r'Partial translation', out, re.DOTALL | re.I)

0 commit comments

Comments
 (0)