-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
346 lines (290 loc) · 11.5 KB
/
main.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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import base64
import json
import os
from flask import Flask, request, Response
from flask_sock import Sock
from twilio.rest import Client
from dotenv import load_dotenv
from queue import Queue , Empty
import threading
import time
import requests
from deepgram import (
DeepgramClient,
LiveTranscriptionEvents,
LiveOptions,
)
load_dotenv()
# Flask settings
PORT = 5000
DEBUG = False
INCOMING_CALL_ROUTE = '/'
WEBSOCKET_ROUTE = '/realtime'
# Twilio authentication
account_sid = os.environ['TWILIO_ACCOUNT_SID']
api_key = os.environ['TWILIO_API_KEY_SID']
api_secret = os.environ['TWILIO_API_SECRET']
client = Client(api_key, api_secret, account_sid)
TWILIO_NUMBER = os.environ['TWILIO_NUMBER']
# Deepgram settings
DEEPGRAM_API_KEY = os.environ['DEEPGRAM_API_KEY']
# Endpoint for RAG query
RAG_QUERY_URL = 'http://206.189.138.143:5000/rag_query'
app = Flask(__name__)
sock = Sock(app)
# Global variables for managing state
audio_queue = Queue()
call_active = threading.Event()
deepgram_connection = None
lock_exit = threading.Lock()
exit_flag = False
transcript = ""
response_to_speak = None # Variable to hold the response to speak
current_call_sid = None
def send_transcript_to_rag_query(final_transcript):
"""Send transcript to RAG query endpoint"""
try:
callwise_api = os.getenv('CALLWISE_API') # Make sure 'callwise_api' is set in your environment
if callwise_api is None:
print("Error: CALLWISE_API environment variable is not set.")
return None
# Prepare payload with conversation history
payload = {
"user_id": callwise_api, # Replace with actual user ID
"query": final_transcript,
"conversation_history": [] # Add conversation history if needed
}
response = requests.post(RAG_QUERY_URL, json=payload)
if response.status_code == 200:
output = response.json().get("response", "No response")
print("RAG Response:", output)
return output
else:
print("Error:", response.json())
return None
except Exception as e:
print(f"Error sending to RAG query: {e}")
return None
def setup_deepgram():
"""Set up the Deepgram connection"""
try:
# Create Deepgram client
deepgram = DeepgramClient(DEEPGRAM_API_KEY)
# Create websocket connection
connection = deepgram.listen.live.v("1")
# Define event handlers
def on_message(self, result, **kwargs):
global transcript, response_to_speak
transcript = result.channel.alternatives[0].transcript
if transcript.strip():
print(f"Transcript: {transcript}")
# Send transcript to RAG query immediately
rag_response = send_transcript_to_rag_query(transcript)
if rag_response:
# Set the response to speak
response_to_speak = rag_response
def on_metadata(self, metadata, **kwargs):
print(f"Metadata: {metadata}")
def on_error(self, error, **kwargs):
print(f"Error: {error}")
# Register event handlers
connection.on(LiveTranscriptionEvents.Transcript, on_message)
connection.on(LiveTranscriptionEvents.Metadata, on_metadata)
connection.on(LiveTranscriptionEvents.Error, on_error)
# Configure Deepgram options
options = LiveOptions(
model="nova-2",
language="en-IN",
encoding="mulaw",
sample_rate=8000,
smart_format=True,
interim_results=False,
punctuate=True,
)
# Start the connection
connection.start(options)
print("Successfully connected to Deepgram")
return connection
except Exception as e:
print(f"Error setting up Deepgram: {e}")
return None
def process_audio_queue():
"""Process audio data from the queue and send to Deepgram"""
global deepgram_connection, exit_flag, call_active
try:
silence_data = b'\xff' * 160 # mu-law encoded silence
last_audio_time = time.time()
while call_active.is_set() or not audio_queue.empty():
try:
audio_data = audio_queue.get(timeout=0.1)
if audio_data is None:
break
lock_exit.acquire()
if exit_flag:
lock_exit.release()
break
lock_exit.release()
current_time = time.time()
# Send silence if more than 100ms has passed without audio
if current_time - last_audio_time > 0.1:
deepgram_connection.send(silence_data)
deepgram_connection.send(audio_data)
last_audio_time = current_time
time.sleep(0.01) # Small delay to prevent overwhelming the connection
except Empty:
# Send silence if queue is empty
deepgram_connection.send(silence_data)
last_audio_time = time.time()
except Exception as e:
print(f"Error sending audio to Deepgram: {e}")
break
except Exception as e:
print(f"Error processing audio queue: {e}")
finally:
try:
if deepgram_connection:
deepgram_connection.finish()
except:
pass
@app.route(INCOMING_CALL_ROUTE, methods=['GET', 'POST'])
def receive_call():
"""Handle incoming Twilio calls"""
global current_call_sid # Ensure this is global
if request.method == 'POST':
current_call_sid = request.values.get('CallSid') # Store CallSid here
if not current_call_sid:
print("Error: CallSid not found.")
xml = f"""
<Response>
<Say>
Start speaking.
</Say>
<Connect>
<Stream url='wss://{request.host}{WEBSOCKET_ROUTE}' />
</Connect>
</Response>
""".strip()
return Response(xml, mimetype='text/xml')
else:
return "Real-time phone call transcription service"
@sock.route(WEBSOCKET_ROUTE)
def handle_twilio_connection(ws):
"""Handle WebSocket connection from Twilio"""
global deepgram_connection, exit_flag, response_to_speak, call_active
print("New Twilio WebSocket connection established")
# Set up Deepgram connection
call_active.set()
deepgram_connection = setup_deepgram()
if not deepgram_connection:
print("Failed to establish Deepgram connection")
return
# Start audio processing thread
audio_thread = threading.Thread(target=process_audio_queue)
audio_thread.daemon = True
audio_thread.start()
try:
last_activity_time = time.time()
while call_active.is_set(): # Change to use call_active event
try:
# Use a timeout to periodically check call_active
message = ws.receive(timeout=5) # Add timeout
if message is None:
# Check if call is still active
if not call_active.is_set():
break
continue
data = json.loads(message)
match data['event']:
case "connected":
print('Twilio WebSocket connected')
last_activity_time = time.time()
case "start":
print('Call started')
last_activity_time = time.time()
case "media":
try:
payload_b64 = data['media']['payload']
audio_data = base64.b64decode(payload_b64)
audio_queue.put(audio_data)
last_activity_time = time.time()
except Exception as e:
print(f"Error processing media: {e}")
case "stop":
print('Call stop event received')
call_active.clear()
break
# Handle response to speak
if response_to_speak:
try:
client.calls(current_call_sid).update(
twiml=f"""
<Response>
<Pause length="1"/>
<Say>Just a Second looking for the information</Say>
<Say>{response_to_speak}</Say>
<Pause length="1"/>
<Say>Please continue speaking.</Say>
<Connect>
<Stream url='wss://{request.host}{WEBSOCKET_ROUTE}' />
</Connect>
</Response>
"""
)
print("Spoken response sent via Twilio")
response_to_speak = None
except Exception as e:
print(f"Error speaking response: {e}")
# Periodic keep-alive check
if time.time() - last_activity_time > 30: # 30 seconds inactivity
try:
client.calls(current_call_sid).update(
twiml="""
<Response>
<Pause length="1"/>
<Say>Still here and listening.</Say>
<Connect>
<Stream url='wss://{request.host}{WEBSOCKET_ROUTE}' />
</Connect>
</Response>
"""
)
print("Sent keep-alive message to Twilio")
last_activity_time = time.time()
except Exception as e:
print(f"Error sending keep-alive message: {e}")
except websocket.WebSocketTimeoutException:
# Timeout occurred, but we want to continue
if not call_active.is_set():
break
continue
except Exception as e:
print(f"WebSocket receive error: {e}")
break
except Exception as e:
print(f"Error in Twilio connection: {e}")
finally:
print("Cleaning up connections...")
# Signal threads to stop
call_active.clear()
# Clean up queue
audio_queue.put(None)
# Wait for audio processing to finish
audio_thread.join(timeout=5)
# Close Deepgram connection
if deepgram_connection:
try:
deepgram_connection.finish()
except Exception as e:
print(f"Error closing Deepgram connection: {e}")
print("Call session ended")
def main():
"""Main function to start the Flask server"""
try:
print(f"Starting server on port {PORT}...")
app.run(port=PORT, debug=DEBUG, threaded=True)
except Exception as e:
print(f"Error starting server: {e}")
finally:
call_active.clear()
if __name__ == "__main__":
main()