Skip to content
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

feat: adds media translation samples #3117

Merged
merged 23 commits into from
Mar 31, 2020
Merged
Show file tree
Hide file tree
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 Mar 18, 2020
9fdd891
Merge branch 'master' into media-translation
telpirion Mar 23, 2020
31af894
fix: linter errors
telpirion Mar 23, 2020
6f28d9e
fix: changes library version requirement
telpirion Mar 23, 2020
d995fd1
fix: per reviewer
telpirion Mar 23, 2020
7a5e3ca
fix: typo
telpirion Mar 23, 2020
37e3371
Merge branch 'master' into media-translation
telpirion Mar 23, 2020
7f246a7
Merge branch 'master' into media-translation
telpirion Mar 24, 2020
c231715
Merge branch 'master' into media-translation
telpirion Mar 26, 2020
8f96d12
fix: removes CLI from translate_from_file
telpirion Mar 26, 2020
344769a
fix: linter issues
telpirion Mar 26, 2020
890f19d
fix: per reviewer
telpirion Mar 26, 2020
c7075da
Merge branch 'master' into media-translation
telpirion Mar 27, 2020
dfbb2c4
Merge branch 'master' into media-translation
telpirion Mar 30, 2020
6338b8f
Merge branch 'master' into media-translation
telpirion Mar 30, 2020
b0ee94b
Merge branch 'master' into media-translation
telpirion Mar 30, 2020
41a13cc
Merge branch 'master' into media-translation
telpirion Mar 30, 2020
11e857d
Merge branch 'master' into media-translation
telpirion Mar 30, 2020
2d5ffb4
Merge branch 'master' into media-translation
telpirion Mar 30, 2020
2b681fd
fix: per reviewer feedback
telpirion Mar 30, 2020
90e33bb
Merge branch 'master' into media-translation
telpirion Mar 30, 2020
55aee0a
Merge branch 'master' into media-translation
telpirion Mar 30, 2020
955b9fe
Merge branch 'master' into media-translation
telpirion Mar 31, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions media-translation/cloud-client/README.rst
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/
24 changes: 24 additions & 0 deletions media-translation/cloud-client/README.rst.in
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
3 changes: 3 additions & 0 deletions media-translation/cloud-client/requirements.txt
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.
73 changes: 73 additions & 0 deletions media-translation/cloud-client/translate_from_file.py
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

nnegrey marked this conversation as resolved.
Show resolved Hide resolved

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 media-translation/cloud-client/translate_from_file_test.py
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems fine, you can also just do : assert 'Partial translation' in out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know!

Loading