From 75f75f97a6b7f31bab3b15bfd50a1807aec9b94d Mon Sep 17 00:00:00 2001 From: huyao Date: Mon, 17 Feb 2025 09:53:20 +0800 Subject: [PATCH] feat: support doubao online ASR --- .gitignore | 1 + config.yaml | 7 + core/handle/audioHandle.py | 34 +++- core/providers/asr/base.py | 26 +++ core/providers/asr/doubao.py | 285 ++++++++++++++++++++++++++++++++ core/providers/asr/fun_local.py | 110 ++++++++++++ core/utils/asr.py | 138 ++-------------- core/websocket_server.py | 5 +- 8 files changed, 474 insertions(+), 132 deletions(-) create mode 100644 core/providers/asr/base.py create mode 100644 core/providers/asr/doubao.py create mode 100644 core/providers/asr/fun_local.py diff --git a/.gitignore b/.gitignore index 284409f5e..161f209ee 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ # Byte-compiled / optimized / DLL files __pycache__/ +.idea/ *.py[cod] *$py.class diff --git a/config.yaml b/config.yaml index 7916f436f..f8921d06d 100644 --- a/config.yaml +++ b/config.yaml @@ -83,8 +83,15 @@ selected_module: ASR: FunASR: + type: fun_local model_dir: models/SenseVoiceSmall output_dir: tmp/ + DoubaoASR: + type: doubao + appid: 你的火山引擎语音合成服务appid + access_token: 你的火山引擎语音合成服务access_token + cluster: volcengine_input_common + output_dir: tmp/ VAD: SileroVAD: diff --git a/core/handle/audioHandle.py b/core/handle/audioHandle.py index 394e7fe5d..c4077251d 100644 --- a/core/handle/audioHandle.py +++ b/core/handle/audioHandle.py @@ -28,18 +28,36 @@ async def handleAudioMessage(conn, audio): if conn.client_voice_stop: conn.client_abort = False conn.asr_server_receive = False - text, file_path = conn.asr.speech_to_text(conn.asr_audio, conn.session_id) - 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 - if text_len > 0: - await startToChat(conn, text) - else: + # 音频太短了,无法识别 + if len(conn.asr_audio) < 3: conn.asr_server_receive = True + else: + text, file_path = await conn.asr.speech_to_text(conn.asr_audio, conn.session_id) + 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 + if text_len > 0: + await startToChat(conn, text) + else: + conn.asr_server_receive = True conn.asr_audio.clear() conn.reset_vad_states() +async def speechToText(conn): + conn.client_abort = False + conn.asr_server_receive = False + text, file_path = await conn.asr.speech_to_text(conn.asr_audio, conn.session_id) + logger.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 + if text_len > 0: + await startToChat(conn, text) + else: + conn.asr_server_receive = True + conn.asr_audio.clear() + conn.reset_vad_states() async def handleCMDMessage(conn, text): cmd_exit = conn.cmd_exit diff --git a/core/providers/asr/base.py b/core/providers/asr/base.py new file mode 100644 index 000000000..c18027ad5 --- /dev/null +++ b/core/providers/asr/base.py @@ -0,0 +1,26 @@ +import time +import wave +import os +from abc import ABC, abstractmethod +import logging +from typing import Optional, Tuple, List +import uuid + +import opuslib + +from config.logger import setup_logging + +TAG = __name__ +logger = setup_logging() + + +class ASRProviderBase(ABC): + @abstractmethod + def save_audio_to_file(self, opus_data: List[bytes], session_id: str) -> str: + """解码Opus数据并保存为WAV文件""" + pass + + @abstractmethod + async def speech_to_text(self, opus_data: List[bytes], session_id: str) -> Tuple[Optional[str], Optional[str]]: + """将语音数据转换为文本""" + pass diff --git a/core/providers/asr/doubao.py b/core/providers/asr/doubao.py new file mode 100644 index 000000000..491cdf0e3 --- /dev/null +++ b/core/providers/asr/doubao.py @@ -0,0 +1,285 @@ +import time +import io +import wave +import os +import logging +from typing import Optional, Tuple, List +import uuid +import websockets +import json +import gzip + +import opuslib +from core.providers.asr.base import ASRProviderBase + +from config.logger import setup_logging + +TAG = __name__ +logger = setup_logging() + +CLIENT_FULL_REQUEST = 0b0001 +CLIENT_AUDIO_ONLY_REQUEST = 0b0010 + +NO_SEQUENCE = 0b0000 +NEG_SEQUENCE = 0b0010 + +SERVER_FULL_RESPONSE = 0b1001 +SERVER_ACK = 0b1011 +SERVER_ERROR_RESPONSE = 0b1111 + +NO_SERIALIZATION = 0b0000 +JSON = 0b0001 +THRIFT = 0b0011 +CUSTOM_TYPE = 0b1111 +NO_COMPRESSION = 0b0000 +GZIP = 0b0001 +CUSTOM_COMPRESSION = 0b1111 + +def parse_response(res): + """ + protocol_version(4 bits), header_size(4 bits), + message_type(4 bits), message_type_specific_flags(4 bits) + serialization_method(4 bits) message_compression(4 bits) + reserved (8bits) 保留字段 + header_extensions 扩展头(大小等于 8 * 4 * (header_size - 1) ) + payload 类似与http 请求体 + """ + protocol_version = res[0] >> 4 + header_size = res[0] & 0x0f + message_type = res[1] >> 4 + message_type_specific_flags = res[1] & 0x0f + serialization_method = res[2] >> 4 + message_compression = res[2] & 0x0f + reserved = res[3] + header_extensions = res[4:header_size * 4] + payload = res[header_size * 4:] + result = {} + payload_msg = None + payload_size = 0 + if message_type == SERVER_FULL_RESPONSE: + payload_size = int.from_bytes(payload[:4], "big", signed=True) + payload_msg = payload[4:] + elif message_type == SERVER_ACK: + seq = int.from_bytes(payload[:4], "big", signed=True) + result['seq'] = seq + if len(payload) >= 8: + payload_size = int.from_bytes(payload[4:8], "big", signed=False) + payload_msg = payload[8:] + elif message_type == SERVER_ERROR_RESPONSE: + code = int.from_bytes(payload[:4], "big", signed=False) + result['code'] = code + payload_size = int.from_bytes(payload[4:8], "big", signed=False) + payload_msg = payload[8:] + if payload_msg is None: + return result + if message_compression == GZIP: + payload_msg = gzip.decompress(payload_msg) + if serialization_method == JSON: + payload_msg = json.loads(str(payload_msg, "utf-8")) + elif serialization_method != NO_SERIALIZATION: + payload_msg = str(payload_msg, "utf-8") + result['payload_msg'] = payload_msg + result['payload_size'] = payload_size + return result + +class ASRProvider(ASRProviderBase): + def __init__(self, config: dict, delete_audio_file: bool): + self.appid = config.get("appid") + self.cluster = config.get("cluster") + self.access_token = config.get("access_token") + self.output_dir = config.get("output_dir") + + self.host = "openspeech.bytedance.com" + self.ws_url = f"wss://{self.host}/api/v2/asr" + self.success_code = 1000 + self.seg_duration = 15000 + + # 确保输出目录存在 + os.makedirs(self.output_dir, exist_ok=True) + + def save_audio_to_file(self, opus_data: List[bytes], session_id: str) -> str: + """将Opus音频数据解码并保存为WAV文件""" + file_name = f"asr_{session_id}_{uuid.uuid4()}.wav" + file_path = os.path.join(self.output_dir, file_name) + + decoder = opuslib.Decoder(16000, 1) # 16kHz, 单声道 + pcm_data = [] + + for opus_packet in opus_data: + try: + pcm_frame = decoder.decode(opus_packet, 960) # 960 samples = 60ms + pcm_data.append(pcm_frame) + except opuslib.OpusError as e: + logger.bind(tag=TAG).error(f"Opus解码错误: {e}", exc_info=True) + + with wave.open(file_path, "wb") as wf: + wf.setnchannels(1) + wf.setsampwidth(2) # 2 bytes = 16-bit + wf.setframerate(16000) + wf.writeframes(b"".join(pcm_data)) + + return file_path + + @staticmethod + def _generate_header(message_type=CLIENT_FULL_REQUEST, message_type_specific_flags=NO_SEQUENCE) -> bytearray: + """Generate protocol header.""" + header = bytearray() + header_size = 1 + header.append((0b0001 << 4) | header_size) # Protocol version + header.append((message_type << 4) | message_type_specific_flags) + header.append((0b0001 << 4) | 0b0001) # JSON serialization & GZIP compression + header.append(0x00) # reserved + return header + + def _construct_request(self, reqid) -> dict: + """Construct the request payload.""" + return { + "app": { + "appid": self.appid, + "cluster": self.cluster, + "token": self.access_token, + }, + "user": { + "uid": str(uuid.uuid4()), + }, + "request": { + "reqid": reqid, + "show_utterances": False, + "sequence": 1 + }, + "audio": { + "format": "wav", + "rate": 16000, + "language": "zh-CN", + "bits": 16, + "channel": 1, + "codec": "raw", + }, + } + + async def _send_request(self, audio_data: List[bytes], segment_size: int) -> Optional[str]: + """Send request to Volcano ASR service.""" + try: + auth_header = {'Authorization': 'Bearer; {}'.format(self.access_token)} + async with websockets.connect(self.ws_url, additional_headers=auth_header) as websocket: + # Prepare request data + request_params = self._construct_request(str(uuid.uuid4())) + print(request_params) + payload_bytes = str.encode(json.dumps(request_params)) + payload_bytes = gzip.compress(payload_bytes) + full_client_request = self._generate_header() + full_client_request.extend((len(payload_bytes)).to_bytes(4, 'big')) # payload size(4 bytes) + full_client_request.extend(payload_bytes) # payload + + # Send header and metadata + # full_client_request + await websocket.send(full_client_request) + res = await websocket.recv() + result = parse_response(res) + if 'payload_msg' in result and result['payload_msg']['code'] != self.success_code: + logger.bind(tag=TAG).error(f"ASR error: {result}") + return None + + for seq, (chunk, last) in enumerate(self.slice_data(audio_data, segment_size), 1): + if last: + audio_only_request = self._generate_header( + message_type=CLIENT_AUDIO_ONLY_REQUEST, + message_type_specific_flags=NEG_SEQUENCE + ) + else: + audio_only_request = self._generate_header( + message_type=CLIENT_AUDIO_ONLY_REQUEST + ) + payload_bytes = gzip.compress(chunk) + audio_only_request.extend((len(payload_bytes)).to_bytes(4, 'big')) # payload size(4 bytes) + audio_only_request.extend(payload_bytes) # payload + # Send audio data + await websocket.send(audio_only_request) + + # Receive response + response = await websocket.recv() + result = parse_response(response) + + if 'payload_msg' in result and result['payload_msg']['code'] == self.success_code: + if len(result['payload_msg']['result']) > 0: + return result['payload_msg']['result'][0]["text"] + return None + else: + logger.bind(tag=TAG).error(f"ASR error: {result}") + return None + + except Exception as e: + logger.bind(tag=TAG).error(f"ASR request failed: {e}", exc_info=True) + return None + + @staticmethod + def decode_opus(opus_data: List[bytes], session_id: str) -> List[bytes]: + + decoder = opuslib.Decoder(16000, 1) # 16kHz, 单声道 + pcm_data = [] + + for opus_packet in opus_data: + try: + pcm_frame = decoder.decode(opus_packet, 960) # 960 samples = 60ms + pcm_data.append(pcm_frame) + except opuslib.OpusError as e: + logger.bind(tag=TAG).error(f"Opus解码错误: {e}", exc_info=True) + + return pcm_data + + @staticmethod + def read_wav_info(data: io.BytesIO = None) -> (int, int, int, int, int): + with io.BytesIO(data) as _f: + wave_fp = wave.open(_f, 'rb') + nchannels, sampwidth, framerate, nframes = wave_fp.getparams()[:4] + wave_bytes = wave_fp.readframes(nframes) + return nchannels, sampwidth, framerate, nframes, len(wave_bytes) + + @staticmethod + def slice_data(data: bytes, chunk_size: int) -> (list, bool): + """ + slice data + :param data: wav data + :param chunk_size: the segment size in one request + :return: segment data, last flag + """ + data_len = len(data) + offset = 0 + while offset + chunk_size < data_len: + yield data[offset: offset + chunk_size], False + offset += chunk_size + else: + yield data[offset: data_len], True + + async def speech_to_text(self, opus_data: List[bytes], session_id: str) -> Tuple[Optional[str], Optional[str]]: + """将语音数据转换为文本""" + try: + # 合并所有opus数据包 + pcm_data = self.decode_opus(opus_data, session_id) + combined_pcm_data = b''.join(pcm_data) + + wav_buffer = io.BytesIO() + + with wave.open(wav_buffer, "wb") as wav_file: + wav_file.setnchannels(1) # 设置声道数 + wav_file.setsampwidth(2) # 设置采样宽度 + wav_file.setframerate(16000) # 设置采样率 + wav_file.writeframes(combined_pcm_data) # 写入 PCM 数据 + + # 获取封装后的 WAV 数据 + wav_data = wav_buffer.getvalue() + nchannels, sampwidth, framerate, nframes, wav_len = self.read_wav_info(wav_data) + size_per_sec = nchannels * sampwidth * framerate + segment_size = int(size_per_sec * self.seg_duration / 1000) + + # 语音识别 + start_time = time.time() + text = await self._send_request(wav_data, segment_size) + if text: + logger.bind(tag=TAG).debug(f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text}") + return text, None + return "", None + + except Exception as e: + logger.bind(tag=TAG).error(f"语音识别失败: {e}", exc_info=True) + return "", None diff --git a/core/providers/asr/fun_local.py b/core/providers/asr/fun_local.py new file mode 100644 index 000000000..299713f75 --- /dev/null +++ b/core/providers/asr/fun_local.py @@ -0,0 +1,110 @@ +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 +import uuid +import opuslib_next +from core.providers.asr.base import ASRProviderBase + +from funasr import AutoModel +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 ASRProvider(ASRProviderBase): + def __init__(self, config: dict, delete_audio_file: bool): + self.model_dir = config.get("model_dir") + self.output_dir = config.get("output_dir") # 修正配置键名 + self.delete_audio_file = delete_audio_file + + # 确保输出目录存在 + os.makedirs(self.output_dir, exist_ok=True) + 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文件""" + file_name = f"asr_{session_id}_{uuid.uuid4()}.wav" + file_path = os.path.join(self.output_dir, file_name) + + decoder = opuslib_next.Decoder(16000, 1) # 16kHz, 单声道 + pcm_data = [] + + for opus_packet in opus_data: + try: + pcm_frame = decoder.decode(opus_packet, 960) # 960 samples = 60ms + pcm_data.append(pcm_frame) + except opuslib_next.OpusError as e: + logger.bind(tag=TAG).error(f"Opus解码错误: {e}", exc_info=True) + + with wave.open(file_path, "wb") as wf: + wf.setnchannels(1) + wf.setsampwidth(2) # 2 bytes = 16-bit + wf.setframerate(16000) + wf.writeframes(b"".join(pcm_data)) + + return file_path + + async def speech_to_text(self, opus_data: List[bytes], session_id: str) -> Tuple[Optional[str], Optional[str]]: + """语音转文本主处理逻辑""" + file_path = None + try: + # 保存音频文件 + start_time = time.time() + file_path = self.save_audio_to_file(opus_data, session_id) + logger.bind(tag=TAG).debug(f"音频文件保存耗时: {time.time() - start_time:.3f}s | 路径: {file_path}") + + # 语音识别 + start_time = time.time() + result = self.model.generate( + input=file_path, + cache={}, + language="auto", + use_itn=True, + batch_size_s=60, + ) + text = rich_transcription_postprocess(result[0]["text"]) + logger.bind(tag=TAG).debug(f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text}") + + return text, file_path + + except Exception as e: + logger.bind(tag=TAG).error(f"语音识别失败: {e}", exc_info=True) + return "", None + + finally: + # 文件清理逻辑 + if self.delete_audio_file and file_path and os.path.exists(file_path): + try: + os.remove(file_path) + logger.bind(tag=TAG).debug(f"已删除临时音频文件: {file_path}") + except Exception as e: + logger.bind(tag=TAG).error(f"文件删除失败: {file_path} | 错误: {e}") diff --git a/core/utils/asr.py b/core/utils/asr.py index ab1c35c4f..605630630 100644 --- a/core/utils/asr.py +++ b/core/utils/asr.py @@ -1,132 +1,24 @@ -import time -import wave +import importlib +import logging import os import sys -import io -from abc import ABC, abstractmethod -from config.logger import setup_logging -from typing import Optional, Tuple, List +import time +import wave import uuid - -import opuslib_next -from funasr import AutoModel -from funasr.utils.postprocess_utils import rich_transcription_postprocess +from abc import ABC, abstractmethod +from typing import Optional, Tuple, List +from core.providers.asr.base import ASRProviderBase +from config.logger import setup_logging 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: - """解码Opus数据并保存为WAV文件""" - pass - - @abstractmethod - def speech_to_text(self, opus_data: List[bytes], session_id: str) -> Tuple[Optional[str], Optional[str]]: - """将语音数据转换为文本""" - pass - - -class FunASR(ASR): - def __init__(self, config: dict, delete_audio_file: bool): - self.model_dir = config.get("model_dir") - self.output_dir = config.get("output_dir") # 修正配置键名 - self.delete_audio_file = delete_audio_file - - # 确保输出目录存在 - os.makedirs(self.output_dir, exist_ok=True) - 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文件""" - file_name = f"asr_{session_id}_{uuid.uuid4()}.wav" - file_path = os.path.join(self.output_dir, file_name) - - decoder = opuslib_next.Decoder(16000, 1) # 16kHz, 单声道 - pcm_data = [] - - for opus_packet in opus_data: - try: - pcm_frame = decoder.decode(opus_packet, 960) # 960 samples = 60ms - pcm_data.append(pcm_frame) - except opuslib_next.OpusError as e: - logger.bind(tag=TAG).error(f"Opus解码错误: {e}", exc_info=True) - - with wave.open(file_path, "wb") as wf: - wf.setnchannels(1) - wf.setsampwidth(2) # 2 bytes = 16-bit - wf.setframerate(16000) - wf.writeframes(b"".join(pcm_data)) - - return file_path - - def speech_to_text(self, opus_data: List[bytes], session_id: str) -> Tuple[Optional[str], Optional[str]]: - """语音转文本主处理逻辑""" - file_path = None - try: - # 保存音频文件 - start_time = time.time() - file_path = self.save_audio_to_file(opus_data, session_id) - logger.bind(tag=TAG).debug(f"音频文件保存耗时: {time.time() - start_time:.3f}s | 路径: {file_path}") - - # 语音识别 - start_time = time.time() - result = self.model.generate( - input=file_path, - cache={}, - language="auto", - use_itn=True, - batch_size_s=60, - ) - text = rich_transcription_postprocess(result[0]["text"]) - logger.bind(tag=TAG).debug(f"语音识别耗时: {time.time() - start_time:.3f}s | 结果: {text}") - - return text, file_path - - except Exception as e: - logger.bind(tag=TAG).error(f"语音识别失败: {e}", exc_info=True) - return None, None - - finally: - # 文件清理逻辑 - if self.delete_audio_file and file_path and os.path.exists(file_path): - try: - os.remove(file_path) - logger.bind(tag=TAG).debug(f"已删除临时音频文件: {file_path}") - except Exception as e: - logger.bind(tag=TAG).error(f"文件删除失败: {file_path} | 错误: {e}") - - -def create_instance(class_name: str, *args, **kwargs) -> ASR: +def create_instance(class_name: str, *args, **kwargs) -> ASRProviderBase: """工厂方法创建ASR实例""" - cls_map = { - "FunASR": FunASR, - # 可扩展其他ASR实现 - } + if os.path.exists(os.path.join('core', 'providers', 'asr', f'{class_name}.py')): + lib_name = f'core.providers.asr.{class_name}' + if lib_name not in sys.modules: + sys.modules[lib_name] = importlib.import_module(f'{lib_name}') + return sys.modules[lib_name].ASRProvider(*args, **kwargs) - if cls := cls_map.get(class_name): - return cls(*args, **kwargs) - raise ValueError(f"不支持的ASR类型: {class_name}") + raise ValueError(f"不支持的ASR类型: {class_name},请检查该配置的type是否设置正确") diff --git a/core/websocket_server.py b/core/websocket_server.py index 2d6ac6d1c..829801ba2 100644 --- a/core/websocket_server.py +++ b/core/websocket_server.py @@ -21,7 +21,10 @@ class WebSocketServer: self.config["VAD"][self.config["selected_module"]["VAD"]] ), asr.create_instance( - self.config["selected_module"]["ASR"], + self.config["selected_module"]["ASR"] + if not 'type' in self.config["ASR"][self.config["selected_module"]["ASR"]] + else + self.config["ASR"][self.config["selected_module"]["ASR"]]["type"], self.config["ASR"][self.config["selected_module"]["ASR"]], self.config["delete_audio"] ),