Skip to content

Commit

Permalink
修正语音对话时 句子末尾显示异常的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
binary-husky committed Sep 7, 2023
1 parent 97cb9a4 commit 63219ba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
7 changes: 3 additions & 4 deletions crazy_functions/live_audio/aliyunASR.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import time, threading, json
import time, logging, json


class AliyunASR():
Expand All @@ -12,14 +12,14 @@ def test_on_sentence_end(self, message, *args):
message = json.loads(message)
self.parsed_sentence = message['payload']['result']
self.event_on_entence_end.set()
print(self.parsed_sentence)
# print(self.parsed_sentence)

def test_on_start(self, message, *args):
# print("test_on_start:{}".format(message))
pass

def test_on_error(self, message, *args):
print("on_error args=>{}".format(args))
logging.error("on_error args=>{}".format(args))
pass

def test_on_close(self, *args):
Expand All @@ -36,7 +36,6 @@ def test_on_completed(self, message, *args):
# print("on_completed:args=>{} message=>{}".format(args, message))
pass


def audio_convertion_thread(self, uuid):
# 在一个异步线程中采集音频
import nls # pip install git+https://github.com/aliyun/alibabacloud-nls-python-sdk.git
Expand Down
14 changes: 9 additions & 5 deletions crazy_functions/语音助手.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class InterviewAssistant(AliyunASR):
def __init__(self):
self.capture_interval = 0.5 # second
self.stop = False
self.parsed_text = ""
self.parsed_sentence = ""
self.buffered_sentence = ""
self.parsed_text = "" # 下个句子中已经说完的部分, 由 test_on_result_chg() 写入
self.parsed_sentence = "" # 某段话的整个句子,由 test_on_sentence_end() 写入
self.buffered_sentence = "" #
self.event_on_result_chg = threading.Event()
self.event_on_entence_end = threading.Event()
self.event_on_commit_question = threading.Event()
Expand Down Expand Up @@ -132,7 +132,7 @@ def begin(self, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
self.plugin_wd.feed()

if self.event_on_result_chg.is_set():
# update audio decode result
# called when some words have finished
self.event_on_result_chg.clear()
chatbot[-1] = list(chatbot[-1])
chatbot[-1][0] = self.buffered_sentence + self.parsed_text
Expand All @@ -144,7 +144,11 @@ def begin(self, llm_kwargs, plugin_kwargs, chatbot, history, system_prompt):
# called when a sentence has ended
self.event_on_entence_end.clear()
self.parsed_text = self.parsed_sentence
self.buffered_sentence += self.parsed_sentence
self.buffered_sentence += self.parsed_text
chatbot[-1] = list(chatbot[-1])
chatbot[-1][0] = self.buffered_sentence
history = chatbot2history(chatbot)
yield from update_ui(chatbot=chatbot, history=history) # 刷新界面

if self.event_on_commit_question.is_set():
# called when a question should be commited
Expand Down

0 comments on commit 63219ba

Please sign in to comment.