Update services/streaming_voice_service.py
Browse files
services/streaming_voice_service.py
CHANGED
|
@@ -586,7 +586,116 @@ class StreamingVoiceService:
|
|
| 586 |
finally:
|
| 587 |
with self.processing_lock:
|
| 588 |
self.is_processing = False
|
| 589 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 590 |
def process_audio_chunk(self, audio_data: tuple) -> Dict[str, Any]:
|
| 591 |
"""Xử lý audio chunk với VAD"""
|
| 592 |
if not audio_data or not self.is_listening:
|
|
|
|
| 586 |
finally:
|
| 587 |
with self.processing_lock:
|
| 588 |
self.is_processing = False
|
| 589 |
+
# THÊM LẠI PHƯƠNG THỨC process_streaming_audio ĐÃ BỊ THIẾU
|
| 590 |
+
def process_streaming_audio(self, audio_data: tuple) -> Dict[str, Any]:
|
| 591 |
+
"""Xử lý audio streaming (phương thức cũ cho compatibility với Gradio)"""
|
| 592 |
+
if not audio_data:
|
| 593 |
+
return {
|
| 594 |
+
'transcription': "❌ Không có dữ liệu âm thanh",
|
| 595 |
+
'response': "Vui lòng nói lại",
|
| 596 |
+
'tts_audio': None,
|
| 597 |
+
'status': 'error'
|
| 598 |
+
}
|
| 599 |
+
|
| 600 |
+
# Nếu đang xử lý VAD, trả về trạng thái processing
|
| 601 |
+
if self.is_processing:
|
| 602 |
+
return {
|
| 603 |
+
'transcription': "Đang xử lý...",
|
| 604 |
+
'response': "",
|
| 605 |
+
'tts_audio': None,
|
| 606 |
+
'status': 'processing'
|
| 607 |
+
}
|
| 608 |
+
|
| 609 |
+
try:
|
| 610 |
+
# Lấy dữ liệu audio từ Gradio
|
| 611 |
+
sample_rate, audio_array = audio_data
|
| 612 |
+
|
| 613 |
+
print(f"🎯 Nhận audio manual: {len(audio_array)} samples, SR: {sample_rate}")
|
| 614 |
+
|
| 615 |
+
# Kiểm tra kiểu dữ liệu và chuyển đổi nếu cần
|
| 616 |
+
if isinstance(audio_array, np.ndarray):
|
| 617 |
+
if audio_array.dtype == np.float32 or audio_array.dtype == np.float64:
|
| 618 |
+
# Chuyển từ float sang int16
|
| 619 |
+
audio_array = (audio_array * 32767).astype(np.int16)
|
| 620 |
+
|
| 621 |
+
# Kiểm tra audio có dữ liệu không
|
| 622 |
+
if len(audio_array) == 0:
|
| 623 |
+
return {
|
| 624 |
+
'transcription': "❌ Âm thanh trống",
|
| 625 |
+
'response': "Vui lòng nói lại",
|
| 626 |
+
'tts_audio': None,
|
| 627 |
+
'status': 'error'
|
| 628 |
+
}
|
| 629 |
+
|
| 630 |
+
# Tính toán âm lượng
|
| 631 |
+
audio_abs = np.abs(audio_array.astype(np.float32))
|
| 632 |
+
audio_rms = np.sqrt(np.mean(audio_abs**2)) / 32767.0
|
| 633 |
+
print(f"📊 Âm lượng RMS: {audio_rms:.4f}")
|
| 634 |
+
|
| 635 |
+
if audio_rms < 0.005:
|
| 636 |
+
return {
|
| 637 |
+
'transcription': "❌ Âm thanh quá yếu",
|
| 638 |
+
'response': "Xin vui lòng nói to hơn",
|
| 639 |
+
'tts_audio': None,
|
| 640 |
+
'status': 'error'
|
| 641 |
+
}
|
| 642 |
+
|
| 643 |
+
# Sử dụng VAD để kiểm tra speech
|
| 644 |
+
if not self.vad_processor.is_speech(audio_array, sample_rate):
|
| 645 |
+
return {
|
| 646 |
+
'transcription': "❌ Không phát hiện giọng nói",
|
| 647 |
+
'response': "Vui lòng nói rõ hơn",
|
| 648 |
+
'tts_audio': None,
|
| 649 |
+
'status': 'error'
|
| 650 |
+
}
|
| 651 |
+
|
| 652 |
+
# Chuyển đổi thành văn bản
|
| 653 |
+
transcription = self._transcribe_audio(audio_array, sample_rate)
|
| 654 |
+
|
| 655 |
+
if not transcription or len(transcription.strip()) == 0:
|
| 656 |
+
return {
|
| 657 |
+
'transcription': "❌ Không nghe rõ",
|
| 658 |
+
'response': "Xin vui lòng nói lại rõ hơn",
|
| 659 |
+
'tts_audio': None,
|
| 660 |
+
'status': 'error'
|
| 661 |
+
}
|
| 662 |
+
|
| 663 |
+
# Kiểm tra nếu transcription quá ngắn
|
| 664 |
+
if len(transcription.strip()) < 2:
|
| 665 |
+
return {
|
| 666 |
+
'transcription': "❌ Câu nói quá ngắn",
|
| 667 |
+
'response': "Xin vui lòng nói câu dài hơn",
|
| 668 |
+
'tts_audio': None,
|
| 669 |
+
'status': 'error'
|
| 670 |
+
}
|
| 671 |
+
|
| 672 |
+
print(f"📝 Đã chuyển đổi: {transcription}")
|
| 673 |
+
|
| 674 |
+
# Cập nhật transcription hiện tại
|
| 675 |
+
self.current_transcription = transcription
|
| 676 |
+
|
| 677 |
+
# Tạo phản hồi AI
|
| 678 |
+
response = self._generate_ai_response(transcription)
|
| 679 |
+
|
| 680 |
+
# Tạo TTS
|
| 681 |
+
tts_audio_path = self._text_to_speech(response)
|
| 682 |
+
|
| 683 |
+
return {
|
| 684 |
+
'transcription': transcription,
|
| 685 |
+
'response': response,
|
| 686 |
+
'tts_audio': tts_audio_path,
|
| 687 |
+
'status': 'completed'
|
| 688 |
+
}
|
| 689 |
+
|
| 690 |
+
except Exception as e:
|
| 691 |
+
print(f"❌ Lỗi xử lý streaming audio: {e}")
|
| 692 |
+
print(f"Chi tiết lỗi: {traceback.format_exc()}")
|
| 693 |
+
return {
|
| 694 |
+
'transcription': f"❌ Lỗi: {str(e)}",
|
| 695 |
+
'response': "Xin lỗi, có lỗi xảy ra trong quá trình xử lý",
|
| 696 |
+
'tts_audio': None,
|
| 697 |
+
'status': 'error'
|
| 698 |
+
}
|
| 699 |
def process_audio_chunk(self, audio_data: tuple) -> Dict[str, Any]:
|
| 700 |
"""Xử lý audio chunk với VAD"""
|
| 701 |
if not audio_data or not self.is_listening:
|