-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
363 lines (291 loc) · 11.8 KB
/
Copy pathapp.py
File metadata and controls
363 lines (291 loc) · 11.8 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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
from flask import Flask, request
from flask_socketio import SocketIO, emit
from flask_cors import CORS
import sys
import os
import time
import base64
import logging
from dotenv import load_dotenv
from collections import defaultdict
from utils.logger import setup_logging
setup_logging(level=logging.INFO)
import warnings
warnings.filterwarnings('ignore', category=UserWarning, module='jieba')
warnings.filterwarnings('ignore', message='.*is deprecated.*')
from intentRecognizer import IntentRecognizer
from ttsModule import TTSService
from asrModule import ASRService
load_dotenv()
app = Flask(__name__, static_folder='static', static_url_path='/static')
SECRET_KEY = os.getenv('SECRET_KEY')
if not SECRET_KEY:
raise ValueError("SECRET_KEY must be set in .env")
app.config['SECRET_KEY'] = SECRET_KEY
socketio = SocketIO(app, cors_allowed_origins="*", max_http_buffer_size=10 ** 7, manage_session=False)
CORS(app)
logger = logging.getLogger(__name__)
session_conversations = defaultdict(list)
MAX_CONVERSATION_HISTORY = 50 # Max conversation turns to keep per session
# PIPELINE CONFIGURATION
ENABLE_ALGORITHMIC = True
ENABLE_SEMANTIC = True
ENABLE_LLM = True
MIN_CONFIDENCE = 0.5 # Default Minimum confidence to accept intent
ALGORITHMIC_THRESHOLD = 0.65 # Min confidence for algorithmic layer to skip next layers
SEMANTIC_THRESHOLD = 0.5 # Min confidence for semantic layer to skip LLM layer
SEMANTIC_MODEL = "all-mpnet-base-v2" # Options: "all-MiniLM-L6-v2"
LLM_MODEL = "llama3.2:3b-instruct-q4_K_M" # Options: "gpt-oss:120b-cloud", "gemma3:4b-it-qat"
ENABLE_LOGGING = True
TEST_MODE = False
# TTS Configuration
TTS_MODEL = "tts_models/en/ljspeech/vits"
TTS_OUTPUT_DIR = "./static/audio"
# ASR Configuration
ASR_MODEL = "tiny.en"
ENABLE_AUDIO_PREPROCESSING = True # Noise reduction, Normalization, Silence trimming
def initialize_intent_recognizer():
"""Initialize intent recognizer with error handling"""
try:
recognizer = IntentRecognizer(
enable_logging=ENABLE_LOGGING,
enable_algorithmic=ENABLE_ALGORITHMIC,
enable_semantic=ENABLE_SEMANTIC,
enable_llm=ENABLE_LLM,
algorithmic_threshold=ALGORITHMIC_THRESHOLD,
semantic_threshold=SEMANTIC_THRESHOLD,
semantic_model=SEMANTIC_MODEL,
llm_model=LLM_MODEL,
device="auto", # "cuda" , "cpu" , "auto" (for semantic model)
min_confidence=MIN_CONFIDENCE,
test_mode=TEST_MODE
)
return recognizer
except ValueError as e:
logger.error(f"Intent Recognizer Configuration Error: {e}\n")
sys.exit(1)
except Exception as e:
logger.error(f"Intent Recognizer Initialization Error: {e}\n")
sys.exit(1)
def initialize_tts_service():
"""Initialize TTS service with error handling"""
try:
tts = TTSService(
model_name=TTS_MODEL,
device="auto", # "cuda" , "cpu" , "auto"
enable_logging=ENABLE_LOGGING,
output_dir=TTS_OUTPUT_DIR,
)
return tts
except ImportError as e:
logger.error(f"\nTTS Import Error: {e}")
logger.error("Install with: pip install -r requirements.txt\n")
sys.exit(1)
except Exception as e:
logger.error(f"\nTTS Initialization Error: {e}\n")
sys.exit(1)
def initialize_asr_service():
"""Initialize ASR service with error handling"""
try:
asr = ASRService(
model_size=ASR_MODEL,
device="auto", # "cuda" , "cpu" , "auto"
enable_logging=ENABLE_LOGGING,
enable_preprocessing=ENABLE_AUDIO_PREPROCESSING
)
return asr
except ImportError as e:
logger.error("\nASR Error: Dependencies not installed")
logger.error("Install with: pip install openai-whisper")
if ENABLE_AUDIO_PREPROCESSING:
logger.info("For preprocessing: pip install librosa soundfile noisereduce\n")
sys.exit(1)
except Exception as e:
logger.error(f"\nASR Initialization Error: {e}\n")
sys.exit(1)
def perform_system_warmup():
"""Warm up Local LLM and TTS components to eliminate cold start latency"""
logger.info("Performing system warmup")
warmup_start = time.time()
if intent_recognizer:
try:
if ENABLE_LLM and not LLM_MODEL.endswith('-cloud'):
intent_recognizer.recognize_intent("sample text", [])
except Exception as e:
logger.warning(f"Warning: Intent recognizer warmup query failed: {e}")
if tts_service:
try:
warmup_audio = tts_service.generate_speech("System warmup", output_filename="warmup.wav")
if warmup_audio and os.path.exists(warmup_audio):
os.unlink(warmup_audio)
except Exception as e:
logger.warning(f"\nWarning: TTS warmup failed: {e}")
total_warmup_time = time.time() - warmup_start
intent_recognizer.reset_statistics()
tts_service.reset_statistics()
logger.info(f"Total System Warm-up Time: {total_warmup_time:.2f}s")
def generate_tts_audio(tts, response):
"""Generate TTS audio and return URL"""
if not (tts and response):
return None
try:
timestamp = int(time.time() * 1000)
audio_filename = f"response_{timestamp}.wav"
audio_path = tts.generate_speech(text=response, output_filename=audio_filename)
if audio_path:
return f"/static/audio/{audio_filename}"
except Exception as e:
logger.error(f"TTS generation error: {e}")
return None
def get_session_id() -> str:
"""Get or create a unique session ID for client"""
return request.sid # type: ignore
def add_to_conversation_history(session_id, entry_type, message, intent_info=None, audio_url=None, is_audio=False):
"""Add entry to conversation history for a specific session"""
entry = {
'type': entry_type,
'message': message,
'timestamp': int(time.time() * 1000),
'audio': is_audio
}
if intent_info:
entry.update({
'intent': intent_info.intent,
'confidence': intent_info.confidence_level,
'similarity': intent_info.confidence,
'layer_used': intent_info.layer_used,
'processing_method': intent_info.processing_method,
'audio_url': audio_url
})
session_conversations[session_id].append(entry)
# Enforce maximum conversation history limit
if len(session_conversations[session_id]) > MAX_CONVERSATION_HISTORY:
session_conversations[session_id] = session_conversations[session_id][-MAX_CONVERSATION_HISTORY:]
def process_user_input(session_id, user_text, is_audio=False):
"""Process user input and generate response for a specific session"""
add_to_conversation_history(session_id, 'user', user_text, is_audio=is_audio)
conversation_history = session_conversations[session_id]
intent_info = intent_recognizer.recognize_intent(user_text, conversation_history)
response = intent_info.response
audio_url = generate_tts_audio(tts_service, response)
add_to_conversation_history(session_id, 'assistant', response, intent_info, audio_url, is_audio)
return {
'user_text': user_text,
'assistant_response': response,
'conversation_history': conversation_history,
'intent_info': {
'intent': intent_info.intent,
'confidence': intent_info.confidence_level,
'similarity': intent_info.confidence,
'layer_used': intent_info.layer_used,
'processing_method': intent_info.processing_method,
},
'audio_url': audio_url
}
@socketio.on('connect')
def handle_connect():
session_id = get_session_id()
logger.info(f'Client connected : [{session_id}]')
@socketio.on('disconnect')
def handle_disconnect():
session_id = get_session_id()
logger.info(f'Client disconnected : [{session_id}]')
# Clean up session data
if session_id in session_conversations:
del session_conversations[session_id]
@socketio.on('voice_input')
def handle_voice_input(data):
"""Handle incoming voice audio for transcription and processing"""
if not asr_service:
emit('error', {'message': 'ASR service not available'})
return
try:
session_id = get_session_id()
audio_data = data.get('audio')
if not audio_data:
emit('error', {'message': 'No audio data received'})
return
audio_bytes = base64.b64decode(audio_data.split(',')[1] if ',' in audio_data else audio_data)
emit('transcription_status', {'status': 'processing'})
transcribed_text, confidence = asr_service.transcribe_audio_data(audio_bytes)
if not transcribed_text:
emit('transcription_result', {
'text': '',
'confidence': 0.0,
'error': 'No speech detected'
})
return
emit('transcription_result', {
'text': transcribed_text,
'confidence': confidence
})
result = process_user_input(session_id, transcribed_text, is_audio=True)
result['transcribed_text'] = transcribed_text
emit('voice_response', result)
except Exception as e:
logger.error(f" Voice input error: {e}", exc_info=True)
emit('error', {'message': f'Processing error: {str(e)}'})
@socketio.on('text_input')
def handle_text_input(data):
"""Handle incoming text messages directly without ASR"""
try:
session_id = get_session_id()
text = data.get('text')
if not text:
emit('error', {'message': 'No text received'})
return
result = process_user_input(session_id, text, is_audio=False)
emit('text_response', result)
except Exception as e:
logger.error(f"Text input error: {e}", exc_info=True)
emit('error', {'message': f'Processing error: {str(e)}'})
@socketio.on('clear_history')
def handle_clear_history():
session_id = get_session_id()
session_conversations[session_id].clear()
logger.info(f'Cleared History : [{session_id}]')
emit('history_cleared')
@app.route('/')
def index():
"""Static HTML page"""
return app.send_static_file('index.html')
@app.route('/statistics')
def get_statistics():
"""Get system statistics including layer usage, TTS, and ASR"""
from flask import jsonify
stats = intent_recognizer.get_statistics()
stats.pop('intent_distribution', None)
stats.pop('average_confidence', None)
if tts_service:
stats['tts'] = tts_service.get_statistics()
if asr_service:
stats['asr'] = asr_service.get_statistics()
organized = {
"overview": {
"total_queries": stats.pop('total_queries_processed', 0),
"pipeline": stats.pop('pipeline_configuration', {}),
"layer_usage": stats.pop('layer_usage', {})
},
"layers": {k: v for k, v in stats.items() if k.endswith('_layer')},
"services": {k: v for k, v in stats.items() if k in ['tts', 'asr']}
}
organized['layers'].get('algorithmic_layer', {}).pop('patterns_evaluated', None)
return jsonify(organized)
# System Start
intent_recognizer = initialize_intent_recognizer()
tts_service = initialize_tts_service()
asr_service = initialize_asr_service()
layers = []
if ENABLE_ALGORITHMIC:
layers.append("Algorithmic")
if ENABLE_SEMANTIC:
layers.append("Semantic")
if ENABLE_LLM:
model_type = "Cloud" if LLM_MODEL.endswith('-cloud') else "Local"
layers.append(f"LLM (Ollama-{model_type})")
pipeline = "Pipeline: " + " -> ".join(layers)
logger.info(pipeline)
# System Warmup
perform_system_warmup()
if __name__ == '__main__':
socketio.run(app, debug=True, host='0.0.0.0', port=5000, log_output=True)