Skip to content

Commit

Permalink
feat: 增加闲聊插件
Browse files Browse the repository at this point in the history
  • Loading branch information
wzpan committed Apr 8, 2023
1 parent aef4d11 commit c071590
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 7 deletions.
25 changes: 25 additions & 0 deletions plugins/Gossip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# -*- coding: utf-8 -*-
# author: wzpan
# 闲聊一下

import logging
from robot.sdk.AbstractPlugin import AbstractPlugin

logger = logging.getLogger(__name__)


class Plugin(AbstractPlugin):

IS_IMMERSIVE = True

def handle(self, text, parsed):

if "闲聊一下" in text or "进入闲聊" in text:
# 进入闲聊模式
self.say("好的,已进入闲聊模式", cache=True)
else:
self.clearImmersive() # 去掉沉浸式
self.say("结束闲聊", cache=True)

def isValid(self, text, parsed):
return any(word in text.lower() for word in ["闲聊一下", "进入闲聊", "结束闲聊", "退出闲聊"])
14 changes: 9 additions & 5 deletions robot/Conversation.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def _ttsAction(self, msg, index, cache):
logger.info(f"第{index}段TTS命中缓存,播放缓存语音")
voice = utils.getCache(msg)
while index != self.tts_index:
# 阻塞直到轮到这个音频播放
# 阻塞直到轮到这个音频播放
continue
with self.play_lock:
self.player.play(voice, not cache)
Expand All @@ -76,11 +76,11 @@ def _ttsAction(self, msg, index, cache):
logger.info(f"合成第{index}段TTS合成成功:{msg}")
logger.debug(f"self.tts_index: {self.tts_index}")
while index != self.tts_index:
# 阻塞直到轮到这个音频播放
# 阻塞直到轮到这个音频播放
continue
with self.play_lock:
self.player.play(voice, not cache)
self.tts_index += 1
self.tts_index += 1
return (voice, index)
except Exception as e:
logger.error(f"语音合成失败:{e}", stack_info=True)
Expand Down Expand Up @@ -115,6 +115,9 @@ def checkRestore(self):
self.lifeCycleHandler.onRestore()
self.brain.restore()

def _InGossip(self, query):
return self.immersiveMode == "Gossip" and "闲聊" not in query

def doResponse(self, query, UUID="", onSay=None):
"""
响应指令
Expand All @@ -137,7 +140,8 @@ def doResponse(self, query, UUID="", onSay=None):
lastImmersiveMode = self.immersiveMode

parsed = self.doParse(query)
if not self.brain.query(query, parsed):
if self._InGossip(query) or not self.brain.query(query, parsed):
# 进入闲聊
if self.nlu.hasIntent(parsed, "PAUSE") or "闭嘴" in query:
# 停止说话
self.player.stop()
Expand Down Expand Up @@ -308,7 +312,7 @@ def say(
cached_audios.append(audio)
logger.info(f"onSay: {msg}, {cached_audios}")
self.onSay(msg, cached_audios, plugin=plugin)
self.onSay = None
self.onSay = None
if onCompleted is None:
onCompleted = lambda: self._onCompleted(msg)
onCompleted and onCompleted()
Expand Down
2 changes: 1 addition & 1 deletion robot/Scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def del_job_by_id(self, job_id):
:param job_id: 提醒id
"""
try:
if (self._sched.get_job(job_id=job_id)):
if self._sched.get_job(job_id=job_id):
self._sched.remove_job(job_id=job_id)
self._jobs = [job for job in self._jobs if job.job_id != job_id]
except Exception as e:
Expand Down
6 changes: 5 additions & 1 deletion robot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,13 @@ def getTimemStap():
def getCache(msg):
"""获取缓存的语音"""
md5 = hashlib.md5(msg.encode("utf-8")).hexdigest()
cache_paths = [os.path.join(constants.TEMP_PATH, md5 + ext) for ext in ['.mp3', '.wav', '.asiff']]
cache_paths = [
os.path.join(constants.TEMP_PATH, md5 + ext)
for ext in [".mp3", ".wav", ".asiff"]
]
return next((path for path in cache_paths if os.path.exists(path)), None)


def saveCache(voice, msg):
"""获取缓存的语音"""
_, ext = os.path.splitext(voice)
Expand Down

0 comments on commit c071590

Please sign in to comment.