🦄 refactor(log): colorful log

This commit is contained in:
kalicyh
2025-02-18 00:07:19 +08:00
parent 921e3c2c75
commit 2acee60d73
32 changed files with 423 additions and 190 deletions
+5 -5
View File
@@ -1,6 +1,6 @@
import logging
from config.logger import setup_logging
logger = logging.getLogger(__name__)
TAG = __name__
class AuthenticationError(Exception):
@@ -37,15 +37,15 @@ class AuthMiddleware:
# 验证Authorization header
auth_header = headers.get("authorization", "")
if not auth_header.startswith("Bearer "):
logger.error("Missing or invalid Authorization header")
logger.bind(tag=TAG).error("Missing or invalid Authorization header")
raise AuthenticationError("Missing or invalid Authorization header")
token = auth_header.split(" ")[1]
if token not in self.tokens:
logger.error(f"Invalid token: {token}")
logger.bind(tag=TAG).error(f"Invalid token: {token}")
raise AuthenticationError("Invalid token")
logger.info(f"Authentication successful - Device: {device_id}, Token: {self.tokens[token]}")
logger.bind(tag=TAG).info(f"Authentication successful - Device: {device_id}, Token: {self.tokens[token]}")
return True
def get_token_name(self, token):
+30 -28
View File
@@ -4,7 +4,7 @@ import uuid
import time
import queue
import asyncio
import logging
from config.logger import setup_logging
import threading
import websockets
from typing import Dict, Any
@@ -19,10 +19,12 @@ from config.private_config import PrivateConfig
from core.auth import AuthMiddleware, AuthenticationError
from core.utils.auth_code_gen import AuthCodeGenerator # 添加导入
TAG = __name__
class ConnectionHandler:
def __init__(self, config: Dict[str, Any], _vad, _asr, _llm, _tts):
self.config = config
self.logger = logging.getLogger(__name__)
self.logger = setup_logging()
self.auth = AuthMiddleware(config)
self.websocket = None
@@ -85,7 +87,7 @@ class ConnectionHandler:
try:
# 获取并验证headers
self.headers = dict(ws.request.headers)
self.logger.info(f"New connection request - Headers: {self.headers}")
self.logger.bind(tag=TAG).info(f"New connection request - Headers: {self.headers}")
# 进行认证
await self.auth.authenticate(self.headers)
@@ -94,7 +96,7 @@ class ConnectionHandler:
# Load private configuration if device_id is provided
bUsePrivateConfig = self.config.get("use_private_config", False)
logging.info(f"bUsePrivateConfig: {bUsePrivateConfig}, device_id: {device_id}")
self.info(f"bUsePrivateConfig: {bUsePrivateConfig}, device_id: {device_id}")
if bUsePrivateConfig and device_id:
try:
self.private_config = PrivateConfig(device_id, self.config, self.auth_code_gen)
@@ -112,12 +114,12 @@ class ConnectionHandler:
self.asr = asr
self.llm = llm
self.tts = tts
self.logger.info(f"Loaded private config and instances for device {device_id}")
self.logger.bind(tag=TAG).info(f"Loaded private config and instances for device {device_id}")
else:
self.logger.error(f"Failed to create instances for device {device_id}")
self.logger.bind(tag=TAG).error(f"Failed to create instances for device {device_id}")
self.private_config = None
except Exception as e:
self.logger.error(f"Error initializing private config: {e}")
self.logger.bind(tag=TAG).error(f"Error initializing private config: {e}")
self.private_config = None
raise
@@ -138,15 +140,15 @@ class ConnectionHandler:
async for message in self.websocket:
await self._route_message(message)
except websockets.exceptions.ConnectionClosed:
self.logger.info("客户端断开连接")
self.logger.bind(tag=TAG).info("客户端断开连接")
await self.close()
except AuthenticationError as e:
self.logger.error(f"Authentication failed: {str(e)}")
self.logger.bind(tag=TAG).error(f"Authentication failed: {str(e)}")
await ws.close()
return
except Exception as e:
self.logger.error(f"Connection error: {str(e)}")
self.logger.bind(tag=TAG).error(f"Connection error: {str(e)}")
await ws.close()
return
@@ -208,7 +210,7 @@ class ConnectionHandler:
start_time = time.time() # 记录开始时间
llm_responses = self.llm.response(self.session_id, self.dialogue.get_llm_dialogue())
except Exception as e:
self.logger.error(f"LLM 处理出错 {query}: {e}")
self.logger.bind(tag=TAG).error(f"LLM 处理出错 {query}: {e}")
return None
# 提交 TTS 任务到线程池
self.llm_finish_task = False
@@ -220,7 +222,7 @@ class ConnectionHandler:
break
end_time = time.time() # 记录结束时间
self.logger.debug(f"大模型返回时间时间: {end_time - start_time} 秒, 生成token={content}")
self.logger.bind(tag=TAG).debug(f"大模型返回时间时间: {end_time - start_time} 秒, 生成token={content}")
if is_segment(response_message):
segment_text = "".join(response_message[start:])
segment_text = get_string_no_punctuation_or_emoji(segment_text)
@@ -241,7 +243,7 @@ class ConnectionHandler:
self.llm_finish_task = True
# 更新对话
self.dialogue.put(Message(role="assistant", content="".join(response_message)))
self.logger.debug(json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False))
self.logger.bind(tag=TAG).debug(json.dumps(self.dialogue.get_llm_dialogue(), indent=4, ensure_ascii=False))
return True
def _priority_thread(self):
@@ -253,25 +255,25 @@ class ConnectionHandler:
continue
text = None
try:
self.logger.debug("正在处理TTS任务...")
self.logger.bind(tag=TAG).debug("正在处理TTS任务...")
tts_file, text = future.result(timeout=10)
if text is None or len(text) <= 0:
continue
if tts_file is None:
self.logger.error(f"TTS文件生成失败: {text}")
self.logger.bind(tag=TAG).error(f"TTS文件生成失败: {text}")
continue
self.logger.debug(f"TTS文件生成完毕,文件路径: {tts_file}")
self.logger.bind(tag=TAG).debug(f"TTS文件生成完毕,文件路径: {tts_file}")
if os.path.exists(tts_file):
opus_datas, duration = self.tts.wav_to_opus_data(tts_file)
else:
self.logger.error(f"TTS文件不存在: {tts_file}")
self.logger.bind(tag=TAG).error(f"TTS文件不存在: {tts_file}")
opus_datas = []
duration = 0
except TimeoutError:
self.logger.error("TTS 任务超时")
self.logger.bind(tag=TAG).error("TTS 任务超时")
continue
except Exception as e:
self.logger.error(f"TTS 任务出错: {e}")
self.logger.bind(tag=TAG).error(f"TTS 任务出错: {e}")
continue
if not self.client_abort:
# 如果没有中途打断就发送语音
@@ -281,27 +283,27 @@ class ConnectionHandler:
if self.tts.delete_audio_file and os.path.exists(tts_file):
os.remove(tts_file)
except Exception as e:
self.logger.error(f"TTS任务处理错误: {e}")
self.logger.bind(tag=TAG).error(f"TTS任务处理错误: {e}")
self.clearSpeakStatus()
asyncio.run_coroutine_threadsafe(
self.websocket.send(json.dumps({"type": "tts", "state": "stop", "session_id": self.session_id})),
self.loop
)
self.logger.error(f"tts_priority priority_thread: {text}{e}")
self.logger.bind(tag=TAG).error(f"tts_priority priority_thread: {text}{e}")
def speak_and_play(self, text):
if text is None or len(text) <= 0:
self.logger.info(f"无需tts转换,query为空,{text}")
self.logger.bind(tag=TAG).info(f"无需tts转换,query为空,{text}")
return None, text
tts_file = self.tts.to_tts(text)
if tts_file is None:
self.logger.error(f"tts转换失败,{text}")
self.logger.bind(tag=TAG).error(f"tts转换失败,{text}")
return None, text
self.logger.debug(f"TTS 文件生成完毕: {tts_file}")
self.logger.bind(tag=TAG).debug(f"TTS 文件生成完毕: {tts_file}")
return tts_file, text
def clearSpeakStatus(self):
self.logger.debug(f"清除服务端讲话状态")
self.logger.bind(tag=TAG).debug(f"清除服务端讲话状态")
self.asr_server_receive = True
self.tts_last_text = None
self.tts_first_text = None
@@ -310,7 +312,7 @@ class ConnectionHandler:
def recode_first_last_text(self, text):
if not self.tts_first_text:
self.logger.info(f"大模型说出第一句话: {text}")
self.logger.bind(tag=TAG).info(f"大模型说出第一句话: {text}")
self.tts_first_text = text
self.tts_last_text = text
@@ -320,14 +322,14 @@ class ConnectionHandler:
self.executor.shutdown(wait=False)
if self.websocket:
await self.websocket.close()
self.logger.info("连接资源已释放")
self.logger.bind(tag=TAG).info("连接资源已释放")
def reset_vad_states(self):
self.client_audio_buffer = bytes()
self.client_have_voice = False
self.client_have_voice_last_time = 0
self.client_voice_stop = False
self.logger.debug("VAD states reset.")
self.logger.bind(tag=TAG).debug("VAD states reset.")
def stop_all_tasks(self):
while self.scheduled_tasks:
+5 -4
View File
@@ -1,11 +1,12 @@
import json
import logging
from config.logger import setup_logging
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
async def handleAbortMessage(conn):
logger.info("Abort message received")
logger.bind(tag=TAG).info("Abort message received")
# 设置成打断状态,会自动打断llm、tts任务
conn.client_abort = True
# 打断屏显任务
@@ -13,4 +14,4 @@ async def handleAbortMessage(conn):
# 打断客户端说话状态
await conn.websocket.send(json.dumps({"type": "tts", "state": "stop", "session_id": conn.session_id}))
conn.clearSpeakStatus()
logger.info("Abort message received-end")
logger.bind(tag=TAG).info("Abort message received-end")
+8 -6
View File
@@ -1,15 +1,17 @@
import logging
from config.logger import setup_logging
import json
import asyncio
import time
from core.utils.util import remove_punctuation_and_length, get_string_no_punctuation_or_emoji
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
async def handleAudioMessage(conn, audio):
if not conn.asr_server_receive:
logger.debug(f"前期数据处理中,暂停接收")
logger = setup_logging()
logger.bind(tag=TAG).debug(f"前期数据处理中,暂停接收")
return
if conn.client_listen_mode == "auto":
have_voice = conn.vad.is_vad(conn, audio)
@@ -28,7 +30,7 @@ async def handleAudioMessage(conn, audio):
conn.client_abort = False
conn.asr_server_receive = False
text, file_path = conn.asr.speech_to_text(conn.asr_audio, conn.session_id)
logger.info(f"识别文本: {text}")
logger.bind(tag=TAG).info(f"识别文本: {text}")
text_len, text_without_punctuation = remove_punctuation_and_length(text)
if text_len <= conn.max_cmd_length and await handleCMDMessage(conn, text_without_punctuation):
return
@@ -44,7 +46,7 @@ async def handleCMDMessage(conn, text):
cmd_exit = conn.cmd_exit
for cmd in cmd_exit:
if text == cmd:
logger.info("识别到明确的退出命令".format(text))
logger.bind(tag=TAG).info("识别到明确的退出命令".format(text))
await finishToChat(conn)
return True
return False
@@ -80,7 +82,7 @@ async def sendAudioMessage(conn, audios, duration, text):
# 发送 tts.start
if text == conn.tts_first_text:
logger.info(f"发送第一段语音: {text}")
logger.bind(tag=TAG).info(f"发送第一段语音: {text}")
conn.tts_start_speak_time = time.time()
# 发送 sentence_start(每个音频文件之前发送一次)
+2 -2
View File
@@ -1,7 +1,7 @@
import json
import logging
from config.logger import setup_logging
logger = logging.getLogger(__name__)
logger = setup_logging()
async def handleHelloMessage(conn):
+5 -4
View File
@@ -1,15 +1,16 @@
import logging
from config.logger import setup_logging
import json
from core.handle.abortHandle import handleAbortMessage
from core.handle.helloHandle import handleHelloMessage
from core.handle.audioHandle import startToChat
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
async def handleTextMessage(conn, message):
"""处理文本消息"""
logger.info(f"收到文本消息:{message}")
logger.bind(tag=TAG).info(f"收到文本消息:{message}")
try:
msg_json = json.loads(message)
if isinstance(msg_json, int):
@@ -22,7 +23,7 @@ async def handleTextMessage(conn, message):
elif msg_json["type"] == "listen":
if "mode" in msg_json:
conn.client_listen_mode = msg_json["mode"]
logger.debug(f"客户端拾音模式:{conn.client_listen_mode}")
logger.bind(tag=TAG).debug(f"客户端拾音模式:{conn.client_listen_mode}")
if msg_json["state"] == "start":
conn.client_have_voice = True
conn.client_voice_stop = False
+9 -8
View File
@@ -1,10 +1,11 @@
import logging
from config.logger import setup_logging
import requests
import json
import re
from core.providers.llm.base import LLMProviderBase
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
# 定义用于匹配中文标点符号的正则表达式(包括句号、感叹号、问号、分号)
punctuation_pattern = re.compile(r'([。!?;])')
@@ -27,7 +28,7 @@ class LLMProvider(LLMProviderBase):
"query": last_msg["content"],
"stream": True
}
logger.info(f"发送到 Coze API 的请求数据: {json.dumps(data, ensure_ascii=False)}")
logger.bind(tag=TAG).info(f"发送到 Coze API 的请求数据: {json.dumps(data, ensure_ascii=False)}")
headers = {
'Authorization': f'Bearer {self.personal_access_token}',
@@ -43,7 +44,7 @@ class LLMProvider(LLMProviderBase):
json=data,
stream=True
)
logger.info(f"请求状态: {response.status_code}")
logger.bind(tag=TAG).info(f"请求状态: {response.status_code}")
if response.status_code == 200:
# 对每一行流数据进行处理,不做跨块累积
@@ -54,7 +55,7 @@ class LLMProvider(LLMProviderBase):
# 使用 utf-8 解码,错误部分用替换符
line = line_bytes.decode('utf-8', errors='replace')
except Exception as e:
logger.error(f"解码失败: {e}")
logger.bind(tag=TAG).error(f"解码失败: {e}")
continue
if line.startswith("data:"):
data_str = line[len("data:"):].strip()
@@ -63,7 +64,7 @@ class LLMProvider(LLMProviderBase):
try:
data_chunk = json.loads(data_str)
except json.JSONDecodeError as e:
logger.error(f"JSON解析失败: {e} 数据: {line}")
logger.bind(tag=TAG).error(f"JSON解析失败: {e} 数据: {line}")
continue
msg = data_chunk.get("message", {})
if msg.get("role") == "assistant" and msg.get("type") == "answer":
@@ -88,8 +89,8 @@ class LLMProvider(LLMProviderBase):
if content.strip():
yield content.strip()
else:
logger.error(f"请求失败,状态码: {response.status_code}")
logger.bind(tag=TAG).error(f"请求失败,状态码: {response.status_code}")
yield f"【Coze服务响应异常:请求失败,状态码 {response.status_code}"
except Exception as e:
logger.error(f"Error in Coze response generation: {e}")
logger.bind(tag=TAG).error(f"Error in Coze response generation: {e}")
yield "【Coze服务响应异常】"
+4 -4
View File
@@ -1,10 +1,10 @@
import json
import logging
from config.logger import setup_logging
import requests
from core.providers.llm.base import LLMProviderBase
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
class LLMProvider(LLMProviderBase):
def __init__(self, config):
@@ -35,5 +35,5 @@ class LLMProvider(LLMProviderBase):
yield event['answer']
except Exception as e:
logger.error(f"Error in response generation: {e}")
logger.bind(tag=TAG).error(f"Error in response generation: {e}")
yield "【服务响应异常】"
+6 -5
View File
@@ -1,8 +1,9 @@
import logging
from config.logger import setup_logging
import google.generativeai as genai
from core.providers.llm.base import LLMProviderBase
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
class LLMProvider(LLMProviderBase):
def __init__(self, config):
@@ -11,7 +12,7 @@ class LLMProvider(LLMProviderBase):
self.api_key = config.get("api_key")
if not self.api_key or "" in self.api_key:
logger.error("你还没配置Gemini LLM的密钥,请在配置文件中配置密钥,否则无法正常工作")
logger.bind(tag=TAG).error("你还没配置Gemini LLM的密钥,请在配置文件中配置密钥,否则无法正常工作")
return
try:
@@ -28,7 +29,7 @@ class LLMProvider(LLMProviderBase):
}
self.chat = None
except Exception as e:
logger.error(f"Gemini初始化失败: {e}")
logger.bind(tag=TAG).error(f"Gemini初始化失败: {e}")
self.model = None
def response(self, session_id, dialogue):
@@ -69,7 +70,7 @@ class LLMProvider(LLMProviderBase):
except Exception as e:
error_msg = str(e)
logger.error(f"Gemini响应生成错误: {error_msg}")
logger.bind(tag=TAG).error(f"Gemini响应生成错误: {error_msg}")
# 针对不同错误返回友好提示
if "Rate limit" in error_msg:
+4 -3
View File
@@ -1,8 +1,9 @@
import logging
from config.logger import setup_logging
import requests, json
from core.providers.llm.base import LLMProviderBase
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
class LLMProvider(LLMProviderBase):
@@ -41,5 +42,5 @@ class LLMProvider(LLMProviderBase):
yield json_response["response"]
except Exception as e:
logger.error(f"Error in Ollama response generation: {e}")
logger.bind(tag=TAG).error(f"Error in Ollama response generation: {e}")
yield "【Ollama服务响应异常】"
+5 -4
View File
@@ -1,8 +1,9 @@
import logging
from config.logger import setup_logging
import openai
from core.providers.llm.base import LLMProviderBase
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
class LLMProvider(LLMProviderBase):
@@ -14,7 +15,7 @@ class LLMProvider(LLMProviderBase):
else:
self.base_url = config.get("url")
if "" in self.api_key:
logger.error("你还没配置LLM的密钥,请在配置文件中配置密钥,否则无法正常工作")
logger.bind(tag=TAG).error("你还没配置LLM的密钥,请在配置文件中配置密钥,否则无法正常工作")
self.client = openai.OpenAI(api_key=self.api_key, base_url=self.base_url)
def response(self, session_id, dialogue):
@@ -32,4 +33,4 @@ class LLMProvider(LLMProviderBase):
if content: # 仅在content非空时生成
yield content
except Exception as e:
logger.error(f"Error in response generation: {e}")
logger.bind(tag=TAG).error(f"Error in response generation: {e}")
+6 -5
View File
@@ -1,12 +1,13 @@
import asyncio
import logging
from config.logger import setup_logging
import os
import numpy as np
import opuslib
from pydub import AudioSegment
from abc import ABC, abstractmethod
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
class TTSProviderBase(ABC):
@@ -26,14 +27,14 @@ class TTSProviderBase(ABC):
asyncio.run(self.text_to_speak(text, tmp_file))
if not os.path.exists(tmp_file):
max_repeat_time = max_repeat_time - 1
logger.error(f"语音生成失败: {text}:{tmp_file},再试{max_repeat_time}")
logger.bind(tag=TAG).error(f"语音生成失败: {text}:{tmp_file},再试{max_repeat_time}")
if max_repeat_time > 0:
logger.info(f"语音生成成功: {text}:{tmp_file},重试{5 - max_repeat_time}")
logger.bind(tag=TAG).info(f"语音生成成功: {text}:{tmp_file},重试{5 - max_repeat_time}")
return tmp_file
except Exception as e:
logger.info(f"Failed to generate TTS file: {e}")
logger.bind(tag=TAG).info(f"Failed to generate TTS file: {e}")
return None
@abstractmethod
+5 -3
View File
@@ -3,10 +3,12 @@ import uuid
import json
import base64
import requests
import logging
from config.logger import setup_logging
from datetime import datetime
from core.providers.tts.base import TTSProviderBase
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
class TTSProvider(TTSProviderBase):
def __init__(self, config, delete_audio_file):
@@ -62,4 +64,4 @@ class TTSProvider(TTSProviderBase):
with open(output_file, "wb") as file:
file.write(resp.content)
else:
logger.error(f"GPT_SoVITS_V2 TTS请求失败: {resp.status_code} - {resp.text}")
logger.bind(tag=TAG).error(f"GPT_SoVITS_V2 TTS请求失败: {resp.status_code} - {resp.text}")
+9 -9
View File
@@ -2,7 +2,7 @@ import time
import wave
import os
from abc import ABC, abstractmethod
import logging
from config.logger import setup_logging
from typing import Optional, Tuple, List
import uuid
@@ -10,8 +10,8 @@ import opuslib
from funasr import AutoModel
from funasr.utils.postprocess_utils import rich_transcription_postprocess
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
class ASR(ABC):
@abstractmethod
@@ -55,7 +55,7 @@ class FunASR(ASR):
pcm_frame = decoder.decode(opus_packet, 960) # 960 samples = 60ms
pcm_data.append(pcm_frame)
except opuslib.OpusError as e:
logger.error(f"Opus解码错误: {e}", exc_info=True)
logger.bind(tag=TAG).error(f"Opus解码错误: {e}", exc_info=True)
with wave.open(file_path, "wb") as wf:
wf.setnchannels(1)
@@ -72,7 +72,7 @@ class FunASR(ASR):
# 保存音频文件
start_time = time.time()
file_path = self.save_audio_to_file(opus_data, session_id)
logger.debug(f"音频文件保存耗时: {time.time() - start_time:.3f}s | 路径: {file_path}")
logger.bind(tag=TAG).debug(f"音频文件保存耗时: {time.time() - start_time:.3f}s | 路径: {file_path}")
# 语音识别
start_time = time.time()
@@ -84,12 +84,12 @@ class FunASR(ASR):
batch_size_s=60,
)
text = rich_transcription_postprocess(result[0]["text"])
logger.debug(f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text}")
logger.bind(tag=TAG).debug(f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text}")
return text, file_path
except Exception as e:
logger.error(f"语音识别失败: {e}", exc_info=True)
logger.bind(tag=TAG).error(f"语音识别失败: {e}", exc_info=True)
return None, None
finally:
@@ -97,9 +97,9 @@ class FunASR(ASR):
if self.delete_audio_file and file_path and os.path.exists(file_path):
try:
os.remove(file_path)
logger.debug(f"已删除临时音频文件: {file_path}")
logger.bind(tag=TAG).debug(f"已删除临时音频文件: {file_path}")
except Exception as e:
logger.error(f"文件删除失败: {file_path} | 错误: {e}")
logger.bind(tag=TAG).error(f"文件删除失败: {file_path} | 错误: {e}")
def create_instance(class_name: str, *args, **kwargs) -> ASR:
+2 -2
View File
@@ -1,13 +1,13 @@
import os
import sys
import logging
from config.logger import setup_logging
import importlib
from datetime import datetime
from core.utils.util import is_segment
from core.utils.util import get_string_no_punctuation_or_emoji
from core.utils.util import read_config, get_project_dir
logger = logging.getLogger(__name__)
logger = setup_logging()
def create_instance(class_name, *args, **kwargs):
+6 -5
View File
@@ -1,8 +1,9 @@
import asyncio
from typing import Dict
import logging
from config.logger import setup_logging
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
class FileLockManager:
_instance = None
@@ -25,7 +26,7 @@ class FileLockManager:
"""获取锁"""
lock = cls.get_lock(file_path)
await lock.acquire()
logger.debug(f"Acquired lock for {file_path}")
logger.bind(tag=TAG).debug(f"Acquired lock for {file_path}")
@classmethod
def release_lock(cls, file_path: str):
@@ -33,6 +34,6 @@ class FileLockManager:
if file_path in cls._locks:
try:
cls._locks[file_path].release()
logger.debug(f"Released lock for {file_path}")
logger.bind(tag=TAG).debug(f"Released lock for {file_path}")
except RuntimeError as e:
logger.warning(f"Failed to release lock for {file_path}: {e}")
logger.bind(tag=TAG).warning(f"Failed to release lock for {file_path}: {e}")
+2 -2
View File
@@ -1,11 +1,11 @@
import os
import sys
import logging
from config.logger import setup_logging
import importlib
from datetime import datetime
from core.utils.util import read_config, get_project_dir
logger = logging.getLogger(__name__)
logger = setup_logging()
def create_instance(class_name, *args, **kwargs):
+6 -6
View File
@@ -1,12 +1,12 @@
from abc import ABC, abstractmethod
import logging
from config.logger import setup_logging
import opuslib
import time
import numpy as np
import torch
logger = logging.getLogger(__name__)
TAG = __name__
logger = setup_logging()
class VAD(ABC):
@abstractmethod
@@ -17,7 +17,7 @@ class VAD(ABC):
class SileroVAD(VAD):
def __init__(self, config):
logger.info("SileroVAD", config)
logger.bind(tag=TAG).info("SileroVAD", config)
self.model, self.utils = torch.hub.load(repo_or_dir=config["model_dir"],
source='local',
model='silero_vad',
@@ -60,9 +60,9 @@ class SileroVAD(VAD):
return client_have_voice
except opuslib.OpusError as e:
logger.info(f"解码错误: {e}")
logger.bind(tag=TAG).info(f"解码错误: {e}")
except Exception as e:
logger.error(f"Error processing audio packet: {e}")
logger.bind(tag=TAG).error(f"Error processing audio packet: {e}")
def create_instance(class_name, *args, **kwargs) -> VAD:
+5 -5
View File
@@ -1,15 +1,16 @@
import asyncio
import websockets
import logging
from config.logger import setup_logging
from core.connection import ConnectionHandler
from core.utils.util import get_local_ip
from core.utils import asr, vad, llm, tts
TAG = __name__
class WebSocketServer:
def __init__(self, config: dict):
self.config = config
self.logger = logging.getLogger(__name__)
self.logger = setup_logging()
self._vad, self._asr, self._llm, self._tts = self._create_processing_instances()
def _create_processing_instances(self):
@@ -46,9 +47,8 @@ class WebSocketServer:
host = server_config["ip"]
port = server_config["port"]
self.logger.info("=======下面的地址是websocket协议地址,请勿用浏览器访问=======")
self.logger.info("Server is running at ws://%s:%s", get_local_ip(), port)
self.logger.info("=======上面的地址是websocket协议地址,请勿用浏览器访问=======")
self.logger.bind(tag=TAG).info("Server is running at ws://{}:{}", get_local_ip(), port)
self.logger.bind(tag=TAG).info("=======上面的地址是websocket协议地址,请勿用浏览器访问=======")
async with websockets.serve(
self._handle_connection,
host,