liumaolin
commited on
Commit
·
037e5ae
1
Parent(s):
8366d25
Refactor speech recognizer, audio capture, and system routes for improved clarity and functionality
Browse files- Update `speech/recognizer.py` to handle empty queue exceptions.
- Replace `pause_event` with `_pause_event` in `audio/capture.py` for consistency.
- Cleanup and standardize formatting in `system_routes.py`.
src/voice_dialogue/api/routes/system_routes.py
CHANGED
|
@@ -219,13 +219,13 @@ async def pause_system(request: Request):
|
|
| 219 |
success=False,
|
| 220 |
message="系统未启动,无法暂停"
|
| 221 |
)
|
| 222 |
-
|
| 223 |
if _system_status["status"] == "paused":
|
| 224 |
return SystemResponse(
|
| 225 |
success=False,
|
| 226 |
message="系统已经暂停"
|
| 227 |
)
|
| 228 |
-
|
| 229 |
if _system_status["status"] in ["starting", "stopping"]:
|
| 230 |
return SystemResponse(
|
| 231 |
success=False,
|
|
@@ -294,19 +294,19 @@ async def resume_system(request: Request):
|
|
| 294 |
success=False,
|
| 295 |
message="系统未启动,请先启动系统"
|
| 296 |
)
|
| 297 |
-
|
| 298 |
if _system_status["status"] == "running":
|
| 299 |
return SystemResponse(
|
| 300 |
success=False,
|
| 301 |
message="系统已经在运行中"
|
| 302 |
)
|
| 303 |
-
|
| 304 |
if _system_status["status"] in ["starting", "stopping"]:
|
| 305 |
return SystemResponse(
|
| 306 |
success=False,
|
| 307 |
message="系统正在启动或停止中,请稍后再试"
|
| 308 |
)
|
| 309 |
-
|
| 310 |
if _system_status["status"] != "paused":
|
| 311 |
return SystemResponse(
|
| 312 |
success=False,
|
|
@@ -360,4 +360,4 @@ async def resume_system(request: Request):
|
|
| 360 |
# 恢复状态
|
| 361 |
if _system_status["status"] == "running":
|
| 362 |
_system_status["status"] = "paused"
|
| 363 |
-
raise HTTPException(status_code=500, detail=f"恢复语音对话系统失败: {str(e)}")
|
|
|
|
| 219 |
success=False,
|
| 220 |
message="系统未启动,无法暂停"
|
| 221 |
)
|
| 222 |
+
|
| 223 |
if _system_status["status"] == "paused":
|
| 224 |
return SystemResponse(
|
| 225 |
success=False,
|
| 226 |
message="系统已经暂停"
|
| 227 |
)
|
| 228 |
+
|
| 229 |
if _system_status["status"] in ["starting", "stopping"]:
|
| 230 |
return SystemResponse(
|
| 231 |
success=False,
|
|
|
|
| 294 |
success=False,
|
| 295 |
message="系统未启动,请先启动系统"
|
| 296 |
)
|
| 297 |
+
|
| 298 |
if _system_status["status"] == "running":
|
| 299 |
return SystemResponse(
|
| 300 |
success=False,
|
| 301 |
message="系统已经在运行中"
|
| 302 |
)
|
| 303 |
+
|
| 304 |
if _system_status["status"] in ["starting", "stopping"]:
|
| 305 |
return SystemResponse(
|
| 306 |
success=False,
|
| 307 |
message="系统正在启动或停止中,请稍后再试"
|
| 308 |
)
|
| 309 |
+
|
| 310 |
if _system_status["status"] != "paused":
|
| 311 |
return SystemResponse(
|
| 312 |
success=False,
|
|
|
|
| 360 |
# 恢复状态
|
| 361 |
if _system_status["status"] == "running":
|
| 362 |
_system_status["status"] = "paused"
|
| 363 |
+
raise HTTPException(status_code=500, detail=f"恢复语音对话系统失败: {str(e)}")
|
src/voice_dialogue/services/audio/capture.py
CHANGED
|
@@ -4,8 +4,8 @@
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import ctypes
|
| 7 |
-
import time
|
| 8 |
import threading
|
|
|
|
| 9 |
|
| 10 |
import numpy as np
|
| 11 |
|
|
@@ -24,17 +24,17 @@ class EchoCancellingAudioCapture(BaseThread):
|
|
| 24 |
super().__init__(group, target, name, args, kwargs, daemon=daemon)
|
| 25 |
|
| 26 |
self.audio_frames_queue = audio_frames_queue
|
| 27 |
-
self.
|
| 28 |
-
|
| 29 |
@property
|
| 30 |
def is_paused(self):
|
| 31 |
-
return self.
|
| 32 |
|
| 33 |
def pause(self):
|
| 34 |
-
self.
|
| 35 |
-
|
| 36 |
def resume(self):
|
| 37 |
-
self.
|
| 38 |
|
| 39 |
def run(self):
|
| 40 |
"""主运行循环,持续获取音频数据"""
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
import ctypes
|
|
|
|
| 7 |
import threading
|
| 8 |
+
import time
|
| 9 |
|
| 10 |
import numpy as np
|
| 11 |
|
|
|
|
| 24 |
super().__init__(group, target, name, args, kwargs, daemon=daemon)
|
| 25 |
|
| 26 |
self.audio_frames_queue = audio_frames_queue
|
| 27 |
+
self._pause_event = threading.Event()
|
| 28 |
+
|
| 29 |
@property
|
| 30 |
def is_paused(self):
|
| 31 |
+
return self._pause_event.is_set()
|
| 32 |
|
| 33 |
def pause(self):
|
| 34 |
+
self._pause_event.set()
|
| 35 |
+
|
| 36 |
def resume(self):
|
| 37 |
+
self._pause_event.clear()
|
| 38 |
|
| 39 |
def run(self):
|
| 40 |
"""主运行循环,持续获取音频数据"""
|
src/voice_dialogue/services/speech/recognizer.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import time
|
| 2 |
import typing
|
| 3 |
-
from queue import Queue
|
| 4 |
|
| 5 |
import numpy as np
|
| 6 |
|
|
@@ -15,7 +15,7 @@ class ASRWorker(BaseThread):
|
|
| 15 |
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None,
|
| 16 |
user_voice_queue: Queue,
|
| 17 |
transcribed_text_queue: Queue,
|
| 18 |
-
language: typing.Literal["auto", "zh", "en"]
|
| 19 |
super().__init__(group, target, name, args, kwargs, daemon=daemon)
|
| 20 |
|
| 21 |
self.language = language
|
|
@@ -32,7 +32,11 @@ class ASRWorker(BaseThread):
|
|
| 32 |
self.is_ready = True
|
| 33 |
|
| 34 |
while not self.stopped():
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
voice_task.language = self.language
|
| 37 |
voice_task.whisper_start_time = time.time()
|
| 38 |
user_voice: np.array = voice_task.user_voice
|
|
|
|
| 1 |
import time
|
| 2 |
import typing
|
| 3 |
+
from queue import Queue, Empty
|
| 4 |
|
| 5 |
import numpy as np
|
| 6 |
|
|
|
|
| 15 |
def __init__(self, group=None, target=None, name=None, args=(), kwargs=None, *, daemon=None,
|
| 16 |
user_voice_queue: Queue,
|
| 17 |
transcribed_text_queue: Queue,
|
| 18 |
+
language: typing.Literal["auto", "zh", "en"]):
|
| 19 |
super().__init__(group, target, name, args, kwargs, daemon=daemon)
|
| 20 |
|
| 21 |
self.language = language
|
|
|
|
| 32 |
self.is_ready = True
|
| 33 |
|
| 34 |
while not self.stopped():
|
| 35 |
+
try:
|
| 36 |
+
voice_task: VoiceTask = self.user_voice_queue.get(block=False, timeout=0.1)
|
| 37 |
+
except Empty:
|
| 38 |
+
continue
|
| 39 |
+
|
| 40 |
voice_task.language = self.language
|
| 41 |
voice_task.whisper_start_time = time.time()
|
| 42 |
user_voice: np.array = voice_task.user_voice
|