Skip to content

Commit

Permalink
Merge pull request #711 from Ikaros-521/owner
Browse files Browse the repository at this point in the history
补充webui聊天记录回调内容,优化代码实现;复读处理新增type传参,动态文案数据附带type
  • Loading branch information
Ikaros-521 authored Mar 20, 2024
2 parents 0e35a8e + 469cbb3 commit a52c22b
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 68 deletions.
14 changes: 12 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ async def run_trends_copywriting():
# 空数据判断
if data_json["content"] != None and data_json["content"] != "":
# 发给直接复读进行处理
my_handle.reread_handle(data_json, filter=True)
my_handle.reread_handle(data_json, filter=True, type="trends_copywriting")

await asyncio.sleep(config.get("trends_copywriting", "play_interval"))
except Exception as e:
Expand Down Expand Up @@ -1220,12 +1220,14 @@ def _on_danmaku(self, client: blivedm.BLiveClient, message: web_models.DanmakuMe
# logging.info(f'[{client.room_id}] {message.uname}:{message.msg}')
content = message.msg # 获取弹幕内容
username = message.uname # 获取发送弹幕的用户昵称
user_face = message.face

logging.info(f"[{username}]: {content}")

data = {
"platform": platform,
"username": username,
"user_face": user_face,
"content": content
}

Expand All @@ -1237,6 +1239,7 @@ def _on_gift(self, client: blivedm.BLiveClient, message: web_models.GiftMessage)

gift_name = message.gift_name
username = message.uname
user_face = message.face
# 礼物数量
combo_num = message.num
# 总金额
Expand All @@ -1248,6 +1251,7 @@ def _on_gift(self, client: blivedm.BLiveClient, message: web_models.GiftMessage)
"platform": platform,
"gift_name": gift_name,
"username": username,
"user_face": user_face,
"num": combo_num,
"unit_price": combo_total_coin / combo_num / 1000,
"total_price": combo_total_coin / 1000
Expand All @@ -1263,6 +1267,7 @@ def _on_super_chat(self, client: blivedm.BLiveClient, message: web_models.SuperC

message = message.message
uname = message.uname
user_face = message.face
price = message.price

logging.info(f"用户:{uname} 发送 {price}元 SC:{message}")
Expand All @@ -1271,6 +1276,7 @@ def _on_super_chat(self, client: blivedm.BLiveClient, message: web_models.SuperC
"platform": platform,
"gift_name": "SC",
"username": uname,
"user_face": user_face,
"num": 1,
"unit_price": price,
"total_price": price,
Expand Down Expand Up @@ -1303,7 +1309,7 @@ def _on_open_live_danmaku(self, client: blivedm.OpenLiveClient, message: open_mo
data = {
"platform": platform,
"username": username,
"uface": user_face,
"user_face": user_face,
"content": content
}

Expand All @@ -1312,6 +1318,7 @@ def _on_open_live_danmaku(self, client: blivedm.OpenLiveClient, message: open_mo
def _on_open_live_gift(self, client: blivedm.OpenLiveClient, message: open_models.GiftMessage):
gift_name = message.gift_name
username = message.uname
user_face = message.uface
# 礼物数量
combo_num = message.gift_num
# 总金额
Expand All @@ -1323,6 +1330,7 @@ def _on_open_live_gift(self, client: blivedm.OpenLiveClient, message: open_model
"platform": platform,
"gift_name": gift_name,
"username": username,
"user_face": user_face,
"num": combo_num,
"unit_price": combo_total_coin / combo_num / 1000,
"total_price": combo_total_coin / 1000
Expand All @@ -1341,6 +1349,7 @@ def _on_open_live_super_chat(

message = message.message
uname = message.uname
user_face = message.uface
price = message.rmb

logging.info(f"用户:{uname} 发送 {price}元 SC:{message}")
Expand All @@ -1349,6 +1358,7 @@ def _on_open_live_super_chat(
"platform": platform,
"gift_name": "SC",
"username": uname,
"user_face": user_face,
"num": 1,
"unit_price": price,
"total_price": price,
Expand Down
121 changes: 58 additions & 63 deletions utils/my_handle.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,39 @@ def reload_config(self, config_path):
self.config_load()


# 回传给webui,用于聊天内容显示
def webui_show_chat_log_callback(self, data_type: str, data: dict, resp_content: str):
"""回传给webui,用于聊天内容显示
Args:
data_type (str): 数据内容的类型(多指LLM)
data (dict): 数据JSON
resp_content (str): 显示的聊天内容的文本
"""
try:
if My_handle.config.get("talk", "show_chat_log") == True:
if "ori_username" not in data:
data["ori_username"] = data["username"]
if "ori_content" not in data:
data["ori_content"] = data["content"]

# 返回给webui的数据
return_webui_json = {
"type": "llm",
"data": {
"type": data_type,
"username": data["ori_username"],
"content_type": "answer",
"content": f"错误:{data_type}无返回,请查看日志" if resp_content is None else resp_content,
"timestamp": My_handle.common.get_bj_time(0)
}
}

tmp_json = My_handle.common.send_request(f'http://{My_handle.config.get("webui", "ip")}:{My_handle.config.get("webui", "port")}/callback', "POST", return_webui_json, timeout=5)
except Exception as e:
logging.error(traceback.format_exc())

# 获取房间号
def get_room_id(self):
return My_handle.config.get("room_display_id")

Expand Down Expand Up @@ -651,26 +684,7 @@ def local_qa_handle(self, data):
"content": resp_content
}

if My_handle.config.get("talk", "show_chat_log") == True:
if "ori_username" not in data:
data["ori_username"] = data["username"]
if "ori_content" not in data:
data["ori_content"] = data["content"]


# 返回给webui的数据
return_webui_json = {
"type": "llm",
"data": {
"type": "本地问答",
"username": data["ori_username"],
"content_type": "answer",
"content": resp_content,
"timestamp": My_handle.common.get_bj_time(0)
}
}
tmp_json = My_handle.common.send_request(f'http://{My_handle.config.get("webui", "ip")}:{My_handle.config.get("webui", "port")}/callback', "POST", return_webui_json, timeout=5)

self.webui_show_chat_log_callback("本地问答-文本", data, resp_content)

self.audio_synthesis_handle(message)

Expand Down Expand Up @@ -713,26 +727,7 @@ def local_qa_handle(self, data):
"file_path": resp_content
}

if My_handle.config.get("talk", "show_chat_log") == True:
if "ori_username" not in data:
data["ori_username"] = data["username"]
if "ori_content" not in data:
data["ori_content"] = data["content"]


# 返回给webui的数据
return_webui_json = {
"type": "llm",
"data": {
"type": "本地问答",
"username": data["ori_username"],
"content_type": "answer",
"content": resp_content,
"timestamp": My_handle.common.get_bj_time(0)
}
}
tmp_json = My_handle.common.send_request(f'http://{My_handle.config.get("webui", "ip")}:{My_handle.config.get("webui", "port")}/callback', "POST", return_webui_json, timeout=5)

self.webui_show_chat_log_callback("本地问答-音频", data, resp_content)


self.audio_synthesis_handle(message)
Expand All @@ -755,6 +750,8 @@ def choose_song_handle(self, data):
username = data["username"]
content = data["content"]



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

Expand Down Expand Up @@ -784,6 +781,8 @@ def choose_song_handle(self, data):

self.audio_synthesis_handle(message)

self.webui_show_chat_log_callback("点歌", data, resp_content)

return True
# 判断点歌命令是否正确
elif start_cmd:
Expand All @@ -797,17 +796,20 @@ def choose_song_handle(self, data):

# 说明用户仅发送命令,没有发送歌名,说明用户不会用
if content == "":
resp_content = f'点歌命令错误,命令为 {My_handle.config.get("choose_song", "start_cmd")}+歌名'
message = {
"type": "comment",
"tts_type": My_handle.config.get("audio_synthesis_type"),
"data": My_handle.config.get(My_handle.config.get("audio_synthesis_type")),
"config": My_handle.config.get("filter"),
"username": username,
"content": f'点歌命令错误,命令为 {My_handle.config.get("choose_song", "start_cmd")}+歌名'
"content": resp_content
}

self.audio_synthesis_handle(message)

self.webui_show_chat_log_callback("点歌", data, resp_content)

return True

# 判断是否有此歌曲
Expand All @@ -830,6 +832,8 @@ def choose_song_handle(self, data):

self.audio_synthesis_handle(message)

self.webui_show_chat_log_callback("点歌", data, resp_content)

return True

resp_content = My_handle.audio.search_files(My_handle.config.get('choose_song', 'song_path'), song_filename, True)
Expand All @@ -852,6 +856,7 @@ def choose_song_handle(self, data):
"content": resp_content
}

self.webui_show_chat_log_callback("点歌", data, resp_content)

self.audio_synthesis_handle(message)

Expand Down Expand Up @@ -1056,12 +1061,13 @@ def prohibitions_handle(self, content):


# 直接复读
def reread_handle(self, data, filter=False):
def reread_handle(self, data, filter=False, type="reread"):
"""复读处理
Args:
data (dict): 包含用户名,弹幕内容
filter (bool): 是否开启复读内容的过滤
type (str): 复读数据的类型(reread | trends_copywriting)
Returns:
_type_: 寂寞
Expand Down Expand Up @@ -1090,7 +1096,7 @@ def reread_handle(self, data, filter=False):

# 音频合成时需要用到的重要数据
message = {
"type": "reread",
"type": type,
"tts_type": My_handle.config.get("audio_synthesis_type"),
"data": My_handle.config.get(My_handle.config.get("audio_synthesis_type")),
"config": My_handle.config.get("filter"),
Expand Down Expand Up @@ -1226,25 +1232,7 @@ def llm_handle(self, chat_type, data, type="chat"):
if resp_content is None:
self.abnormal_alarm_handle("llm")

if My_handle.config.get("talk", "show_chat_log") == True:
if "ori_username" not in data:
data["ori_username"] = data["username"]
if "ori_content" not in data:
data["ori_content"] = data["content"]

# 返回给webui的数据
return_webui_json = {
"type": "llm",
"data": {
"type": chat_type,
"username": data["ori_username"],
"content_type": "answer",
"content": "错误:LLM无返回,请查看日志" if resp_content is None else resp_content,
"timestamp": My_handle.common.get_bj_time(0)
}
}

tmp_json = My_handle.common.send_request(f'http://{My_handle.config.get("webui", "ip")}:{My_handle.config.get("webui", "port")}/callback', "POST", return_webui_json, timeout=5)
self.webui_show_chat_log_callback(chat_type, data, resp_content)

return resp_content
except Exception as e:
Expand Down Expand Up @@ -1843,6 +1831,9 @@ def custom_cmd_handle(self, type, data):
# 使用 eval() 执行字符串表达式并获取结果
resp_content = eval(custom_cmd_config["data_analysis"])

# 将字符串中的换行符替换为句号
resp_content = resp_content.replace('\n', '。')

logging.debug(f"resp_content={resp_content}")

# 违禁词处理
Expand Down Expand Up @@ -1879,6 +1870,8 @@ def custom_cmd_handle(self, type, data):

self.audio_synthesis_handle(message)

self.webui_show_chat_log_callback("自定义命令", data, resp_content)

flag = True


Expand Down Expand Up @@ -1933,14 +1926,16 @@ def comment_handle(self, data):
data["ori_username"] = data["username"]
if "ori_content" not in data:
data["ori_content"] = data["content"]

if "user_face" not in data:
data["user_face"] = 'https://robohash.org/ui'

# 返回给webui的数据
return_webui_json = {
"type": "llm",
"data": {
"type": "弹幕信息",
"username": data["ori_username"],
"user_face": data["user_face"],
"content_type": "question",
"content": data["ori_content"],
"timestamp": My_handle.common.get_bj_time(0)
Expand Down
9 changes: 6 additions & 3 deletions webui.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,13 +396,16 @@ def data_handle_show_chat_log(data_json):
if data_json["type"] == "llm":
if data_json["data"]["content_type"] == "question":
name = data_json["data"]['username']
avatar = 'https://robohash.org/ui'
if 'user_face' in data_json["data"]:
# 由于直接请求b站头像返回403 所以暂时还是用默认头像
# avatar = data_json["data"]['user_face']
avatar = 'https://robohash.org/ui'
else:
avatar = 'https://robohash.org/ui'
else:
name = data_json["data"]['type']
avatar = "http://127.0.0.1:8081/favicon.ico"



with scroll_area_chat_box:
ui.chat_message(data_json["data"]["content"],
name=name,
Expand Down

0 comments on commit a52c22b

Please sign in to comment.