diff --git a/media-translation/cloud-client/README.rst b/media-translation/cloud-client/README.rst new file mode 100644 index 00000000000..38a742d836e --- /dev/null +++ b/media-translation/cloud-client/README.rst @@ -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/ \ No newline at end of file diff --git a/media-translation/cloud-client/README.rst.in b/media-translation/cloud-client/README.rst.in new file mode 100644 index 00000000000..301b91fc3e7 --- /dev/null +++ b/media-translation/cloud-client/README.rst.in @@ -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 \ No newline at end of file diff --git a/media-translation/cloud-client/requirements.txt b/media-translation/cloud-client/requirements.txt new file mode 100644 index 00000000000..d189cbf7e9c --- /dev/null +++ b/media-translation/cloud-client/requirements.txt @@ -0,0 +1,3 @@ +google-cloud-media-translation==0.1.1 +pyaudio==0.2.11 +six==1.14.0 \ No newline at end of file diff --git a/media-translation/cloud-client/resources/audio.raw b/media-translation/cloud-client/resources/audio.raw new file mode 100644 index 00000000000..5ebf79d3c9c Binary files /dev/null and b/media-translation/cloud-client/resources/audio.raw differ diff --git a/media-translation/cloud-client/translate_from_file.py b/media-translation/cloud-client/translate_from_file.py new file mode 100644 index 00000000000..d02cc6e7ea4 --- /dev/null +++ b/media-translation/cloud-client/translate_from_file.py @@ -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] diff --git a/media-translation/cloud-client/translate_from_file_test.py b/media-translation/cloud-client/translate_from_file_test.py new file mode 100644 index 00000000000..ce7a0ba55f0 --- /dev/null +++ b/media-translation/cloud-client/translate_from_file_test.py @@ -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) diff --git a/media-translation/cloud-client/translate_from_mic.py b/media-translation/cloud-client/translate_from_mic.py new file mode 100644 index 00000000000..dc28a1a6f0a --- /dev/null +++ b/media-translation/cloud-client/translate_from_mic.py @@ -0,0 +1,156 @@ +# 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 API sample application using a microphone. + +Example usage: + python translate_from_mic.py +""" + +# [START media_translation_translate_from_mic] +from __future__ import division + +import itertools + +from google.cloud import mediatranslation +import pyaudio +from six.moves import queue + +# Audio recording parameters +RATE = 16000 +CHUNK = int(RATE / 10) # 100ms + + +class MicrophoneStream(object): + """Opens a recording stream as a generator yielding the audio chunks.""" + + def __init__(self, rate, chunk): + self._rate = rate + self._chunk = chunk + + # Create a thread-safe buffer of audio data + self._buff = queue.Queue() + self.closed = True + + def __enter__(self): + self._audio_interface = pyaudio.PyAudio() + self._audio_stream = self._audio_interface.open( + format=pyaudio.paInt16, + channels=1, rate=self._rate, + input=True, frames_per_buffer=self._chunk, + # Run the audio stream asynchronously to fill the buffer object. + # This is necessary so that the input device's buffer doesn't + # overflow while the calling thread makes network requests, etc. + stream_callback=self._fill_buffer, + ) + + self.closed = False + + return self + + def __exit__(self, type, value, traceback): + self._audio_stream.stop_stream() + self._audio_stream.close() + self.closed = True + # Signal the generator to terminate so that the client's + # streaming_recognize method will not block the process termination. + self._buff.put(None) + self._audio_interface.terminate() + + def _fill_buffer(self, in_data, frame_count, time_info, status_flags): + """Continuously collect data from the audio stream, into the buffer.""" + self._buff.put(in_data) + return None, pyaudio.paContinue + + def generator(self): + while not self.closed: + # Use a blocking get() to ensure there's at least one chunk of + # data, and stop iteration if the chunk is None, indicating the + # end of the audio stream. + chunk = self._buff.get() + if chunk is None: + return + data = [chunk] + + # Now consume whatever other data's still buffered. + while True: + try: + chunk = self._buff.get(block=False) + if chunk is None: + return + data.append(chunk) + except queue.Empty: + break + + yield b''.join(data) + + +def listen_print_loop(responses): + """Iterates through server responses and prints them. + + The responses passed is a generator that will block until a response + is provided by the server. + """ + 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)) + + +def main(): + client = mediatranslation.SpeechTranslationServiceClient() + + speech_config = mediatranslation.TranslateSpeechConfig( + audio_encoding='linear16', + source_language_code='en-US', + target_language_code='es-ES') + + config = mediatranslation.StreamingTranslateSpeechConfig( + audio_config=speech_config) + + # The first request contains the configuration. + # Note that audio_content is explicitly set to None. + first_request = mediatranslation.StreamingTranslateSpeechRequest( + streaming_config=config, audio_content=None) + + with MicrophoneStream(RATE, CHUNK) as stream: + audio_generator = stream.generator() + mic_requests = (mediatranslation.StreamingTranslateSpeechRequest( + audio_content=content, + streaming_config=config) + for content in audio_generator) + + requests = itertools.chain(iter([first_request]), mic_requests) + + responses = client.streaming_translate_speech(requests) + + # Print the translation responses as they arrive + listen_print_loop(responses) + + +if __name__ == '__main__': + print('Start speaking ...') + main() +# [END media_translation_translate_from_mic]