-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws.py
55 lines (50 loc) · 1.97 KB
/
aws.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import time, json
import boto3
transcribe_client = boto3.client('transcribe')
s3 = boto3.client('s3')
def upload_file(filename, bucket, key):
s3.upload_file(filename, bucket, key)
def transcribe_file(job_name, file_uri, bucket, output_url):
print(file_uri)
transcribe_client.start_transcription_job(
TranscriptionJobName=job_name,
Media={'MediaFileUri': file_uri},
MediaFormat='wav',
LanguageCode='en-US',
OutputBucketName=bucket
)
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':
#download json file and access it
file = output_url + job_name + ".json"
s3.download_file(bucket, job_name + ".json", file)
transcriptionfile = json.load(open(file))
annotation = annotation_info(transcriptionfile)
#remove json file once all the information is extracted
os.remove(file)
return annotation
break
else:
print(f"Waiting for {job_name}. Current status is {job_status}.")
time.sleep(10)
#returns the transcription information as a list of dictionaries
def annotation_info(transcription):
split_labels = []
list_of_items = transcription['results']['items']
for items in list_of_items:
if items['type'] == "punctuation": continue
start = int(float(items['start_time']) * 1000)
end = int(float(items['end_time']) * 1000)
content = items["alternatives"][0]["content"]
split_labels.append(dict( \
[('start', start), \
('end', end),
("token", content)]))
return split_labels