forked from Ivan-Kudryavtsev/WWHack2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transcriber.py
78 lines (67 loc) · 2.21 KB
/
transcriber.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# This is a sample Python script.
import base64
import time
import json
import asyncio
import wave
import requests
import json
# import configure
FRAMES_PER_BUFFER = 3200
CHANNELS = 1
# CHUNK = 1024
RATE = 16000
DURATION = 5
auth_key = "1fb21cd48fdd4de79c3e7f9e454afb0c"
# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.s
# Press the green button in the gutter to run the script.
# See PyCharm help at https://www.jetbrains.com/help/pycharm/
def transcribeFile(filecontents):
# endpoint = "https://api.assemblyai.com/v2/transcript"
#filename = 'Recording_3.mp4'
#file = open(filename, 'rb')
data = filecontents
headers = {'authorization': "1fb21cd48fdd4de79c3e7f9e454afb0c"}
response = requests.post('https://api.assemblyai.com/v2/upload',
headers=headers,
data=data
)
print(response.json())
# info = json.loads(response)
url = response.json()['upload_url']
endpoint = "https://api.assemblyai.com/v2/transcript"
json1 = {
"audio_url": url
}
headers = {
"authorization": auth_key,
"content-type": "application/json"
}
response = requests.post(endpoint, json=json1, headers=headers)
json_object = json.dumps(response.json(), indent=4)
with open("sample1.json", "w") as outfile:
outfile.write(json_object)
print(response.json())
transcript = ''
time.sleep(2)
headers = {
"authorization": auth_key,
}
c = 0
endpoint = endpoint + "/" + response.json()['id']
while True:
time.sleep(1)
response = requests.get(endpoint, headers=headers)
#response = requests.post(endpoint, json=json1, headers=headers)
json_object = json.dumps(response.json(), indent = 4)
with open("sample.json", "w") as outfile:
outfile.write(json_object)
print(response.json())
if(response.json()['status'] == 'completed'):
transcript = transcript+response.json()['text']
break;
else:
c = c+1
print(transcript)
return transcript