利用预测时长方式,避免回复间隔问题

main
fanpt 2 weeks ago
parent d48c86fbd1
commit 3c2caa37ed

@ -12,7 +12,7 @@ import traceback
import urllib.parse import urllib.parse
from contextlib import contextmanager from contextlib import contextmanager
from unittest.mock import patch from unittest.mock import patch
from datetime import datetime
import httpx import httpx
import requests import requests
import websocket import websocket
@ -415,6 +415,15 @@ class DouyinLiveWebReply:
self.live_chat_config.insert_message(message, _type, batch_number) self.live_chat_config.insert_message(message, _type, batch_number)
logger.info(f'入库文案:{_type} | {message}') logger.info(f'入库文案:{_type} | {message}')
def text_to_adio_cost_predict(self, text):
"""
计算文本转语音的耗时
:param text: 文本
:return: 耗时 单位秒
"""
return len(text)/5
def __call__(self): def __call__(self):
""" """
优先从用户交互队列中取提示词如果没有用户交互的数据则输出系统提示词 优先从用户交互队列中取提示词如果没有用户交互的数据则输出系统提示词
@ -424,17 +433,18 @@ class DouyinLiveWebReply:
logger.info(f'livetalking address -> {self.live_chat_config.livetalking_address}') logger.info(f'livetalking address -> {self.live_chat_config.livetalking_address}')
logger.info(f'ollama_address -> {self.live_chat_config.ollama_address}') logger.info(f'ollama_address -> {self.live_chat_config.ollama_address}')
# 下次调用post_to_human的时间
next_to_human_call_time = None
while True: while True:
try: try:
is_speaking = self.config['is_speaking'] # next_to_human_call_time大于当前时间立即调用
if is_speaking: call_immediate = next_to_human_call_time is None or time.time() >= next_to_human_call_time
if not call_immediate:
prompt_data = self.queue.get(False) prompt_data = self.queue.get(False)
if prompt_data is not None: if prompt_data is not None:
product_name, product_specification, product_description = self.live_chat_config.product_info product_name, product_specification, product_description = self.live_chat_config.product_info
# 兼容老/新两种数据结构:
# 老结构: (message_type, prompt, live_chat)
# 新结构: (message_type, prompt, live_chat, user_name)
if isinstance(prompt_data, tuple) and len(prompt_data) == 4: if isinstance(prompt_data, tuple) and len(prompt_data) == 4:
message_type, prompt, live_chat, sender_name = prompt_data message_type, prompt, live_chat, sender_name = prompt_data
else: else:
@ -500,6 +510,7 @@ class DouyinLiveWebReply:
reply_message, from_user = self.response_queue.get() reply_message, from_user = self.response_queue.get()
reply_message = self.reply_message_postprocess(reply_message) reply_message = self.reply_message_postprocess(reply_message)
if from_user: if from_user:
reply_message = f"{from_user}哥哥,{reply_message}"
logger.info(f"开始播放回复弹幕({from_user}{reply_message}") logger.info(f"开始播放回复弹幕({from_user}{reply_message}")
else: else:
logger.info(f"开始播放回复弹幕:{reply_message}") logger.info(f"开始播放回复弹幕:{reply_message}")
@ -519,9 +530,12 @@ class DouyinLiveWebReply:
reply_message, _id = precedence_message reply_message, _id = precedence_message
self.live_chat_config.flush_precedence_reply_message() self.live_chat_config.flush_precedence_reply_message()
logger.info(f"开始播放系统文案:{reply_message}") logger.info(f"开始播放系统文案:{reply_message}")
cost = self.text_to_adio_cost_predict(reply_message)
logger.info(f'文本:{reply_message}预计算出的语音时长:{cost}')
next_to_human_call_time = time.time() + cost
formatted_time = datetime.fromtimestamp(next_to_human_call_time).strftime("%Y-%m-%d %H:%M:%S")
logger.info(f'预计在{formatted_time}发送下一段语音')
self.post_to_human_sync(reply_message) self.post_to_human_sync(reply_message)
time.sleep(1)
except Exception: except Exception:
logger.error(traceback.format_exc()) logger.error(traceback.format_exc())

Loading…
Cancel
Save