From 469cbb313a8dbbfa2be6eab5906c46710a83fc17 Mon Sep 17 00:00:00 2001 From: ikaros <327209194@qq.com> Date: Wed, 20 Mar 2024 22:07:25 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=A5=E5=85=85webui=E8=81=8A=E5=A4=A9?= =?UTF-8?q?=E8=AE=B0=E5=BD=95=E5=9B=9E=E8=B0=83=E5=86=85=E5=AE=B9=EF=BC=8C?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81=E5=AE=9E=E7=8E=B0=EF=BC=9B?= =?UTF-8?q?=E5=A4=8D=E8=AF=BB=E5=A4=84=E7=90=86=E6=96=B0=E5=A2=9Etype?= =?UTF-8?q?=E4=BC=A0=E5=8F=82=EF=BC=8C=E5=8A=A8=E6=80=81=E6=96=87=E6=A1=88?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E9=99=84=E5=B8=A6type?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.py | 14 +++++- utils/my_handle.py | 121 ++++++++++++++++++++++----------------------- webui.py | 9 ++-- 3 files changed, 76 insertions(+), 68 deletions(-) diff --git a/main.py b/main.py index 9be7cd8e..8818b8ea 100644 --- a/main.py +++ b/main.py @@ -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: @@ -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 } @@ -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 # 总金额 @@ -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 @@ -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}") @@ -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, @@ -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 } @@ -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 # 总金额 @@ -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 @@ -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}") @@ -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, diff --git a/utils/my_handle.py b/utils/my_handle.py index c7b0fc45..e8e2e60e 100644 --- a/utils/my_handle.py +++ b/utils/my_handle.py @@ -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") @@ -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) @@ -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) @@ -755,6 +750,8 @@ def choose_song_handle(self, data): username = data["username"] content = data["content"] + + # 合并字符串末尾连续的* 主要针对获取不到用户名的情况 username = My_handle.common.merge_consecutive_asterisks(username) @@ -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: @@ -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 # 判断是否有此歌曲 @@ -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) @@ -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) @@ -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_: 寂寞 @@ -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"), @@ -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: @@ -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}") # 违禁词处理 @@ -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 @@ -1933,7 +1926,8 @@ 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 = { @@ -1941,6 +1935,7 @@ def comment_handle(self, data): "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) diff --git a/webui.py b/webui.py index dc1d4c96..536274db 100644 --- a/webui.py +++ b/webui.py @@ -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,