-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtranscribe.py
54 lines (40 loc) · 1.8 KB
/
transcribe.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
import argparse
import os
import utils
def main():
parser = argparse.ArgumentParser()
parser.add_argument('audio_file', help='url to file or local audio filename')
parser.add_argument('--local', action='store_true', help='must be set if audio_file is a local filename')
parser.add_argument('--api_key', action='store', help='<YOUR-API-KEY>')
args = parser.parse_args()
if args.api_key is None:
args.api_key = os.getenv("AAI_API_KEY")
if args.api_key is None:
raise RuntimeError("AAI_API_KEY environment variable not set. Try setting it now, or passing in your "
"API key as a command line argument with `--api_key`.")
# Create header with authorization along with content-type
header = {
'authorization': args.api_key,
'content-type': 'application/json'
}
if args.local:
# Upload the audio file to AssemblyAI
upload_url = utils.upload_file(args.audio_file, header)
else:
upload_url = {'upload_url': args.audio_file}
# Request a transcription
transcript_response = utils.request_transcript(upload_url, header)
# Create a polling endpoint that will let us check when the transcription is complete
polling_endpoint = utils.make_polling_endpoint(transcript_response)
# Wait until the transcription is complete
utils.wait_for_completion(polling_endpoint, header)
# Request the paragraphs of the transcript
paragraphs = utils.get_paragraphs(polling_endpoint, header)
# Save and print transcript
with open('transcript.txt', 'w') as f:
for para in paragraphs:
print(para['text'] + '\n')
f.write(para['text'] + '\n')
return
if __name__ == '__main__':
main()