-
Notifications
You must be signed in to change notification settings - Fork 6.6k
feat: adds media translation samples #3117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
telpirion
merged 23 commits into
GoogleCloudPlatform:master
from
telpirion:media-translation
Mar 31, 2020
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
451c746
feat: adds media translation samples
telpirion 9fdd891
Merge branch 'master' into media-translation
telpirion 31af894
fix: linter errors
telpirion 6f28d9e
fix: changes library version requirement
telpirion d995fd1
fix: per reviewer
telpirion 7a5e3ca
fix: typo
telpirion 37e3371
Merge branch 'master' into media-translation
telpirion 7f246a7
Merge branch 'master' into media-translation
telpirion c231715
Merge branch 'master' into media-translation
telpirion 8f96d12
fix: removes CLI from translate_from_file
telpirion 344769a
fix: linter issues
telpirion 890f19d
fix: per reviewer
telpirion c7075da
Merge branch 'master' into media-translation
telpirion dfbb2c4
Merge branch 'master' into media-translation
telpirion 6338b8f
Merge branch 'master' into media-translation
telpirion b0ee94b
Merge branch 'master' into media-translation
telpirion 41a13cc
Merge branch 'master' into media-translation
telpirion 11e857d
Merge branch 'master' into media-translation
telpirion 2d5ffb4
Merge branch 'master' into media-translation
telpirion 2b681fd
fix: per reviewer feedback
telpirion 90e33bb
Merge branch 'master' into media-translation
telpirion 55aee0a
Merge branch 'master' into media-translation
telpirion 955b9fe
Merge branch 'master' into media-translation
telpirion File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
.. This file is automatically generated. Do not edit this file directly. | ||
|
||
Cloud Media Translation Python Samples | ||
=============================================================================== | ||
|
||
.. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
: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 | ||
|
||
|
||
This directory contains samples for Cloud Media Translation. With `Cloud Media Translation`, you can dynamically translate text between thousands of language pairs. | ||
|
||
|
||
|
||
|
||
.. _Cloud Media Translation: https://cloud.google.com/translate/media/docs | ||
|
||
Setup | ||
------------------------------------------------------------------------------- | ||
|
||
|
||
Authentication | ||
++++++++++++++ | ||
|
||
This sample requires you to have authentication setup. Refer to the | ||
`Authentication Getting Started Guide`_ for instructions on setting up | ||
credentials for applications. | ||
|
||
.. _Authentication Getting Started Guide: | ||
https://cloud.google.com/docs/authentication/getting-started | ||
|
||
Install Dependencies | ||
++++++++++++++++++++ | ||
|
||
#. Clone python-docs-samples and change directory to the sample directory you want to use. | ||
|
||
.. code-block:: bash | ||
|
||
$ git clone https://github.com/GoogleCloudPlatform/python-docs-samples.git | ||
|
||
#. 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. | ||
|
||
.. _Python Development Environment Setup Guide: | ||
https://cloud.google.com/python/setup | ||
|
||
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. | ||
|
||
.. code-block:: bash | ||
|
||
$ virtualenv env | ||
$ source env/bin/activate | ||
|
||
#. Install the dependencies needed to run the samples. | ||
|
||
.. code-block:: bash | ||
|
||
$ pip install -r requirements.txt | ||
|
||
.. _pip: https://pip.pypa.io/ | ||
.. _virtualenv: https://virtualenv.pypa.io/ | ||
|
||
Samples | ||
------------------------------------------------------------------------------- | ||
|
||
Translate from file | ||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
||
.. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
: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 | ||
|
||
|
||
|
||
|
||
To run this sample: | ||
|
||
.. code-block:: bash | ||
|
||
$ python translate_from_file.py | ||
|
||
|
||
|
||
|
||
Translate from mic | ||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
|
||
.. image:: https://gstatic.com/cloudssh/images/open-btn.png | ||
: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 | ||
|
||
|
||
|
||
|
||
To run this sample: | ||
|
||
.. code-block:: bash | ||
|
||
$ python translate_from_mic.py | ||
|
||
|
||
|
||
|
||
The client library | ||
------------------------------------------------------------------------------- | ||
|
||
This sample uses the `Google Cloud Client Library for Python`_. | ||
You can read the documentation for more details on API usage and use GitHub | ||
to `browse the source`_ and `report issues`_. | ||
|
||
.. _Google Cloud Client Library for Python: | ||
https://googlecloudplatform.github.io/google-cloud-python/ | ||
.. _browse the source: | ||
https://github.com/GoogleCloudPlatform/google-cloud-python | ||
.. _report issues: | ||
https://github.com/GoogleCloudPlatform/google-cloud-python/issues | ||
|
||
|
||
.. _Google Cloud SDK: https://cloud.google.com/sdk/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
# This file is used to generate README.rst | ||
|
||
product: | ||
name: Cloud Media Translation | ||
short_name: Media Translation | ||
url: https://cloud.google.com/translate/media/docs | ||
description: > | ||
With `Cloud Media Translation`, you can dynamically translate text between | ||
thousands of language pairs. | ||
|
||
setup: | ||
- auth | ||
- install_deps | ||
|
||
samples: | ||
- name: Translate from file | ||
file: translate_from_file.py | ||
show_help: true | ||
- name: Translate from mic | ||
file: translate_from_mic.py | ||
|
||
cloud_client_library: true | ||
|
||
folder: media-translation/cloud-client |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
google-cloud-media-translation==0.1.1 | ||
pyaudio==0.2.11 | ||
six==1.14.0 |
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Cloud Media Translation sample application. | ||
|
||
Example usage: | ||
python translate_from_file.py resources/audio.raw | ||
""" | ||
|
||
# [START media_translation_translate_from_file] | ||
from google.cloud import mediatranslation | ||
|
||
|
||
def translate_from_file(file_path='path/to/your/file'): | ||
|
||
client = mediatranslation.SpeechTranslationServiceClient() | ||
|
||
# The `sample_rate_hertz` field is not required for FLAC and WAV (Linear16) | ||
# encoded data. Other audio encodings must provide the sampling rate. | ||
audio_config = mediatranslation.TranslateSpeechConfig( | ||
audio_encoding='linear16', | ||
nnegrey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
source_language_code='en-US', | ||
target_language_code='fr-FR') | ||
|
||
streaming_config = mediatranslation.StreamingTranslateSpeechConfig( | ||
audio_config=audio_config) | ||
|
||
def request_generator(config, audio_file_path): | ||
|
||
# The first request contains the configuration. | ||
# Note that audio_content is explicitly set to None. | ||
yield mediatranslation.StreamingTranslateSpeechRequest( | ||
streaming_config=config, audio_content=None) | ||
|
||
with open(audio_file_path, 'rb') as audio: | ||
while True: | ||
chunk = audio.read(4096) | ||
if not chunk: | ||
break | ||
yield mediatranslation.StreamingTranslateSpeechRequest( | ||
audio_content=chunk, | ||
streaming_config=config) | ||
|
||
requests = request_generator(streaming_config, file_path) | ||
responses = client.streaming_translate_speech(requests) | ||
|
||
for response in responses: | ||
# Once the transcription settles, the response contains the | ||
# is_final result. The other results will be for subsequent portions of | ||
# the audio. | ||
nnegrey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
result = response.result | ||
translation = result.text_translation_result.translation | ||
source = result.recognition_result | ||
|
||
if result.text_translation_result.is_final: | ||
print(u'\nFinal translation: {0}'.format(translation)) | ||
print(u'Final recognition result: {0}'.format(source)) | ||
break | ||
|
||
print(u'\nPartial translation: {0}'.format(translation)) | ||
print(u'Partial recognition result: {0}'.format(source)) | ||
# [END media_translation_translate_from_file] |
28 changes: 28 additions & 0 deletions
28
media-translation/cloud-client/translate_from_file_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# Copyright 2020 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
import re | ||
|
||
import translate_from_file | ||
|
||
RESOURCES = os.path.join(os.path.dirname(__file__), 'resources') | ||
|
||
|
||
def test_translate_streaming(capsys): | ||
translate_from_file.translate_from_file( | ||
os.path.join(RESOURCES, 'audio.raw')) | ||
out, err = capsys.readouterr() | ||
|
||
assert re.search(r'Partial translation', out, re.DOTALL | re.I) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems fine, you can also just do : There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good to know! |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.