Skip to content

Commit

Permalink
chore(python): run blacken session for all directories with a noxfile (
Browse files Browse the repository at this point in the history
…#133)

Source-Link: googleapis/synthtool@bc0de6e
Post-Processor: gcr.io/cloud-devrel-public-resources/owlbot-python:latest@sha256:39ad8c0570e4f5d2d3124a509de4fe975e799e2b97e0f58aed88f8880d5a8b60

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
2 people authored and dandhlee committed Apr 13, 2023
1 parent 674fbea commit 050ea11
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
7 changes: 3 additions & 4 deletions media-translation/snippets/translate_from_file_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@

import translate_from_file

RESOURCES = os.path.join(os.path.dirname(__file__), 'resources')
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'))
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)
assert re.search(r"Partial translation", out, re.DOTALL | re.I)
45 changes: 24 additions & 21 deletions media-translation/snippets/translate_from_mic.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ 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,
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.
Expand Down Expand Up @@ -97,7 +99,7 @@ def generator(self):
except queue.Empty:
break

yield b''.join(data)
yield b"".join(data)


def listen_print_loop(responses):
Expand All @@ -106,45 +108,46 @@ def listen_print_loop(responses):
The responses passed is a generator that will block until a response
is provided by the server.
"""
translation = ''
translation = ""
for response in responses:
# Once the transcription settles, the response contains the
# END_OF_SINGLE_UTTERANCE event.
if (response.speech_event_type ==
SpeechEventType.END_OF_SINGLE_UTTERANCE):
if response.speech_event_type == SpeechEventType.END_OF_SINGLE_UTTERANCE:

print(u'\nFinal translation: {0}'.format(translation))
print(u"\nFinal translation: {0}".format(translation))
return 0

result = response.result
translation = result.text_translation_result.translation

print(u'\nPartial translation: {0}'.format(translation))
print(u"\nPartial translation: {0}".format(translation))


def do_translation_loop():
print('Begin speaking...')
print("Begin speaking...")

client = media.SpeechTranslationServiceClient()

speech_config = media.TranslateSpeechConfig(
audio_encoding='linear16',
source_language_code='en-US',
target_language_code='es-ES')
audio_encoding="linear16",
source_language_code="en-US",
target_language_code="es-ES",
)

config = media.StreamingTranslateSpeechConfig(
audio_config=speech_config, single_utterance=True)
audio_config=speech_config, single_utterance=True
)

# The first request contains the configuration.
# Note that audio_content is explicitly set to None.
first_request = media.StreamingTranslateSpeechRequest(
streaming_config=config)
first_request = media.StreamingTranslateSpeechRequest(streaming_config=config)

with MicrophoneStream(RATE, CHUNK) as stream:
audio_generator = stream.generator()
mic_requests = (media.StreamingTranslateSpeechRequest(
audio_content=content)
for content in audio_generator)
mic_requests = (
media.StreamingTranslateSpeechRequest(audio_content=content)
for content in audio_generator
)

requests = itertools.chain(iter([first_request]), mic_requests)

Expand All @@ -159,14 +162,14 @@ def do_translation_loop():
def main():
while True:
print()
option = input('Press any key to translate or \'q\' to quit: ')
option = input("Press any key to translate or 'q' to quit: ")

if option.lower() == 'q':
if option.lower() == "q":
break

do_translation_loop()


if __name__ == '__main__':
if __name__ == "__main__":
main()
# [END mediatranslation_translate_from_mic]

0 comments on commit 050ea11

Please sign in to comment.