datbkpro commited on
Commit
6d6105b
·
verified ·
1 Parent(s): c912bbd

Update ui/tabs.py

Browse files
Files changed (1) hide show
  1. ui/tabs.py +38 -12
ui/tabs.py CHANGED
@@ -200,11 +200,14 @@ def create_audio_tab(audio_service: AudioService):
200
  outputs=[transcription_output, response_output, tts_audio_output, language_display] # UPDATED
201
  )
202
  def create_streaming_voice_tab(streaming_service: StreamingVoiceService):
203
- """Tạo tab streaming voice với VAD optimized"""
204
 
205
  with gr.Blocks() as streaming_tab:
206
  gr.Markdown("## 🎤 Trò chuyện giọng nói thời gian thực - Tối ưu hóa")
207
 
 
 
 
208
  with gr.Row():
209
  with gr.Column(scale=1):
210
  # Controls
@@ -268,14 +271,19 @@ def create_streaming_voice_tab(streaming_service: StreamingVoiceService):
268
  # State variables
269
  is_vad_active = gr.State(value=False)
270
 
 
 
 
 
 
271
  def start_vad():
272
- """Bắt đầu VAD"""
273
- success = streaming_service.start_listening(lambda result: None)
274
  if success:
275
  is_vad_active.value = True
276
  status = "✅ VAD đang chạy - Hãy nói gì đó!"
277
- state_info = streaming_service.get_conversation_state()
278
- state_text = f"VAD: Đang hoạt động\nQueue: {state_info['queue_size']}\nThreads: {state_info['worker_threads']}"
279
  else:
280
  status = "❌ Không thể khởi động VAD"
281
  state_text = "Lỗi khởi động"
@@ -286,8 +294,8 @@ def create_streaming_voice_tab(streaming_service: StreamingVoiceService):
286
  """Dừng VAD"""
287
  streaming_service.stop_listening()
288
  is_vad_active.value = False
289
- state_info = streaming_service.get_conversation_state()
290
- state_text = f"VAD: Đã dừng\nHistory: {state_info['history_length']} messages"
291
  return "🛑 VAD đã dừng", state_text
292
 
293
  def process_microphone(audio_data):
@@ -297,17 +305,29 @@ def create_streaming_voice_tab(streaming_service: StreamingVoiceService):
297
 
298
  try:
299
  result = streaming_service.process_streaming_audio(audio_data)
300
- state_info = streaming_service.get_conversation_state()
301
- state_text = f"Manual mode\nHistory: {state_info['history_length']} messages"
302
  return result['transcription'], result['response'], result['tts_audio'], state_text
303
  except Exception as e:
304
  return f"Lỗi: {e}", "Xin lỗi, có lỗi xảy ra", None, "Lỗi xử lý"
305
 
 
 
 
 
 
 
 
 
 
 
 
 
306
  def clear_chat():
307
  """Xóa hội thoại"""
308
  streaming_service.clear_conversation()
309
- state_info = streaming_service.get_conversation_state()
310
- state_text = f"Đã xóa hội thoại\nHistory: {state_info['history_length']} messages"
311
  return "", "", None, state_text
312
 
313
  def refresh_latency():
@@ -349,7 +369,13 @@ def create_streaming_voice_tab(streaming_service: StreamingVoiceService):
349
  outputs=[latency_display]
350
  )
351
 
352
- # Tự động cập nhật mỗi 3 giây
 
 
 
 
 
 
353
  gr.Timer(3.0).tick(
354
  fn=update_state_info,
355
  outputs=[state_info, latency_display]
 
200
  outputs=[transcription_output, response_output, tts_audio_output, language_display] # UPDATED
201
  )
202
  def create_streaming_voice_tab(streaming_service: StreamingVoiceService):
203
+ """Tạo tab streaming voice với VAD optimized - FIXED VERSION"""
204
 
205
  with gr.Blocks() as streaming_tab:
206
  gr.Markdown("## 🎤 Trò chuyện giọng nói thời gian thực - Tối ưu hóa")
207
 
208
+ # FIXED: Thêm state để lưu kết quả VAD
209
+ vad_result_state = gr.State(value=None)
210
+
211
  with gr.Row():
212
  with gr.Column(scale=1):
213
  # Controls
 
271
  # State variables
272
  is_vad_active = gr.State(value=False)
273
 
274
+ def vad_callback(result):
275
+ """Callback khi VAD phát hiện speech - FIXED VERSION"""
276
+ print(f"🎯 Nhận kết quả từ VAD: {result['transcription']}")
277
+ vad_result_state.value = result
278
+
279
  def start_vad():
280
+ """Bắt đầu VAD - FIXED VERSION"""
281
+ success = streaming_service.start_listening(vad_callback)
282
  if success:
283
  is_vad_active.value = True
284
  status = "✅ VAD đang chạy - Hãy nói gì đó!"
285
+ state = streaming_service.get_conversation_state()
286
+ state_text = f"VAD: Đang hoạt động\nQueue: {state['queue_size']}\nThreads: {state['worker_threads']}"
287
  else:
288
  status = "❌ Không thể khởi động VAD"
289
  state_text = "Lỗi khởi động"
 
294
  """Dừng VAD"""
295
  streaming_service.stop_listening()
296
  is_vad_active.value = False
297
+ state = streaming_service.get_conversation_state()
298
+ state_text = f"VAD: Đã dừng\nHistory: {state['history_length']} messages"
299
  return "🛑 VAD đã dừng", state_text
300
 
301
  def process_microphone(audio_data):
 
305
 
306
  try:
307
  result = streaming_service.process_streaming_audio(audio_data)
308
+ state = streaming_service.get_conversation_state()
309
+ state_text = f"Manual mode\nHistory: {state['history_length']} messages"
310
  return result['transcription'], result['response'], result['tts_audio'], state_text
311
  except Exception as e:
312
  return f"Lỗi: {e}", "Xin lỗi, có lỗi xảy ra", None, "Lỗi xử lý"
313
 
314
+ def check_vad_results():
315
+ """Kiểm tra kết quả VAD - FIXED: Cập nhật UI khi có kết quả mới"""
316
+ if vad_result_state.value is not None:
317
+ result = vad_result_state.value
318
+ vad_result_state.value = None # Reset sau khi dùng
319
+
320
+ state = streaming_service.get_conversation_state()
321
+ state_text = f"VAD mode\nHistory: {state['history_length']} messages\nQueue: {state['queue_size']}"
322
+
323
+ return result['transcription'], result['response'], result['tts_audio'], state_text
324
+ return gr.skip(), gr.skip(), gr.skip(), gr.skip()
325
+
326
  def clear_chat():
327
  """Xóa hội thoại"""
328
  streaming_service.clear_conversation()
329
+ state = streaming_service.get_conversation_state()
330
+ state_text = f"Đã xóa hội thoại\nHistory: {state['history_length']} messages"
331
  return "", "", None, state_text
332
 
333
  def refresh_latency():
 
369
  outputs=[latency_display]
370
  )
371
 
372
+ # FIXED: Timer để kiểm tra kết quả VAD
373
+ gr.Timer(1.0).tick(
374
+ fn=check_vad_results,
375
+ outputs=[transcription_box, response_box, audio_output, state_info]
376
+ )
377
+
378
+ # Timer để cập nhật system info
379
  gr.Timer(3.0).tick(
380
  fn=update_state_info,
381
  outputs=[state_info, latency_display]