Skip to content

Commit

Permalink
新增 念用户名的功能,例:回复xxx。你好;新增用户名过滤(违禁词、链接、拼音)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ikaros-521 committed Jul 20, 2023
1 parent d9b0e57 commit ec8347b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,8 @@ cmd输入命令即可:`doctoc /path/to/file`
- 文案模式增加了一组文案配置,也就是说有2块文案,会进行左右切换的播放,都播放完毕后会进行新的一轮播放。
- 新增LLM后的第二重过滤(违禁词、链接、拼音)
- 违禁拼音配置内容需要是中文,需要注意,已更正
- 新增 念用户名的功能,例:回复xxx。你好
- 新增用户名过滤(违禁词、链接、拼音)

</details>

Expand Down
1 change: 1 addition & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"before_prompt": "请简要回复:",
"after_prompt": "",
"commit_log_type": "回答",
"read_user_name": true,
"filter": {
"before_must_str": [],
"after_must_str": [],
Expand Down
1 change: 1 addition & 0 deletions config.json.bak
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"before_prompt": "请简要回复:",
"after_prompt": "",
"commit_log_type": "回答",
"read_user_name": true,
"filter": {
"before_must_str": [],
"after_must_str": [],
Expand Down
19 changes: 15 additions & 4 deletions utils/audio.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,16 +274,27 @@ def audio_synthesis(self, message):

self.voice_tmp_path_queue.put(data_json)
return
# 是否为本地问答音频
elif message['type'] == "local_qa_audio":
# 拼接json数据,存入队列
data_json = {
"voice_path": message['content'],
"content": message["content"]
}

# 由于线程是独立的,所以回复音频的合成会慢于本地音频直接播放,所以以倒述的形式回复
message['content'] = f"以上内容回复{message['user_name']}。"
message['type'] = "commit"
self.message_queue.put(message)
self.voice_tmp_path_queue.put(data_json)
return

# 只有信息类型是 弹幕,才会进行念用户名
if message['type'] == "commit":
# 回复时是否念用户名字
if self.config.get("read_user_name"):
message['content'] = f"回复{message['user_name']}{message['content']}"

# 中文语句切分
sentences = self.common.split_sentences(message['content'])
for s in sentences:
Expand Down Expand Up @@ -332,7 +343,7 @@ async def voice_change_and_put_to_queue(voice_tmp_path):
self.voice_tmp_path_queue.put(data_json)

# 区分TTS类型
if message["type"] == "vits":
if message["tts_type"] == "vits":
try:
# 语言检测
language = self.common.lang_check(message["content"])
Expand Down Expand Up @@ -366,7 +377,7 @@ async def voice_change_and_put_to_queue(voice_tmp_path):
except Exception as e:
logging.error(e)
return
elif message["type"] == "edge-tts":
elif message["tts_type"] == "edge-tts":
try:
voice_tmp_path = './out/' + self.common.get_bj_time(4) + '.mp3'
# 过滤" '字符
Expand All @@ -380,7 +391,7 @@ async def voice_change_and_put_to_queue(voice_tmp_path):
await voice_change_and_put_to_queue(voice_tmp_path)
except Exception as e:
logging.error(e)
elif message["type"] == "elevenlabs":
elif message["tts_type"] == "elevenlabs":
try:
# 如果配置了密钥就设置上0.0
if message["data"]["elevenlabs_api_key"] != "":
Expand All @@ -396,7 +407,7 @@ async def voice_change_and_put_to_queue(voice_tmp_path):
except Exception as e:
logging.error(e)
return
elif message["type"] == "genshinvoice_top":
elif message["tts_type"] == "genshinvoice_top":
try:
voice_tmp_path = await self.genshinvoice_top_api(message["content"])
print(f"genshinvoice.top合成成功,输出到={voice_tmp_path}")
Expand Down
23 changes: 22 additions & 1 deletion utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,25 @@ def get_random_value(self, lower_limit, upper_limit):
lower_limit, upper_limit = upper_limit, lower_limit

random_float = round(random.uniform(lower_limit, upper_limit), 2)
return random_float
return random_float


def merge_consecutive_asterisks(self, s):
"""合并字符串末尾连续的*
Args:
s (str): 待处理的字符串
Returns:
str: 处理完后的字符串
"""
# 从字符串末尾开始遍历,找到连续的*的起始索引
idx = len(s) - 1
while idx >= 0 and s[idx] == '*':
idx -= 1

# 如果找到了超过3个连续的*,则进行替换
if len(s) - 1 - idx > 3:
s = s[:idx + 1] + '*' + s[len(s) - 1:]

return s
47 changes: 42 additions & 5 deletions utils/my_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,26 @@ def commit_handle(self, user_name, content):
Returns:
_type_: 寂寞
"""

# 合并字符串末尾连续的* 主要针对获取不到用户名的情况
user_name = self.common.merge_consecutive_asterisks(user_name)

"""
用户名也得过滤一下,防止炸弹人
"""
# 含有违禁词/链接
if self.common.profanity_content(user_name) or self.common.check_sensitive_words2(
self.filter_config["badwords_path"], user_name) or \
self.common.is_url_check(user_name):
logging.warning(f"违禁词/链接:{user_name}")
return

# 同拼音违禁词过滤
if self.filter_config["bad_pinyin_path"] != "":
if self.common.check_sensitive_words3(self.filter_config["bad_pinyin_path"], user_name):
logging.warning(f"同音违禁词:{user_name}")
return

# 1、匹配本地问答库 触发后不执行后面的其他功能
if self.local_qa["text"]["enable"] == True:
# 输出当前用户发送的弹幕消息
Expand Down Expand Up @@ -230,7 +250,8 @@ def commit_handle(self, user_name, content):
f.write(f"[AI回复{user_name}]:{resp_content_joined}\n" + tmp_content)

message = {
"type": self.audio_synthesis_type,
"type": "commit",
"tts_type": self.audio_synthesis_type,
"data": self.config.get(self.audio_synthesis_type),
"config": self.filter_config,
"user_name": user_name,
Expand Down Expand Up @@ -261,6 +282,9 @@ def commit_handle(self, user_name, content):
logging.info(f"匹配到的音频路径:{resp_content}")
message = {
"type": "local_qa_audio",
"tts_type": self.audio_synthesis_type,
"data": self.config.get(self.audio_synthesis_type),
"config": self.filter_config,
"user_name": user_name,
"content": resp_content
}
Expand Down Expand Up @@ -288,7 +312,8 @@ def commit_handle(self, user_name, content):
logging.info(f"[AI回复{user_name}]:{resp_content}")

message = {
"type": self.audio_synthesis_type,
"type": "commit",
"tts_type": self.audio_synthesis_type,
"data": self.config.get(self.audio_synthesis_type),
"config": self.filter_config,
"user_name": user_name,
Expand All @@ -311,6 +336,9 @@ def commit_handle(self, user_name, content):
logging.info(f"匹配到的音频路径:{resp_content}")
message = {
"type": "song",
"tts_type": self.audio_synthesis_type,
"data": self.config.get(self.audio_synthesis_type),
"config": self.filter_config,
"user_name": user_name,
"content": resp_content
}
Expand Down Expand Up @@ -552,7 +580,8 @@ def commit_handle(self, user_name, content):
f.write(f"[AI回复{user_name}]:\n{resp_content_joined}\n" + tmp_content)

message = {
"type": self.audio_synthesis_type,
"type": "commit",
"tts_type": self.audio_synthesis_type,
"data": self.config.get(self.audio_synthesis_type),
"config": self.filter_config,
"user_name": user_name,
Expand All @@ -565,6 +594,9 @@ def commit_handle(self, user_name, content):

# 礼物处理
def gift_handle(self, data):
# 合并字符串末尾连续的* 主要针对获取不到用户名的情况
data['username'] = self.common.merge_consecutive_asterisks(data['username'])

# logging.debug(f"[{data['username']}]: {data}")

try:
Expand All @@ -578,7 +610,8 @@ def gift_handle(self, data):
resp_content = self.thanks_config["gift_copy"].format(username=data["username"], gift_name=data["gift_name"])

message = {
"type": self.audio_synthesis_type,
"type": "gift",
"tts_type": self.audio_synthesis_type,
"data": self.config.get(self.audio_synthesis_type),
"config": self.filter_config,
"user_name": data["username"],
Expand All @@ -593,6 +626,9 @@ def gift_handle(self, data):

# 入场处理
def entrance_handle(self, data):
# 合并字符串末尾连续的* 主要针对获取不到用户名的情况
data['username'] = self.common.merge_consecutive_asterisks(data['username'])

# logging.debug(f"[{data['username']}]: {data['content']}")

try:
Expand All @@ -602,7 +638,8 @@ def entrance_handle(self, data):
resp_content = self.thanks_config["entrance_copy"].format(username=data["username"])

message = {
"type": self.audio_synthesis_type,
"type": "entrance",
"tts_type": self.audio_synthesis_type,
"data": self.config.get(self.audio_synthesis_type),
"config": self.filter_config,
"user_name": data['username'],
Expand Down

0 comments on commit ec8347b

Please sign in to comment.