-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathChatMojiBot.py
More file actions
138 lines (122 loc) · 4.42 KB
/
ChatMojiBot.py
File metadata and controls
138 lines (122 loc) · 4.42 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import audioop
# from distutils.command.upload import upload
import websockets
import asyncio
import base64
import json
# from configure import auth_key
import pyaudio
import time
from emojiTranslator import emojiMaker
# from emojitranslator import *
FRAMES_PER_BUFFER = 3200
FORMAT = pyaudio.paInt16
CHANNELS = 1
RATE = 16000
p = pyaudio.PyAudio()
# starts recording
stream = p.open(
format=FORMAT,
channels=CHANNELS,
rate=RATE,
input=True,
frames_per_buffer=FRAMES_PER_BUFFER
)
auth_key = "646acad2d1114c38a644e646876a9553"
URL = "wss://api.assemblyai.com/v2/realtime/ws?sample_rate=16000"
class ChatMoji():
def __init__(self) -> None:
self.chatmoji_message = ""
self.finish = False
async def send_receive(self):
"""
Send audio to AssemblyAI server and recieve message
"""
print(f'Connecting websocket to url ${URL}')
async with websockets.connect(
URL,
extra_headers=(("Authorization", auth_key),),
ping_interval=5,
ping_timeout=20
) as _ws:
await asyncio.sleep(0.1)
print("Receiving SessionBegins ...")
session_begins = await _ws.recv()
print(session_begins)
print("Sending messages ...")
async def send():
"""
records and sends message to server, stops recording once voice is lowered
"""
var = True
start = -1
while var:
try:
data = stream.read(FRAMES_PER_BUFFER)
mx = audioop.rms(data, 2)
if mx > 2300:
start = -1
elif mx < 2300:
if start < 0:
start = time.time()
var = True
else:
if time.time() - start >= 5: #Wait five seconds
var=False
return
data = base64.b64encode(data).decode("utf-8")
json_data = json.dumps({"audio_data":str(data)})
await _ws.send(json_data)
except websockets.exceptions.ConnectionClosedError as e:
print(e)
assert e.code == 4008
break
except Exception as e:
assert False, "Not a websocket 4008 error"
await asyncio.sleep(0.01)
return True
async def receive():
"""
recieve message from AssemblyAI server
"""
while True:
try:
result_str = await _ws.recv()
API_ret = json.loads(result_str)
cont=True
# print(API_ret['text'])
if (API_ret['message_type'] == "FinalTranscript"):
#determines when user has finished speaking
self.strmake(API_ret['text'])
return
except websockets.exceptions.ConnectionClosedError as e:
print(e)
assert e.code == 4008
break
except Exception as e:
assert False, "Not a websocket 4008 error"
send_result, receive_result = await asyncio.gather(send(), receive())
def strmake(self, strm):
"""
Creates the Emoji
"""
sp = str(strm)
# print(emojiMaker(sp))
self.chatmoji_message = emojiMaker(sp)
def runner(self):
asyncio.run(self.send_receive())
# if __name__ == '__main__':
# chat = ChatMoji()
# chat.runner()
# # translation to different languages for speech to text
# endpoint = "https://api.assemblyai.com/v2/transcript"
# json = {
# "audio_url": "https://bit.ly/3JNcp4Y",
# "language_detection": True
# }
# headers = {
# "authorization": "YOUR-API-TOKEN",
# "content-type": "application/json"
# }
# response = requests.post(endpoint, json=json, headers=headers)
# print(response.json())