forked from nexmo-community/AnsweringMachineDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
330 lines (284 loc) · 8.86 KB
/
app.py
File metadata and controls
330 lines (284 loc) · 8.86 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python
from __future__ import absolute_import, print_function
import glob
import wave
import random
import struct
import datetime
import io
import logging
import os
import sys
import time
from logging import debug, info
import uuid
import cgi
import audioop
import requests
import tornado.ioloop
import tornado.websocket
import tornado.httpserver
import tornado.template
import tornado.web
import webrtcvad
from tornado.web import url
import json
from base64 import b64decode
import nexmo
import collections
import pickle
import librosa
import numpy as np
# Only used for record function
logging.captureWarnings(True)
# Constants:
MS_PER_FRAME = 20 # Duration of a frame in ms
RATE = 16000
SILENCE = 10 # How many continuous frames of silence determine the end of a phrase
CLIP_MIN_MS = 200 # ms - the minimum audio clip that will be used
MAX_LENGTH = 3000 # Max length of a sound clip for processing in ms
VAD_SENSITIVITY = 3
CLIP_MIN_FRAMES = CLIP_MIN_MS // MS_PER_FRAME
# Global variables
conns = {}
conversation_uuids = collections.defaultdict(list)
nexmo_client = None
model = None
from dotenv import load_dotenv
load_dotenv()
# Environment Variables, these are set in .env locally
PORT = os.getenv("PORT")
MY_LVN = os.getenv("MY_LVN")
APP_ID = os.getenv("APP_ID")
ANSWERING_MACHINE_TEXT = os.getenv("ANSWERING_MACHINE_TEXT")
def _get_private_key():
try:
return os.getenv("PRIVATE_KEY")
except:
with open('private.key', 'r') as f:
private_key = f.read()
return private_key
PRIVATE_KEY = _get_private_key()
class BufferedPipe(object):
def __init__(self, max_frames, sink):
"""
Create a buffer which will call the provided `sink` when full.
It will call `sink` with the number of frames and the accumulated bytes when it reaches
`max_buffer_size` frames.
"""
self.sink = sink
self.max_frames = max_frames
self.count = 0
self.payload = b''
def append(self, data, id):
""" Add another data to the buffer. `data` should be a `bytes` object. """
self.count += 1
self.payload += data
if self.count == self.max_frames:
self.process(id)
def process(self, id):
""" Process and clear the buffer. """
self.sink(self.count, self.payload, id)
self.count = 0
self.payload = b''
class NexmoClient(object):
def __init__(self):
self.client = nexmo.Client(application_id=APP_ID, private_key=PRIVATE_KEY)
def hangup(self,conversation_uuid):
for event in conversation_uuids[conversation_uuid]:
try:
response = self.client.update_call(event["uuid"], action='hangup')
debug("hangup uuid {} response: {}".format(event["conversation_uuid"], response))
except Exception as e:
debug("Hangup error",e)
conversation_uuids[conversation_uuid].clear()
def speak(self, conversation_uuid):
uuids = [event["uuid"] for event in conversation_uuids[conversation_uuid] if event["from"] == MY_LVN and "ws" not in event["to"]]
uuid = next(iter(uuids), None)
debug(uuid)
if uuid is not None:
debug('found {}'.format(uuid))
response = self.client.send_speech(uuid, text=ANSWERING_MACHINE_TEXT)
debug("send_speech response",response)
else:
debug("{} does not exist in list {}".format(conversation_uuid, conversation_uuids[conversation_uuid]))
class AudioProcessor(object):
def __init__(self, path, conversation_uuid):
self._path = path
self.conversation_uuid = conversation_uuid
def process(self, count, payload, conversation_uuid):
if count > CLIP_MIN_FRAMES : # If the buffer is less than CLIP_MIN_MS, ignore it
debug("record clip")
fn = "rec-{}-{}.wav".format(conversation_uuid,datetime.datetime.now().strftime("%Y%m%dT%H%M%S"))
output = wave.open(fn, 'wb')
output.setparams(
(1, 2, RATE, 0, 'NONE', 'not compressed'))
output.writeframes(payload)
output.close()
prediction = model.predict_from_file(fn)
info("prediction {}".format(prediction))
self.remove_file(fn)
if prediction == 0 or prediction == 1:
info("** beep detected **")
nexmo_client.speak(conversation_uuid)
else:
info('Discarding {} frames'.format(str(count)))
def remove_file(self, wav_file):
os.remove(wav_file)
class MLModel(object):
def __init__(self):
self.model = pickle.load(open("models/GaussianProcessClassifier-20190807T1859.pkl", "rb"))
info(self.model)
def predict_from_file(self, wav_file, verbose=False):
X, sample_rate = librosa.load(wav_file, res_type='kaiser_fast')
mfccs_40 = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)
prediction = self.model.predict([mfccs_40])
info("GaussianProcessClassifier prediction {}".format(prediction))
return prediction[0]
class WSHandler(tornado.websocket.WebSocketHandler):
def initialize(self):
# Create a buffer which will call `process` when it is full:
self.frame_buffer = None
# Setup the Voice Activity Detector
self.tick = None
self.id = None
self.vad = webrtcvad.Vad()
# Level of sensitivity
self.vad.set_mode(VAD_SENSITIVITY)
self.processor = None
self.path = None
def open(self, path):
info("client connected")
debug(self.request.uri)
self.path = self.request.uri
self.tick = 0
def on_message(self, message):
# Check if message is Binary or Text
if type(message) != str:
if self.vad.is_speech(message, RATE):
debug("SPEECH from {}".format(self.id))
self.tick = SILENCE
self.frame_buffer.append(message, self.id)
else:
debug("Silence from {} TICK: {}".format(self.id, self.tick))
self.tick -= 1
if self.tick == 0:
# Force processing and clearing of the buffer
self.frame_buffer.process(self.id)
else:
info(message)
# Here we should be extracting the meta data that was sent and attaching it to the connection object
data = json.loads(message)
if data.get('content-type'):
conversation_uuid = data.get('conversation_uuid') #change to use
self.id = conversation_uuid
conns[self.id] = self
self.processor = AudioProcessor(
self.path, conversation_uuid).process
self.frame_buffer = BufferedPipe(MAX_LENGTH // MS_PER_FRAME, self.processor)
self.write_message('ok')
def on_close(self):
# Remove the connection from the list of connections
del conns[self.id]
info("client disconnected")
class EventHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def post(self):
data = json.loads(self.request.body)
if data["status"] == "answered":
debug(data)
conversation_uuid = data["conversation_uuid"]
conversation_uuids[conversation_uuid].append(data)
if data["to"] == MY_LVN and data["status"] == "completed":
conversation_uuid = data["conversation_uuid"]
nexmo_client.hangup(conversation_uuid)
self.content_type = 'text/plain'
self.write('ok')
self.finish()
class EnterPhoneNumberHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
debug(self.request)
ncco =[
{
"action": "talk",
"text": "Please enter a phone number to dial"
},
{
"action": "input",
"eventUrl": [self.request.protocol +"://" + self.request.host +"/ivr"],
"timeOut":10,
"maxDigits":12,
"submitOnHash":True
}
]
self.write(json.dumps(ncco))
self.set_header("Content-Type", 'application/json; charset="utf-8"')
self.finish()
class AcceptNumberHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def post(self):
data = json.loads(self.request.body)
debug(data)
ncco = [
{
"action": "talk",
"text": "Thanks. Connecting you now"
},
{
"action": "connect",
# "eventUrl": [self.request.protocol +"://" + self.request.host + "/event"],
"from": MY_LVN,
"endpoint": [
{
"type": "phone",
"number": data["dtmf"]
}
]
},
{
"action": "connect",
# "eventUrl": [self.request.protocol +"://" + self.request.host +"/event"],
"from": MY_LVN,
"endpoint": [
{
"type": "websocket",
"uri" : "ws://"+self.request.host +"/socket",
"content-type": "audio/l16;rate=16000",
"headers": {
"conversation_uuid":data["conversation_uuid"] #change to user
}
}
]
}
]
self.write(json.dumps(ncco))
self.set_header("Content-Type", 'application/json; charset="utf-8"')
self.finish()
class PingHandler(tornado.web.RequestHandler):
@tornado.web.asynchronous
def get(self):
self.write('ok')
self.set_header("Content-Type", 'text/plain')
self.finish()
def main():
try:
global nexmo_client, model
nexmo_client = NexmoClient()
model = MLModel()
logging.getLogger().setLevel(logging.INFO)
application = tornado.web.Application([
url(r"/ping", PingHandler),
(r"/event", EventHandler),
(r"/ncco", EnterPhoneNumberHandler),
(r"/ivr", AcceptNumberHandler),
url(r"/(.*)", WSHandler),
])
http_server = tornado.httpserver.HTTPServer(application)
port = int(os.getenv('PORT', 8000))
http_server.listen(port)
tornado.ioloop.IOLoop.instance().start()
except KeyboardInterrupt:
pass # Suppress the stack-trace on quit
if __name__ == "__main__":
main()