Skip to content

Commit

Permalink
feat: adds media translation samples (#3117)
Browse files Browse the repository at this point in the history
* feat: adds media translation samples
* fix: changes library version requirement
  • Loading branch information
telpirion authored Mar 31, 2020
1 parent 597965c commit 7bc4783
Show file tree
Hide file tree
Showing 7 changed files with 399 additions and 0 deletions.
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


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',
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.
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)
Loading

0 comments on commit 7bc4783

Please sign in to comment.