getting_started.py - AWS Code Sample

getting_started.py

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Purpose Shows how to use AWS SDK for Python (Boto3) to call Amazon Transcribe to make a transcription of an audio file. This script is intended to be used with the instructions for getting started in the Amazon Transcribe Developer Guide here: https://docs.aws.amazon.com/transcribe/latest/dg/getting-started-python.html. """ import time import boto3 def transcribe_file(job_name, file_uri, transcribe_client): transcribe_client.start_transcription_job( TranscriptionJobName=job_name, Media={'MediaFileUri': file_uri}, MediaFormat='wav', LanguageCode='en-US' ) max_tries = 60 while max_tries > 0: max_tries -= 1 job = transcribe_client.get_transcription_job(TranscriptionJobName=job_name) job_status = job['TranscriptionJob']['TranscriptionJobStatus'] if job_status in ['COMPLETED', 'FAILED']: print(f"Job {job_name} is {job_status}.") if job_status == 'COMPLETED': print( f"Download the transcript from\n" f"\t{job['TranscriptionJob']['Transcript']['TranscriptFileUri']}.") break else: print(f"Waiting for {job_name}. Current status is {job_status}.") time.sleep(10) def main(): transcribe_client = boto3.client('transcribe') file_uri = 's3://test-transcribe/answer2.wav' transcribe_file('Example-job', file_uri, transcribe_client) if __name__ == '__main__': main()