-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaai.py
47 lines (32 loc) · 1019 Bytes
/
aai.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
import os
import assemblyai as aai
from pprint import pprint as pp
aai.settings.api_key = os.getenv("ASSEMBLY_API_KEY")
def on_open(session_opened: aai.RealtimeSessionOpened):
print("Session ID:", session_opened.session_id)
def on_data(transcript: aai.RealtimeTranscript):
#print(333, transcript.text)
if not transcript.text:
return
if isinstance(transcript, aai.RealtimeFinalTranscript):
#print(111)
print(777,transcript.text, end="\r\n")
#pp(dir(transcript))
else:
#print(222)
print(transcript.text, end="\r")
def on_error(error: aai.RealtimeError):
print("An error occured:", error)
def on_close():
print("Closing Session")
transcriber = aai.RealtimeTranscriber(
sample_rate=16_000,
on_data=on_data,
on_error=on_error,
on_open=on_open,
on_close=on_close,
)
transcriber.connect()
microphone_stream = aai.extras.MicrophoneStream(sample_rate=16_000)
transcriber.stream(microphone_stream)
transcriber.close()