Repository navigation
Expand file tree
/
Copy pathSlavica.py
More file actions
84 lines (71 loc) · 2.14 KB
/
Copy pathSlavica.py
File metadata and controls
84 lines (71 loc) · 2.14 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import pyaudio
import wave
import time
import webbrowser
FRAMES_PER_BUFFER = 3200
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
p = pyaudio.PyAudio()
stream = p.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=FRAMES_PER_BUFFER
)
print("Start recording")
seconds = 5
frames = []
t0 = time.time()
for i in range(0,int(RATE/FRAMES_PER_BUFFER*seconds)):
data = stream.read(FRAMES_PER_BUFFER)
frames.append(data)
stream.stop_stream()
stream.close()
p.terminate()
obj = wave.open("output.wav", "wb")
obj.setnchannels(CHANNELS)
obj.setsampwidth(p.get_sample_size(FORMAT))
obj.setframerate(RATE)
obj.writeframes(b"".join(frames))
obj.close()
import requests
from api_secrets import API_KEY
uploadEndpoint = 'https://api.assemblyai.com/v2/upload'
transcriptEndpoint = "https://api.assemblyai.com/v2/transcript"
#upload
filename = r"C:\Users\X-TREME\Desktop\faks\epos\Python-Google-Speech\output.wav"
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(uploadEndpoint,
headers=headers,
data=read_file(filename))
#print(response.json())
audioURL = response.json()['upload_url']
#transcribe
json = { "audio_url": audioURL }
response = requests.post(transcriptEndpoint, json=json, headers=headers)
#print(response.json())
transribeID = response.json()['id']
#poll
pollingEndpoint = transcriptEndpoint + '/' + transribeID
pollingResponse = requests.get(pollingEndpoint,headers=headers)
#print(pollingResponse.json())
while True:
pollingResponse = requests.get(pollingEndpoint,headers=headers)
if pollingResponse.json()['status'] == 'completed':
print(time.time()-t0)
data = pollingResponse.json()['text']
print(data)
if 'OPEN YOUTUBE.' in data.upper():
webbrowser.open("https://www.youtube.com/")
break
elif pollingResponse.json()['status'] == 'error':
print('It is what it is')