Skip to content

Commit

Permalink
本地问答板块 新增文本框用于json编辑,默认用于编辑 本地问答.json,方便修改内容,你也可以用作修改其他json文件。格式一定要对啊…
Browse files Browse the repository at this point in the history
…;修复睿声格式报错错误的bug
  • Loading branch information
Ikaros-521 committed Jul 3, 2024
1 parent 2e81ae7 commit 8467cf1
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 78 deletions.
148 changes: 74 additions & 74 deletions data/本地问答库.json
Original file line number Diff line number Diff line change
@@ -1,76 +1,76 @@
[
{
"关键词": [
"这是触发的关键词",
"触发的关键词可以是多个,注意json格式"
],
"回答": [
"这是触发关键词后对应的回答",
"回答也可以是多个,随机挑一个,注意json格式",
"支持这种[1|2|3],三选一的回答语法",
"{username}这个大括号里面是要替换的用户名,这就是动态变量"
]
},
{
"关键词": [
"打卡"
],
"回答": [
"打卡成功喵",
"感谢{username}的打卡,好感度加一",
"感谢{username}打卡,给你颁发虚拟世界的打卡勋章!"
]
},
{
"关键词": [
"积分有什么用",
"积分有啥用",
"积分是什么",
"积分是啥"
],
"回答": [
"积分暂时仅用于统计用户互动、投食数据,暂无实际作用"
]
},
{
"关键词": [
"在吗",
"在?"
],
"回答": [
"在的",
"在哟",
"在的,{username}"
]
},
{
"关键词": [
"是录播吗"
],
"回答": [
"现在是{cur_time},不是录播哦"
]
},
{
"关键词": [
"你的主人是谁",
"你主人叫什么",
"你主人是谁"
],
"回答": [
"我的主人是Love丶伊卡洛斯喵~",
"我主人是Love丶伊卡洛斯喵~"
]
},
{
"关键词": [
"你的主人呢",
"你主人在哪"
],
"回答": [
"我的主人不是在摸鱼,就是在摸鱼的路上",
"主人在摸鱼喵",
"主人在摆烂喵"
]
}
{
"关键词": [
"这是触发的关键词,注意关键词有相似度,相似度就是 关键词和发送内容的相似程度,0.8就是80%",
"触发的关键词可以是多个,注意json格式"
],
"回答": [
"这是触发关键词后对应的回答",
"回答也可以是多个,随机挑一个,注意json格式",
"支持这种[1|2|3],三选一的回答语法",
"{username}这个大括号里面是要替换的用户名,这就是动态变量"
]
},
{
"关键词": [
"打卡"
],
"回答": [
"打卡成功喵",
"感谢{username}的打卡,好感度加一",
"感谢{username}打卡,给你颁发虚拟世界的打卡勋章!"
]
},
{
"关键词": [
"积分有什么用",
"积分有啥用",
"积分是什么",
"积分是啥"
],
"回答": [
"积分暂时仅用于统计用户互动、投食数据,暂无实际作用"
]
},
{
"关键词": [
"在吗",
"在?"
],
"回答": [
"在的",
"在哟",
"在的,{username}"
]
},
{
"关键词": [
"是录播吗"
],
"回答": [
"现在是{cur_time},不是录播哦"
]
},
{
"关键词": [
"你的主人是谁",
"你主人叫什么",
"你主人是谁"
],
"回答": [
"我的主人是Love丶伊卡洛斯喵~",
"我主人是Love丶伊卡洛斯喵~"
]
},
{
"关键词": [
"你的主人呢",
"你主人在哪"
],
"回答": [
"我的主人不是在摸鱼,就是在摸鱼的路上",
"主人在摸鱼喵",
"主人在摆烂喵"
]
}
]
38 changes: 38 additions & 0 deletions utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,23 @@ def find_keys_by_value(self, dictionary, target_value):
"""

# 判断文本是否可以转为dict JSON格式
def is_json_convertible(self, text: str) -> bool:
"""判断文本是否可以转为dict JSON格式
Args:
text (str): 待判断内容
Returns:
bool: T / F
"""
try:
import json
json.loads(text)
return True
except json.JSONDecodeError:
return False

# 生成hash字符串 用于gradio请求
def generate_session_hash(self, length: int=11):
import hashlib
Expand Down Expand Up @@ -875,6 +892,24 @@ def get_live2d_model_name(self, path):
,@/[ .[\@` =@@@ @@@@ \@@@` ,` @@@^ .[ =@@@. @@@^
"""

# 读取文件内容 它接受文件路径和返回类型参数,并根据参数返回文件内容作为字典或纯文本。如果读取文件过程中出现异常,则返回 None。
def read_file(self, file_path: str, return_type: str):
try:
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()

if return_type == 'dict':
return json.loads(content)
elif return_type == 'text':
return content
else:
logger.error("Invalid return type. Use 'dict' or 'text'.")
return None
except Exception as e:
logger.error(traceback.format_exc())
return None

