-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathspeechToText.py
50 lines (39 loc) · 1.25 KB
/
speechToText.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
import requests
import os
import time
import sys
filename = "test1.mp3"
api_key = "{your_api_key}"
def read_file(filename, chunk_size=5242880):
with open(filename, "rb") as _file:
while True:
data = _file.read(chunk_size)
if not data:
break
yield data
headers = {"authorization": api_key}
response = requests.post(
"https://api.assemblyai.com/v2/upload", headers=headers, data=read_file(filename)
)
audio_url = response.json()["upload_url"]
endpoint = "https://api.assemblyai.com/v2/transcript"
json = {"audio_url": audio_url}
headers = {"authorization": api_key, "content-type": "application/json"}
response = requests.post(endpoint, json=json, headers=headers)
response_json = response.json()
print(response_json)
status = ""
while status != "completed":
response_result = requests.get(
os.path.join(endpoint, response_json["id"]), headers=headers
)
status = response_result.json()["status"]
print(f"Status: {status}")
if status == "error":
sys.exit("Audio file failed to process.")
elif status != "completed":
time.sleep(5)
transcript = response_result.json()["text"]
print(transcript)
with open("transcript.txt", "w") as f:
f.write(transcript)