diff --git a/core/utils/asr.py b/core/utils/asr.py index c6840f7f8..ab1c35c4f 100644 --- a/core/utils/asr.py +++ b/core/utils/asr.py @@ -1,6 +1,8 @@ import time import wave import os +import sys +import io from abc import ABC, abstractmethod from config.logger import setup_logging from typing import Optional, Tuple, List @@ -13,6 +15,22 @@ from funasr.utils.postprocess_utils import rich_transcription_postprocess TAG = __name__ logger = setup_logging() +# 捕获标准输出 +class CaptureOutput: + def __enter__(self): + self._output = io.StringIO() + self._original_stdout = sys.stdout + sys.stdout = self._output + + def __exit__(self, exc_type, exc_value, traceback): + sys.stdout = self._original_stdout + self.output = self._output.getvalue() + self._output.close() + + # 将捕获到的内容通过 logger 输出 + if self.output: + logger.bind(tag=TAG).info(self.output.strip()) + class ASR(ABC): @abstractmethod def save_audio_to_file(self, opus_data: List[bytes], session_id: str) -> str: @@ -33,14 +51,14 @@ class FunASR(ASR): # 确保输出目录存在 os.makedirs(self.output_dir, exist_ok=True) - - self.model = AutoModel( - model=self.model_dir, - vad_kwargs={"max_single_segment_time": 30000}, - disable_update=True, - hub="hf" - # device="cuda:0", # 启用GPU加速 - ) + with CaptureOutput(): + self.model = AutoModel( + model=self.model_dir, + vad_kwargs={"max_single_segment_time": 30000}, + disable_update=True, + hub="hf" + # device="cuda:0", # 启用GPU加速 + ) def save_audio_to_file(self, opus_data: List[bytes], session_id: str) -> str: """将Opus音频数据解码并保存为WAV文件"""