diff --git a/crazy_functions/live_audio/aliyunASR.py b/crazy_functions/live_audio/aliyunASR.py index ac1ea34..5cb1176 100644 --- a/crazy_functions/live_audio/aliyunASR.py +++ b/crazy_functions/live_audio/aliyunASR.py @@ -23,7 +23,7 @@ class AliyunASR(): pass def test_on_close(self, *args): - # print("on_close: args=>{}".format(args)) + self.aliyun_service_ok = False pass def test_on_result_chg(self, message, *args): @@ -50,7 +50,7 @@ class AliyunASR(): rad.clean_up() temp_folder = tempfile.gettempdir() TOKEN, APPKEY = get_conf('ALIYUN_TOKEN', 'ALIYUN_APPKEY') - + self.aliyun_service_ok = True URL="wss://nls-gateway.aliyuncs.com/ws/v1" sr = nls.NlsSpeechTranscriber( url=URL, @@ -86,4 +86,8 @@ class AliyunASR(): for i in slices: sr.send_audio(bytes(i)) else: time.sleep(0.1) + + if not self.aliyun_service_ok: + self.stop = True + self.stop_msg = 'Aliyun音频服务异常,请检查ALIYUN_TOKEN和ALIYUN_APPKEY是否过期。' r = sr.stop() diff --git a/crazy_functions/语音助手.py b/crazy_functions/语音助手.py index b99f014..09eeac2 100644 --- a/crazy_functions/语音助手.py +++ b/crazy_functions/语音助手.py @@ -14,9 +14,11 @@ class WatchDog(): self.bark_fn = bark_fn self.interval = interval self.msg = msg + self.kill_dog = False def watch(self): while True: + if self.kill_dog: break if time.time() - self.last_feed > self.timeout: if len(self.msg) > 0: print(self.msg) self.bark_fn() @@ -87,6 +89,9 @@ class InterviewAssistant(AliyunASR): def __del__(self): self.stop = True + self.stop_msg = "" + self.commit_wd.kill_dog = True + self.plugin_wd.kill_dog = True def init(self, chatbot): # 初始化音频采集线程 @@ -119,7 +124,7 @@ class InterviewAssistant(AliyunASR): self.commit_wd = WatchDog(timeout=self.commit_after_pause_n_second, bark_fn=self.no_audio_for_a_while, interval=0.2) self.commit_wd.begin_watch() - while True: + while not self.stop: self.event_on_result_chg.wait(timeout=0.25) # run once every 0.25 second chatbot = self.agt.update_chatbot(chatbot) # 将子线程的gpt结果写入chatbot history = chatbot2history(chatbot) @@ -158,6 +163,8 @@ class InterviewAssistant(AliyunASR): chatbot.append(["[请讲话]", "[正在等您说完问题]"]) yield from update_ui(chatbot=chatbot, history=history) # 刷新界面 + if len(self.stop_msg) != 0: + raise RuntimeError(self.stop_msg)