-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecognition.py
More file actions
59 lines (44 loc) · 1.66 KB
/
recognition.py
File metadata and controls
59 lines (44 loc) · 1.66 KB
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
56
57
58
59
import requests
import config
# upload
upload_endpoint = "https://api.assemblyai.com/v2/upload"
transcript_endpoint = "https://api.assemblyai.com/v2/transcript"
headers = {'authorization': config.API_KEY}
def upload(filename):
def read_file(filename):
# Open the file in binary mode for reading
with open(filename, 'rb') as _file:
while True:
# Read a chunk of data from the file
data = _file.read()
# If there's no more data, stop reading
if not data:
break
# Yield the data as a generator
yield data
upload_response = requests.post(upload_endpoint,
headers = headers,
data = read_file(filename))
audio_url = upload_response.json()['upload_url']
return audio_url
# transcribe
def transcribe(audio_url):
transcipt_request = {'audio_url': audio_url}
transcript_response = requests.post(transcript_endpoint,
json = transcipt_request,
headers = headers)
job_id = transcript_response.json()['id']
return job_id
# poll
def poll(transcript_id):
polling_endpoint = transcript_endpoint + '/' + transcript_id
polling_response = requests.get(polling_endpoint, headers = headers)
return polling_response.json()
def get_transcription(audio_url):
transcript_id = transcribe(audio_url)
while True:
data = poll(transcript_id)
if data['status'] == 'completed':
return data, None
elif data['status'] == 'error':
return data, data['error']