Last active
June 28, 2020 17:40
-
-
Save yegorkryukov/ef483129a51afa2d93b477535352b5c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_transcription_text(transcribe, job_name): | |
"""Returns transcription text for the AWS Transcribe job | |
Parameters | |
---------- | |
transcribe : AWS `transcribe` service client instance | |
job_name : transcribe service `job` name | |
Returns | |
------- | |
Current job status if job is still in progress | |
Transcription text if job is Completed | |
""" | |
import urllib.request | |
import json | |
import time | |
# let's obtain the job instance | |
job = transcribe.get_transcription_job(TranscriptionJobName=job_name) | |
# and it's status | |
status = job['TranscriptionJob']['TranscriptionJobStatus'] | |
# check the status every 5 seconds and | |
# return the transcribed text if the job is finished | |
# otherwise return None if job failed | |
while True: | |
if status == 'COMPLETED': | |
print(f"Job {job_name} completed") | |
with urllib.request.urlopen(job['TranscriptionJob']['Transcript']['TranscriptFileUri']) as r: | |
data = json.loads(r.read()) | |
return data['results']['transcripts'][0]['transcript'] | |
elif status == 'FAILED': | |
print(f"Job {job_name} failed") | |
return None | |
else: | |
print(f"Status of job {job_name}: {status}") | |
time.sleep(5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment