Compare commits

..

1 Commits

Author SHA1 Message Date
jocelyn
3b939b8ce9 [ADD]add logic of loop frame 2025-06-13 10:48:37 +08:00
3 changed files with 36 additions and 63 deletions

View File

@ -13,8 +13,10 @@ from eventbus import EventBus
from human_handler import AudioHandler
from utils import load_model, mirror_index, get_device, SyncQueue
from .huaman_status import HumanStatus
from utils.log import logger
logger = logging.getLogger(__name__)
# logger = logging.getLogger(__name__)
current_file_path = os.path.dirname(os.path.abspath(__file__))

View File

@ -1,13 +1,8 @@
import logging
import os
import sys
import redis
from loguru import logger as logurulogger
import redis.exceptions
from app.config import config
import json
from redis.retry import Retry
from redis.backoff import ExponentialBackoff
LOG_FORMAT = (
"<level>{level: <8}</level> "
@ -20,17 +15,6 @@ LOG_FORMAT = (
)
LOG_NAME = ["uvicorn", "uvicorn.access", "uvicorn.error", "flask"]
# 配置 Redis 连接池
redis_pool = redis.ConnectionPool(
host=config.LOG_REDIS_HOST, # Redis 服务器地址
port=config.LOG_REDIS_PORT, # Redis 服务器端口
db=config.LOG_REDIS_DB, # 数据库编号
password=config.LOG_REDIS_AUTH, # 密码
max_connections=config.max_connections, # 最大连接数
socket_connect_timeout=config.socket_connect_timeout, # 连接超时时间
socket_timeout=config.socket_timeout, # 等待超时时间
)
class InterceptHandler(logging.Handler):
def emit(self, record):
@ -53,20 +37,14 @@ class Logging:
def __init__(self):
self.log_path = "logs"
self._connect_redis()
if config.IS_LOCAL:
os.makedirs(self.log_path, exist_ok=True)
self._initlogger()
self._reset_log_handler()
def _connect_redis(self):
retry = Retry(ExponentialBackoff(), 3) # 重试3次指数退避
self.redis_client = redis.Redis(connection_pool=redis_pool,retry=retry) # 使用连接池
def _initlogger(self):
"""初始化loguru配置"""
logurulogger.remove()
if config.IS_LOCAL:
logurulogger.add(
os.path.join(self.log_path, "error.log.{time:YYYY-MM-DD}"),
format=LOG_FORMAT,
@ -92,20 +70,9 @@ class Logging:
colorize=True,
)
logurulogger.add(self._log_to_redis, level="INFO", format=LOG_FORMAT)
self.logger = logurulogger
def _log_to_redis(self, message):
"""将日志写入 Redis 列表"""
try:
self.redis_client.rpush(f"nlp.logger.{config.env_version}.log", json.dumps({"message": message}))
except redis.exceptions.ConnectionError as e:
logger.error(f"write {message} Redis connection error: {e}")
except redis.exceptions.TimeoutError as e:
logger.error(f"write {message} Redis operation timed out: {e}")
except Exception as e:
logger.error(f"write {message} Unexpected error: {e}")
def _reset_log_handler(self):
for log in LOG_NAME:

View File

@ -317,13 +317,15 @@ def action2silent(
if __name__ == "__main__":
startfrom = 0 # 上一个batch的最后一帧
frame_config= []
frame_config= [[1, 200, True]]
audio_frame_length = len(mel_chunks) # TODO: 确认是否为 batch_size
startfrom = startfrom if startfrom>= frame_config[0][0] else frame_config[0][0]
first_speak, last_speak = True, False
is_silent= True # 当前batch是否为静默
last_direction = 1 # -1 为反方向
start_idx_list, current_direction = play_in_loop_v2(
i = 0
while i<=10:
start_idx_list, last_direction = play_in_loop_v2(
frame_config,
startfrom,
audio_frame_length,
@ -332,3 +334,5 @@ if __name__ == "__main__":
first_speak,
last_speak,
)
startfrom = start_idx_list[-1]
i+=1