def ensure_directory_exists(self, path):
# 检查路径是否存在
if not os.path.exists(path):
Expand All @@ -895,6 +930,9 @@ def write_content_to_file(self, file_path, content, write_log=True):
except IOError as e:
logger.error(f"无法写入 【{content}】 到文件:{file_path}\n{e}")
return False
except Exception as e:
logger.error(traceback.format_exc())
return False

# 移动文件到指定路径 src dest
def move_file(self, source_path, destination_path, rename=None, format="wav"):
Expand Down
40 changes: 36 additions & 4 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -1354,6 +1354,10 @@ def check_config():
if select_bilibili_login_type.value == "不登录":
ui.notify(position="top", type="warning", message=f"哔哩哔哩2 在不登录的情况下,无法获取用户完整的用户名")

if common.is_json_convertible(textarea_local_qa_text_json_file_content.value) == False:
ui.notify(position="top", type="negative", message=f"本地问答json数据格式不正确,请检查JSON语法!")
return False

return True

"""
Expand Down Expand Up @@ -2256,8 +2260,8 @@ def common_textarea_handle(content):
config_data["reecho_ai"]["stability_boost"] = int(number_reecho_ai_stability_boost.value)
config_data["reecho_ai"]["promptId"] = input_reecho_ai_promptId.value
config_data["reecho_ai"]["probability_optimization"] = int(number_reecho_ai_probability_optimization.value)
config_data["reecho_ai"]["break_clone"] = int(switch_reecho_ai_break_clone.value)
config_data["reecho_ai"]["flash"] = int(switch_reecho_ai_flash.value)
config_data["reecho_ai"]["break_clone"] = switch_reecho_ai_break_clone.value
config_data["reecho_ai"]["flash"] = switch_reecho_ai_flash.value

if config.get("webui", "show_card", "tts", "gradio_tts"):
config_data["gradio_tts"]["request_parameters"] = textarea_gradio_tts_request_parameters.value
Expand Down Expand Up @@ -2750,6 +2754,17 @@ def save_config():
if config_data is None:
return False

# 写入本地问答json数据到文件
try:
ret = common.write_content_to_file(input_local_qa_text_json_file_path.value, textarea_local_qa_text_json_file_content.value, write_log=False)
if ret == False:
ui.notify(position="top", type="negative", message=f"无法写入本地问答json数据到文件!\n详细报错见日志")
return False
except Exception as e:
logger.error(f"无法写入本地问答json数据到文件!\n{str(e)}")
ui.notify(position="top", type="negative", message=f"无法写入本地问答json数据到文件!\n{str(e)}")
return False

# 写入配置到配置文件
try:
with open(config_path, 'w', encoding="utf-8") as config_file:
Expand All @@ -2761,8 +2776,8 @@ def save_config():

return True
except Exception as e:
logger.error(f"无法写入配置文件!\n{e}")
ui.notify(position="top", type="negative", message=f"无法写入配置文件!\n{e}")
logger.error(f"无法写入配置文件!\n{str(e)}")
ui.notify(position="top", type="negative", message=f"无法写入配置文件!\n{str(e)}")
return False


Expand Down Expand Up @@ -3091,6 +3106,23 @@ def save_config():
switch_local_qa_audio_enable = ui.switch('启用音频匹配', value=config.get("local_qa", "audio", "enable")).style(switch_internal_css)
input_local_qa_audio_file_path = ui.input(label='音频存储路径', placeholder='本地问答音频文件存储路径', value=config.get("local_qa", "audio", "file_path")).style("width:200px;")
input_local_qa_audio_similarity = ui.input(label='音频最低相似度', placeholder='最低音频匹配相似度,就是说用户发送的内容和本地音频库中音频文件名的最低相似度。\n低了就会被当做一般弹幕处理', value=config.get("local_qa", "audio", "similarity")).style("width:200px;")
with ui.row():
input_local_qa_text_json_file_path = ui.input(label='json文件路径', placeholder='填写json文件路径,默认为本地问答文本数据存储路径', value=config.get("local_qa", "text", "file_path")).style("width:200px;").tooltip("填写json文件路径,默认为本地问答文本数据存储路径")

def local_qa_text_json_file_reload():
try:
# 只做了个判空 所以别乱填
if input_local_qa_text_json_file_path.value != "":
textarea_local_qa_text_json_file_content.value = json.dumps(common.read_file(input_local_qa_text_json_file_path.value, "dict"), ensure_ascii=False, indent=3)
except Exception as e:
logger.error(traceback.format_exc())
ui.notify(f"文件路径有误或其他问题。报错:{str(e)}", position="top", type="negative")

button_local_qa_text_json_file_reload = ui.button('加载文件', on_click=lambda: local_qa_text_json_file_reload(), color=button_internal_color).style(button_internal_css)

textarea_local_qa_text_json_file_content = ui.textarea(label='JSON文件内容', placeholder='注意格式!').style("width:700px;")

local_qa_text_json_file_reload()
if config.get("webui", "show_card", "common_config", "filter"):
with ui.card().style(card_css):
ui.label('过滤')
Expand Down

0 comments on commit 8467cf1

Please sign in to comment.