diff --git a/config.json b/config.json index a671f036..6543feaa 100644 --- a/config.json +++ b/config.json @@ -771,8 +771,16 @@ ], "idle_time_task": { "enable": false, - "idle_time": 120, - "random_time": false, + "idle_time_min": 60, + "idle_time_max": 120, + "copywriting": { + "enable": false, + "random": true, + "copy": [ + "当前时间是{time},这里是新的语句[1|2|3],可以在括号内随机一个数据[4|5|6]", + "请给我简单地讲一个笑话,让我在忙碌的生活中找到轻松和快乐。控制在100字以内" + ] + }, "comment": { "enable": false, "random": true, diff --git a/config.json.bak b/config.json.bak index a671f036..6543feaa 100644 --- a/config.json.bak +++ b/config.json.bak @@ -771,8 +771,16 @@ ], "idle_time_task": { "enable": false, - "idle_time": 120, - "random_time": false, + "idle_time_min": 60, + "idle_time_max": 120, + "copywriting": { + "enable": false, + "random": true, + "copy": [ + "当前时间是{time},这里是新的语句[1|2|3],可以在括号内随机一个数据[4|5|6]", + "请给我简单地讲一个笑话,让我在忙碌的生活中找到轻松和快乐。控制在100字以内" + ] + }, "comment": { "enable": false, "random": true, diff --git a/main.py b/main.py index 4c84ff80..1e03a828 100644 --- a/main.py +++ b/main.py @@ -103,7 +103,7 @@ def send(): data_json = request.get_json() logging.info(f"API收到数据:{data_json}") - if data_json["type"] == "reread": + if data_json["type"] in ["reread", "reread_top_priority"]: my_handle.reread_handle(data_json) elif data_json["type"] == "comment": my_handle.process_data(data_json, "comment") @@ -665,27 +665,32 @@ async def idle_time_task(): # 记录上一次触发的任务类型 last_mode = 0 + copywriting_copy_list = None comment_copy_list = None local_audio_path_list = None - overflow_time = int(config.get("idle_time_task", "idle_time")) - # 是否开启了随机闲时时间 - if config.get("idle_time_task", "random_time"): - overflow_time = random.randint(0, overflow_time) + overflow_time_min = int(config.get("idle_time_task", "idle_time_min")) + overflow_time_max = int(config.get("idle_time_task", "idle_time_max")) + overflow_time = random.randint(overflow_time_min, overflow_time_max) - logging.info(f"闲时时间={overflow_time}秒") + logging.info(f"下一个闲时任务将在{overflow_time}秒后执行") def load_data_list(type): - if type == "comment": + if type == "copywriting": + tmp = config.get("idle_time_task", "copywriting", "copy") + elif type == "comment": tmp = config.get("idle_time_task", "comment", "copy") elif type == "local_audio": tmp = config.get("idle_time_task", "local_audio", "path") tmp2 = copy.copy(tmp) return tmp2 + # 加载数据到list + copywriting_copy_list = load_data_list("copywriting") comment_copy_list = load_data_list("comment") local_audio_path_list = load_data_list("local_audio") + logging.debug(f"copywriting_copy_list={copywriting_copy_list}") logging.debug(f"comment_copy_list={comment_copy_list}") logging.debug(f"local_audio_path_list={local_audio_path_list}") @@ -700,11 +705,90 @@ def load_data_list(type): global_idle_time = 0 # 闲时任务处理 + if config.get("idle_time_task", "copywriting", "enable"): + if last_mode == 0: + # 是否开启了随机触发 + if config.get("idle_time_task", "copywriting", "random"): + logging.debug("切换到文案触发模式") + if copywriting_copy_list != []: + # 随机打乱列表中的元素 + random.shuffle(copywriting_copy_list) + copywriting_copy = copywriting_copy_list.pop(0) + else: + # 刷新list数据 + copywriting_copy_list = load_data_list("copywriting") + # 随机打乱列表中的元素 + random.shuffle(copywriting_copy_list) + copywriting_copy = copywriting_copy_list.pop(0) + else: + if copywriting_copy_list != []: + copywriting_copy = copywriting_copy_list.pop(0) + else: + # 刷新list数据 + copywriting_copy_list = load_data_list("copywriting") + copywriting_copy = copywriting_copy_list.pop(0) + + hour, min = common.get_bj_time(6) + + if 0 <= hour and hour < 6: + time = f"凌晨{hour}点{min}分" + elif 6 <= hour and hour < 9: + time = f"早晨{hour}点{min}分" + elif 9 <= hour and hour < 12: + time = f"上午{hour}点{min}分" + elif hour == 12: + time = f"中午{hour}点{min}分" + elif 13 <= hour and hour < 18: + time = f"下午{hour - 12}点{min}分" + elif 18 <= hour and hour < 20: + time = f"傍晚{hour - 12}点{min}分" + elif 20 <= hour and hour < 24: + time = f"晚上{hour - 12}点{min}分" + + # 动态变量替换 + # 假设有多个未知变量,用户可以在此处定义动态变量 + variables = { + 'time': time, + 'user_num': "N", + 'last_username': last_username_list[-1], + } + + # 有用户数据情况的平台特殊处理 + if platform in ["dy", "tiktok"]: + variables['user_num'] = last_liveroom_data["OnlineUserCount"] + + # 使用字典进行字符串替换 + if any(var in copywriting_copy for var in variables): + copywriting_copy = copywriting_copy.format(**{var: value for var, value in variables.items() if var in copywriting_copy}) + + # [1|2]括号语法随机获取一个值,返回取值完成后的字符串 + copywriting_copy = common.brackets_text_randomize(copywriting_copy) + + # 发送给处理函数 + data = { + "platform": platform, + "username": "闲时任务-文案模式", + "type": "reread", + "content": copywriting_copy + } + + my_handle.process_data(data, "idle_time_task") + + # 模式切换 + last_mode = 1 + + overflow_time = random.randint(overflow_time_min, overflow_time_max) + logging.info(f"下一个闲时任务将在{overflow_time}秒后执行") + + continue + else: + last_mode = 1 + if config.get("idle_time_task", "comment", "enable"): - if last_mode == 0 or not config.get("idle_time_task", "local_audio", "enable"): + if last_mode == 1: # 是否开启了随机触发 if config.get("idle_time_task", "comment", "random"): - logging.debug("切换到文案触发模式") + logging.debug("切换到弹幕触发LLM模式") if comment_copy_list != []: # 随机打乱列表中的元素 random.shuffle(comment_copy_list) @@ -762,7 +846,7 @@ def load_data_list(type): # 发送给处理函数 data = { "platform": platform, - "username": "闲时任务", + "username": "闲时任务-弹幕触发LLM模式", "type": "comment", "content": comment_copy } @@ -770,18 +854,17 @@ def load_data_list(type): my_handle.process_data(data, "idle_time_task") # 模式切换 - last_mode = 1 + last_mode = 2 - overflow_time = int(config.get("idle_time_task", "idle_time")) - # 是否开启了随机闲时时间 - if config.get("idle_time_task", "random_time"): - overflow_time = random.randint(0, overflow_time) - logging.info(f"闲时时间={overflow_time}秒") + overflow_time = random.randint(overflow_time_min, overflow_time_max) + logging.info(f"下一个闲时任务将在{overflow_time}秒后执行") continue - + else: + last_mode = 2 + if config.get("idle_time_task", "local_audio", "enable"): - if last_mode == 1 or (not config.get("idle_time_task", "comment", "enable")): + if last_mode == 2: logging.debug("切换到本地音频模式") # 是否开启了随机触发 @@ -809,7 +892,7 @@ def load_data_list(type): # 发送给处理函数 data = { "platform": platform, - "username": "闲时任务", + "username": "闲时任务-本地音频模式", "type": "local_audio", "content": common.extract_filename(local_audio_path, False), "file_path": local_audio_path @@ -820,14 +903,12 @@ def load_data_list(type): # 模式切换 last_mode = 0 - overflow_time = int(config.get("idle_time_task", "idle_time")) - # 是否开启了随机闲时时间 - if config.get("idle_time_task", "random_time"): - overflow_time = random.randint(0, overflow_time) - logging.info(f"闲时时间={overflow_time}秒") + overflow_time = random.randint(overflow_time_min, overflow_time_max) + logging.info(f"下一个闲时任务将在{overflow_time}秒后执行") continue - + else: + last_mode = 0 except Exception as e: logging.error(traceback.format_exc()) diff --git a/utils/audio.py b/utils/audio.py index 81865964..029c3572 100644 --- a/utils/audio.py +++ b/utils/audio.py @@ -480,7 +480,7 @@ def audio_synthesis(self, message): Audio.message_queue.put(tmp_message) # 闲时任务 elif message['type'] == "idle_time_task": - if message['content_type'] == "comment": + if message['content_type'] in ["comment", "reread"]: pass elif message['content_type'] == "local_audio": # 拼接json数据,存入队列 @@ -1004,6 +1004,7 @@ async def only_play_audio(self): elif self.config.get("visual_body") == "digital_human_video_player": await self.digital_human_video_player_api(voice_tmp_path) else: + # 根据播放器类型进行区分 if self.config.get("play_audio", "player") in ["audio_player", "audio_player_v2"]: if "insert_index" in data_json: data_json = { diff --git a/utils/my_handle.py b/utils/my_handle.py index f6a83984..f6d2e648 100644 --- a/utils/my_handle.py +++ b/utils/my_handle.py @@ -375,6 +375,7 @@ def audio_synthesis_handle(self, data_json): 核心参数: type目前有 + reread_top_priority 最高优先级-复读 comment 弹幕 local_qa_audio 本地问答音频 song 歌曲 @@ -2373,7 +2374,40 @@ def idle_time_task_handle(self, data): content = data["content"] username = data["username"] - if type == "comment": + if type == "reread": + # 输出当前用户发送的弹幕消息 + logging.info(f"[{username}]: {content}") + + # 弹幕格式检查和特殊字符替换 + content = self.comment_check_and_replace(content) + if content is None: + return + + # 判断按键映射触发类型 + if My_handle.config.get("key_mapping", "type") == "弹幕" or My_handle.config.get("key_mapping", "type") == "弹幕+回复": + # 按键映射 触发后不执行后面的其他功能 + if self.key_mapping_handle("弹幕", data): + return + + # 判断自定义命令触发类型 + if My_handle.config.get("custom_cmd", "type") == "弹幕" or My_handle.config.get("custom_cmd", "type") == "弹幕+回复": + # 自定义命令 触发后不执行后面的其他功能 + if self.custom_cmd_handle("弹幕", data): + return + + # 音频合成时需要用到的重要数据 + message = { + "type": "idle_time_task", + "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": content, + "content_type": type + } + + self.audio_synthesis_handle(message) + elif type == "comment": # 记录数据库 if My_handle.config.get("database", "comment_enable"): insert_data_sql = ''' @@ -2497,7 +2531,7 @@ def idle_time_task_handle(self, data): "config": My_handle.config.get("filter"), "username": username, "content": resp_content, - "content_type": "comment" + "content_type": type } @@ -2512,7 +2546,7 @@ def idle_time_task_handle(self, data): "config": My_handle.config.get("filter"), "username": username, "content": content, - "content_type": "local_audio", + "content_type": type, "file_path": os.path.abspath(data["file_path"]) } diff --git a/webui.py b/webui.py index 51b8bcfd..e6ccf055 100644 --- a/webui.py +++ b/webui.py @@ -1131,10 +1131,13 @@ def common_textarea_handle(content): # 闲时任务 if config.get("webui", "show_card", "common_config", "idle_time_task"): config_data["idle_time_task"]["enable"] = switch_idle_time_task_enable.value - config_data["idle_time_task"]["idle_time"] = input_idle_time_task_idle_time.value - config_data["idle_time_task"]["random_time"] = switch_idle_time_task_random_time.value + config_data["idle_time_task"]["idle_time_min"] = int(input_idle_time_task_idle_time_min.value) + config_data["idle_time_task"]["idle_time_max"] = int(input_idle_time_task_idle_time_max.value) config_data["idle_time_task"]["comment"]["enable"] = switch_idle_time_task_comment_enable.value config_data["idle_time_task"]["comment"]["random"] = switch_idle_time_task_comment_random.value + config_data["idle_time_task"]["copywriting"]["copy"] = common_textarea_handle(textarea_idle_time_task_copywriting_copy.value) + config_data["idle_time_task"]["copywriting"]["enable"] = switch_idle_time_task_copywriting_enable.value + config_data["idle_time_task"]["copywriting"]["random"] = switch_idle_time_task_copywriting_random.value config_data["idle_time_task"]["comment"]["copy"] = common_textarea_handle(textarea_idle_time_task_comment_copy.value) config_data["idle_time_task"]["local_audio"]["enable"] = switch_idle_time_task_local_audio_enable.value config_data["idle_time_task"]["local_audio"]["random"] = switch_idle_time_task_local_audio_random.value @@ -2397,12 +2400,18 @@ def common_textarea_handle(content): ui.label('闲时任务') with ui.row(): switch_idle_time_task_enable = ui.switch('启用', value=config.get("idle_time_task", "enable")).style(switch_internal_css) - input_idle_time_task_idle_time = ui.input(label='闲时时间', value=config.get("idle_time_task", "idle_time"), placeholder='闲时间隔时间(正整数,单位:秒),就是在没有弹幕情况下经过的时间').style("width:200px;") - switch_idle_time_task_random_time = ui.switch('随机闲时时间', value=config.get("idle_time_task", "random_time")).style(switch_internal_css) + input_idle_time_task_idle_time_min = ui.input(label='最小闲时时间', value=config.get("idle_time_task", "idle_time_min"), placeholder='最小闲时间隔时间(正整数,单位:秒),就是在没有弹幕情况下经过的时间').style("width:150px;") + input_idle_time_task_idle_time_max = ui.input(label='最大闲时时间', value=config.get("idle_time_task", "idle_time_max"), placeholder='最大闲时间隔时间(正整数,单位:秒),就是在没有弹幕情况下经过的时间').style("width:150px;") + + with ui.row(): + switch_idle_time_task_copywriting_enable = ui.switch('文案模式', value=config.get("idle_time_task", "copywriting", "enable")).style(switch_internal_css) + switch_idle_time_task_copywriting_random = ui.switch('随机文案', value=config.get("idle_time_task", "copywriting", "random")).style(switch_internal_css) + textarea_idle_time_task_copywriting_copy = ui.textarea(label='文案列表', value=textarea_data_change(config.get("idle_time_task", "copywriting", "copy")), placeholder='文案列表,文案之间用换行分隔,文案会丢LLM进行处理后直接合成返回的结果').style("width:800px;") + with ui.row(): - switch_idle_time_task_comment_enable = ui.switch('LLM模式', value=config.get("idle_time_task", "comment", "enable")).style(switch_internal_css) - switch_idle_time_task_comment_random = ui.switch('随机文案', value=config.get("idle_time_task", "comment", "random")).style(switch_internal_css) - textarea_idle_time_task_comment_copy = ui.textarea(label='文案列表', value=textarea_data_change(config.get("idle_time_task", "comment", "copy")), placeholder='文案列表,文案之间用换行分隔,文案会丢LLM进行处理后直接合成返回的结果').style("width:800px;") + switch_idle_time_task_comment_enable = ui.switch('弹幕触发LLM模式', value=config.get("idle_time_task", "comment", "enable")).style(switch_internal_css) + switch_idle_time_task_comment_random = ui.switch('随机弹幕', value=config.get("idle_time_task", "comment", "random")).style(switch_internal_css) + textarea_idle_time_task_comment_copy = ui.textarea(label='弹幕列表', value=textarea_data_change(config.get("idle_time_task", "comment", "copy")), placeholder='弹幕列表,弹幕之间用换行分隔,文案会丢LLM进行处理后直接合成返回的结果').style("width:800px;") with ui.row(): switch_idle_time_task_local_audio_enable = ui.switch('本地音频模式', value=config.get("idle_time_task", "local_audio", "enable")).style(switch_internal_css) switch_idle_time_task_local_audio_random = ui.switch('随机本地音频', value=config.get("idle_time_task", "local_audio", "random")).style(switch_internal_css) @@ -3970,7 +3979,7 @@ def talk_chat_box_send(): # 发送 聊天框内容 进行复读 - def talk_chat_box_reread(insert_index=-1): + def talk_chat_box_reread(insert_index=-1, type="reread"): global running_flag if running_flag != 1: @@ -3986,7 +3995,7 @@ def talk_chat_box_reread(insert_index=-1): if insert_index == -1: data = { - "type": "reread", + "type": type, "username": username, "content": content } @@ -4000,7 +4009,7 @@ def talk_chat_box_reread(insert_index=-1): return data = { - "type": "reread", + "type": type, "username": username, "content": content, "insert_index": insert_index @@ -4010,7 +4019,7 @@ def talk_chat_box_reread(insert_index=-1): show_chat_log_json = { "type": "llm", "data": { - "type": "reread", + "type": type, "username": username, "content_type": "question", "content": content, @@ -4047,7 +4056,7 @@ def talk_chat_box_tuning(): button_talk_chat_box_send = ui.button('发送', on_click=lambda: talk_chat_box_send(), color=button_internal_color).style(button_internal_css) button_talk_chat_box_reread = ui.button('直接复读', on_click=lambda: talk_chat_box_reread(), color=button_internal_color).style(button_internal_css) button_talk_chat_box_tuning = ui.button('调教', on_click=lambda: talk_chat_box_tuning(), color=button_internal_color).style(button_internal_css) - button_talk_chat_box_reread_first = ui.button('直接复读-插队首', on_click=lambda: talk_chat_box_reread(0), color=button_internal_color).style(button_internal_css) + button_talk_chat_box_reread_first = ui.button('直接复读-插队首', on_click=lambda: talk_chat_box_reread(0, "reread_top_priority"), color=button_internal_color).style(button_internal_css) with ui.tab_panel(image_recognition_page).style(tab_panel_css): with ui.card().style(card_css):